From 1100c66ce03a059ebe7ece9734e799b49b3a5a9e Mon Sep 17 00:00:00 2001 From: WuKong Date: Sat, 23 Dec 2017 21:49:35 +0100 Subject: moonv4 cleanup Change-Id: Icef927f3236d985ac13ff7376f6ce6314b2b39b0 Signed-off-by: WuKong --- .../static/app/policy/policy.controller.list.js | 175 +++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100755 moon_gui/static/app/policy/policy.controller.list.js (limited to 'moon_gui/static/app/policy/policy.controller.list.js') diff --git a/moon_gui/static/app/policy/policy.controller.list.js b/moon_gui/static/app/policy/policy.controller.list.js new file mode 100755 index 00000000..fc2c6503 --- /dev/null +++ b/moon_gui/static/app/policy/policy.controller.list.js @@ -0,0 +1,175 @@ +(function() { + + 'use strict'; + + angular + .module('moon') + .controller('PolicyListController', PolicyListController); + + PolicyListController.$inject = ['$scope', 'policies', 'NgTableParams', '$filter', '$modal', '$rootScope']; + + function PolicyListController($scope, policies, NgTableParams, $filter, $modal, $rootScope) { + + var list = this; + + list.policies = policies; + + list.getPolicies = getPolicies; + list.hasPolicies = hasPolicies; + list.addPolicy = addPolicy; + list.refreshPolicies = refreshPolicies; + list.deletePolicy = deletePolicy; + + list.table = {}; + + list.search = { query: '', + find: searchPolicy, + reset: searchReset }; + + list.add = { modal: $modal({ template: 'html/policy/action/policy-add.tpl.html', show: false }), + showModal: showAddModal }; + + list.del = { modal: $modal({ template: 'html/policy/action/policy-delete.tpl.html', show: false }), + showModal: showDeleteModal }; + + activate(); + + function activate(){ + + newPoliciesTable(); + + } + + + /* + * ---- events + */ + + var rootListeners = { + + 'event:policyCreatedSuccess': $rootScope.$on('event:policyCreatedSuccess', policyCreatedSuccess), + 'event:policyCreatedError': $rootScope.$on('event:policyCreatedError', policyCreatedError), + + 'event:policyDeletedSuccess': $rootScope.$on('event:policyDeletedSuccess', policyDeletedSuccess), + 'event:policyDeletedError': $rootScope.$on('event:policyDeletedError', policyDeletedError) + + }; + + for (var unbind in rootListeners) { + $scope.$on('$destroy', rootListeners[unbind]); + } + + function getPolicies() { + return (list.policies) ? list.policies : []; + } + + function hasPolicies() { + return list.getPolicies().length > 0; + } + + function newPoliciesTable() { + + list.table = new NgTableParams({ + + page: 1, // show first page + count: 10, // count per page + sorting: { + name: 'asc', + genre: 'asc' + } + + }, { + + total: function () { return list.getPolicies().length; }, // length of data + getData: function($defer, params) { + + var orderedData = params.sorting() ? $filter('orderBy')(list.getPolicies(), params.orderBy()) : list.getPolicies(); + $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count())); + + }, + $scope: { $data: {} } + + }); + + return list.table; + + } + + + /* + * ---- search + */ + + function searchPolicy(policy){ + return (policy.name.indexOf(list.search.query) !== -1 || policy.genre.indexOf(list.search.query) !== -1 || policy.description.indexOf(list.search.query) !== -1); + } + + function searchReset() { + list.search.query = ''; + } + + /* + * ---- add + */ + function showAddModal() { + list.add.modal.$promise.then(list.add.modal.show); + } + + function policyCreatedSuccess(event, pdp) { + + list.addPolicy(pdp); + list.refreshPolicies(); + + list.add.modal.hide(); + + } + + function policyCreatedError(event, pdp) { + list.add.modal.hide(); + } + + function addPolicy(policy) { + list.policies.push(policy); + } + + function refreshPolicies() { + + list.table.total(list.policies.length); + list.table.reload(); + + } + + /* + * ---- delete + */ + + function showDeleteModal(policy) { + + list.del.modal.$scope.policy = policy; + list.del.modal.$promise.then(list.del.modal.show); + + } + + function deletePolicy(policy) { + + list.policies = _.chain(list.policies).reject({id: policy.id}).value(); + + } + + function policyDeletedSuccess(event, policy) { + + list.deletePolicy(policy); + list.refreshPolicies(); + + list.del.modal.hide(); + + } + + function policyDeletedError(event, policy) { + list.del.modal.hide(); + } + + + } + +})(); -- cgit 1.2.3-korg