FredIsRedModel.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2017, 2020
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['gemini/lib/@waca/dashboard-common/dist/core/Model', 'underscore'], function (Model, _) {
  13. 'use strict';
  14. /**
  15. * The fredIsRedModel implements the boardModelExtension defined in fredIsRed.json
  16. * It is used to persist information about mapped colours etc.
  17. */
  18. var FredIsRedModel = Model.extend({
  19. // constructor
  20. /**
  21. * The fredIsRedModel will persist a saveId (incremented each time a user saves)
  22. * and a colorMap (with the labels/virtual palette indexes selected based on the data).
  23. * The saveId can be used to optimize the table on save (though this is not yet implemented)
  24. */
  25. whitelistAttrs: ['id', 'colorMap', 'saveId'],
  26. //Pick a 'virtual palette size' that is larger than all palettes (Most palettes are multiples of 4 so a multiple of 4 has been chosen).
  27. _VIRTUAL_PALETTE_SIZE: 48,
  28. init: function init(params) {
  29. FredIsRedModel.inherited('init', this, arguments);
  30. this.colorMap = params && params.colorMap || {};
  31. this.saveId = params && params.saveId || 0;
  32. },
  33. destroy: function destroy() {
  34. FredIsRedModel.inherited('destroy', this, arguments);
  35. },
  36. /**
  37. * @returns the colorMap property of the fredIsRed model
  38. */
  39. getColorMap: function getColorMap() {
  40. return this.get('colorMap') || {};
  41. },
  42. /**
  43. * Find a colorIndex in a virtual palette for the passed in label.
  44. *
  45. * whose value is a colour index in 'virtual palette domain' and tag it with the saveId.
  46. * @param label - a label to add an entry for.
  47. */
  48. getColorIndex: function getColorIndex(label) {
  49. var colorMapEntry = this.colorMap[label];
  50. var colorIndex = colorMapEntry && colorMapEntry.v;
  51. if (colorIndex === undefined) {
  52. colorIndex = _.keys(this.colorMap).length % this._VIRTUAL_PALETTE_SIZE;
  53. this.colorMap[label] = { v: colorIndex, s: this.saveId };
  54. } else {
  55. colorMapEntry.s = this.saveId;
  56. }
  57. return colorIndex;
  58. },
  59. /**
  60. * Keep track of what values have been used by associating a value with a saveId.
  61. */
  62. updateSaveId: function updateSaveId() {
  63. this.saveId = (this.get('saveId') || 0) + 1;
  64. },
  65. /**
  66. * setUserMap - sets color map - not used atm, exists for completeness
  67. * @param colorMap
  68. */
  69. setColorMap: function setColorMap(colorMap) {
  70. this.set('userPalette', colorMap);
  71. }
  72. });
  73. return FredIsRedModel;
  74. });
  75. //# sourceMappingURL=FredIsRedModel.js.map