PropertyLayoutHelper.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore'], function (_) {
  9. var PropertyLayoutHelper = function () {
  10. function PropertyLayoutHelper() {
  11. _classCallCheck(this, PropertyLayoutHelper);
  12. }
  13. /**
  14. * Given a layout list and a list of properties will return the fully expended layout spec that includes the properties
  15. * @param {*} layoutList
  16. * @param {*} propertiesList
  17. */
  18. PropertyLayoutHelper.getExpandedLayoutList = function getExpandedLayoutList(layoutList, propertiesList, profile) {
  19. var expandedLayoutList = [];
  20. PropertyLayoutHelper._expandSpec(layoutList, propertiesList, expandedLayoutList, profile);
  21. // It's possible we have layout only properties in the layoutSpec. These are properties
  22. // we want to show in the UI but don't have clear set/get methods
  23. PropertyLayoutHelper._expandSpec(layoutList, layoutList, expandedLayoutList, profile);
  24. return PropertyLayoutHelper._sortItems(expandedLayoutList);
  25. };
  26. PropertyLayoutHelper._expandSpec = function _expandSpec(layoutList, propertiesList, expandedLayoutList, profile) {
  27. propertiesList.forEach(function (property) {
  28. var editor = property.editor;
  29. if (editor && !editor.hidden && (!profile || PropertyLayoutHelper._shouldFilterProfile(property, profile))) {
  30. var uiControl = editor.uiControl;
  31. uiControl.id = property.id;
  32. uiControl.position = editor.position || 0;
  33. uiControl.readOnly = !!editor.readOnly;
  34. if (editor.sectionId) {
  35. var sectionIds = [];
  36. PropertyLayoutHelper.splitEscapedString(editor.sectionId, '.', sectionIds);
  37. var layoutSection = PropertyLayoutHelper._findLayoutSection(layoutList, expandedLayoutList, sectionIds);
  38. if (!layoutSection) {
  39. throw new Error('Could not create the needed layout section ' + editor.sectionId);
  40. }
  41. layoutSection.push(uiControl);
  42. } else {
  43. expandedLayoutList.push(uiControl);
  44. }
  45. } else if (property.type === 'Banner') {
  46. expandedLayoutList.push(property);
  47. }
  48. });
  49. };
  50. PropertyLayoutHelper._shouldFilterProfile = function _shouldFilterProfile(property, profile) {
  51. return property.profile && property.profile.indexOf(profile) !== -1;
  52. };
  53. /**
  54. * Sorts the layout spec using the 'position' property. If items are found then will recursively call this method on those items
  55. * @param {Object[]} expandedLayoutList
  56. */
  57. PropertyLayoutHelper._sortItems = function _sortItems() {
  58. var expandedLayoutList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  59. expandedLayoutList = _.sortBy(expandedLayoutList, 'position');
  60. expandedLayoutList.forEach(function (layout) {
  61. delete layout.position;
  62. if (layout.items) {
  63. layout.items = PropertyLayoutHelper._sortItems(layout.items);
  64. }
  65. });
  66. return expandedLayoutList;
  67. };
  68. /**
  69. * Finds the correct layout location in the spec according to the 'sectionIds' array passed in. If
  70. * the layout isn't already in the expanded spec, it'll be created using the layout information
  71. * @param {Object[]} layoutList
  72. * @param {Object[]} expandedLayoutList
  73. * @param {String[]} sectionIds
  74. */
  75. PropertyLayoutHelper._findLayoutSection = function _findLayoutSection(layoutList, expandedLayoutList) {
  76. var sectionIds = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  77. var sectionId = sectionIds.shift();
  78. if (!sectionId) {
  79. return expandedLayoutList;
  80. }
  81. var existingSection = _.find(expandedLayoutList, function (property) {
  82. return property.id === sectionId;
  83. });
  84. if (existingSection) {
  85. if (!existingSection.items) {
  86. throw new Error('Layout section with no items array.');
  87. }
  88. return PropertyLayoutHelper._findLayoutSection(layoutList, existingSection.items, sectionIds);
  89. }
  90. // Didn't find an existing section, so create one
  91. var layoutSectionInfo = _.find(layoutList, function (layout) {
  92. return layout.id === sectionId;
  93. });
  94. if (!layoutSectionInfo) {
  95. throw new Error('Section ' + sectionId + ' is referenced but isn\'t defined anywhere.');
  96. }
  97. layoutSectionInfo = _.extend({}, layoutSectionInfo);
  98. if (!layoutSectionInfo.items) {
  99. layoutSectionInfo.items = [];
  100. }
  101. expandedLayoutList.push(layoutSectionInfo);
  102. return PropertyLayoutHelper._findLayoutSection(layoutList, layoutSectionInfo.items, sectionIds);
  103. };
  104. PropertyLayoutHelper.splitEscapedString = function splitEscapedString(input, delimiter, outputs) {
  105. var index = input.indexOf(delimiter);
  106. if (index !== -1 && index !== input.length && input[index - 1] !== '\\') {
  107. var result = input.slice(0, index);
  108. input = input.slice(index + 1, input.length);
  109. outputs.push(result);
  110. PropertyLayoutHelper.splitEscapedString(input, delimiter, outputs);
  111. } else {
  112. outputs.push(input);
  113. }
  114. };
  115. return PropertyLayoutHelper;
  116. }();
  117. return PropertyLayoutHelper;
  118. });
  119. //# sourceMappingURL=PropertyLayoutHelper.js.map