123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- define(['underscore'], function (_) {
- var PropertyLayoutHelper = function () {
- function PropertyLayoutHelper() {
- _classCallCheck(this, PropertyLayoutHelper);
- }
- /**
- * Given a layout list and a list of properties will return the fully expended layout spec that includes the properties
- * @param {*} layoutList
- * @param {*} propertiesList
- */
- PropertyLayoutHelper.getExpandedLayoutList = function getExpandedLayoutList(layoutList, propertiesList, profile) {
- var expandedLayoutList = [];
- PropertyLayoutHelper._expandSpec(layoutList, propertiesList, expandedLayoutList, profile);
- // It's possible we have layout only properties in the layoutSpec. These are properties
- // we want to show in the UI but don't have clear set/get methods
- PropertyLayoutHelper._expandSpec(layoutList, layoutList, expandedLayoutList, profile);
- return PropertyLayoutHelper._sortItems(expandedLayoutList);
- };
- PropertyLayoutHelper._expandSpec = function _expandSpec(layoutList, propertiesList, expandedLayoutList, profile) {
- propertiesList.forEach(function (property) {
- var editor = property.editor;
- if (editor && !editor.hidden && (!profile || PropertyLayoutHelper._shouldFilterProfile(property, profile))) {
- var uiControl = editor.uiControl;
- uiControl.id = property.id;
- uiControl.position = editor.position || 0;
- uiControl.readOnly = !!editor.readOnly;
- if (editor.sectionId) {
- var sectionIds = [];
- PropertyLayoutHelper.splitEscapedString(editor.sectionId, '.', sectionIds);
- var layoutSection = PropertyLayoutHelper._findLayoutSection(layoutList, expandedLayoutList, sectionIds);
- if (!layoutSection) {
- throw new Error('Could not create the needed layout section ' + editor.sectionId);
- }
- layoutSection.push(uiControl);
- } else {
- expandedLayoutList.push(uiControl);
- }
- } else if (property.type === 'Banner') {
- expandedLayoutList.push(property);
- }
- });
- };
- PropertyLayoutHelper._shouldFilterProfile = function _shouldFilterProfile(property, profile) {
- return property.profile && property.profile.indexOf(profile) !== -1;
- };
- /**
- * Sorts the layout spec using the 'position' property. If items are found then will recursively call this method on those items
- * @param {Object[]} expandedLayoutList
- */
- PropertyLayoutHelper._sortItems = function _sortItems() {
- var expandedLayoutList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
- expandedLayoutList = _.sortBy(expandedLayoutList, 'position');
- expandedLayoutList.forEach(function (layout) {
- delete layout.position;
- if (layout.items) {
- layout.items = PropertyLayoutHelper._sortItems(layout.items);
- }
- });
- return expandedLayoutList;
- };
- /**
- * Finds the correct layout location in the spec according to the 'sectionIds' array passed in. If
- * the layout isn't already in the expanded spec, it'll be created using the layout information
- * @param {Object[]} layoutList
- * @param {Object[]} expandedLayoutList
- * @param {String[]} sectionIds
- */
- PropertyLayoutHelper._findLayoutSection = function _findLayoutSection(layoutList, expandedLayoutList) {
- var sectionIds = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
- var sectionId = sectionIds.shift();
- if (!sectionId) {
- return expandedLayoutList;
- }
- var existingSection = _.find(expandedLayoutList, function (property) {
- return property.id === sectionId;
- });
- if (existingSection) {
- if (!existingSection.items) {
- throw new Error('Layout section with no items array.');
- }
- return PropertyLayoutHelper._findLayoutSection(layoutList, existingSection.items, sectionIds);
- }
- // Didn't find an existing section, so create one
- var layoutSectionInfo = _.find(layoutList, function (layout) {
- return layout.id === sectionId;
- });
- if (!layoutSectionInfo) {
- throw new Error('Section ' + sectionId + ' is referenced but isn\'t defined anywhere.');
- }
- layoutSectionInfo = _.extend({}, layoutSectionInfo);
- if (!layoutSectionInfo.items) {
- layoutSectionInfo.items = [];
- }
- expandedLayoutList.push(layoutSectionInfo);
- return PropertyLayoutHelper._findLayoutSection(layoutList, layoutSectionInfo.items, sectionIds);
- };
- PropertyLayoutHelper.splitEscapedString = function splitEscapedString(input, delimiter, outputs) {
- var index = input.indexOf(delimiter);
- if (index !== -1 && index !== input.length && input[index - 1] !== '\\') {
- var result = input.slice(0, index);
- input = input.slice(index + 1, input.length);
- outputs.push(result);
- PropertyLayoutHelper.splitEscapedString(input, delimiter, outputs);
- } else {
- outputs.push(input);
- }
- };
- return PropertyLayoutHelper;
- }();
- return PropertyLayoutHelper;
- });
- //# sourceMappingURL=PropertyLayoutHelper.js.map
|