ConditionalStepsProperty.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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: Dashboard
  8. * (C) Copyright IBM Corp. 2019
  9. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define(['../../../../lib/@waca/core-client/js/core-client/ui/properties/BaseProperty', '../../../../lib/@waca/core-client/js/core-client/ui/properties/PropertyUIControl', '../../../../widgets/livewidget/nls/StringResources', '../api/impl/DeprecatedConditionalFormatting', '../../../../util/DashboardFormatter', './IconPicker'], function (BaseProperty, PropertyUIControl, StringResources, DeprecatedConditionalFormatting, Formatter, IconPicker) {
  12. var ConditionalFormattingProperty = function (_BaseProperty) {
  13. _inherits(ConditionalFormattingProperty, _BaseProperty);
  14. function ConditionalFormattingProperty() {
  15. _classCallCheck(this, ConditionalFormattingProperty);
  16. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  17. args[_key] = arguments[_key];
  18. }
  19. return _possibleConstructorReturn(this, _BaseProperty.call.apply(_BaseProperty, [this].concat(args)));
  20. }
  21. ConditionalFormattingProperty.prototype.init = function init(options) {
  22. _BaseProperty.prototype.init.call(this, options);
  23. this.dashboardApi = options.dashboardApi;
  24. this._glassContext = options.glassContext;
  25. this._conditionalFormattingFeature = options.conditionalFormattingFeature;
  26. this._paletteColors = options.paletteColors;
  27. this._colorsService = this.dashboardApi.getFeature('Colors');
  28. this._iconPicker = options.iconPicker || this._createIconPicker();
  29. this._addButton = options.addButton !== undefined ? !!options.addButton : true;
  30. };
  31. ConditionalFormattingProperty.prototype.doRender = function doRender() {
  32. if (this._propertyUIControl) {
  33. this._propertyUIControl.remove();
  34. }
  35. this._selectedRangeIndex = this._conditionalFormattingFeature.getSelectedRangeIndex();
  36. this._ranges = this._conditionalFormattingFeature.getRanges();
  37. this._selectedRange = this._ranges[this._selectedRangeIndex];
  38. this._percentageRanges = this._conditionalFormattingFeature.getRangeScale() === DeprecatedConditionalFormatting.RANGE_SCALE_PERCENTAGE;
  39. this._currentStartValue = this._conditionalFormattingFeature.getStartValueForRange(this._selectedRangeIndex);
  40. this._currentEndValue = this._conditionalFormattingFeature.getEndValueForRange(this._selectedRangeIndex);
  41. this._propertyUIControl = new PropertyUIControl(this._getPropertySpec());
  42. return this._propertyUIControl.render();
  43. };
  44. ConditionalFormattingProperty.prototype.createCustomColor = function createCustomColor(hex) {
  45. var className = this._colorsService.addCustomColor(hex);
  46. var colorProp = this._colorsService.createColorDefinition(className, hex, hex);
  47. colorProp.type = 'ColorCode';
  48. colorProp.hidden = true;
  49. return colorProp;
  50. };
  51. ConditionalFormattingProperty.prototype._createIconPicker = function _createIconPicker() {
  52. var _this2 = this;
  53. var container = document.body;
  54. var shapeNameList = this._conditionalFormattingFeature.getShapeList();
  55. var icons = shapeNameList.map(function (shapeName) {
  56. var shapeDef = _this2._conditionalFormattingFeature.getShapeDefinition(shapeName);
  57. return { id: shapeDef.id, label: shapeDef.label, name: shapeName };
  58. });
  59. var properties = {
  60. icons: icons,
  61. onIconSelected: this._handleIconSelect.bind(this),
  62. containerNode: container
  63. };
  64. return new IconPicker(properties);
  65. };
  66. ConditionalFormattingProperty.prototype._getPropertySpec = function _getPropertySpec() {
  67. var _this3 = this;
  68. var iconsFeature = this.dashboardApi.getFeature('Icons');
  69. var trashIcon = iconsFeature.getIcon('TrashIcon');
  70. var restoreIcon = iconsFeature.getIcon('RestoreIcon');
  71. var reverseIcon = iconsFeature.getIcon('ReverseIcon');
  72. var deleteSelectedStepAction = {
  73. type: 'icon',
  74. svgIcon: trashIcon.id,
  75. iconTooltip: StringResources.get('conditionalFormattingDeleteStep'),
  76. class: 'deleteSelectedStepAction'
  77. };
  78. if (this._ranges.length > 1) {
  79. deleteSelectedStepAction.clickCallback = function () {
  80. _this3._conditionalFormattingFeature.removeRange(_this3._selectedRangeIndex);
  81. };
  82. } else {
  83. deleteSelectedStepAction.disabled = true;
  84. deleteSelectedStepAction.class += ' stepActionDisabled';
  85. }
  86. var childPropertyItems = [{
  87. type: 'SingleLineLinks',
  88. id: 'selectedSteps',
  89. name: 'selectedSteps',
  90. items: [{
  91. align: 'left',
  92. items: [{
  93. type: 'text',
  94. class: 'boldPropertyText',
  95. value: StringResources.get('conditionalFormattingSelectedStep')
  96. }]
  97. }, {
  98. align: 'right',
  99. items: [deleteSelectedStepAction, {
  100. type: 'icon',
  101. svgIcon: restoreIcon.id,
  102. iconTooltip: StringResources.get('conditionalFormattingRestoreSteps'),
  103. class: 'restoreStepsAction',
  104. clickCallback: function clickCallback() {
  105. _this3._conditionalFormattingFeature.resetRanges();
  106. }
  107. }, {
  108. type: 'icon',
  109. svgIcon: reverseIcon.id,
  110. iconTooltip: StringResources.get('conditionalFormattingReversePalette'),
  111. class: 'reversePaletteAction',
  112. clickCallback: function clickCallback() {
  113. _this3._conditionalFormattingFeature.reverseColors();
  114. }
  115. }]
  116. }]
  117. }, {
  118. id: 'conditionalColorRange',
  119. module: 'dashboard-analytics/features/widget/conditionalFormatting/properties/ColourRange',
  120. properties: {
  121. onSelectCallback: function onSelectCallback(selectedRange) {
  122. _this3._conditionalFormattingFeature.setSelectedRangeIndex(selectedRange.rangeIndex);
  123. _this3.doRender();
  124. },
  125. items: this._buildColorRangePayload()
  126. }
  127. }, {
  128. id: 'startValue',
  129. name: 'startValue',
  130. type: 'InputLabel',
  131. label: StringResources.get('conditionalFormattingStartValue'),
  132. value: this._getFormattedStartValue(),
  133. handleReturnKey: true,
  134. decimalPlaces: 2,
  135. placeHolderText: this._selectedRangeIndex === 0 ? StringResources.get('conditionalFormattingMin') : '',
  136. newLook: true,
  137. ellipses: true,
  138. inputStyles: this._percentageRanges ? 'width: 80px' : '',
  139. showInlineError: true,
  140. customValidatorCallback: function customValidatorCallback(startValue) {
  141. return _this3._conditionalFormattingFeature.checkValidStartValueForRange(_this3._getNumberFromFormattedValue(startValue), _this3._selectedRangeIndex);
  142. },
  143. onChange: function onChange(propertyName, startValue) {
  144. var value = _this3._getNumberFromFormattedValue(startValue);
  145. if (value === _this3._currentStartValue) {
  146. // If the value hasn't changed, make sure the input control is still showing the formatted value
  147. _this3._propertyUIControl.getProperty('startValue').setValue(_this3._getFormattedStartValue());
  148. } else {
  149. _this3._conditionalFormattingFeature.setStartValueForRange(value, _this3._selectedRangeIndex);
  150. }
  151. }
  152. }, {
  153. id: 'endValue',
  154. name: 'endValue',
  155. type: 'InputLabel',
  156. label: StringResources.get('conditionalFormattingEndValue'),
  157. value: this._getFormattedEndValue(),
  158. handleReturnKey: true,
  159. decimalPlaces: 2,
  160. placeHolderText: this._selectedRangeIndex === this._ranges.length - 1 ? StringResources.get('conditionalFormattingMax') : '',
  161. newLook: true,
  162. ellipses: true,
  163. inputStyles: this._percentageRanges ? 'width: 80px' : '',
  164. showInlineError: true,
  165. customValidatorCallback: function customValidatorCallback(endValue) {
  166. return _this3._conditionalFormattingFeature.checkValidEndValueForRange(_this3._getNumberFromFormattedValue(endValue), _this3._selectedRangeIndex);
  167. },
  168. onChange: function onChange(propertyName, endValue) {
  169. var value = _this3._getNumberFromFormattedValue(endValue);
  170. if (value === _this3._currentEndValue) {
  171. // If the value hasn't changed, make sure the input control is still showing the formatted value
  172. _this3._propertyUIControl.getProperty('endValue').setValue(_this3._getFormattedEndValue());
  173. } else {
  174. _this3._conditionalFormattingFeature.setEndValueForRange(value, _this3._selectedRangeIndex);
  175. }
  176. }
  177. }, {
  178. id: 'rangeColor',
  179. name: 'rangeColor',
  180. type: 'ColorPicker',
  181. label: StringResources.get('conditionalFormattingTextColor'),
  182. open: false,
  183. ariaLabel: StringResources.get('conditionalFormattingTextColor'),
  184. paletteType: 'ConditionalPalette',
  185. showHexValue: true,
  186. onChange: function onChange(propertyName, valueInfo) {
  187. _this3._conditionalFormattingFeature.setColorIdForRange(valueInfo.name, _this3._selectedRangeIndex);
  188. },
  189. createCustomColor: this.createCustomColor.bind(this),
  190. items: this._getColorPickerItems(),
  191. addButton: this._addButton,
  192. colorClass: true,
  193. selectedName: this._conditionalFormattingFeature.getColorIdForRange(this._selectedRangeIndex)
  194. }];
  195. childPropertyItems.push(this._createIconPickerPropertySpec());
  196. return {
  197. el: this.$el,
  198. glassContext: this._glassContext,
  199. primaryUIControl: false,
  200. items: childPropertyItems
  201. };
  202. };
  203. ConditionalFormattingProperty.prototype._createIconPickerPropertySpec = function _createIconPickerPropertySpec() {
  204. var _this4 = this;
  205. var shape = this._conditionalFormattingFeature.getShapeForRange(this._selectedRangeIndex);
  206. var iconDef = this._conditionalFormattingFeature.getShapeDefinition(shape);
  207. return {
  208. id: 'conditionalFormattingShape',
  209. name: 'conditionalFormattingShape',
  210. type: 'SingleLineLinks',
  211. items: [{
  212. align: 'left',
  213. items: [{
  214. type: 'text',
  215. id: 'conditionalFormattingShapeText',
  216. value: StringResources.get('conditionalFormattingShapeIndicator')
  217. }]
  218. }, {
  219. align: 'right',
  220. items: [{
  221. type: 'icon',
  222. id: 'conditionalFormattingShapeIcon',
  223. name: 'conditionalFormattingShapeIcon',
  224. svgIcon: iconDef.id,
  225. iconTooltip: iconDef.label,
  226. clickCallback: function clickCallback(evt) {
  227. _this4._toggleShapeIndicator(evt);
  228. }
  229. }]
  230. }]
  231. };
  232. };
  233. ConditionalFormattingProperty.prototype._toggleShapeIndicator = function _toggleShapeIndicator(evt) {
  234. if (this._iconPicker.isOpen()) {
  235. this._iconPicker.close();
  236. return;
  237. }
  238. this._iconPicker.open(evt.currentTarget);
  239. };
  240. ConditionalFormattingProperty.prototype._handleIconSelect = function _handleIconSelect(icon) {
  241. this._conditionalFormattingFeature.setShapeForRange(icon.name, this._selectedRangeIndex);
  242. };
  243. ConditionalFormattingProperty.prototype._getFormattedStartValue = function _getFormattedStartValue() {
  244. return this._formatValueForInput(this._currentStartValue);
  245. };
  246. ConditionalFormattingProperty.prototype._getFormattedEndValue = function _getFormattedEndValue() {
  247. return this._formatValueForInput(this._currentEndValue);
  248. };
  249. ConditionalFormattingProperty.prototype._getColorPickerItems = function _getColorPickerItems() {
  250. var propertyItems = [];
  251. if (!this._paletteColors) {
  252. return [];
  253. }
  254. this._paletteColors.forEach(function (color) {
  255. propertyItems.push({
  256. type: 'ColorCode',
  257. value: color.hexValue,
  258. name: color.name,
  259. label: color.label
  260. });
  261. });
  262. var ranges = this._conditionalFormattingFeature.getRanges();
  263. var range = ranges[this._selectedRangeIndex];
  264. if (range && range.userSetColorId && this._colorsService.isCustomColor(range.userSetColorId)) {
  265. var hexValue = this._colorsService.getHexColorFromClassName(range.userSetColorId);
  266. propertyItems.push({
  267. type: 'ColorCode',
  268. value: hexValue,
  269. name: range.userSetColorId,
  270. label: hexValue,
  271. hidden: true
  272. });
  273. }
  274. return propertyItems;
  275. };
  276. ConditionalFormattingProperty.prototype._getNumberFromFormattedValue = function _getNumberFromFormattedValue(value) {
  277. var stringValue = value + '';
  278. if (stringValue.endsWith('%')) {
  279. stringValue = stringValue.substring(0, stringValue.length - 1);
  280. }
  281. if (value === '') {
  282. return null;
  283. }
  284. return Formatter.getNumberFromFormattedValue(stringValue);
  285. };
  286. ConditionalFormattingProperty.prototype._formatValueForInput = function _formatValueForInput(value) {
  287. if (value === null) {
  288. return '';
  289. }
  290. var number = Formatter.format(value, {
  291. maximumFractionDigits: 2,
  292. type: 'number'
  293. });
  294. if (this._percentageRanges) {
  295. number += '%';
  296. }
  297. return number;
  298. };
  299. ConditionalFormattingProperty.prototype._formatValueForRangeControlLabel = function _formatValueForRangeControlLabel(value) {
  300. var number = Formatter.format(value, {
  301. maximumFractionDigits: 2,
  302. decimalFormatLength: 'short',
  303. type: 'number'
  304. });
  305. return number;
  306. };
  307. ConditionalFormattingProperty.prototype._buildColorRangePayload = function _buildColorRangePayload() {
  308. var _this5 = this;
  309. var items = [];
  310. this._ranges.forEach(function (range, index) {
  311. items.push({
  312. start_label: range.startValue === null ? StringResources.get('conditionalFormattingMin') : _this5._formatValueForRangeControlLabel(range.startValue),
  313. end_label: range.endValue === null ? StringResources.get('conditionalFormattingMax') : _this5._formatValueForRangeControlLabel(range.endValue),
  314. color: _this5._conditionalFormattingFeature.getColorHexForRange(index),
  315. rangeIndex: index
  316. });
  317. });
  318. items[this._selectedRangeIndex].selected = true;
  319. return items;
  320. };
  321. return ConditionalFormattingProperty;
  322. }(BaseProperty);
  323. return ConditionalFormattingProperty;
  324. });
  325. //# sourceMappingURL=ConditionalStepsProperty.js.map