aboutsummaryrefslogtreecommitdiffstats
path: root/old/moon_gui/static/app/policy/policy.controller.list.js
blob: fc2c6503dfdeb75b3be66c48aad8478b02696698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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();
        }


    }

})();