rsanchez
2014-11-14 8076f3e62e24f23cc8eb866fe18cc6f00ceb02b8
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
242
(function() {
   'use strict';
   var app = angular.module('securis');
   
   var HTTP_ERRORS = {
            401: "Unathorized action",
            403: "Forbidden action",
           500: "Server error",
           418: "Application error",
           404: "Element not found"
   }
   
   app.directive(
                   'catalogField',
                   function() {
                       return {
                           restrict : 'A', // only activate on element
                                           // attribute
                           require : '?ngModel', // get a hold of
                                                   // NgModelController
                           link : function(scope, element, attrs, ngModel) {
                               if (!ngModel)
                                   return; // do nothing if no ng-model
                               // TODO: Replace the hard-coded form ID ('catalogForm') by the
                               // appropiate dynamic field
                               scope.catalogForm[attrs.name] = scope.catalogForm[scope.field.name];
                               scope.catalogForm[attrs.name].$name = attrs.name;
                           }
                       };
                   });
   app.controller('AdminCtrl', [
           '$scope',
           '$http',
           'toaster',
           'Catalogs',
           '$store',
           '$L',
           function($scope, $http, toaster, Catalogs, $store, $L) {
                $store.set('location', '/admin');
                $scope.showForm = false;
               $scope.isNew = false;
               $scope.formu = {};
               $scope.catalogIndex = 0;
               $scope.catalogMetadata = {};
               $scope.catalogsList = null;
               $scope.list = null;
               
               
               var _changeCatalog = function(index) {
                   $scope.showForm = false;
                   $scope.formu = {};
                   if (!$scope.catalogsList) $scope.catalogsList = Catalogs.getList(); // catalog list is also in index.data
                   if (typeof index === 'number') $scope.catalogIndex = index;
                   Catalogs.setCurrent($scope.catalogIndex);
                   $scope.catalogMetadata = Catalogs.getMetadata();
                   $scope.list = Catalogs.query();
                   $scope.refs = {}
                   Catalogs.loadRefs(function(refs) {
                       console.log('Updated refs in form');
                       console.log(refs);
                       $scope.refs = refs;
                   });
               }
               
               Catalogs.init().then(_changeCatalog); 
               $scope.selectCatalog = _changeCatalog;
               
               $scope.edit = function(data) {
                   $scope.showForm = true;
                   $scope.isNew = false;
                   // Next line is a workaround due to some issues with values with ID == 0
                   $('select').val(null);
                   $scope.formu = {}
                   var fields = Catalogs.getMetadata().fields;
                   console.log($scope);
                   
                   fields.forEach(function(field) {
                       if (field.type === 'select') {
                           // next lines are a workaround to avoid an issue where we try to show a form with "select" fields (if select field value doesn't change
                           $scope.formu[field.name] = null;
                           setTimeout(function() {
                               $scope.formu[field.name] = data[field.name];
                               $scope.$apply();
                           }, 0);
                       } else {
                           if (!field.listingOnly) $scope.formu[field.name] = data[field.name] || null;
                       }
                   })
                   setTimeout(function() {
                       $('#'+Catalogs.getFFF()).focus();
                   }, 0);
               }
               $scope.delete = function(data) {
                   BootstrapDialog.confirm($L.get('The record will be deleted, are you sure?'), function(result){
                       if(result) {
                           var promise = Catalogs.remove(data).$promise;
                           promise.then(function(data) {
                               $scope.list = Catalogs.query();
                               Catalogs.refreshRef($scope.refs, Catalogs.getMetadata().resource, $scope.list);
                               toaster.pop('success', Catalogs.getName(), $L.get("Element deleted successfully"));
                           },function(error) {
                               console.log(error);
                               toaster.pop('error', Catalogs.getName(), $L.get("Error deleting element, reason: {0}. Details: {1}", $L.get(HTTP_ERRORS[error.status]), error.headers('X-SECURIS-ERROR-MSG')), 10000);
                           });
                       }
                   });
                   $scope.showForm = false;
                   $scope.isNew = false;
               }
           } ]);
   app.controller('CatalogFormCtrl', [ '$scope', '$http', 'toaster', 'Catalogs', '$L',
           function($scope, $http, toaster, Catalogs, $L) {
               $scope.scope = $scope;
               console.log('Form: currentCatalog:' + $scope.cataLogIndex);
               
               $scope.inputType = function(field) {
                   
                   if (field.readOnly && field.type === 'date')
                       return 'readonly_date';
                   if (field.readOnly && (!field.pk || !$scope.isNew ))
                       return 'readonly';
                   if (field.type === 'select')
                       return 'select';
                   if (field.type === 'multiselect')
                       return 'multiselect';
                   if (field.type === 'metadata')
                       return 'metadata';
                   if (field.type === 'password')
                       return 'password';
                   if (!field.multiline)
                       return 'normal';
                   if (field.multiline)
                       return 'textarea';
               }
               
               $scope.editNew = function() {
                   $scope.$parent.isNew = true;
                   $scope.$parent.showForm = true;
                   $('select').val(null);
                   $scope.$parent.formu = {};
                   
                   var fields = Catalogs.getMetadata().fields;
                   fields.forEach(function(field) {
                       if (!field.listingOnly) $scope.$parent.formu[field.name] = null;
                   })
                   setTimeout(function() {
                       $('#'+Catalogs.getFFF()).focus();
                   }, 0);
                   
               }
               $scope.cancel = function() {
                   $scope.$parent.showForm = false;
                   $scope.catalogForm.$setPristine();
               }
               $scope.saveCatalog = function() {
                   if ($scope.catalogForm.$invalid) {
                       toaster.pop('error', Catalogs.getName(), $L.get("There are wrong data in current form, please fix it before to save"));
                   } else {
                       var promise = Catalogs.save($scope.formu).$promise;
                       promise.then(function(data, otro) {
                           if ($scope.isNew) {
                               $scope.catalogForm.$setPristine();
                               $scope.$parent.formu = {}
                               $('#'+ Catalogs.getFFF()).focus();
                           } else {
                               $scope.cancel();
                           }
                           $scope.$parent.list = Catalogs.query();
                           Catalogs.refreshRef($scope.refs, Catalogs.getMetadata().resource, $scope.$parent.list);
                           toaster.pop('success', Catalogs.getName(), $L.get("Element saved successfully"));
                       }, function(error) {
                           console.log(error);
                           toaster.pop('error', Catalogs.getName(), $L.get("Error saving element, reason: {0}. Details: {1}", $L.get(HTTP_ERRORS[error.status]), error.headers('X-SECURIS-ERROR-MSG')), 10000);
                       });
                   }
               }
               
               $scope.selectFieldChanged = function(onchangehandler) {
                   if (onchangehandler) {
                       $scope[onchangehandler]();
                   }
               }
               // Metadata management
               
               $scope.createMetadataRow = function() {
                   if (!$scope.formu.metadata) {
                       $scope.formu.metadata = [];
                   }
                   $scope.formu.metadata.push({key: '', value: '', mandatory: true});
               }
               $scope.removeMetadataKey = function(row_md) {
                   $scope.formu.metadata.splice( $scope.formu.metadata.indexOf(row_md), 1 );
               }
               $scope.updateMetadata = function() {
                   // Called when Application ID change in current field
                   var newAppId = $scope.formu['application_id'];
                   if (newAppId) {
                       // Only if there is a "valid" value selected we should update the metadata
                       Catalogs.getResource('application').get({appId: newAppId}).$promise.then(function(app) {
                           $scope.formu.metadata = [];
                           app.metadata.forEach(function(md) {
                               $scope.formu.metadata.push({
                                   key: md.key,
                                   value: md.value,
                                   mandatory: md.mandatory
                               });
                           });
                       });
                   }
               }
               
           } ]);
   app.controller('CatalogListCtrl', [ '$scope', '$http', '$filter', 'Catalogs', 
           function($scope, $http, $filter, Catalogs) {
               $scope.print = function(name, row) {
                   var value = row[name];
                   var type = Catalogs.getField(name).type;
                   var printedValue = type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value;
                   if (printedValue !== value) // this line is a work around to allow search in formatted fields
                       row['_display_'+name] = printedValue;
                   return printedValue;
               }
               
               $scope.display = function(name) {
                   return Catalogs.getField(name).display;
               }
           } ]);
})();