Roberto Sánchez
2014-01-22 79121484b7e6f721f5435a102018152a164ed655
securis/src/main/resources/static/js/catalogs.js
....@@ -2,8 +2,8 @@
22 'use strict';
33
44 /*
5
- * Catalogs module
6
- */
5
+ * Catalogs module
6
+ */
77
88 angular
99 .module('catalogs', [ 'ngResource' ])
....@@ -44,6 +44,12 @@
4444 })
4545 }
4646 this.init = function() {
47
+ if (_metadata) {
48
+ console.debug('Catalogs already initilizated');
49
+ var defer = $q.defer();
50
+ defer.resolve(_metadata);
51
+ return defer.promise;
52
+ }
4753 return _list();
4854 }
4955 this.getList = function() {
....@@ -59,7 +65,7 @@
5965 if (res === undefined)
6066 return _current ? resources[_current.resource]
6167 : null;
62
- return _current ? resources[res] : null;
68
+ return resources[res];
6369 }
6470 this.getPk = function(catalogMetadata) {
6571 if (!catalogMetadata)
....@@ -72,14 +78,14 @@
7278 return null;
7379 }
7480 /**
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
- */
81
+ * Returns catalog metadata
82
+ *
83
+ * @param index:
84
+ * Return current catalog if
85
+ * undefined, if string It find the
86
+ * catalog by resoource name if
87
+ * number it find it by position
88
+ */
8389 this.getMetadata = function(index) {
8490 if (!_metadata)
8591 throw new Error(
....@@ -105,14 +111,14 @@
105111 _current = _metadata[index];
106112 }
107113 /***********************************************
108
- * Catalog fields methods *
109
- **********************************************/
114
+ * Catalog fields methods *
115
+ **********************************************/
110116
111117 /**
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
- */
118
+ * Returns the first field in form that should
119
+ * get the focus. We find the first field that
120
+ * is not read only
121
+ */
116122 this.getFFF = this.getFirstFocusableField = function() {
117123 if (!_current)
118124 throw new Error(
....@@ -126,16 +132,17 @@
126132 }
127133
128134 /**
129
- * Find the field by name or position
130
- */
131
- this.getField = function(key) {
132
- if (!_current)
135
+ * Find the field by name or position
136
+ */
137
+ this.getField = function(key, catalog) {
138
+ catalog = catalog || _current;
139
+ if (!catalog)
133140 throw new Error(
134141 'There is no current catalog selected');
135142 var index = -1;
136143 if (typeof key === 'string') {
137
- for (var i = _current.fields.length - 1; i >= 0
138
- && _current.fields[i].name !== key; i--)
144
+ for (var i = catalog.fields.length - 1; i >= 0
145
+ && catalog.fields[i].name !== key; i--)
139146 ;
140147 index = i;
141148 } else {
....@@ -144,12 +151,12 @@
144151 }
145152
146153 return index === -1 ? {}
147
- : _current.fields[index];
154
+ : catalog.fields[index];
148155 }
149156
150157 /***********************************************
151
- * Catalog resource operations on server *
152
- **********************************************/
158
+ * Catalog resource operations on server *
159
+ **********************************************/
153160
154161 function _success(response) {
155162 console.log('$resource')
....@@ -214,16 +221,18 @@
214221 refs[field.name] = comboData;
215222 })
216223 }
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
- });
224
+ this.loadRefs = function(refs, refsFields) {
225
+ if (!refsFields || refsFields.length === 0) {
226
+ if (!_current)
227
+ throw new Error(
228
+ 'There is no current catalog selected');
229
+ refsFields = [];
230
+ _current.fields.forEach(function(f) {
231
+ if (f.resource)
232
+ refsFields.push(f)
233
+
234
+ });
235
+ }
227236
228237 var that = this;
229238 var promises = []
....@@ -238,56 +247,38 @@
238247 console.log('promises: ' + promises.length
239248 + ' ')
240249 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
- })
250
+ $q.all(promises)
251
+ .then(function() {
252
+ for(var i in refsFields) {
253
+ //for ( var k in refs) {
254
+ var rf = refsFields[i];
255
+ var cat = that.getResource(rf.resource);
256
+ var pk = that.getPk(that.getMetadata(rf.resource))
257
+ console.log('PK field for '
258
+ + rf.name
259
+ + ' is '
260
+ + pk)
261
+ var comboData = []
262
+ refs[rf.name].forEach(function(row) {
263
+ comboData.push({
264
+ id : row[pk],
265
+ label : row.label
266
+ || row.name
267
+ || row.code
268
+ || row.first_name
269
+ + ' '
270
+ + (row.last_name || '')
271
+ });
272
+ })
273
+ refs[rf.name] = comboData;
274
+ console.log('Ready for combo for ' + rf.name)
275
+ console.log(comboData);
276
+ }
277
+ _current && _current.fields.forEach(function(f) {
278
+ if (f.values)
279
+ refs[f.name] = f.values;
280
+ });
281
+ })
291282
292283 console.log(refs);
293284 return refs;