120 lines
4.8 KiB
JavaScript
120 lines
4.8 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
function ScreenSectionController($scope, idService) {
|
|
var self = this;
|
|
this.handleSectionHeaderClick = function (sectionGroup) {
|
|
if ($scope.layout.specialHandlingSections.indexOf(sectionGroup.name) !== -1 || sectionGroup.sections.length === 1) {
|
|
var section = sectionGroup.sections[0];
|
|
openSectionCustomizationDialog(sectionGroup, section);
|
|
}
|
|
};
|
|
this.handleSectionItemClick = function (sectionGroup, section) {
|
|
openSectionCustomizationDialog(sectionGroup, section);
|
|
};
|
|
this.onMouseOver = function ($event, panel) {
|
|
$event.stopPropagation();
|
|
$event.stopImmediatePropagation();
|
|
$scope.screen.hoveredPanelId = panel.name;
|
|
};
|
|
this.onMouseLeave = function ($event) {
|
|
$event.stopPropagation();
|
|
$event.stopImmediatePropagation();
|
|
$scope.screen.hoveredPanelId = '';
|
|
};
|
|
this.openSectionEditor = function (panel, allPanels) {
|
|
if (angular.isFunction($scope.onItemClick)) {
|
|
$scope.onItemClick({ panel: panel, allPanels: allPanels });
|
|
}
|
|
};
|
|
function init() {
|
|
if ($scope.layout.isCreateChangeLayout()) {
|
|
prepareCreateChangeRequestLayout($scope.layout);
|
|
}
|
|
$scope.layout.panels.forEach(function (tree) {
|
|
var flatTree = flattenPanelsTree(tree.panels);
|
|
tree.sections = flatTree.filter(function (item) {
|
|
return !item.layout;
|
|
});
|
|
});
|
|
}
|
|
/**
|
|
* Helper method, that creates basic section in create change request layout and puts all
|
|
* panels that have layout property not equal to 'fixed' to this virtual section.
|
|
* @param layout {LayoutConfigurationVO} - layout object containing all sections and panels
|
|
*/
|
|
function prepareCreateChangeRequestLayout(layout) {
|
|
var basicSection = {
|
|
name: 'basicSection',
|
|
id: idService.getRandomId(),
|
|
layout: 'fixed',
|
|
panels: (layout.panels.filter(function (panel) {
|
|
return panel.layout !== 'fixed';
|
|
}) || [])
|
|
};
|
|
layout.panels = [].concat(basicSection, layout.panels.filter(function (panel) {
|
|
return panel.layout === 'fixed';
|
|
}));
|
|
}
|
|
function flattenPanelsTree(panelsTree) {
|
|
return panelsTree.reduce(function (acc, panel) {
|
|
return acc.concat(panel.children ? panel : [], flattenPanelsTree(panel.panels || []));
|
|
}, []);
|
|
}
|
|
function openSectionCustomizationDialog(sectionGroup, section) {
|
|
var panel = _.find($scope.screen.panels, function (p) {
|
|
return p.id === section.id;
|
|
}), allSectionPanels = [];
|
|
if (!panel) {
|
|
panel = createNewPanel({
|
|
name: section.name,
|
|
id: section.id
|
|
});
|
|
}
|
|
sectionGroup.sections.forEach(function (p) {
|
|
var panel = _.find($scope.screen.panels, function (item) {
|
|
return p.id === item.id;
|
|
});
|
|
if (!panel) {
|
|
panel = createNewPanel({
|
|
name: p.name,
|
|
id: p.id
|
|
});
|
|
}
|
|
panel.sectionName = sectionGroup.name;
|
|
allSectionPanels.push(panel);
|
|
});
|
|
self.openSectionEditor(panel, allSectionPanels);
|
|
}
|
|
function createNewPanel(config) {
|
|
var defaultPanel = {
|
|
id: '',
|
|
name: '',
|
|
type: 'simplePanel',
|
|
fields: []
|
|
};
|
|
var panel = new PanelVO().build(_.assign(defaultPanel, config));
|
|
panel.parentScreenTitle = $scope.screen.title;
|
|
panel.parentScreenName = $scope.screen.name;
|
|
panel.parentScreenId = $scope.screen.id;
|
|
panel.dataSource = $scope.screen.datasource;
|
|
$scope.screen.panels.push(panel);
|
|
return panel;
|
|
}
|
|
$scope.$watch('layout', init);
|
|
}
|
|
angular.module('adminModule').directive('screenSectionsTree', [function () {
|
|
return {
|
|
restrict: 'E',
|
|
scope: {
|
|
layout: '=',
|
|
onItemClick: '&',
|
|
screen: '='
|
|
},
|
|
templateUrl: 'views/admin/screen-configuration/screen-sections-tree.html',
|
|
controller: ['$scope', 'idService', ScreenSectionController],
|
|
controllerAs: '$ctrl'
|
|
};
|
|
}]);
|
|
})();
|