102 lines
4.5 KiB
JavaScript
102 lines
4.5 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Created by Abhranil Naha on 7/10/2017.
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
angular.module('layoutConfigModule')
|
|
.factory('layoutConfigurationModel', ['$log', '$q', 'layoutConfigurationService', 'screenConfigurationModel',
|
|
function ($log, $q, layoutConfigurationService, screenConfigurationModel) {
|
|
var layoutConfigurationModel = {
|
|
layoutsByScreenName: {}
|
|
};
|
|
/**
|
|
* Public functions
|
|
*/
|
|
layoutConfigurationModel.getLayoutFromCacheByScreenName = function (screenName) {
|
|
return angular.copy(layoutConfigurationModel.layoutsByScreenName[screenName]);
|
|
};
|
|
/**
|
|
* Can load all layouts, if no params specified or layout for specific datasource and screen name
|
|
*
|
|
* @param {String} datasource - layout datasource name
|
|
* @param {String} screenName - layout screen name
|
|
* @returns {Promise}
|
|
*/
|
|
layoutConfigurationModel.loadLayout = function (screenName) {
|
|
var params = {};
|
|
if (arguments.length) {
|
|
params = {
|
|
screen: screenName
|
|
};
|
|
}
|
|
return layoutConfigurationService.loadLayout(params)
|
|
.then(function (layouts) {
|
|
(layouts || []).forEach(function (layout) {
|
|
if (layout.name) {
|
|
layoutConfigurationModel.layoutsByScreenName[layout.name] = layout;
|
|
}
|
|
});
|
|
return angular.copy(layouts);
|
|
})
|
|
.catch(function (e) {
|
|
$log.error('Could not load screen configurations! ', e);
|
|
throw e;
|
|
});
|
|
};
|
|
/**
|
|
* Loads screen layout for specific datasource and screen name
|
|
*
|
|
* @param {String} screenName
|
|
* @param {Boolean} force - whether to force request to be sent
|
|
* @returns {*}
|
|
*/
|
|
layoutConfigurationModel.loadScreenLayout = function (screenName, force) {
|
|
var cachedLayout = force ? angular.noop() : layoutConfigurationModel.layoutsByScreenName[screenName], layoutPromise;
|
|
if (cachedLayout) {
|
|
layoutPromise = $q.when(cachedLayout);
|
|
}
|
|
else {
|
|
layoutPromise = screenConfigurationModel.isV2CompatibleScreen(screenName) ? layoutConfigurationModel.loadLayout(screenName) : $q.when({});
|
|
}
|
|
return layoutPromise.then(function () {
|
|
return angular.copy(layoutConfigurationModel.layoutsByScreenName[screenName]);
|
|
});
|
|
};
|
|
/**
|
|
* Sends reuqest to update screen layout by its name
|
|
*
|
|
* @param {Object} panel
|
|
* @returns {*}
|
|
*/
|
|
layoutConfigurationModel.applyCustomizationChangesToScreenLayout = function (panel) {
|
|
var layout = layoutConfigurationModel.layoutsByScreenName[panel.parentScreenName], rawLayout = layout.getRawLayout(), flatLayoutPanels = flattenLayoutPanels(rawLayout), section = _.find(flatLayoutPanels, { id: panel.id });
|
|
section.children = panel.fields.map(function (field) {
|
|
return _.pick(field, ['name', 'id', 'type', 'dataType']);
|
|
});
|
|
section.name = panel.name;
|
|
return layoutConfigurationModel.updateScreenLayout(rawLayout, panel.parentScreenName);
|
|
};
|
|
/**
|
|
* Sends reuqest to update screen layout by its name
|
|
*
|
|
* @param {Object} layout -
|
|
* @param {String} screenId
|
|
* @returns {*}
|
|
*/
|
|
layoutConfigurationModel.updateScreenLayout = function (screenName, layout) {
|
|
return layoutConfigurationService.updateScreenLayout(layout, screenName);
|
|
};
|
|
function flattenLayoutPanels(layout) {
|
|
return layout.panels.reduce(function (acc, item) {
|
|
acc.push(item);
|
|
if (item.panels) {
|
|
acc = acc.concat(flattenLayoutPanels(item));
|
|
}
|
|
return acc;
|
|
}, []);
|
|
}
|
|
return layoutConfigurationModel;
|
|
}]);
|
|
})();
|