ContentInfoImpl.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Business Analytics (C) Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /**
  9. * @class ContentInfoImpl
  10. * @hideconstructor
  11. * @classdesc Widget Service Implementation class
  12. */
  13. define(['../../../dashboard/dashboardServiceability/nls/StringResources'], function (StringResources) {
  14. var GENERAL_INFO = 'generalInfo';
  15. var ContentInfoImpl = function () {
  16. function ContentInfoImpl(contentApi, logger) {
  17. _classCallCheck(this, ContentInfoImpl);
  18. this._logger = logger;
  19. this._contentAPI = contentApi;
  20. this._data = new Map();
  21. this._isDisplayInfoOnDOM = false;
  22. }
  23. ContentInfoImpl.prototype.getId = function getId() {
  24. return this._contentAPI.getId();
  25. };
  26. /**
  27. * @public
  28. * @function getType
  29. * @description retrieves the type of the widget and if the widget is static it will transform the string to a user friendly
  30. * @return visType the type of the associated widget
  31. */
  32. ContentInfoImpl.prototype.getType = function getType() {
  33. return this._contentAPI.getType();
  34. };
  35. /**
  36. * @public
  37. * @function getVisTypeLabel
  38. * @description retrieves the visualization type label
  39. * @return {string} the label for the visualization type
  40. */
  41. ContentInfoImpl.prototype.getVisTypeLabel = function getVisTypeLabel() {
  42. var generalInfo = this.getData(GENERAL_INFO);
  43. var visLabel = '';
  44. if (generalInfo.getLabel !== undefined) {
  45. visLabel = generalInfo.getLabel();
  46. } else {
  47. // Remove the "widget." from the string "widget.shape" and capitalize the first letter
  48. visLabel = this._contentAPI.getType();
  49. visLabel = visLabel.slice(7, visLabel.length);
  50. visLabel = visLabel[0].toUpperCase() + visLabel.slice(1);
  51. }
  52. return visLabel;
  53. };
  54. ContentInfoImpl.prototype._getState = function _getState() {
  55. return this._contentAPI.getFeature('state');
  56. };
  57. ContentInfoImpl.prototype.setData = function setData(key, value) {
  58. if (typeof key !== 'string') {
  59. this._logger.error('invalid or undefined key', key);
  60. } else {
  61. this._data.set(key, value);
  62. }
  63. };
  64. ContentInfoImpl.prototype.getData = function getData(key) {
  65. return this._data.get(key);
  66. };
  67. ContentInfoImpl.prototype.getSpec = function getSpec() {
  68. var _this = this;
  69. var error = this._getState().getError();
  70. var spec = {
  71. id: this.getId(),
  72. type: this.getType(),
  73. visType: this.getVisTypeLabel(),
  74. state: {
  75. error: error ? error.toJson() : undefined
  76. }
  77. };
  78. this._data.forEach(function (value, key) {
  79. if (typeof value.toJSON === 'function') {
  80. try {
  81. spec[key] = value.toJSON();
  82. } catch (error) {
  83. _this._logger.error('error when adding custom data in spec', key, error);
  84. }
  85. } else {
  86. if (key !== 'generalInfo') {
  87. spec[key] = value;
  88. }
  89. }
  90. });
  91. return spec;
  92. };
  93. ContentInfoImpl.prototype.toUIJSON = function toUIJSON() {
  94. var _this2 = this;
  95. var uiSpec = [{
  96. sectionName: StringResources.get('generalInfo'),
  97. sectionValues: [{
  98. fieldName: StringResources.get('widgetId'),
  99. fieldValue: this.getId()
  100. }, {
  101. fieldName: StringResources.get('widgetType'),
  102. fieldValue: this.getVisTypeLabel()
  103. }],
  104. properties: {
  105. open: true
  106. }
  107. }];
  108. this._data.forEach(function (value, key) {
  109. if (typeof value.toUIJSON === 'function') {
  110. try {
  111. uiSpec = uiSpec.concat(value.toUIJSON());
  112. } catch (error) {
  113. _this2._logger.error('error when adding custom UI spec', key, error);
  114. }
  115. }
  116. });
  117. try {
  118. uiSpec = this._sanitizeUIJSON(uiSpec);
  119. } catch (error) {
  120. this._logger.error('error when adding custom UI spec', error);
  121. }
  122. return uiSpec;
  123. };
  124. ContentInfoImpl.prototype._sanitizeUIJSON = function _sanitizeUIJSON(uiSpec) {
  125. uiSpec.forEach(function (sectionSpec) {
  126. if (sectionSpec.sectionName === undefined) {
  127. throw new Error('Error: no section name');
  128. }
  129. if (sectionSpec.sectionValues === undefined) {
  130. sectionSpec.sectionValues = [];
  131. }
  132. if (sectionSpec.properties === undefined) {
  133. sectionSpec.properties = {
  134. open: true
  135. };
  136. } else if (sectionSpec.properties !== undefined && sectionSpec.properties.open === undefined) {
  137. sectionSpec.properties.open = true;
  138. }
  139. sectionSpec.sectionValues.forEach(function (fieldSpec) {
  140. if (fieldSpec.type === undefined) {
  141. fieldSpec.type = 'Default';
  142. }
  143. if (fieldSpec.type === 'SimpleTable') {
  144. if (!fieldSpec.fieldValue.columnLength) {
  145. throw new Error('Error: no columnLength. Need to specify the column length');
  146. }
  147. if (!fieldSpec.fieldValue.rowLength) {
  148. throw new Error('Error: no rowLength. Need to specify the row length');
  149. }
  150. if (!fieldSpec.fieldValue.cellValue || typeof fieldSpec.fieldValue.cellValue !== 'function') {
  151. throw new Error('Error: no cellValue or headerValue is not a function');
  152. }
  153. }
  154. });
  155. });
  156. return uiSpec;
  157. };
  158. ContentInfoImpl.prototype.isDisplayInfoOnDOM = function isDisplayInfoOnDOM() {
  159. return this._isDisplayInfoOnDOM;
  160. };
  161. return ContentInfoImpl;
  162. }();
  163. return ContentInfoImpl;
  164. });
  165. //# sourceMappingURL=ContentInfoImpl.js.map