ModelUtils.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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: Dashboard (C) Copyright IBM Corp. 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['./content/ContentModel'], function (ContentModel) {
  9. // Utility class for models
  10. var ModelUtils = function () {
  11. function ModelUtils() {
  12. _classCallCheck(this, ModelUtils);
  13. }
  14. /**
  15. * Adds a contentModel to the provided model.
  16. * Transfers from model properties to content model specification if they exist or else uses content model specification.
  17. * @public
  18. * @static
  19. * @function ModelUtils#initializeContentModel
  20. * @param {Object} model A model object that can contain a ContentModel (ex. LayoutModel, BoardModel)
  21. * @param {String[]} [excludedProperties=[]] An array of properties to exclude in the property transfer
  22. */
  23. ModelUtils.initializeContentModel = function initializeContentModel(model) {
  24. var excludedProperties = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  25. var contentModelSpec = model.content || {};
  26. if (model.hasOwnProperty('properties')) {
  27. contentModelSpec.properties = {};
  28. Object.keys(model.properties).forEach(function (property) {
  29. if (!(excludedProperties.indexOf(property) !== -1)) {
  30. contentModelSpec.properties[property] = model.properties[property];
  31. delete model.properties[property];
  32. }
  33. });
  34. if (Object.keys(model.properties).length === 0) {
  35. delete model.properties;
  36. }
  37. }
  38. if (model.hasOwnProperty('features')) {
  39. contentModelSpec.features = model.features;
  40. delete model.features;
  41. }
  42. delete model.content;
  43. return new ContentModel(contentModelSpec);
  44. };
  45. /**
  46. * Merges properties from the `content` in the spec to the root of spec
  47. * Removes content from spec if empty after merge.
  48. * Object properties to be transferred from content
  49. * - properties
  50. * - features
  51. * @public
  52. * @static
  53. * @function ModelUtils#pullPropertiesFromContentModelSpec
  54. * @param {Object} spec A json spec of a model (LayoutModel/BoardModel)
  55. */
  56. ModelUtils.pullPropertiesFromContentModelSpec = function pullPropertiesFromContentModelSpec(spec) {
  57. var PropertiesToInclude = ['properties', 'features'];
  58. var contentModelSpec = spec.content;
  59. if (contentModelSpec) {
  60. PropertiesToInclude.forEach(function (prop) {
  61. if (contentModelSpec.hasOwnProperty(prop)) {
  62. if (contentModelSpec[prop] && Object.keys(contentModelSpec[prop]).length) {
  63. spec[prop] = Object.assign({}, spec[prop], contentModelSpec[prop]);
  64. }
  65. delete contentModelSpec[prop];
  66. }
  67. });
  68. if (Object.keys(contentModelSpec).length === 0) {
  69. delete spec.content;
  70. }
  71. }
  72. };
  73. return ModelUtils;
  74. }();
  75. return ModelUtils;
  76. });
  77. //# sourceMappingURL=ModelUtils.js.map