VisPropertyCollection.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/Collection', './VisProperty', 'underscore'], function (Collection, VisProperty, _) {
  8. 'use strict';
  9. /**
  10. * A collection of properties used by a visualization
  11. */
  12. var VisPropertyCollection = null;
  13. VisPropertyCollection = Collection.extend({
  14. // The class of objects that this collection manages
  15. modelClass: VisProperty,
  16. // Hard coded list of multilingual visProperties. These properties came from VIPRConfig.js
  17. localizedPropIds: ['itemAxis.title', 'valueAxis.title', 'valueAxis.column.title', 'color.title', 'series.title', 'categories.title', 'values.title', 'size.title', 'locationColor.title', 'pointSize.title', 'pointColor.title', 'pointcolor.title', 'latlongSize.title', 'latlongColor.title', 'valueAxis.line.title', 'baseValueLabel', 'targetValueLabel'],
  18. init: function init(models, options) {
  19. // Each VisProperty is given a reference to the collection so that it can access the visModel. This is for access to the
  20. // theme definitions.
  21. this.modelConstructorOptions = {
  22. collection: this
  23. };
  24. options = options || {};
  25. // Access to the visModel for theme definitions
  26. this.visModel = options.visModel;
  27. VisPropertyCollection.inherited('init', this, arguments);
  28. },
  29. /**
  30. * Silently apply a set of defaults to the property collection, preserving any overrides.
  31. *
  32. * @param defaults The new set of defaults to apply
  33. */
  34. applyDefaults: function applyDefaults(defaults) {
  35. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  36. var currentProps = this.toJSON();
  37. this.reset(defaults, _.extend(options, { silent: true }));
  38. this.set(currentProps, _.extend(options, { remove: false, merge: true, silent: true }));
  39. },
  40. /**
  41. * @param propertySet - A set of properties as stored in a change:properties event (and the widget model),
  42. * @returns an object that contains one or more of the data properties that are defined in this set.
  43. */
  44. _getDataPropertiesAndValues: function _getDataPropertiesAndValues(propertySet) {
  45. var dataProperties = {};
  46. var maintainAxisScales = _.find(propertySet, function (property) {
  47. return property.id === 'maintainAxisScales';
  48. });
  49. dataProperties.maintainAxisScales = maintainAxisScales;
  50. return dataProperties;
  51. },
  52. /**
  53. * Override of Collection.toJSON to only serialize the properties that have an override
  54. */
  55. toJSON: function toJSON() {
  56. // only serialize the properties that have a value
  57. var definedProps = this.filter(function (visProp) {
  58. return visProp.value !== undefined;
  59. });
  60. definedProps = definedProps.map(function (entry) {
  61. if (entry.toJSON) {
  62. return entry.toJSON();
  63. }
  64. return entry;
  65. });
  66. return definedProps;
  67. },
  68. /**
  69. * Given a property name, will return information about it's multilingualAttribute object
  70. */
  71. getMultilingualAttribute: function getMultilingualAttribute(propertyName) {
  72. if (this.localizedPropIds.indexOf(propertyName) === -1) {
  73. return null;
  74. }
  75. var propertyModel = this.get(propertyName);
  76. if (!propertyModel) {
  77. return null;
  78. }
  79. return propertyModel.getMultilingualAttribute('value');
  80. },
  81. /**
  82. * Returns all the multilingual visProperties
  83. */
  84. getMultilingualAttributes: function getMultilingualAttributes() {
  85. var _this = this;
  86. var multilingualAttributes = [];
  87. this.localizedPropIds.forEach(function (propId) {
  88. var multilingualAttribute = _this.getMultilingualAttribute(propId);
  89. if (multilingualAttribute) {
  90. multilingualAttributes.push(multilingualAttribute);
  91. }
  92. });
  93. return multilingualAttributes;
  94. },
  95. /**
  96. * If there has been a change to any 'data oriented property' (one where a change means new data is needed), return true.
  97. * Eg: if maintainAxisScales was not set before and now it is, refresh data.
  98. * AND: if maintainAxisScales was set before and now its not, refresh data.
  99. * @propertySet - the latest property set that may or may not include a data oriented property
  100. * @previousPropertySet - the previous property set that may or may not include a data oriented property
  101. * @returns true if EITHER the current propertySet or previousPropertySet includes a property that would need data refreshed on a change.
  102. */
  103. _refreshDataNeeded: function _refreshDataNeeded(propName) {
  104. return propName === 'maintainAxisScales';
  105. },
  106. /**
  107. * Override the onModelEvent to introduce dataRefresh.
  108. */
  109. _onModelEvent: function _onModelEvent(event) {
  110. event.dataRefresh = this._refreshDataNeeded(event.model.id);
  111. VisPropertyCollection.inherited('_onModelEvent', this, arguments);
  112. }
  113. });
  114. return VisPropertyCollection;
  115. });
  116. //# sourceMappingURL=VisPropertyCollection.js.map