RangePromptView.js 7.0 KB

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