ColourRange.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery', 'doT', 'text!./ColourRange.html', 'dashboard-analytics/lib/@waca/core-client/js/core-client/ui/properties/BaseProperty', 'dashboard-analytics/lib/@waca/core-client/js/core-client/ui/KeyCodes'], function ($, dot, ColourRangeTemplate, BaseProperty, KeyCodes) {
  8. 'use strict';
  9. var ColourRange = BaseProperty.extend({
  10. /**
  11. * Creates a property with round checkbox.
  12. * @param options.id {string} - property id
  13. * @param options.el {node} - parent element
  14. * @param options.ariaLabel {string} - aria label
  15. * @param options.properties.onSelectCallback {function} - callback function
  16. * @param options.properties.items {array} - array of objects
  17. * Example:[
  18. * { start_label: '0', end_label: '50', color: 'red', selected: true },
  19. * { start_label: '50', end_label: '100', color: 'orange', selected: false },
  20. * { start_label: '100', end_label: 'Max', color: 'green', selected: false }
  21. * ]
  22. */
  23. init: function init(options) {
  24. this.events = {};
  25. this.events['clicktap .property_' + options.id + ' .box-container .box-inner-container .box'] = '_onBoxClick';
  26. this.events['keydown .property_' + options.id + ' .box-container .box-inner-container .box'] = '_onKeyDown';
  27. ColourRange.inherited('init', this, arguments);
  28. this._selectedItemIndex = -1;
  29. },
  30. remove: function remove() {
  31. ColourRange.inherited('remove', this, arguments);
  32. },
  33. /**
  34. * @function ColourRange#doRender
  35. * @description renders the colour range. Currently supporting up to 4 bars
  36. */
  37. doRender: function doRender() {
  38. ColourRange.inherited('doRender', this, arguments);
  39. try {
  40. this.stepCount = this.properties.items.length;
  41. if (this.stepCount > 4) {
  42. throw new Error(this.stepCount + ' bars is not currently supported in the color range widget');
  43. }
  44. if (this.$el.find('.conditional-formatting-colour-range').length === 0) {
  45. this.$el.append($(this._getRenderedHtml()));
  46. }
  47. if (this._selectedItemIndex === -1) {
  48. this._selectedItemIndex = this._findSelectedBoxIndex();
  49. }
  50. this._updateCss();
  51. } catch (e) {
  52. this.glassContext.getCoreSvc('.Logger').error('Unable to render Colour Range component.', e);
  53. }
  54. },
  55. /**
  56. * [10%][color1][color2][10%]
  57. * [20% label][padding][label2][padding][20% label]
  58. * This is to create the padding for the edges so that the box labels center corrently
  59. */
  60. _getPaddingWidth: function _getPaddingWidth() {
  61. //calculate width per box
  62. var boxWidth = 80. / this.stepCount;
  63. //the padding is the remainder from the 20% width for the end-box divs
  64. var leftoverPadding = boxWidth / 2. - 10.;
  65. return leftoverPadding + '%';
  66. },
  67. _updateCss: function _updateCss() {
  68. this._updateBoxSelection();
  69. this._updateLabelSelection();
  70. },
  71. _getRenderedHtml: function _getRenderedHtml() {
  72. var outTemplate = dot.template(ColourRangeTemplate);
  73. if (!outTemplate) {
  74. outTemplate = this.templateString;
  75. }
  76. return outTemplate({
  77. id: this.id,
  78. ariaLabel: this.ariaLabel,
  79. items: this.properties.items,
  80. stepCount: this.stepCount,
  81. paddingWidth: this._getPaddingWidth()
  82. });
  83. },
  84. _onBoxClick: function _onBoxClick(event) {
  85. this._onEventTriggered(event);
  86. },
  87. _onKeyDown: function _onKeyDown(event) {
  88. if (event.keyCode === KeyCodes.SPACE || event.keyCode === KeyCodes.ENTER) {
  89. this._onEventTriggered(event);
  90. }
  91. },
  92. _onEventTriggered: function _onEventTriggered(event) {
  93. this._selectedItemIndex = parseInt(event.currentTarget.dataset.index, 10);
  94. this._updateSelection(this._selectedItemIndex);
  95. this._updateCss();
  96. this.properties.onSelectCallback(this.properties.items[this._selectedItemIndex]);
  97. },
  98. _updateSelection: function _updateSelection(selectedItemIndex) {
  99. for (var i = 0; i < this.properties.items.length; i++) {
  100. if (i === selectedItemIndex) {
  101. this.properties.items[i].selected = true;
  102. } else {
  103. this.properties.items[i].selected = false;
  104. }
  105. }
  106. },
  107. _findSelectedBoxIndex: function _findSelectedBoxIndex() {
  108. var selectedItemIndex = -1;
  109. this.properties.items.forEach(function (item, index) {
  110. if (item.selected === true) {
  111. selectedItemIndex = index;
  112. }
  113. });
  114. return selectedItemIndex;
  115. },
  116. _updateBoxSelection: function _updateBoxSelection() {
  117. var boxes = this.$el.find('.box');
  118. Array.prototype.forEach.call(boxes, function (box) {
  119. box.classList.remove('selected');
  120. });
  121. boxes[this._selectedItemIndex].classList.add('selected');
  122. },
  123. _updateLabelSelection: function _updateLabelSelection() {
  124. var boxLabels = this.$el.find('.box-label');
  125. Array.prototype.forEach.call(boxLabels, function (boxLabel) {
  126. boxLabel.classList.remove('selected');
  127. });
  128. boxLabels[this._selectedItemIndex].classList.add('selected');
  129. boxLabels[this._selectedItemIndex + 1].classList.add('selected');
  130. }
  131. });
  132. return ColourRange;
  133. });
  134. //# sourceMappingURL=ColourRange.js.map