ConditionalPalettePicker.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. *
  5. * IBM Cognos Products: Dashboard
  6. *
  7. * (C) Copyright IBM Corp. 2016, 2020
  8. *
  9. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define(['jquery', 'underscore', '../../../lib/@waca/core-client/js/core-client/ui/core/View', '../../../util/DashboardFormatter', '../../../lib/@waca/core-client/js/core-client/ui/MultiHandleSlider', '../../../lib/@waca/dashboard-common/dist/utils/Flyout', '../../../data/models/ConditionalPalette', './ConditionalFlyoutView', 'jquery-ui', 'touch-punch'], function ($, _, View, Formatter, MultiHandleSlider, Flyout, ConditionalPalette, ConditionalFlyoutView) {
  12. 'use strict';
  13. /**
  14. * Represents an Conditional colour palette
  15. */
  16. var ConditionalPalettePicker = View.extend({
  17. /**
  18. * @param options Object An object of options for this class
  19. * @param options.min
  20. * @param options.max
  21. * @param options.palette
  22. * @param options.widget
  23. */
  24. init: function init(options) {
  25. ConditionalPalettePicker.inherited('init', this, arguments);
  26. _.extend(this, options);
  27. },
  28. render: function render(coordinates) {
  29. var _this = this;
  30. var eventsExist = this.slider;
  31. return this._createSlider().then(function () {
  32. _this.slider.render();
  33. !eventsExist && _this._setEvents();
  34. if (coordinates) {
  35. _this._resize(coordinates);
  36. }
  37. });
  38. },
  39. _createSlider: function _createSlider() {
  40. var _this2 = this;
  41. // Use existing slider only if it's JQuery Slider exists
  42. if (this.slider && this.slider.$slider && this.slider.$slider.slider('instance')) {
  43. return Promise.resolve();
  44. }
  45. var promise = this.palette.getSize() === 0 && this.widget ? this.visModel.createDefaultConditionalPalette(this.min, this.max) : Promise.resolve();
  46. return promise.then(function () {
  47. _this2._values = [];
  48. var paletteSize = _this2.palette.getSize();
  49. for (var i = 0; i < paletteSize; i++) {
  50. var mark = _this2.palette.getColor(i);
  51. _this2._values.push(mark.getValue());
  52. }
  53. // Used to know how to handle the flyout when user is click on handles
  54. _this2.processNext = true;
  55. _this2.hasSlid = false;
  56. _this2.canAddHandle = false;
  57. _this2.slider = new MultiHandleSlider({
  58. $el: _this2.$el,
  59. min: _this2.min,
  60. max: _this2.max,
  61. maxHandles: _this2.visModel.getConditionalPaletteLength() + 1,
  62. values: _this2._values,
  63. format: function (value) {
  64. return Formatter.format(value, this.format);
  65. }.bind(_this2),
  66. styles: _this2.palette.getColors().models,
  67. addHandleTrack: true,
  68. sliderClasses: 'ConditionalPalettePicker',
  69. refreshSlider: function refreshSlider(values, styles) {
  70. // Update the JQuery Slider with new values
  71. if (this.$slider && this.$slider.slider('instance')) {
  72. this.$slider.slider('values', values);
  73. this._createSliderElements({ target: this.$slider[0] });
  74. if (styles && styles.length > 0) {
  75. this.styles = styles;
  76. this._createRangeDivs(this.$slider);
  77. }
  78. }
  79. },
  80. sliderCallback: {
  81. 'slidechange': function (values) {
  82. var options = ConditionalPalette.createUndoRedoTransactionOptionsId('updateStyleValues');
  83. this.palette.updateStyleValues(values, options);
  84. // we only refresh the slider if the min or max value has changed.
  85. if (this.slider.maxValue.original !== this.slider.maxValue.value || this.slider.minValue.original !== this.slider.minValue.value) {
  86. this.palette.trigger('palette:refreshSlider', values);
  87. }
  88. // there is no need to callback to refresh.
  89. // once the model changes, the entire VisView will be re-rendered.
  90. }.bind(_this2)
  91. },
  92. sliderEvents: [{
  93. event: 'slide',
  94. callback: function () {
  95. this.hasSlid = true;
  96. }.bind(_this2)
  97. }, {
  98. event: 'mousedown touchstart',
  99. element: 'div.ui-slider-handle',
  100. callback: _this2._slideHandleMousedownEvent.bind(_this2)
  101. }, {
  102. event: 'mouseup touchend',
  103. element: 'div.ui-slider-handle',
  104. callback: _this2._slideHandleMouseupEvent.bind(_this2)
  105. }, {
  106. event: 'mousedown touchstart',
  107. element: 'div.sliderBarAddTrack, div.slider-handle-value',
  108. callback: function (event) {
  109. if (this.flyout) {
  110. this.flyout.close();
  111. }
  112. this.canAddHandle = true;
  113. event.stopPropagation();
  114. }.bind(_this2)
  115. }, {
  116. event: 'mouseup touchend',
  117. element: 'div.sliderBarAddTrack',
  118. callback: _this2._sliderAddHandle.bind(_this2)
  119. }, {
  120. event: 'mousemove mouseout touchmove',
  121. element: 'div.sliderBarAddTrack',
  122. callback: _this2._sliderGhostHandle.bind(_this2)
  123. }]
  124. });
  125. });
  126. },
  127. _resize: function _resize(coordinates) {
  128. var $picker = this.$el.find('.ConditionalPalettePicker');
  129. $picker.css('left', coordinates.x1 + 'px');
  130. $picker.css('width', coordinates.x2 - coordinates.x1 + 'px');
  131. },
  132. remove: function remove() {
  133. this._deleteFlyout();
  134. this.slider && this.slider.remove();
  135. this.palette && this.palette.off('palette:refreshSlider');
  136. ConditionalPalettePicker.inherited('remove', this, arguments);
  137. },
  138. _setEvents: function _setEvents() {
  139. this.palette.on('palette:refreshSlider', function () {
  140. var models = this.palette.getColors().models;
  141. var values = _.map(models, function (model) {
  142. return model.value;
  143. }) || [];
  144. this.slider.reRender({ styles: models, values: values });
  145. }.bind(this));
  146. },
  147. _createFlyout: function _createFlyout(event) {
  148. var handlePosition = this.slider.$handles.index(event.currentTarget);
  149. var options = {
  150. container: document.body,
  151. selector: event.currentTarget,
  152. popoverClass: 'actionToolbarPopover',
  153. viewport: 'body',
  154. viewClass: ConditionalFlyoutView,
  155. viewOptions: {
  156. visModel: this.visModel,
  157. widget: this.widget,
  158. palette: this.palette,
  159. slider: this.slider,
  160. icons: this.icons,
  161. handlePosition: handlePosition
  162. }
  163. };
  164. this.flyout = new Flyout(options);
  165. this.flyout.open(event.currentTarget);
  166. },
  167. _deleteFlyout: function _deleteFlyout() {
  168. if (this.flyout) {
  169. this.flyout.close();
  170. this.flyout.remove();
  171. delete this.flyout;
  172. }
  173. },
  174. _slideHandleMousedownEvent: function _slideHandleMousedownEvent(event) {
  175. this.hasSlid = false;
  176. if (this.slider.getHandleCount() > this.slider.minHandles && this.flyout) {
  177. if (this.flyout.selector === event.currentTarget && this.processNext) {
  178. this.processNext = false;
  179. } else {
  180. this.processNext = true;
  181. }
  182. if (this.flyout.view.isVisible) {
  183. this.flyout.close();
  184. } else {
  185. this.processNext = true;
  186. }
  187. }
  188. },
  189. _slideHandleMouseupEvent: function _slideHandleMouseupEvent(event) {
  190. if (this.slider.getHandleCount() > this.slider.minHandles && !this.hasSlid) {
  191. if (this.flyout) {
  192. if (this.flyout.selector === event.currentTarget && !this.flyout.view.isVisible && this.processNext) {
  193. this.flyout.open(event.currentTarget);
  194. } else if (this.flyout.selector !== event.currentTarget) {
  195. this.flyout.destroy().then(this._deleteFlyout.bind(this)).then(this._createFlyout.bind(this, event));
  196. }
  197. } else if (!this.flyout) {
  198. this._createFlyout(event);
  199. }
  200. }
  201. },
  202. _calculateSliderValue: function _calculateSliderValue(event) {
  203. var clientX = event.clientX;
  204. if (event.type === 'touchend' || event.type === 'touchmove') {
  205. clientX = event.originalEvent.changedTouches[0].clientX;
  206. }
  207. var $track = $(event.target);
  208. var width = $track.width();
  209. var offset = $track.offset();
  210. var options = this.slider.$slider.slider('option');
  211. var value = (clientX - offset.left) / width * (options.max - options.min) + options.min;
  212. if (options.step === 1) {
  213. value = Math.round(value);
  214. }
  215. return value;
  216. },
  217. _sliderAddHandle: function _sliderAddHandle(event) {
  218. if (this.canAddHandle === true && this.slider.getHandleCount() < this.slider.maxHandles) {
  219. var value = this._calculateSliderValue(event);
  220. var style = {
  221. value: value,
  222. color: '',
  223. bgcolor: '',
  224. pattern: ''
  225. };
  226. var options = ConditionalPalette.createUndoRedoTransactionOptionsId('addColor');
  227. this.palette.addColor(style, options);
  228. this.visModel.updateConditionalPalette().then(function () {
  229. this.slider.styles = this.palette.getColors().models;
  230. this.slider.addHandle(value);
  231. this.slider.$slider.addClass('ConditionalPalettePicker');
  232. this.canAddHandle = false;
  233. }.bind(this));
  234. event.stopPropagation();
  235. event.preventDefault();
  236. }
  237. },
  238. _sliderGhostHandle: function _sliderGhostHandle(event) {
  239. if (event.type === 'mouseout') {
  240. this.slider.removeGhostHandle();
  241. } else {
  242. if (this.slider.getHandleCount() < this.slider.maxHandles) {
  243. var value = this._calculateSliderValue(event);
  244. if (value >= this.slider.min && value <= this.slider.max) {
  245. this.slider.displayGhostHandle(event, value);
  246. }
  247. }
  248. }
  249. }
  250. });
  251. return ConditionalPalettePicker;
  252. });
  253. //# sourceMappingURL=ConditionalPalettePicker.js.map