RangePromptView.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2016, 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../../lib/@waca/core-client/js/core-client/ui/AccessibleView', '../../util/DashboardFormatter', '../../lib/@waca/dashboard-common/dist/ui/Slider', '../../widgets/livewidget/nls/StringResources', 'text!./templates/BasePromptView.html', 'text!./templates/RangePromptViewContent.html', 'jquery', 'underscore', 'doT'], function (View, Formatter, RangeSlider, stringResources, BasePromptViewTemplate, RangePromptViewContent, $, _, dot) {
  8. 'use strict';
  9. var RangePromptView = View.extend({
  10. events: {
  11. 'primaryaction .tooltip-inner': '_enableOk',
  12. 'primaryaction .postAggregate': '_onAggregateTypeChange',
  13. 'primaryaction .preAggregate': '_onAggregateTypeChange'
  14. },
  15. init: function init(options) {
  16. options.height = '150px';
  17. options.width = '500px';
  18. RangePromptView.inherited('init', this, arguments);
  19. _.extend(this, options);
  20. _.defaults(options, {
  21. style: 'dual',
  22. sliderId: 'slider_filter'
  23. });
  24. this.minimized = options.minimized;
  25. this.enabled = options.enabled;
  26. this.sliderStyle = options.style;
  27. this.sliderId = options.sliderId;
  28. this.postAutoAggregation = options.postAutoAggregation;
  29. // Used to preserve saved prompt values at re-prompt, and while the aggregation type is changed.
  30. this._initialDefault = {
  31. 'postAggregate': this.postAutoAggregation,
  32. 'defaultValues': this.defaultValues
  33. };
  34. },
  35. render: function render() {
  36. var sBasePromptViewHtml = dot.template(BasePromptViewTemplate)({
  37. promptModuleName: this.promptModuleName,
  38. postAggregate: this.postAutoAggregation,
  39. isMultiPrompt: this.isMultiPrompt,
  40. preAggregateLabel: stringResources.get('preAggregateLabel'),
  41. postAggregateLabel: stringResources.get('postAggregateLabel'),
  42. preAggregateText: stringResources.get('preAggregateText')
  43. });
  44. this.$el.addClass('promptDialogContainer rangeDialog').height(this.height).width(this.width).append(sBasePromptViewHtml);
  45. this.$el.attr('role', 'region');
  46. this.$el.attr('aria-label', this.viewTitle);
  47. this.$postAggregateNode = this.$el.find('.postAggregate svg');
  48. this.$preAggregateNode = this.$el.find('.preAggregate svg');
  49. this.$content = this.$('.content');
  50. this.$content.html(RangePromptViewContent);
  51. // Search Field
  52. this.$('.search').attr('placeholder', stringResources.get('search')).attr('aria-label', stringResources.get('search'));
  53. this.$('.clearCaption').text(stringResources.get('clearTextFilterValue'));
  54. this.$('.rowButton.invertButton').text(stringResources.get('invertFilterSelection'));
  55. // Content
  56. return this._populateContent();
  57. },
  58. _populateContent: function _populateContent() {
  59. var _this = this;
  60. this._slideContent = this.$('.sliderContent');
  61. this._slideContent.empty();
  62. if (!this.minimized) {
  63. this._showLoading();
  64. }
  65. return this._updateMinMax().then(function () {
  66. return _this._renderSlider();
  67. });
  68. },
  69. setFocus: function setFocus() {
  70. if (this._rangeSlider) {
  71. this._rangeSlider.setFocus();
  72. }
  73. },
  74. _renderSlider: function _renderSlider() {
  75. // Remove everything from slider content, including the loading indicator.
  76. this._slideContent.empty();
  77. this.trigger('change:state', {
  78. state: 'loaded'
  79. });
  80. var min = this.minMax[0].u;
  81. var max = this.minMax[1].u;
  82. var value = [min, max];
  83. var model = {
  84. el: this._slideContent,
  85. sliderId: this.sliderId,
  86. min: min,
  87. max: max,
  88. value: value,
  89. showMinMax: false,
  90. inverted: false,
  91. minimized: this.minimized,
  92. enabled: this.enabled,
  93. style: this.sliderStyle,
  94. prettyValue: this._formatValues.bind({
  95. category: this.category
  96. })
  97. };
  98. if (!this.minimized) {
  99. model.tooltipTemplate = ['<span>&ge;</span><input aria-label="&ge;" type="text"></input>', '<span>&le;</span><input aria-label="&le;" type="text"></input>'];
  100. model.invertedTooltipTemplate = ['<span>&lt;</span><input aria-label="&lt;" type="text"></input>', '<span>&gt;</span><input aria-label ="&gt;" type="text"></input>'];
  101. }
  102. if (this.defaultValues) {
  103. model.value = [this.defaultValues[0].d, this.defaultValues[1].d];
  104. }
  105. this._rangeSlider = new RangeSlider(model);
  106. this._rangeSlider.on('action:slidestop', function () {
  107. this.trigger('action:slidestop');
  108. }.bind(this));
  109. this._rangeSlider.on('action:change', function () {
  110. this._enableOk();
  111. }.bind(this));
  112. return this._rangeSlider.render();
  113. },
  114. _showLoading: function _showLoading() {
  115. this.trigger('change:state', {
  116. state: 'loading'
  117. });
  118. var indicator = $('<div></div>');
  119. indicator.addClass('loading');
  120. indicator.text(stringResources.get('modellingLoading'));
  121. // Remove everything, including the slider.
  122. this._slideContent.html(indicator);
  123. },
  124. _updateMinMax: function _updateMinMax() {
  125. var _this2 = this;
  126. var result = void 0;
  127. // Request the min and max.
  128. if (this.whenColumnsMinMaxQueryReady) {
  129. result = Promise.resolve().then(function () {
  130. return _this2.whenColumnsMinMaxQueryReady({
  131. column: {
  132. columnId: _this2.columnId
  133. },
  134. nativeQuery: true,
  135. promptName: _this2.name
  136. }).then(function (result) {
  137. var minValue = result.getCellValue(0, 0);
  138. var maxValue = result.getCellValue(0, 1);
  139. _this2.minMax = [minValue, maxValue];
  140. return _this2.minMax;
  141. });
  142. });
  143. } else {
  144. result = Promise.resolve();
  145. }
  146. return result;
  147. },
  148. getPromptValues: function getPromptValues() {
  149. return this._rangeSlider.getValue();
  150. },
  151. getAdditionalOptions: function getAdditionalOptions() {
  152. return {
  153. postAutoAggregation: this.postAutoAggregation
  154. };
  155. },
  156. _formatValues: function _formatValues(values) {
  157. var ret = [];
  158. var spec = {
  159. type: 'number'
  160. };
  161. if (this.category === 'time') {
  162. spec.useGrouping = false;
  163. }
  164. _.each(values, function (val) {
  165. ret.push(Formatter.format(val, spec));
  166. });
  167. return ret;
  168. },
  169. _enableOk: function _enableOk() {
  170. if (this.enableOk) {
  171. this.enableOk(true);
  172. }
  173. },
  174. _onAggregateTypeChange: function _onAggregateTypeChange(event) {
  175. this._onClickRadioButton(event);
  176. return this._populateContent();
  177. },
  178. _onClickRadioButton: function _onClickRadioButton(event) {
  179. this.postAutoAggregation = $(event.currentTarget).hasClass('postAggregate');
  180. if (this.postAutoAggregation) {
  181. this.$postAggregateNode.attr('class', 'checked');
  182. this.$preAggregateNode.attr('class', '');
  183. if (this._initialDefault.postAggregate) {
  184. this.defaultValues = this._initialDefault.defaultValues;
  185. } else {
  186. this.defaultValues = null;
  187. }
  188. } else {
  189. this.$postAggregateNode.attr('class', '');
  190. this.$preAggregateNode.attr('class', 'checked');
  191. if (this._initialDefault.postAggregate) {
  192. this.defaultValues = null;
  193. } else {
  194. this.defaultValues = this._initialDefault.defaultValues;
  195. }
  196. }
  197. }
  198. });
  199. return RangePromptView;
  200. });
  201. //# sourceMappingURL=RangePromptView.js.map