ConditionalPalette.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Cognos Products: BI Dashboard
  8. * (C) Copyright IBM Corp. 2016, 2020
  9. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define(['../../lib/@waca/dashboard-common/dist/core/Model', '../../lib/@waca/core-client/js/core-client/utils/UniqueId', './ConditionalStyleCollection', './ConditionalStyle'], function (Model, UniqueId, ConditionalStyleCollection, ConditionalStyle) {
  12. /**
  13. * The conditional attributes of the widget
  14. */
  15. // Declare the class
  16. return function (_Model) {
  17. _inherits(ConditionalPalette, _Model);
  18. function ConditionalPalette() {
  19. _classCallCheck(this, ConditionalPalette);
  20. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  21. args[_key] = arguments[_key];
  22. }
  23. return _possibleConstructorReturn(this, _Model.call.apply(_Model, [this].concat(args)));
  24. }
  25. ConditionalPalette.prototype.init = function init() {
  26. var _Model$prototype$init;
  27. this.nestedCollections = {
  28. colors: ConditionalStyleCollection
  29. };
  30. this.whitelistAttrs = ['colors'];
  31. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  32. args[_key2] = arguments[_key2];
  33. }
  34. (_Model$prototype$init = _Model.prototype.init).call.apply(_Model$prototype$init, [this].concat(args));
  35. };
  36. ConditionalPalette.prototype.getColors = function getColors() {
  37. return this.colors;
  38. };
  39. ConditionalPalette.prototype.getSize = function getSize() {
  40. return this.colors.size();
  41. };
  42. ConditionalPalette.prototype.sortConditionalPaletteByValue = function sortConditionalPaletteByValue() {
  43. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  44. this.colors.models = this.colors.sortBy('value', options);
  45. };
  46. ConditionalPalette.prototype.addColor = function addColor(color, options) {
  47. this.colors.add(new ConditionalStyle(color), options);
  48. // sort the palette by value
  49. this.sortConditionalPaletteByValue(options);
  50. };
  51. /**
  52. * @param values an array of values that is assumed to be in the same order as what currently exists
  53. */
  54. ConditionalPalette.prototype.updateStyleValues = function updateStyleValues() {
  55. var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  56. var options = arguments[1];
  57. if (values.length === this.colors.size()) {
  58. for (var i = 0; i < this.getSize(); i++) {
  59. this.colors.getColor(i).setValue(values[i], options);
  60. }
  61. }
  62. };
  63. ConditionalPalette.prototype.addColors = function addColors() {
  64. var _this2 = this;
  65. var colors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  66. var options = arguments[1];
  67. colors.forEach(function (color) {
  68. _this2.colors.add(new ConditionalStyle(color), options);
  69. });
  70. // sort the palette by value
  71. if (this.colors.size() > 0) {
  72. this.sortConditionalPaletteByValue(options);
  73. }
  74. };
  75. ConditionalPalette.prototype.getColor = function getColor(index) {
  76. return this.colors.getColor(index);
  77. };
  78. ConditionalPalette.prototype.getColorByValue = function getColorByValue(value) {
  79. var match = null;
  80. var valueToValidate = parseFloat(value);
  81. if (!isNaN(valueToValidate)) {
  82. var colors = this.colors.getColors();
  83. var nextColor = null;
  84. for (var i = 0; i < colors.length;) {
  85. var color = colors[i++];
  86. nextColor = i < colors.length ? colors[i] : null;
  87. // Checking the second last to last range is an inclusive test (<= and >=) where as
  88. // checking all other ranges is left inclusive only (<= and >)
  89. if (!nextColor || color.getValue() <= value && nextColor.getValue() > value || i === colors.length - 1 && color.getValue() <= value && nextColor.getValue() >= value) {
  90. match = color;
  91. break;
  92. }
  93. }
  94. }
  95. return match;
  96. };
  97. ConditionalPalette.prototype.updateColors = function updateColors(colors, options) {
  98. if (colors) {
  99. this.set({ colors: colors }, options);
  100. }
  101. };
  102. ConditionalPalette.prototype.removeStyle = function removeStyle(index, options) {
  103. this.removeStyles(index, 1, options);
  104. };
  105. /**
  106. * Remove x number of colors and triggers one update event
  107. */
  108. ConditionalPalette.prototype.removeStyles = function removeStyles(index, count, options) {
  109. for (var i = 0; i < count; i++) {
  110. var model = this.getColor(index);
  111. if (model) {
  112. this.colors.remove(model, options);
  113. }
  114. }
  115. if (index === this.getSize()) {
  116. var lastItem = this.getColor(index - 1);
  117. lastItem.clearStyle();
  118. }
  119. };
  120. ConditionalPalette.prototype.clear = function clear(options) {
  121. if (this.colors) {
  122. this.colors.reset([], options);
  123. }
  124. };
  125. ConditionalPalette.createUndoRedoTransactionOptionsId = function createUndoRedoTransactionOptionsId(action) {
  126. return {
  127. payloadData: {
  128. undoRedoTransactionId: UniqueId.get('conditional_palette_' + action + '_')
  129. }
  130. };
  131. };
  132. return ConditionalPalette;
  133. }(Model);
  134. });
  135. //# sourceMappingURL=ConditionalPalette.js.map