Roberto Sánchez
2014-01-17 04afd774aecc70dca37559fdd8b9a716829c18cd
securis/src/main/resources/static/js/catalogs.js
....@@ -5,209 +5,294 @@
55 * Catalogs module
66 */
77
8
- angular.module('catalogs', ['ngResource'])
8
+ angular
9
+ .module('catalogs', [ 'ngResource' ])
910
10
- .service('Catalogs', ['$rootScope', '$http', '$resource', '$q', function ($rootScope, $http, $resource, $q) {
11
- var resources = {
12
- application : $resource('/application/:appId', {
13
- appId : '@id'
14
- }),
15
- user : $resource('/user/:userId', {
16
- userId : '@username'
17
- }),
18
- organization : $resource('/organization/:orgId', {
19
- orgId : '@id'
20
- }),
21
- licensetype : $resource('/licensetype/:licenseTypeId', {
22
- licenseTypeId : '@id'
23
- })
24
- }
11
+ .service(
12
+ 'Catalogs',
13
+ [
14
+ '$rootScope',
15
+ '$http',
16
+ '$resource',
17
+ '$q',
18
+ function($rootScope, $http, $resource, $q) {
19
+ var resources = {
20
+ application : $resource(
21
+ '/application/:appId', {
22
+ appId : '@id'
23
+ }),
24
+ user : $resource('/user/:userId', {
25
+ userId : '@username'
26
+ }),
27
+ organization : $resource(
28
+ '/organization/:orgId', {
29
+ orgId : '@id'
30
+ }),
31
+ licensetype : $resource(
32
+ '/licensetype/:licenseTypeId', {
33
+ licenseTypeId : '@id'
34
+ })
35
+ }
2536
26
- var _metadata = null;
27
- var _current = null;
37
+ var _metadata = null;
38
+ var _current = null;
2839
29
- var _list = function() {
30
- return $http.get('/js/catalogs.json').success(function(data) {
31
- _metadata = data;
32
- })
33
- }
34
- this.init = function() {
35
- return _list();
36
- }
37
- this.getList = function() {
38
- return _metadata;
39
- }
40
- this.getName = function(index) {
41
- if (index === undefined)
42
- return _current ? _current.name : '';
43
- return _metadata ? _metadata[index].name : '';
44
- }
45
- this.getResource = function(res) {
46
- if (res === undefined)
47
- return _current ? resources[_current.resource] : null;
48
- return _current ? resources[res] : null;
49
- }
50
- this.getPk = function(catalogMetadata) {
51
- if (!catalogMetadata) catalogMetadata = _current;
52
-
53
- for(var i = 0; i < catalogMetadata.fields.length; i++)
54
- if (catalogMetadata.fields[i].pk) return catalogMetadata.fields[i].name;
55
-
56
- return null;
57
- }
58
- /**
59
- * Returns catalog metadata
60
- * @param index: Return current catalog if undefined, if string It find the catalog by resoource name if number it find it by position
61
- */
62
- this.getMetadata = function(index) {
63
- if (!_metadata) throw new Error('There is no catalog metadata info');
64
- if (index === undefined)
65
- return _current;
66
- if (typeof index === 'string') {
67
- for (var i = _metadata.length - 1; i >= 0 && _metadata[i].resource !== index; i--);
68
- index = i;
69
- }
40
+ var _list = function() {
41
+ return $http.get('/js/catalogs.json')
42
+ .success(function(data) {
43
+ _metadata = data;
44
+ })
45
+ }
46
+ this.init = function() {
47
+ return _list();
48
+ }
49
+ this.getList = function() {
50
+ return _metadata;
51
+ }
52
+ this.getName = function(index) {
53
+ if (index === undefined)
54
+ return _current ? _current.name : '';
55
+ return _metadata ? _metadata[index].name
56
+ : '';
57
+ }
58
+ this.getResource = function(res) {
59
+ if (res === undefined)
60
+ return _current ? resources[_current.resource]
61
+ : null;
62
+ return _current ? resources[res] : null;
63
+ }
64
+ this.getPk = function(catalogMetadata) {
65
+ if (!catalogMetadata)
66
+ catalogMetadata = _current;
7067
71
- return _metadata[index];
72
- }
73
- this.setCurrent = function(index) {
74
- if (!_metadata) throw new Error('There is no catalog metadata info');
75
- if (index === undefined)
76
- _current = null;
77
- else
78
- _current = _metadata[index];
79
- }
80
- /********************************************
81
- * Catalog fields methods *
82
- ********************************************/
68
+ for (var i = 0; i < catalogMetadata.fields.length; i++)
69
+ if (catalogMetadata.fields[i].pk)
70
+ return catalogMetadata.fields[i].name;
8371
84
- /**
85
- * Returns the first field in form that should get the focus. We find the first field that is not read only
86
- */
87
- this.getFFF = this.getFirstFocusableField = function() {
88
- if (!_current) throw new Error('There is no current catalog selected');
89
-
90
- for(var i = 0; i < _current.fields.length; i++)
91
- if (!_current.fields[i].readOnly) return _current.fields[i].name;
92
-
93
- return null;
94
- }
72
+ return null;
73
+ }
74
+ /**
75
+ * Returns catalog metadata
76
+ *
77
+ * @param index:
78
+ * Return current catalog if
79
+ * undefined, if string It find the
80
+ * catalog by resoource name if
81
+ * number it find it by position
82
+ */
83
+ this.getMetadata = function(index) {
84
+ if (!_metadata)
85
+ throw new Error(
86
+ 'There is no catalog metadata info');
87
+ if (index === undefined)
88
+ return _current;
89
+ if (typeof index === 'string') {
90
+ for (var i = _metadata.length - 1; i >= 0
91
+ && _metadata[i].resource !== index; i--)
92
+ ;
93
+ index = i;
94
+ }
9595
96
- /**
97
- * Find the field by name or position
98
- */
99
- this.getField = function(key) {
100
- if (!_current) throw new Error('There is no current catalog selected');
101
- var index = -1;
102
- if (typeof key === 'string') {
103
- for (var i = _current.fields.length - 1; i >= 0 && _current.fields[i].name !== key; i--);
104
- index = i;
105
- } else {
106
- index = key; // In this case key === field position
107
- }
96
+ return _metadata[index];
97
+ }
98
+ this.setCurrent = function(index) {
99
+ if (!_metadata)
100
+ throw new Error(
101
+ 'There is no catalog metadata info');
102
+ if (index === undefined)
103
+ _current = null;
104
+ else
105
+ _current = _metadata[index];
106
+ }
107
+ /***********************************************
108
+ * Catalog fields methods *
109
+ **********************************************/
108110
109
- return index === -1 ? {} : _current.fields[index];
110
- }
111
-
112
- /********************************************
113
- * Catalog resource operations on server *
114
- ********************************************/
111
+ /**
112
+ * Returns the first field in form that should
113
+ * get the focus. We find the first field that
114
+ * is not read only
115
+ */
116
+ this.getFFF = this.getFirstFocusableField = function() {
117
+ if (!_current)
118
+ throw new Error(
119
+ 'There is no current catalog selected');
115120
116
- function _success(response) {
117
- console.log('$resource')
118
- console.log(response)
119
- }
120
- function _fail(response) {
121
- console.error('Error trying to get data, HTTP error code: ' + response.status)
122
- }
123
-
124
-
125
- this.save = function(data) {
126
- if (!_current) throw new Error('There is no current catalog selected');
121
+ for (var i = 0; i < _current.fields.length; i++)
122
+ if (!_current.fields[i].readOnly)
123
+ return _current.fields[i].name;
127124
128
- var resource = this.getResource();
129
- return resource.save(data, _success, _fail);
130
- }
131
- this.remove = function(data) {
132
- return this.getResource().remove({}, data, _success, _fail)
133
- }
134
- this.query = function() {
135
- return this.getResource().query({}, _success, _fail);
136
- }
137
- this.refreshRef = function(refs, res, preloadedData) {
138
- // We check if there is some field for the resource passed as parameter
139
- var field = (function() {
140
- for (var i = _current.fields.length - 1; i >= 0; i--) {
141
- if (_current.fields[i].resource === res)
142
- return _current.fields[i];
143
- }
144
- return null;
145
- })();
146
-
147
- // If field for that resource is not found there is nothing to refresh
148
- if (!field) return;
149
- var resource = this.getResource(res);
150
- var data = preloadedData || resource.query({}, _success, _fail);
151
- var that = this;
152
- data.$promise.then(function(responseData) {
153
- var pk = that.getPk(that.getMetadata(field.resource))
154
- var comboData = []
155
- responseData.forEach(function(row) {
156
- comboData.push({
157
- id: row[pk],
158
- label: row.label || row.name || row.code || row.first_name + ' ' + row.last_name
159
- });
160
- })
161
- refs[field.name] = comboData;
162
- })
163
- }
164
- this.loadRefs = function(refs) {
165
- if (!_current) throw new Error('There is no current catalog selected');
166
- var refsFields = [];
167
- _current.fields.forEach(function(f) {
168
- if (f.resource)
169
- refsFields.push(f)
125
+ return null;
126
+ }
170127
171
- });
172
-
173
- var that = this;
174
- var promises = []
175
- refsFields.forEach(function(f) {
176
- var resource = that.getResource(f.resource);
177
- refs[f.name] = resource.query({}, _success, _fail);
178
- promises.push(refs[f.name].$promise);
179
- });
180
-
181
- console.log('promises: ' + promises.length + ' ')
182
- console.log(promises)
183
- $q.all(promises).then(function() {
184
-
185
- for (var k in refs) {
186
- var field = that.getField(k);
187
- var pk = that.getPk(that.getMetadata(field.resource))
188
- console.log('PK field for ' + k + ' is ' + pk)
189
- var comboData = []
190
- refs[k].forEach(function(row) {
191
- console.log('field.resource !== _current.resource: ' + field.resource +' '+ _current.resource)
192
- comboData.push({
193
- id: row[pk],
194
- label: row.label || row.name || row.code || row.first_name + ' ' + (row.last_name || '')
195
- });
196
- })
197
- refs[k] = comboData;
198
- console.log('Ready for combo for ' + k)
199
- console.log(comboData);
200
- }
201
- _current.fields.forEach(function(f) {
202
- if (f.values)
203
- refs[f.name] = f.values;
204
- });
205
- })
206
-
207
- console.log(refs);
208
- return refs;
209
- }
210
-
211
- }])
128
+ /**
129
+ * Find the field by name or position
130
+ */
131
+ this.getField = function(key) {
132
+ if (!_current)
133
+ throw new Error(
134
+ 'There is no current catalog selected');
135
+ var index = -1;
136
+ if (typeof key === 'string') {
137
+ for (var i = _current.fields.length - 1; i >= 0
138
+ && _current.fields[i].name !== key; i--)
139
+ ;
140
+ index = i;
141
+ } else {
142
+ index = key; // In this case key ===
143
+ // field position
144
+ }
145
+
146
+ return index === -1 ? {}
147
+ : _current.fields[index];
148
+ }
149
+
150
+ /***********************************************
151
+ * Catalog resource operations on server *
152
+ **********************************************/
153
+
154
+ function _success(response) {
155
+ console.log('$resource')
156
+ console.log(response)
157
+ }
158
+ function _fail(response) {
159
+ console
160
+ .error('Error trying to get data, HTTP error code: '
161
+ + response.status)
162
+ }
163
+
164
+ this.save = function(data) {
165
+ if (!_current)
166
+ throw new Error(
167
+ 'There is no current catalog selected');
168
+
169
+ var resource = this.getResource();
170
+ return resource.save(data, _success, _fail);
171
+ }
172
+ this.remove = function(data) {
173
+ return this.getResource().remove({}, data,
174
+ _success, _fail)
175
+ }
176
+ this.query = function() {
177
+ return this.getResource().query({},
178
+ _success, _fail);
179
+ }
180
+ this.refreshRef = function(refs, res,
181
+ preloadedData) {
182
+ // We check if there is some field for the
183
+ // resource passed as parameter
184
+ var field = (function() {
185
+ for (var i = _current.fields.length - 1; i >= 0; i--) {
186
+ if (_current.fields[i].resource === res)
187
+ return _current.fields[i];
188
+ }
189
+ return null;
190
+ })();
191
+
192
+ // If field for that resource is not found
193
+ // there is nothing to refresh
194
+ if (!field)
195
+ return;
196
+ var resource = this.getResource(res);
197
+ var data = preloadedData
198
+ || resource.query({}, _success,
199
+ _fail);
200
+ var that = this;
201
+ data.$promise.then(function(responseData) {
202
+ var pk = that.getPk(that
203
+ .getMetadata(field.resource))
204
+ var comboData = []
205
+ responseData.forEach(function(row) {
206
+ comboData.push({
207
+ id : row[pk],
208
+ label : row.label || row.name
209
+ || row.code
210
+ || row.first_name + ' '
211
+ + row.last_name
212
+ });
213
+ })
214
+ refs[field.name] = comboData;
215
+ })
216
+ }
217
+ this.loadRefs = function(refs) {
218
+ if (!_current)
219
+ throw new Error(
220
+ 'There is no current catalog selected');
221
+ var refsFields = [];
222
+ _current.fields.forEach(function(f) {
223
+ if (f.resource)
224
+ refsFields.push(f)
225
+
226
+ });
227
+
228
+ var that = this;
229
+ var promises = []
230
+ refsFields.forEach(function(f) {
231
+ var resource = that
232
+ .getResource(f.resource);
233
+ refs[f.name] = resource.query({},
234
+ _success, _fail);
235
+ promises.push(refs[f.name].$promise);
236
+ });
237
+
238
+ console.log('promises: ' + promises.length
239
+ + ' ')
240
+ console.log(promises)
241
+ $q
242
+ .all(promises)
243
+ .then(
244
+ function() {
245
+
246
+ for ( var k in refs) {
247
+ var field = that
248
+ .getField(k);
249
+ var pk = that
250
+ .getPk(that
251
+ .getMetadata(field.resource))
252
+ console
253
+ .log('PK field for '
254
+ + k
255
+ + ' is '
256
+ + pk)
257
+ var comboData = []
258
+ refs[k]
259
+ .forEach(function(
260
+ row) {
261
+ console
262
+ .log('field.resource !== _current.resource: '
263
+ + field.resource
264
+ + ' '
265
+ + _current.resource)
266
+ comboData
267
+ .push({
268
+ id : row[pk],
269
+ label : row.label
270
+ || row.name
271
+ || row.code
272
+ || row.first_name
273
+ + ' '
274
+ + (row.last_name || '')
275
+ });
276
+ })
277
+ refs[k] = comboData;
278
+ console
279
+ .log('Ready for combo for '
280
+ + k)
281
+ console
282
+ .log(comboData);
283
+ }
284
+ _current.fields
285
+ .forEach(function(
286
+ f) {
287
+ if (f.values)
288
+ refs[f.name] = f.values;
289
+ });
290
+ })
291
+
292
+ console.log(refs);
293
+ return refs;
294
+ }
295
+
296
+ } ])
212297
213298 })();