WidgetPropertiesProvider.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore', '../../../lib/@waca/dashboard-common/dist/api/PropertiesProviderAPI', '../../../lib/@waca/dashboard-common/dist/core/APIFactory'], function (_, PropertiesProviderAPI, APIFactory) {
  9. var WidgetPropertiesProvider = function () {
  10. function WidgetPropertiesProvider(_ref) {
  11. var dashboardAPI = _ref.dashboardAPI,
  12. content = _ref.content,
  13. features = _ref.features;
  14. _classCallCheck(this, WidgetPropertiesProvider);
  15. this.content = content;
  16. this.dashboard = dashboardAPI;
  17. this._api = APIFactory.createAPI(this, [PropertiesProviderAPI]);
  18. features.Properties.registerProvider(this.getAPI());
  19. this.type = this.content.getType().split('.').splice(1).join('.');
  20. }
  21. WidgetPropertiesProvider.prototype._isHandledByTheContentProps = function _isHandledByTheContentProps(id) {
  22. // temporary until we upgrade the widget proeprties
  23. return id === 'datagrid.viewOption';
  24. };
  25. WidgetPropertiesProvider.prototype.getAPI = function getAPI() {
  26. return this._api;
  27. };
  28. WidgetPropertiesProvider.prototype.getContributionSpec = function getContributionSpec() {
  29. //TODO cache spec ?
  30. //if( !this._spec ) {
  31. // getWidgetRegistry returns a spec that is DEEP FROZEN.
  32. var spec = this.dashboard.getFeature('internal.deprecated').getWidgetRegistry()[this.type];
  33. // Since any contribute property layouts can potentially be merged with other contributed properties, we need to ensure the spec is cloned.
  34. this._spec = JSON.parse(JSON.stringify(spec));
  35. //}
  36. return this._spec;
  37. };
  38. WidgetPropertiesProvider.prototype.getWidgetModel = function getWidgetModel() {
  39. return this.content.getFeature('Models.internal').getWidgetModel();
  40. };
  41. WidgetPropertiesProvider.prototype.getPropertyLayoutList = function getPropertyLayoutList() {
  42. var contributionSpec = this.getContributionSpec();
  43. if (!this._propertyLayoutList) {
  44. this._propertyLayoutList = [];
  45. if (contributionSpec && contributionSpec.propertyLayoutList) {
  46. var _propertyLayoutList;
  47. (_propertyLayoutList = this._propertyLayoutList).push.apply(_propertyLayoutList, contributionSpec.propertyLayoutList.slice());
  48. }
  49. }
  50. // Return a copy of our cached property list
  51. return this._propertyLayoutList.slice();
  52. };
  53. /**
  54. * @param {boolean} [overrideTheme] - set to true if the default colors for the widget should not be updated w.r.t. theme
  55. */
  56. WidgetPropertiesProvider.prototype.getPropertyList = function getPropertyList(overrideTheme) {
  57. var _this = this;
  58. var contributionSpec = this.getContributionSpec();
  59. if (!this._propertyList) {
  60. if (contributionSpec && contributionSpec.propertyList) {
  61. this._propertyList = JSON.parse(JSON.stringify(contributionSpec.propertyList));
  62. } else {
  63. this._propertyList = [];
  64. }
  65. // Default the setPropertyValue and getPropertyValue methods
  66. this._propertyList.forEach(function (property) {
  67. // If the property is tagged as multilingual and we're in translation mode and we're missing the translation for the current locale
  68. if (property.editor && property.editor.uiControl && property.editor.uiControl.multilingual) {
  69. var translationService = _this.dashboard.getFeature('TranslationService');
  70. if (translationService) {
  71. var _ref2 = _this.getWidgetModel().getMultilingualAttribute(property.id) || {},
  72. multilingualProperty = _ref2.multilingualProperty;
  73. translationService.processMultilingualProperty(property, multilingualProperty);
  74. }
  75. }
  76. var _customValidatorCallback = property.editor && property.editor.uiControl && property.editor.uiControl.customValidatorCallback;
  77. if (_customValidatorCallback && typeof _this[_customValidatorCallback] === 'function') {
  78. property.editor.uiControl.customValidatorCallback = _this[_customValidatorCallback].bind(_this);
  79. }
  80. if (!_this._isHandledByTheContentProps(property.id)) {
  81. // set the prop gettter and setter to sie the existing model location for widget props
  82. // until those are handled by the content props.
  83. if (!property.getPropertyValue) {
  84. property.getPropertyValue = function () {
  85. return _this._getPropertyValue(property.id);
  86. };
  87. }
  88. if (!property.setPropertyValue) {
  89. property.setPropertyValue = function (propertyValue) {
  90. _this.setPropertyValue(property.id, propertyValue);
  91. };
  92. }
  93. }
  94. });
  95. }
  96. // Return a copy of our cached property list
  97. var propertyList = this._propertyList.slice();
  98. if (!overrideTheme) {
  99. this._setDefaultSpecPropertyColorForCurrentTheme('border' /*themeId*/, 'borderColor' /*specId*/, propertyList);
  100. this._setDefaultSpecPropertyColorForCurrentTheme('backgroundColor' /*themeId*/, 'fillColor' /*specId*/, propertyList);
  101. }
  102. return propertyList;
  103. };
  104. /**
  105. * Get the current theme default color value for the specified property id.
  106. * @param themeId - ID to use when asking the theme for this color property
  107. * @returns defaultSpecColor - a valid color with which a properties default value can be set to, or null.
  108. */
  109. WidgetPropertiesProvider.prototype._getDefaultThemeColorForCurrentTheme = function _getDefaultThemeColorForCurrentTheme(themeId) {
  110. var colors = this.dashboard.getFeature('Colors');
  111. var defaultSpecColor = colors.getPropertyForUIElement('widget', themeId).value;
  112. // The value may be equal to a simple hex or variable name or it may be an object which we have to dig into a little more.
  113. if (_.isObject(defaultSpecColor)) {
  114. defaultSpecColor = defaultSpecColor.color;
  115. }
  116. // I love themes and the theme definition object <insert sarcasm smirk here>. The color may come back
  117. // as a nice hex value or as a variable that needs to be mapped. If its a variable it will begin with
  118. // a '$' and we have to map it to get the hex value.
  119. if (defaultSpecColor.substring(0, 1) === '$') {
  120. defaultSpecColor = colors.getValueForVariable('Color', defaultSpecColor.substr(1));
  121. }
  122. // Ensure we have a valid hex value or transparent
  123. if (defaultSpecColor.substring(0, 1) === '#' && !isNaN(parseInt(defaultSpecColor.substr(1), 16)) || defaultSpecColor === 'transparent') {
  124. return defaultSpecColor;
  125. }
  126. return null;
  127. };
  128. /**
  129. * set the current theme default value for the specified property ids.
  130. * @param themeId - ID to use when asking the theme for this color property
  131. * @param specPropId - ID to use when getting the property from the items array
  132. * @param items - array of properties that better hold a property with an id = specPropId
  133. */
  134. WidgetPropertiesProvider.prototype._setDefaultSpecPropertyColorForCurrentTheme = function _setDefaultSpecPropertyColorForCurrentTheme(themeId, specPropId, items) {
  135. // Ensure the property specified actually is in the items list
  136. // Also ensure we have a valid hex value
  137. var specColorProp = _.find(items, function (item) {
  138. return item.id === specPropId;
  139. });
  140. var defaultSpecColor = this._getDefaultThemeColorForCurrentTheme(themeId);
  141. if (!specColorProp || defaultSpecColor === null) {
  142. return Promise.resolve();
  143. }
  144. var colors = this.dashboard.getFeature('Colors').getDashboardColorSet();
  145. var colorMatched = _.find(colors, function (spec) {
  146. return spec.hexValue === defaultSpecColor;
  147. });
  148. if (!colorMatched) {
  149. colorMatched = colors[colors.length - 1];
  150. }
  151. specColorProp.editor.uiControl.defaultValue = colorMatched.id;
  152. };
  153. WidgetPropertiesProvider.prototype._getPropertyValue = function _getPropertyValue(propertyName) {
  154. return this.getWidgetModel().get(propertyName);
  155. };
  156. /**
  157. * Derived widgets may override
  158. * @param {string} propertyName
  159. * @param {*} propertyValue
  160. */
  161. WidgetPropertiesProvider.prototype.setPropertyValue = function setPropertyValue(propertyName, propertyValue) {
  162. var colors = this.dashboard.getFeature('Colors');
  163. var propertyPayload = {};
  164. propertyPayload[propertyName] = propertyValue;
  165. if (propertyName === 'fillColor' || propertyName === 'borderColor') {
  166. if (propertyValue) {
  167. propertyPayload[propertyName] = colors.getColorClassName(propertyValue);
  168. } else {
  169. propertyPayload[propertyName] = null;
  170. }
  171. colors.prepareForColorModelChange(propertyPayload, propertyName);
  172. }
  173. this.set(propertyPayload, {
  174. payloadData: { skipUndoRedo: true }
  175. });
  176. };
  177. WidgetPropertiesProvider.prototype.set = function set(attributes, options) {
  178. if (attributes) {
  179. options = options || {};
  180. options = _.defaults(options, {
  181. sender: this.id
  182. });
  183. this.getWidgetModel().set(attributes, options);
  184. }
  185. };
  186. return WidgetPropertiesProvider;
  187. }();
  188. return WidgetPropertiesProvider;
  189. });
  190. //# sourceMappingURL=WidgetPropertiesProvider.js.map