Roberto Sánchez
2014-01-10 59cdd2b7ebceae94fbecdb1eeb46a969666dc88f
securis/src/main/resources/static/js/admin.js
....@@ -1,7 +1,7 @@
11 (function() {
22 'use strict';
33
4
- var app = angular.module('app', [ 'ngRoute', 'ngAnimate', 'ngResource', 'toaster' ]);
4
+ var app = angular.module('app', [ 'ngRoute', 'ngAnimate', 'ngResource', 'toaster', 'catalogs' ]);
55
66 app.directive(
77 'catalogField',
....@@ -22,124 +22,34 @@
2222 };
2323 });
2424
25
- app.factory('Catalogs', function($http, $resource, toaster) {
26
- var CatalogsService = {
27
- resources : {
28
- application : $resource('/application/:appId', {
29
- appId : '@id'
30
- }, {
31
- update : {
32
- method : "PUT"
33
- }
34
- }),
35
- user : $resource('/user/:userId', {
36
- userId : '@id'
37
- }, {
38
- update : {
39
- method : "PUT"
40
- }
41
- }),
42
- licensetype : $resource('/licensetype/:licenseTypeId', {
43
- licenseTypeId : '@id'
44
- }, {
45
- update : {
46
- method : "PUT"
47
- }
48
- })
49
-
50
- },
51
- list : function(initFn) {
52
- $http.get('/js/catalogs.json').success(function(data) {
53
- console.log(data);
54
- CatalogsService.data = data;
55
- initFn();
56
- })
57
- return CatalogsService;
58
- },
59
- getName : function(index) {
60
- return CatalogsService.data ? CatalogsService.data[index].name
61
- : '';
62
- },
63
- getResource : function(index) {
64
- return CatalogsService.data ? CatalogsService.data[index].resource
65
- : '';
66
- },
67
- getMetadata : function(index) {
68
- return CatalogsService.data ? CatalogsService.data[index] : {};
69
- },
70
- save: function(catalog, data) {
71
- var resource = CatalogsService.resources[catalog.toLowerCase()];
72
- function success(data) {
73
- console.log('success')
74
- console.log(data)
75
- toaster.pop('success', "Data saved sucessfully in " + catalog);
76
- }
77
- function fail(data, status) {
78
- var errorMsg = {500: 'Server error', 404: 'Item to modify was not found'}[data.status]
79
- toaster.pop('error', "Error saving data in " + catalog, errorMsg);
80
- console.log('error')
81
- console.error(data)
82
- console.error(status)
83
- }
84
- if (data.id && data.id !== '')
85
- return resource.update(data, success, fail)
86
- else
87
- return resource.save(data, success, fail)
88
- },
89
- remove: function(catalog, data) {
90
- var resource = CatalogsService.resources[catalog.toLowerCase()];
91
- function success(data) {
92
- console.log('success')
93
- console.log(data)
94
- }
95
- function fail(data, status) {
96
- console.log('error')
97
- console.error(data)
98
- console.error(status)
99
- }
100
- return resource.remove({}, data, success, fail)
101
- },
102
- query: function(catalog) {
103
- console.log('HI catalog ???? ' + catalog);
104
- var resource = CatalogsService.resources[catalog];
105
- function success(data) {
106
- console.log('success')
107
- console.log(data)
108
- }
109
- function fail(data, status) {
110
- console.log('error')
111
- console.error(data)
112
- console.error(status)
113
- }
114
- return resource.query({}, success, fail);
115
- }
116
- }
117
-
118
- return CatalogsService;
119
-
120
- });
121
-
12225 app.controller('CatalogsCtrl', [
12326 '$scope',
12427 '$http',
12528 'Catalogs',
12629 function($scope, $http, Catalogs) {
127
- $scope.showForm = true;
30
+ $scope.showForm = false;
12831 $scope.isNew = false;
12932 $scope.formu = {};
13033 $scope.catalogIndex = 0;
131
- $scope.catalogs = Catalogs.list(function() {
132
- $scope.catalogMetadata = Catalogs.getMetadata($scope.catalogIndex);
133
- $scope.list = Catalogs.query(Catalogs.getResource($scope.catalogIndex));
134
- });
135
-
13634 $scope.catalogMetadata = {};
137
- $scope.selectCatalog = function(index, $event) {
138
- $scope.catalogIndex = index;
139
- $scope.catalogMetadata = Catalogs.getMetadata($scope.catalogIndex);
140
- $scope.list = Catalogs.query(Catalogs.getResource($scope.catalogIndex));
141
- console.log($event);
35
+ $scope.catalogsList = null;
36
+ $scope.list = null;
37
+
38
+ var _changeCatalog = function(index) {
39
+ if (!$scope.catalogsList) $scope.catalogsList = Catalogs.getList(); // catalog list is also in index.data
40
+ if (typeof index === 'number') $scope.catalogIndex = index;
41
+ Catalogs.setCurrent($scope.catalogIndex);
42
+ $scope.catalogMetadata = Catalogs.getMetadata();
43
+ $scope.list = Catalogs.query();
44
+ $scope.refs = {}
45
+ Catalogs.loadRefs($scope.refs)
46
+ console.log($scope.refs)
14247 }
48
+
49
+ Catalogs.init().then(_changeCatalog);
50
+
51
+ $scope.selectCatalog = _changeCatalog;
52
+
14353 $scope.edit = function(data) {
14454 $scope.showForm = true;
14555 $scope.isNew = false;
....@@ -149,15 +59,16 @@
14959 }
15060 console.log('$scope.edit')
15161 console.log($scope.formu)
62
+ $('#'+ Catalogs.getFFF()).focus();
63
+
15264
15365 }
15466 $scope.delete = function(data) {
15567 BootstrapDialog.confirm('The record will be deleted, are you sure?', function(result){
15668 if(result) {
157
- var catalogName = Catalogs.getResource($scope.catalogIndex);
158
- var promise = Catalogs.remove(catalogName, data).$promise;
69
+ var promise = Catalogs.remove(data).$promise;
15970 promise.then(function(data) {
160
- $scope.list = Catalogs.query(catalogName);
71
+ $scope.list = Catalogs.query();
16172 });
16273 }
16374 });
....@@ -190,7 +101,7 @@
190101 }
191102
192103 $scope.editNew = function() {
193
- $('#name').focus();
104
+ $('#'+ Catalogs.getFFF()).focus();
194105 $scope.$parent.showForm = true;
195106 $scope.$parent.isNew = true;
196107 $scope.$parent.formu = {};
....@@ -203,20 +114,17 @@
203114 if ($scope.catalogForm.$invalid) {
204115 alert(JSON.stringify($scope.catalogForm))
205116 } else {
206
- var catalogName = Catalogs.getResource($scope.catalogIndex);
207
- var promise = Catalogs.save(catalogName, $scope.formu).$promise;
117
+ var promise = Catalogs.save($scope.formu).$promise;
208118 promise.then(function(data, otro) {
209119 if ($scope.isNew) {
210120 $scope.$parent.formu = {}
211
- $('#name').focus();
121
+ $('#'+ Catalogs.getFFF()).focus();
212122 } else {
213123 $scope.cancel();
214124 }
215
- $scope.$parent.list = Catalogs.query(catalogName);
216
- }, function(error, otro) {
217
- console.log('then error');
125
+ $scope.$parent.list = Catalogs.query();
126
+ }, function(error) {
218127 console.log(error);
219
- console.log(otro);
220128 });
221129
222130 }
....@@ -225,24 +133,18 @@
225133
226134 app.controller('CatalogListCtrl', [ '$scope', '$http', '$filter', 'Catalogs',
227135 function($scope, $http, $filter, Catalogs) {
228
- console.log('List: currentCatalog: ' + $scope.currentCatalog);
229
- var _indexOfField = function(name) {
230
- if (!$scope.catalogMetadata) return -1;
231
- for (var i = $scope.catalogMetadata.fields.length - 1; i >= 0 && $scope.catalogMetadata.fields[i].name !== name; i--);
232
- return i;
233
- }
234
-
235
- $scope.print = function(name, value) {
236
- var index = _indexOfField(name);
237
- if (index === -1) return value;
238
- var type = $scope.catalogMetadata.fields[index].type;
239
-
240
- return type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value;
136
+
137
+ $scope.print = function(name, row) {
138
+ var value = row[name];
139
+ var type = Catalogs.getField(name).type;
140
+ var printedValue = type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value;
141
+ if (printedValue !== value) // this line is a work around to allow search in formatted fields
142
+ row['_display_'+name] = printedValue;
143
+ return printedValue;
241144 }
242145
243146 $scope.display = function(name) {
244
- var index = _indexOfField(name);
245
- return index === -1 ? '' : $scope.catalogMetadata.fields[index].display;
147
+ return Catalogs.getField(name).display;
246148 }
247149
248150 } ]);