diff options
author | 2017-04-22 13:25:07 +0200 | |
---|---|---|
committer | 2017-04-22 13:25:07 +0200 | |
commit | d182202fc6001983541504ed323d68479086317e (patch) | |
tree | 11d4c10cdd3e995f519c3e0e324968fdaf175114 /moonv4/moon_gui/static/app/project/project.controller.list.js | |
parent | 83c1c966baf73329fab8ddcfad19ad7fe0c41c2a (diff) |
add moonv4
Change-Id: I247af788d0b0fb961fbc85416486b241eb1d807c
Signed-off-by: WuKong <rebirthmonkey@gmail.com>
Diffstat (limited to 'moonv4/moon_gui/static/app/project/project.controller.list.js')
-rw-r--r-- | moonv4/moon_gui/static/app/project/project.controller.list.js | 310 |
1 files changed, 310 insertions, 0 deletions
diff --git a/moonv4/moon_gui/static/app/project/project.controller.list.js b/moonv4/moon_gui/static/app/project/project.controller.list.js new file mode 100644 index 00000000..b1cb2056 --- /dev/null +++ b/moonv4/moon_gui/static/app/project/project.controller.list.js @@ -0,0 +1,310 @@ +/** + * @author arnaud marhin<arnaud.marhin@orange.com> + */ + +(function() { + + 'use strict'; + + angular + .module('moon') + .controller('ProjectListController', ProjectListController); + + ProjectListController.$inject = ['$rootScope', '$scope', '$filter', '$modal', 'ngTableParams', 'pdpService', 'projects']; + + function ProjectListController($rootScope, $scope, $filter, $modal, ngTableParams, pdpService, projects) { + + var list = this; + + /* + * + */ + + list.projects = projects; + list.pdps = []; + + list.getProjects = getProjects; + list.hasProjects = hasProjects; + list.isProjectMapped = isProjectMapped; + + list.table = {}; + + list.addProject = addProject; + list.deleteProject = deleteProject; + list.refreshProjects = refreshProjects; + + list.getMappedPDPName = getMappedPDPName; + list.getPdpFromProject = getPdpFromProject; + + list.search = { query: '', + find: searchProject, + reset: searchReset }; + + list.add = { modal: $modal({ template: 'html/project/action/project-add.tpl.html', show: false }), + showModal: showAddModal }; + + list.del = { modal: $modal({ template: 'html/project/action/project-delete.tpl.html', show: false }), + showModal: showDeleteModal }; + + list.map = { modal: $modal({ template: 'html/project/action/mapping/project-map.tpl.html', show: false }), + showModal: showMapModal }; + + list.unmap = { modal: $modal({ template: 'html/project/action/mapping/project-unmap.tpl.html', show: false }), + showModal: showUnmapModal }; + + list.view = { modal: $modal({ template: 'html/project/action/project-view.tpl.html', show: false }), + showModal: showViewModal }; + + activate(); + + + function activate(){ + + list.loadingPDPs = true; + + newProjectsTable(); + + pdpService.findAllWithCallBack(function(data){ + + list.pdps = data; + + pdpService.mapPdpsToProjects(list.projects, list.pdps); + + list.loadingPDPs = false; + + }); + } + + + /* + * ---- events + */ + + var rootListeners = { + + 'event:projectCreatedSuccess': $rootScope.$on('event:projectCreatedSuccess', projectCreatedSuccess), + 'event:projectCreatedError': $rootScope.$on('event:projectCreatedError', projectCreatedError), + + 'event:projectDeletedSuccess': $rootScope.$on('event:projectDeletedSuccess', projectDeletedSuccess), + 'event:projectDeletedError': $rootScope.$on('event:projectDeletedError', projectDeletedError), + + 'event:projectMappedSuccess': $rootScope.$on('event:projectMappedSuccess', projectMappedSuccess), + 'event:projectMappedError': $rootScope.$on('event:projectMappedError', projectMappedError), + + 'event:projectUnmappedSuccess': $rootScope.$on('event:projectUnmappedSuccess', projectUnmappedSuccess), + 'event:projectUnmappedError': $rootScope.$on('event:projectUnmappedError', projectUnmappedError) + + }; + + for (var unbind in rootListeners) { + $scope.$on('$destroy', rootListeners[unbind]); + } + + /* + * ---- table + */ + + /** + * Get projects array from the Keystone Moon. + * @return an array containing projects JSON + */ + function getProjects() { + return (list.projects) ? list.projects : []; + } + + function hasProjects() { + return list.getProjects().length > 0; + } + + function isProjectMapped(project){ + return _.has(project, 'pdp'); + } + + /** + * Prerequisite, isProjectMapped should return true + * @param project + * @returns {*} + */ + function getPdpFromProject(project){ + return project.pdp; + } + + function addProject(project) { + list.projects.push(project); + } + + function deleteProject(project) { + list.projects = _.chain(list.projects).reject({id: project.id}).value(); + } + + function refreshProjects() { + + list.table.total(list.projects.length); + list.table.reload(); + + } + + function newProjectsTable() { + + list.table = new ngTableParams({ + + page: 1, // show first page + count: 10, // count per page + sorting: { + name: 'asc' // initial sorting + } + + }, { + + total: function () { return list.getProjects().length; }, // length of data + getData: function($defer, params) { + + var orderedData = params.sorting() ? $filter('orderBy')(list.getProjects(), params.orderBy()) : list.getProjects(); + $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count())); + + }, + $scope: { $data: {} } + + }); + + return list.table; + + } + + /** + * + * @param project should have project.mapping.authz.pdp.name attribute + */ + function getMappedPDPName(project) { + return _.has(project, 'pdp') ? project.pdp.name : 'error'; + } + + /* + * ---- search + */ + + function searchProject(project){ + return (project.name.indexOf(list.search.query) !== -1 || project.description.indexOf(list.search.query) !== -1); + } + + function searchReset() { + list.search.query = ''; + } + + /* + * ---- add + */ + + function showAddModal() { + list.add.modal.$promise.then(list.add.modal.show); + } + + function projectCreatedSuccess(event, project) { + + list.addProject(project); + list.refreshProjects(); + + list.add.modal.hide(); + + } + + function projectCreatedError(event, project) { + list.add.modal.hide(); + } + + /* + * ---- delete + */ + + function showDeleteModal(project) { + + list.del.modal.$scope.project = project; + list.del.modal.$promise.then(list.del.modal.show); + + } + + function projectDeletedSuccess(event, project) { + + list.deleteProject(project); + list.refreshProjects(); + + list.del.modal.hide(); + + } + + function projectDeletedError(event, project) { + list.del.modal.hide(); + } + + /* + * ---- map + */ + + function showMapModal(project) { + + list.map.modal.$scope.project = project; + list.map.modal.$promise.then(list.map.modal.show); + + } + + function projectMappedSuccess(event, project) { + + activate(); + list.map.modal.hide(); + + } + + function projectMappedError(event, project) { + list.map.modal.hide(); + } + + /* + * ---- unmap + */ + + function showUnmapModal(project) { + + list.unmap.modal.$scope.project = project; + list.unmap.modal.$promise.then(list.unmap.modal.show); + + } + + function projectUnmappedSuccess(event, project) { + + + var index = _.findIndex(list.projects, function(aProject){ + return project.id === aProject.id; + }); + + if(index === -1){ + list.unmap.modal.hide(); + return false; + } + + list.projects[index] = project; + + list.refreshProjects(); + + list.unmap.modal.hide(); + + } + + function projectUnmappedError(event, project) { + list.unmap.modal.hide(); + } + + + /* + * ---- view + */ + + function showViewModal(project) { + + list.view.modal.$scope.project = project; + list.view.modal.$promise.then(list.view.modal.show); + + } + + } + +})(); |