Serializer.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Business Analytics (C) Copyright IBM Corp. 2020
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', './SerializerAPI', '../../../dashboard/layouts/SinglePageLayout', '../../../dashboard/layouts/TabLayout', '../../../dashboard/model/ModelUtils', 'text!../../../dashboard/layout/templates/Template1.json', '../../../app/nls/StringResources'], function (APIFactory, SerializerAPI, singlePageLayout, tabLayout, ModelUtils, template1Json, stringResources) {
  10. /**
  11. * @implements {SerializerAPI}
  12. */
  13. var Serializer = function () {
  14. /**
  15. * @param {Object} options
  16. * @param {Object} options.features An object of the defined dependencies from the feature contribution json
  17. */
  18. function Serializer(_ref) {
  19. var features = _ref.features;
  20. _classCallCheck(this, Serializer);
  21. this._boardModel = features.internal.getBoardModel();
  22. this._dashboard = features.API;
  23. this._api = APIFactory.createAPI(this, [SerializerAPI]);
  24. }
  25. Serializer.prototype.getAPI = function getAPI() {
  26. return this._api;
  27. };
  28. Serializer.prototype.destroy = function destroy() {
  29. delete this._boardModel;
  30. delete this._dashboard;
  31. delete this._api;
  32. };
  33. Serializer.prototype.toJSON = function toJSON() {
  34. var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  35. contentIds = _ref2.contentIds,
  36. dashboardFeatures = _ref2.dashboardFeatures;
  37. var json = void 0;
  38. if (contentIds && Array.isArray(contentIds) && contentIds.length > 0) {
  39. json = this._buildDashboardSpecForContents(contentIds);
  40. } else {
  41. var canvas = this._dashboard.getFeature('Canvas');
  42. if (canvas) {
  43. json = this._boardModel.toJSON();
  44. } else {
  45. json = this._boardModel.getInitialSpec();
  46. }
  47. }
  48. ModelUtils.pullPropertiesFromContentModelSpec(json);
  49. if (dashboardFeatures && Array.isArray(dashboardFeatures) && dashboardFeatures.length > 0) {
  50. if (json.features) {
  51. Object.keys(json.features).forEach(function (feature) {
  52. if (dashboardFeatures.indexOf(feature) === -1) {
  53. delete json.features[feature];
  54. }
  55. });
  56. }
  57. }
  58. return json;
  59. };
  60. /**
  61. * build the minimum dashboard spec which only contains the specified list of contents
  62. * For now, we use the template1 (no drop zone) to generate the dashboard spec
  63. * TODO: dynamically retrieving the current template of the dashboard instead of hard code the template
  64. * @param {Array} contentIds - a list of content ids
  65. * @return {Object} spec
  66. */
  67. Serializer.prototype._buildDashboardSpecForContents = function _buildDashboardSpecForContents(contentIds) {
  68. // get unique ids
  69. contentIds = contentIds.filter(function (v, i) {
  70. return contentIds.indexOf(v) === i;
  71. });
  72. // first check if all the content has same parent
  73. var canvas = this._dashboard.getFeature('Canvas');
  74. var contents = contentIds.map(function (contentId) {
  75. var content = canvas.getContent(contentId);
  76. if (!content) {
  77. throw new Error('Cannot get the content by id ' + contentId);
  78. }
  79. return content;
  80. });
  81. var parent = contents[0].getContainer();
  82. if (parent && parent.getType() === 'genericPage') {
  83. for (var i = 1; i < contents.length; i++) {
  84. var parentId = contents[i].getContainer() ? contents[i].getContainer().getId() : null;
  85. if (parentId !== parent.getId()) {
  86. throw new Error('the list of contents must have same parent');
  87. }
  88. }
  89. } else {
  90. throw new Error('the list of contents must be the children of genericPage');
  91. }
  92. // build the spec
  93. var spec = {
  94. version: this._boardModel.version,
  95. content: this._boardModel.getContentModel().toJSON()
  96. };
  97. var template1 = JSON.parse(template1Json);
  98. var genericPage = template1.items[0];
  99. genericPage.items = genericPage.items.concat(contents.map(function (content) {
  100. var contentSpec = content.getFeature('Serializer').toJSON();
  101. return contentSpec;
  102. }));
  103. //get current dashboard layout type
  104. var dashboardLayoutType = this._boardModel.layout.type;
  105. if (dashboardLayoutType === 'tab') {
  106. var theTabLayout = JSON.parse(JSON.stringify(tabLayout.layout)); // multi tab layout
  107. theTabLayout.items = [_extends({}, template1, {
  108. title: stringResources.get('defaultTabTitle', {
  109. index: 1
  110. })
  111. })];
  112. spec.layout = theTabLayout;
  113. } else {
  114. spec.layout = _extends({}, JSON.parse(JSON.stringify(singlePageLayout.layout)), template1);
  115. }
  116. return spec;
  117. };
  118. return Serializer;
  119. }();
  120. return Serializer;
  121. });
  122. //# sourceMappingURL=Serializer.js.map