Canvas.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /**
  6. * Licensed Materials - Property of IBM
  7. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018, 2020
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. define(['jquery', '../lib/@waca/dashboard-common/dist/core/APIFactory', './CanvasAPISpec', '../api/CanvasAPI', 'underscore', '../dashboard/util/PropertyLayoutHelper'], function ($, APIFactory, CanvasAPISpec, CanvasAPI, _, PropertyLayoutHelper) {
  11. var DEFAULT_LAYOUT_TYPES = ['widget', 'group'];
  12. var CanvasAPIImpl = function (_CanvasAPISpec) {
  13. _inherits(CanvasAPIImpl, _CanvasAPISpec);
  14. function CanvasAPIImpl(options) {
  15. _classCallCheck(this, CanvasAPIImpl);
  16. var _this = _possibleConstructorReturn(this, _CanvasAPISpec.call(this, options));
  17. _this.boardModel = options.boardModel;
  18. _this.extensions = options.extensions;
  19. _this.contentFeatureCollection = options.contentFeatureCollection;
  20. // TODO - add inline feature here
  21. _this.dashboardFeatures = options.dashboardFeatures;
  22. _this.logger = _this.dashboardFeatures.getFeature('Logger');
  23. _this.contentFactory = _this.dashboardFeatures.getFeature('ContentFactory');
  24. _this.canvasContent = _this.contentFactory.createContent({
  25. dashboardFeatures: _this.dashboardFeatures,
  26. contentFeatureCollection: _this.contentFeatureCollection,
  27. boardModel: _this.boardModel,
  28. layoutModel: _this.boardModel.layout,
  29. canvas: _this,
  30. profile: options.profile,
  31. type: _this.boardModel.layout.type
  32. });
  33. _this.copyCallback = options.copyCallback;
  34. _this.pasteCallback = options.pasteCallback;
  35. _this._dashboardContent = options.dashboardContent;
  36. APIFactory.setParentChildRelation(_this, _this.canvasContent);
  37. return _this;
  38. }
  39. CanvasAPIImpl.prototype.getRootContentInstance = function getRootContentInstance() {
  40. return this.canvasContent;
  41. };
  42. /**
  43. * @param {*} contentSpecType the type of content.spec
  44. * @return {booelan} if the spec is a layoutSpec(e.g. tab, group, ...)
  45. */
  46. CanvasAPIImpl.prototype.isLayoutType = function isLayoutType(contentSpecType) {
  47. var layoutViewExtentions = this.extensions.getLayoutViewExtensions();
  48. var authoringViewsLayoutTypeList = Object.keys(layoutViewExtentions.authoringViews);
  49. var consumeViewsLayoutTypeList = Object.keys(layoutViewExtentions.consumeViews);
  50. return authoringViewsLayoutTypeList.indexOf(contentSpecType) !== -1 || consumeViewsLayoutTypeList.indexOf(contentSpecType) !== -1;
  51. };
  52. CanvasAPIImpl.prototype.getAPI = function getAPI() {
  53. if (!this.api) {
  54. this.api = APIFactory.createAPI(this, [CanvasAPI]);
  55. }
  56. return this.api;
  57. };
  58. CanvasAPIImpl.prototype.initialize = function initialize() {
  59. var _this2 = this;
  60. return this.canvasContent.initialize().then(function () {
  61. _this2._contentTargets = [_this2._dashboardContent, _this2.canvasContent];
  62. });
  63. };
  64. CanvasAPIImpl.prototype.destroy = function destroy() {
  65. this.boardModel = null;
  66. this.canvasContent.destroy();
  67. this.canvasContent = null;
  68. this.dashboardFeatures = null;
  69. this.api = null;
  70. this._dashboardContent = null;
  71. };
  72. CanvasAPIImpl.prototype.getContentActionList = function getContentActionList(idList) {
  73. if (idList.length) {
  74. return this.dashboardFeatures.getFeature('ContentActions').getContentActionList(idList);
  75. }
  76. return [];
  77. };
  78. CanvasAPIImpl.prototype.copy = function copy() {
  79. // TODO - clean this up
  80. return this.copyCallback.apply(null, arguments);
  81. };
  82. CanvasAPIImpl.prototype.paste = function paste() {
  83. // TODO - clean this up
  84. return this.pasteCallback.apply(null, arguments);
  85. };
  86. CanvasAPIImpl.prototype.addContent = function addContent(options, transactionToken) {
  87. var container = this.getContentInstance(options.containerId);
  88. return container.getAPI().addContent(options, transactionToken);
  89. };
  90. CanvasAPIImpl.prototype.moveContent = function moveContent(containerId, contentIdList, transactionToken, insertBeforeMap) {
  91. var container = this.getContentInstance(containerId);
  92. return container.moveContent(containerId, contentIdList, transactionToken, insertBeforeMap);
  93. };
  94. CanvasAPIImpl.prototype.removeContent = function removeContent(id, transactionToken) {
  95. var content = this.getContent(id);
  96. if (content) {
  97. var container = content.getContainer();
  98. if (container) {
  99. container.removeContent(id, transactionToken);
  100. }
  101. }
  102. };
  103. CanvasAPIImpl.prototype.getContentInstance = function getContentInstance(contentId) {
  104. var content = void 0;
  105. if (!contentId || contentId === this.boardModel.layout.id) {
  106. content = this.canvasContent;
  107. } else {
  108. content = this.canvasContent.getContentInstance(contentId);
  109. }
  110. return content;
  111. };
  112. CanvasAPIImpl.prototype.registerContentFeatures = function registerContentFeatures(contentId, features) {
  113. var content = this.getContentInstance(contentId);
  114. if (content) {
  115. return content.registerFeatures(features);
  116. }
  117. return Promise.reject('The content with id "' + contentId + '" does not exist.');
  118. };
  119. CanvasAPIImpl.prototype.registerContentFeature = function registerContentFeature(contentId, featureId, featureInstance, featureSpec, skipInitialization) {
  120. var content = this.getContentInstance(contentId);
  121. if (content) {
  122. return content.registerFeature(featureId, featureInstance, featureSpec, skipInitialization);
  123. }
  124. return Promise.reject('The content with id "' + contentId + '" does not exist.');
  125. };
  126. CanvasAPIImpl.prototype.getContentFeature = function getContentFeature(contentId, featureId) {
  127. if (contentId === this.boardModel.layout.id) {
  128. return this.canvasContent.getFeature(featureId);
  129. }
  130. var content = this.canvasContent.getContentInstance(contentId);
  131. if (content) {
  132. return content.getFeature(featureId);
  133. }
  134. };
  135. CanvasAPIImpl.prototype.getContent = function getContent(id) {
  136. // Root layout
  137. if (!id || id === this.boardModel.layout.id) {
  138. return this.canvasContent.getAPI();
  139. }
  140. return this.canvasContent.getContent(id);
  141. };
  142. CanvasAPIImpl.prototype.findContent = function findContent(selector) {
  143. // if the type matches the top layout then return it..
  144. if (selector && selector.type === this.boardModel.layout.type) {
  145. return [this.canvasContent.getAPI()];
  146. }
  147. return this.canvasContent.findContent(selector);
  148. };
  149. CanvasAPIImpl.prototype.selectContent = function selectContent() {
  150. var _this3 = this;
  151. var idList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  152. idList.forEach(function (id) {
  153. var model = _this3.boardModel.layout.findModel(id);
  154. if (model) {
  155. var parent = model.getParent();
  156. if (parent) {
  157. parent.select(model.id);
  158. }
  159. }
  160. });
  161. };
  162. CanvasAPIImpl.prototype.deselectContent = function deselectContent() {
  163. var _this4 = this;
  164. var idList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  165. idList.forEach(function (id) {
  166. var model = _this4.boardModel.layout.findModel(id);
  167. if (model) {
  168. var parent = model.getParent();
  169. if (parent) {
  170. parent.deselect(model.id);
  171. }
  172. }
  173. });
  174. };
  175. CanvasAPIImpl.prototype.getSelectedContentList = function getSelectedContentList() {
  176. var _this5 = this;
  177. var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  178. var selectContentList = [];
  179. if (this.boardModel) {
  180. var contentRegistry = this.dashboardFeatures.getFeature('ContentTypeRegistry');
  181. var types = selector.type ? [selector.type] : DEFAULT_LAYOUT_TYPES;
  182. var selectedChildLayouts = this.boardModel.layout.getSelectedChildLayouts();
  183. var selectedFilteredChildLayouts = selectedChildLayouts.filter(function (layout) {
  184. var content = _this5.getContent(layout.id);
  185. var result = false;
  186. if (contentRegistry) {
  187. result = contentRegistry.isTypeRegistered(layout.type);
  188. }
  189. if (!result) {
  190. result = types.indexOf(layout.type) !== -1 || types.indexOf(content.getType()) !== -1;
  191. }
  192. return result;
  193. });
  194. var parents = _.filter(selectedFilteredChildLayouts, function (layout) {
  195. return _.isArray(layout.items);
  196. });
  197. var selectedLayouts = _.filter(selectedFilteredChildLayouts, function (layout) {
  198. // remove layouts that are part of selected parents
  199. return !_.find(parents, function (parent) {
  200. return parent.findChildItem(parent.items, layout.id);
  201. });
  202. });
  203. selectContentList = _.map(selectedLayouts, function (layout) {
  204. return _this5.getContent(layout.id);
  205. });
  206. }
  207. return selectContentList;
  208. };
  209. CanvasAPIImpl.prototype.getPropertyNameList = function getPropertyNameList() {
  210. return Object.keys(this._getPropertyMap());
  211. };
  212. CanvasAPIImpl.prototype.setPropertyValue = function setPropertyValue(propertyName, propertyValue, transactionToken) {
  213. var property = this._getProperty(propertyName);
  214. if (property.validatePropertyValue) {
  215. var validInfo = property.validatePropertyValue(propertyValue) || {};
  216. if (!validInfo.isValid) {
  217. this.logger.error(validInfo.message, propertyName, propertyValue);
  218. return;
  219. }
  220. }
  221. property.setPropertyValue(propertyValue, transactionToken);
  222. };
  223. CanvasAPIImpl.prototype.getPropertyValue = function getPropertyValue(propertyName) {
  224. var property = this._getProperty(propertyName);
  225. return property.getPropertyValue();
  226. };
  227. /**
  228. * Returns fully expanded layout specification. Called when rendering the property panel
  229. */
  230. CanvasAPIImpl.prototype.getPropertyLayoutList = function getPropertyLayoutList() {
  231. var allLayouts = {};
  232. //_.each(this._contentTargets, (target) => {
  233. for (var i = 0; i < this._contentTargets.length; i++) {
  234. var target = this._contentTargets[i];
  235. var targetProperties = target.getFeature('Properties');
  236. var layouts = targetProperties.getPropertyLayoutList();
  237. //_.each(layouts, (layout) => {
  238. for (var j = 0; j < layouts.length; j++) {
  239. var layout = layouts[j];
  240. if (allLayouts[layout.id]) {
  241. $.extend(true, allLayouts[layout.id], layout);
  242. } else {
  243. allLayouts[layout.id] = layout;
  244. }
  245. }
  246. }
  247. return PropertyLayoutHelper.getExpandedLayoutList(_.values(allLayouts), this._getPropertyList());
  248. };
  249. CanvasAPIImpl.prototype._getPropertyList = function _getPropertyList() {
  250. this._initProperties();
  251. return this._propertiesList;
  252. };
  253. CanvasAPIImpl.prototype._getPropertyMap = function _getPropertyMap() {
  254. this._initProperties();
  255. return this._propertiesMap;
  256. };
  257. /**
  258. * Get the list of properties and caches them
  259. */
  260. CanvasAPIImpl.prototype._initProperties = function _initProperties() {
  261. this._propertiesMap = {};
  262. this._contentTargets = this._contentTargets || [];
  263. for (var i = 0; i < this._contentTargets.length; i++) {
  264. var target = this._contentTargets[i];
  265. var properties = target.getPropertyList() || [];
  266. for (var j = 0; j < properties.length; j++) {
  267. var property = properties[j];
  268. if (this._propertiesMap[property.id]) {
  269. $.extend(true, this._propertiesMap[property.id], property);
  270. } else {
  271. this._propertiesMap[property.id] = property;
  272. }
  273. }
  274. }
  275. this._propertiesList = _.values(this._propertiesMap);
  276. };
  277. // TODO : we should define a way to get property using cache, currently,
  278. // every time we call _getProperty, it will init a new _propertiesMap again
  279. // it does not really using cache
  280. CanvasAPIImpl.prototype._getProperty = function _getProperty(propertyName) {
  281. var property = this._getPropertyMap()[propertyName];
  282. if (!property) {
  283. throw new Error('Property ' + propertyName + ' does not exist.');
  284. }
  285. return property;
  286. };
  287. CanvasAPIImpl.prototype.whenContentReady = function whenContentReady(contentId) {
  288. // find content and call intialize
  289. var contentImpl = this.getContentInstance(contentId);
  290. return contentImpl ? contentImpl.initialize() : Promise.reject('Could not find content with id ' + contentId);
  291. };
  292. return CanvasAPIImpl;
  293. }(CanvasAPISpec);
  294. return CanvasAPIImpl;
  295. });
  296. //# sourceMappingURL=Canvas.js.map