LiveWidgetModel.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../../../lib/@waca/dashboard-common/dist/core/Model', '../../../data/models/Conditions', '../../../data/models/VisPropertyCollection', '../../../filters/Filters', 'underscore'], function (Model, Conditions, VisPropertyCollection, FilterCollection, _) {
  8. /**
  9. * INTENT: LiveWidgetModel is the top level object representation of the liveWidgetSpec.
  10. */
  11. var LiveWidgetModel = null;
  12. LiveWidgetModel = Model.extend({
  13. nestedModels: {
  14. // data: VisDataModel,
  15. // slotmapping: VisMappingModel,
  16. conditions: Conditions
  17. },
  18. nestedCollections: {
  19. properties: VisPropertyCollection,
  20. localFilters: FilterCollection,
  21. searchFilters: FilterCollection
  22. },
  23. localizedProps: ['name', 'titleHtml'],
  24. runtimeAttrs: ['filters'],
  25. init: function init(spec, options) {
  26. options = options || {};
  27. // Data item ids map with the keys corresponding to the generated data item ids and the values corresponding to the new hash Ids
  28. this.dataItemIdsMap = {};
  29. options.widgetModel = this;
  30. if (!spec.properties) {
  31. spec.properties = [];
  32. }
  33. if (!spec.localFilters) {
  34. spec.localFilters = [];
  35. }
  36. if (!spec.data) {
  37. spec.data = {};
  38. }
  39. LiveWidgetModel.inherited('init', this, [spec, options]);
  40. if (spec) {
  41. this.whitelistAttrs = ['type', 'id', 'visId', 'titleHtml', 'titleMode', 'name', 'data', 'visTypeLocked', 'slotmapping', 'augmentation', 'annotations', 'forecasting', 'possibleKeyDrivers', 'customData', 'thumbnailId', 'format'];
  42. for (var item in spec) {
  43. if (spec.hasOwnProperty(item)) {
  44. this.whitelistAttrs.push(item);
  45. }
  46. }
  47. }
  48. // The searchFilters are transient properties of the model
  49. // create the default in here after we set the whitelist.. otherwise they end up bbeing persisted
  50. if (!this.searchFilters) {
  51. this.set({ searchFilters: [] });
  52. }
  53. this.colorProperties.push('elementColor');
  54. },
  55. /**
  56. * Given a property name, will return information about it's multilingualAttribute object
  57. * @override
  58. */
  59. getMultilingualAttribute: function getMultilingualAttribute(propertyName) {
  60. var multilingualAttribute = LiveWidgetModel.inherited('getMultilingualAttribute', this, arguments);
  61. if (multilingualAttribute) {
  62. return multilingualAttribute;
  63. } else if (this.properties && this.properties.getMultilingualAttribute) {
  64. return this.properties.getMultilingualAttribute(propertyName);
  65. }
  66. return null;
  67. },
  68. /**
  69. * Returns all the multilingual properties from this model
  70. * @override
  71. */
  72. getMultilingualAttributes: function getMultilingualAttributes() {
  73. var multilingualAttributes = LiveWidgetModel.inherited('getMultilingualAttributes', this, arguments);
  74. if (this.properties && this.properties.getMultilingualAttributes) {
  75. multilingualAttributes = multilingualAttributes.concat(this.properties.getMultilingualAttributes());
  76. }
  77. return multilingualAttributes;
  78. },
  79. getDataItemIdsMap: function getDataItemIdsMap() {
  80. return this.dataItemIdsMap;
  81. },
  82. set: function set(args, options) {
  83. args = args || {};
  84. //ENSURE WE ARE RUNNING WITHOUT MAPPING - delete the mapping member on every model set.
  85. if (args.mapping) {
  86. delete args.mapping;
  87. }
  88. //In the case where a nested model (data, slotmapping) is being set to {}, we need to re-initialize it.
  89. //TODO: This behaviour should likely be in model.js but currently, the base set checks the empty object case
  90. // and does nothing(??). Keep the scope to the 2 nested models for liveWidgetModel until more base class testing is done.
  91. _.forEach(args, function (specItem, key) {
  92. if (this.nestedModels[key], _.isObject(specItem) && _.isEmpty(specItem)) {
  93. var nestedModelClass = this.nestedModels[key];
  94. if (nestedModelClass) {
  95. //@todo Should not be doing this. Instead go through the correct
  96. //way of setting the model. For example:
  97. //this.set({ [key]: new nestedModelClass(/* empty model */null, {widgetModel: this})}, options)
  98. //This way the generic Model.set ensures that the model is listinen to events
  99. this[key] = new nestedModelClass( /* empty model */null, {
  100. widgetModel: this
  101. });
  102. }
  103. }
  104. }.bind(this));
  105. if (options) {
  106. options.widgetModel = this;
  107. }
  108. LiveWidgetModel.inherited('set', this, arguments);
  109. },
  110. cloneWidget: function cloneWidget(idMap) {
  111. var clone = new LiveWidgetModel(this.toJSON());
  112. clone.replaceIds(idMap);
  113. return clone;
  114. },
  115. toJSON: function toJSON() {
  116. var jsonObj = LiveWidgetModel.inherited('toJSON', this, arguments);
  117. if (jsonObj.localFilters && jsonObj.localFilters.length === 0) {
  118. delete jsonObj['localFilters'];
  119. }
  120. if (jsonObj.properties && jsonObj.properties.length === 0) {
  121. delete jsonObj['properties'];
  122. }
  123. return jsonObj;
  124. },
  125. /**
  126. * @override
  127. */
  128. getUsedCustomColors: function getUsedCustomColors(customColors) {
  129. var _this = this;
  130. var usedColors = LiveWidgetModel.inherited('getUsedCustomColors', this, arguments);
  131. if (this.colorProperties && this.properties) {
  132. this.colorProperties.forEach(function (propName) {
  133. var visProperty = _this.properties.get(propName);
  134. if (visProperty) {
  135. var val = visProperty.getValue();
  136. customColors.ids.forEach(function (color, index) {
  137. if (val && val.indexOf(color) !== -1) {
  138. usedColors.push(customColors.fills[index]);
  139. }
  140. });
  141. }
  142. });
  143. }
  144. return usedColors;
  145. }
  146. });
  147. return LiveWidgetModel;
  148. });
  149. //# sourceMappingURL=LiveWidgetModel.js.map