aboutsummaryrefslogtreecommitdiffstats
path: root/moon_gui/static/app/model/model.controller.list.js
blob: 5021a57e1f59403dc6b697e5ae259189a2df0b70 (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
(function() {

    'use strict';

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

    ModelListController.$inject = ['$scope', '$rootScope', 'models', 'NgTableParams', '$filter', '$modal'];

    function ModelListController($scope, $rootScope, models, NgTableParams, $filter, $modal) {

        var list = this;

        list.models = models;

        list.table = {};

        list.search = { query: '',
            find: searchModel,
            reset: searchReset };

        list.getModels = getModels;
        list.hasModels = hasModels;
        list.deleteModel = deleteModel;
        list.refreshModels = refreshModels;

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

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

        list.del = { modal: $modal({ template: 'html/model/action/model-delete.tpl.html', show: false }),
            showModal: showDeleteModal };

        activate();

        function activate(){
            newModelsTable();
        }


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

            'event:modelCreatedSuccess': $rootScope.$on('event:modelCreatedSuccess', modelCreatedSuccess),
            'event:modelCreatedError': $rootScope.$on('event:modelCreatedError', modelCreatedError),

            'event:modelDeletedSuccess': $rootScope.$on('event:modelDeletedSuccess', modelDeletedSuccess),
            'event:modelDeletedError': $rootScope.$on('event:modelDeletedError', modelDeletedError)


        };

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


        function newModelsTable() {

            list.table = new NgTableParams({

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

            }, {

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

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

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

            });

            return list.table;

        }

        function getModels() {
            return (list.models) ? list.models : [];
        }

        function hasModels() {
            return list.getModels().length > 0;
        }

        /**
         * Blank the search field
         */
        function searchReset() {
            list.search.query = '';
        }

        /*
         * ---- search
         */

        function searchModel(model){
            return (model.name.indexOf(list.search.query) !== -1 || model.description.indexOf(list.search.query) !== -1);
        }

        /*
         * ---- add
         */
        function showAddModal() {
            list.add.modal.$promise.then(list.add.modal.show);
        }

        function addModel(model) {
            list.models.push(model);
        }

        /**
         * Refresh the table
         */
        function refreshModels(){
            list.table.total(list.models.length);
            list.table.reload();
        }

        /**
         * This function will add a model to the current list of models and refresh the table
         * @param event
         * @param model
         */
        function modelCreatedSuccess(event, model) {
            addModel(model);
            refreshModels();
            list.add.modal.hide();
        }

        /**
         * This function hide the add modal
         * @param event
         */
        function modelCreatedError(event) {
            list.add.modal.hide();
        }

        /*
         * ---- view
         */

        function showViewModal(model) {

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

        }


        /*
         * ---- delete
         */

        function showDeleteModal(model) {

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

        }

        function deleteModel(model) {
            list.models = _.chain(list.models).reject({id: model.id}).value();
        }


        function modelDeletedSuccess(event, model) {

            list.deleteModel(model);
            list.refreshModels();

            list.del.modal.hide();

        }

        function modelDeletedError(event, model) {
            list.del.modal.hide();
        }


    }

})();