FredIsRed.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Business Analytics (C) Copyright IBM Corp. 2018, 2020
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. /**
  10. * @class FredIsRed
  11. * @hideconstructor
  12. * @classdesc
  13. */
  14. define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', './FredIsRedAPI'], function (APIFactory, FredIsRedAPI) {
  15. var FredIsRed = function () {
  16. function FredIsRed() {
  17. _classCallCheck(this, FredIsRed);
  18. }
  19. _createClass(FredIsRed, [{
  20. key: 'initializeFredIsRedFeature',
  21. /**
  22. * initializeFredIsRedFeature initializes FredIsRed
  23. * @param fredIsRedModel the FredIsRed's model initialized by BoardModelExtension
  24. */
  25. value: function initializeFredIsRedFeature(fredIsRedModel) {
  26. if (!this.isInitialized()) {
  27. this.model = fredIsRedModel;
  28. this.model.updateSaveId();
  29. this.model.getColorMap();
  30. this._preloadUserPaletteValues();
  31. this.keyProviders = [];
  32. }
  33. }
  34. /**
  35. * isInitialized checks if FredIsRed instance is initialized
  36. * @returns {boolean} if FredIsRed has been initialized
  37. */
  38. }, {
  39. key: 'isInitialized',
  40. value: function isInitialized() {
  41. return this.model;
  42. }
  43. /**
  44. * getAPI is an api interface
  45. * @returns an instance of given API
  46. */
  47. }, {
  48. key: 'getAPI',
  49. value: function getAPI() {
  50. if (!this.api) {
  51. this.api = APIFactory.createAPI(this, [FredIsRedAPI]);
  52. }
  53. return this.api;
  54. }
  55. /**
  56. * registerKeyProvider is function that registeres key provider
  57. * @param keyProvider an object that includes key provider to be registered
  58. */
  59. }, {
  60. key: 'registerKeyProvider',
  61. value: function registerKeyProvider() {
  62. var keyProvider = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  63. if (!this.keyProviders.includes(keyProvider)) {
  64. this.keyProviders.push(keyProvider);
  65. }
  66. }
  67. /**
  68. * getColor is the main API to a renderer.
  69. * it honours an existing sharedPalette but, if the value is not there, augments it.
  70. * @param paletteInfo an object that includes a color table, a default index and a Class to be returned with the color
  71. * (where the color expects an RGB object).
  72. * @param labelArray - an array of 1 or more labels that make up a tuple (label value) to be coloured eg: ['Canada'] or ['Canada', '2005']
  73. * @param {DataItemAPI} dataItem - data item type
  74. * @returns an instance of the ColorClass.
  75. */
  76. }, {
  77. key: 'getColor',
  78. value: function getColor(paletteInfo, labelArray, dataItem) {
  79. var label = void 0;
  80. label = this._getkeyUsingProviders(labelArray, dataItem);
  81. if (!label) {
  82. label = labelArray && labelArray.join();
  83. }
  84. if (label) {
  85. if (this._userPalette[label]) {
  86. var userValue = this._userPalette[label];
  87. return new paletteInfo.colorCallback(userValue.r, userValue.g, userValue.b);
  88. }
  89. //Find a colour index in a virtual domain, then adjust it to the palette size.
  90. var virtualColorIndex = this.model.getColorIndex(label);
  91. var selectedColor = paletteInfo.colors[virtualColorIndex % paletteInfo.colors.length];
  92. return new paletteInfo.colorCallback(selectedColor.r, selectedColor.g, selectedColor.b);
  93. } else if (paletteInfo.defaultIndex !== undefined && paletteInfo.colors && paletteInfo.colors[paletteInfo.defaultIndex]) {
  94. var defaultColor = paletteInfo.colors[paletteInfo.defaultIndex];
  95. return new paletteInfo.colorCallback(defaultColor.r, defaultColor.g, defaultColor.b);
  96. }
  97. //Cant do anything...return black.
  98. return new paletteInfo.colorCallback(0, 0, 0);
  99. }
  100. }, {
  101. key: '_getkeyUsingProviders',
  102. value: function _getkeyUsingProviders(labelArray, dataItem) {
  103. //TODO: currently dataItem, passed from VIDA, is used to get to related details e.g. column id, metadataColumn
  104. //need a better way to handle this, so that it is done more generically (not just for VIDA)
  105. var label = void 0;
  106. if (dataItem) {
  107. this.keyProviders && this.keyProviders.some(function (keyProvider) {
  108. label = keyProvider.getKeyForUserPalette(labelArray, dataItem);
  109. return label ? true : false;
  110. });
  111. }
  112. return label;
  113. }
  114. /**
  115. * TODO: For RTC108319 integration purposes.
  116. * In this Epic, users can define custom values via a glass service or at the spec level (shown here)
  117. * This function will be expanded as this epic/UI is completed.
  118. * In the meantime, the dashboard-level userPalette can be demonstrated by adding entries to any dashboard spec (in the fredIsRed model spec) eg:
  119. * fredIsRed: {
  120. * userPalette: {
  121. * 'Canada': { r:200, g:0, b:0 },
  122. * 'USA': { r:0, g:200, b:0 },
  123. * 'Mexico': { r:0, g:0, b:200 }
  124. * },
  125. * ...
  126. * }
  127. **/
  128. }, {
  129. key: '_preloadUserPaletteValues',
  130. value: function _preloadUserPaletteValues() {
  131. this._userPalette = this.model.get('userPalette') || {};
  132. }
  133. /**
  134. * setUserColorMap - sets user color map
  135. * @param colorMap - object representing user color map
  136. */
  137. }, {
  138. key: 'setUserColorMap',
  139. value: function setUserColorMap(colorMap) {
  140. this._userPalette = colorMap;
  141. }
  142. /**
  143. * getUserColorMap - gets user color map
  144. */
  145. }, {
  146. key: 'getUserColorMap',
  147. value: function getUserColorMap() {
  148. var colorMap = JSON.parse(JSON.stringify(this._userPalette));
  149. return colorMap;
  150. }
  151. }]);
  152. return FredIsRed;
  153. }();
  154. return FredIsRed;
  155. });
  156. //# sourceMappingURL=FredIsRed.js.map