aboutsummaryrefslogtreecommitdiffstats
path: root/moon_gui/static/app/model/edit/metarules/metarules.list.dir.js
blob: 9c1bc72ec35fcd168fddb4066b5d8352509ce323 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
(function() {

    'use strict';

    angular
        .module('moon')
        .directive('moonMetaRulesList', moonMetaRulesList);

    moonMetaRulesList.$inject = [];

    function moonMetaRulesList() {

        return {
            templateUrl : 'html/model/edit/metarules/metarules-list.tpl.html',
            bindToController : true,
            controller : moonMetaRulesListController,
            controllerAs : 'list',
            scope : {
                // if edit and delete possibilities are displayed
                // Value are True or False
                editMode : '=',
                mappedModel : '='
            },
            restrict : 'E',
            replace : true
        };
    }

    angular
        .module('moon')
        .controller('moonMetaRulesListController', moonMetaRulesListController);

    moonMetaRulesListController.$inject = ['$scope', '$rootScope', 'NgTableParams', '$filter', '$modal', 'metaRuleService'];

    function moonMetaRulesListController($scope, $rootScope, NgTableParams, $filter, $modal, metaRuleService ){

        var list = this;

        list.table = {};

        list.editMode = $scope.list.editMode;
        list.model = $scope.list.mappedModel;
        list.metaRules = list.model.meta_rules_values;

        list.getMetaRules = getMetaRules;
        list.hasMetaRules = hasMetaRules;
        list.showDetail = showDetail;
        list.getSubjectList = getSubjectList;
        list.getObjectList = getObjectList;
        list.getActionlist = getActionlist;
        list.getShowDetailValue = getShowDetailValue;

        list.showDetailValue = false;

        list.subject_list = [];
        list.object_list = [];
        list.action_list = [];

        list.edit = { modal: $modal({ template: 'html/model/edit/metarules/action/metarules-edit.tpl.html', show: false }),
            showModal: showEditModal };

        /*list.edit.modal.result.finally(function(){
             console.log('CATCHING');
        });*/


        list.map = { modal: $modal({ template: 'html/model/edit/metarules/action/mapping/metarules-map.tpl.html', show: false }),
            showModal: showMapModal };

        list.unmap = { modal: $modal({ template: 'html/model/edit/metarules/action/mapping/metarules-unmap.tpl.html', show: false }),
            showModal: showUnmapModal };

        activate();

        function activate(){

            newMetaRulesTable();

        }

        /*
         * ---- events
         */
        var rootListeners = {

            'event:metaRuleMapToModelSuccess': $rootScope.$on('event:metaRuleMapToModelSuccess', updateModelFromMapSuccess),

            'event:metaRuleUnMappedToModelSuccess': $rootScope.$on('event:metaRuleUnMappedToModelSuccess', modelUnmappedSuccess),
            'event:metaRuleUnMappedToModelError': $rootScope.$on('event:metaRuleUnMappedToModelError', modelUnmappedError),
            

        };

        for (var unbind in rootListeners) {
            $scope.$on('$destroy', rootListeners[unbind]);
        }



        function newMetaRulesTable() {

            list.table = new NgTableParams({

                page: 1,            // show first page
                count: 10,          // count per page
                sorting: {
                    name: 'asc' // initial sorting
                }

            }, {

                total: function () { return list.getMetaRules().length; }, // length of data
                getData: function($defer, params) {

                    var orderedData = params.sorting() ? $filter('orderBy')(list.getMetaRules(), params.orderBy()) : list.getMetaRules();
                    $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));

                },
                $scope: { $data: {} }

            });

            return list.table;

        }

        /**
         * If the directive is not in editMode and displaying MetaData Content, if the editMode change to true, MetaData Content need to be hidden
         */
        $scope.$watch('list.editMode',  function(newValue, oldValue){
            list.showDetailValue = false;
        });

        function getMetaRules() {
            return (list.metaRules) ? list.metaRules : [];
        }

        function hasMetaRules() {
            return list.getMetaRules().length > 0;
        }

        function showDetail(aMetaRule){

            if(aMetaRule.id === getShowDetailValue().id){

                list.showDetailValue = false;
                list.subject_list = [];
                list.object_list = [];
                list.action_list = [];

            }else{

                list.subject_list = aMetaRule.subject_categories_values;
                list.object_list = aMetaRule.object_categories_values;
                list.action_list = aMetaRule.action_categories_values;
                list.showDetailValue = aMetaRule;

            }

        }

        function showEditModal(aMetaRule) {
            list.edit.modal.$scope.metaRule = aMetaRule;
            list.edit.modal.$promise.then(list.edit.modal.show);
        }

        function getShowDetailValue(){
            return list.showDetailValue;
        }

        function getSubjectList(){
            return list.subject_list;
        }

        function getObjectList(){
            return list.object_list;
        }

        function getActionlist(){
            return list.action_list;
        }

        /*
         * ---- add
         */
        function showMapModal() {
            list.map.modal.$scope.model = list.model;
            list.map.modal.$promise.then(list.map.modal.show);
        }

        function refreshRules(){

            list.metaRules = list.model.meta_rules_values;
            list.table.total(list.getMetaRules().length);
            list.table.reload();

        }

        function updateModelFromMapSuccess(event, model){

            list.model = model;

            refreshRules();

            list.map.modal.hide();

        }

        /*
         * ---- unmap
         */

        function showUnmapModal(metaRule) {

            list.unmap.modal.$scope.model = list.model;
            list.unmap.modal.$scope.metaRule = metaRule;
            list.unmap.modal.$promise.then(list.unmap.modal.show);

        }

        function modelUnmappedSuccess(event, model) {

            list.model = model;

            metaRuleService.findSomeWithCallback(list.model.meta_rules, function(meta_rules){

                list.model.meta_rules_values = meta_rules;
                refreshRules();
                list.unmap.modal.hide();

            });

        }

        function modelUnmappedError(event) {
            list.unmap.modal.hide();
        }

    }

})();