| .. | .. |
|---|
| 2 | 2 | 'use strict'; |
|---|
| 3 | 3 | |
|---|
| 4 | 4 | /* |
|---|
| 5 | | - * Catalogs module |
|---|
| 6 | | - */ |
|---|
| 5 | + * Catalogs module |
|---|
| 6 | + */ |
|---|
| 7 | 7 | |
|---|
| 8 | 8 | angular |
|---|
| 9 | 9 | .module('catalogs', [ 'ngResource' ]) |
|---|
| .. | .. |
|---|
| 44 | 44 | }) |
|---|
| 45 | 45 | } |
|---|
| 46 | 46 | 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 | + } |
|---|
| 47 | 53 | return _list(); |
|---|
| 48 | 54 | } |
|---|
| 49 | 55 | this.getList = function() { |
|---|
| .. | .. |
|---|
| 59 | 65 | if (res === undefined) |
|---|
| 60 | 66 | return _current ? resources[_current.resource] |
|---|
| 61 | 67 | : null; |
|---|
| 62 | | - return _current ? resources[res] : null; |
|---|
| 68 | + return resources[res]; |
|---|
| 63 | 69 | } |
|---|
| 64 | 70 | this.getPk = function(catalogMetadata) { |
|---|
| 65 | 71 | if (!catalogMetadata) |
|---|
| .. | .. |
|---|
| 72 | 78 | return null; |
|---|
| 73 | 79 | } |
|---|
| 74 | 80 | /** |
|---|
| 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 | + */ |
|---|
| 83 | 89 | this.getMetadata = function(index) { |
|---|
| 84 | 90 | if (!_metadata) |
|---|
| 85 | 91 | throw new Error( |
|---|
| .. | .. |
|---|
| 105 | 111 | _current = _metadata[index]; |
|---|
| 106 | 112 | } |
|---|
| 107 | 113 | /*********************************************** |
|---|
| 108 | | - * Catalog fields methods * |
|---|
| 109 | | - **********************************************/ |
|---|
| 114 | + * Catalog fields methods * |
|---|
| 115 | + **********************************************/ |
|---|
| 110 | 116 | |
|---|
| 111 | 117 | /** |
|---|
| 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 | + */ |
|---|
| 116 | 122 | this.getFFF = this.getFirstFocusableField = function() { |
|---|
| 117 | 123 | if (!_current) |
|---|
| 118 | 124 | throw new Error( |
|---|
| .. | .. |
|---|
| 126 | 132 | } |
|---|
| 127 | 133 | |
|---|
| 128 | 134 | /** |
|---|
| 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) |
|---|
| 133 | 140 | throw new Error( |
|---|
| 134 | 141 | 'There is no current catalog selected'); |
|---|
| 135 | 142 | var index = -1; |
|---|
| 136 | 143 | 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--) |
|---|
| 139 | 146 | ; |
|---|
| 140 | 147 | index = i; |
|---|
| 141 | 148 | } else { |
|---|
| .. | .. |
|---|
| 144 | 151 | } |
|---|
| 145 | 152 | |
|---|
| 146 | 153 | return index === -1 ? {} |
|---|
| 147 | | - : _current.fields[index]; |
|---|
| 154 | + : catalog.fields[index]; |
|---|
| 148 | 155 | } |
|---|
| 149 | 156 | |
|---|
| 150 | 157 | /*********************************************** |
|---|
| 151 | | - * Catalog resource operations on server * |
|---|
| 152 | | - **********************************************/ |
|---|
| 158 | + * Catalog resource operations on server * |
|---|
| 159 | + **********************************************/ |
|---|
| 153 | 160 | |
|---|
| 154 | 161 | function _success(response) { |
|---|
| 155 | 162 | console.log('$resource') |
|---|
| .. | .. |
|---|
| 214 | 221 | refs[field.name] = comboData; |
|---|
| 215 | 222 | }) |
|---|
| 216 | 223 | } |
|---|
| 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 | + } |
|---|
| 227 | 236 | |
|---|
| 228 | 237 | var that = this; |
|---|
| 229 | 238 | var promises = [] |
|---|
| .. | .. |
|---|
| 238 | 247 | console.log('promises: ' + promises.length |
|---|
| 239 | 248 | + ' ') |
|---|
| 240 | 249 | 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 | + }) |
|---|
| 291 | 282 | |
|---|
| 292 | 283 | console.log(refs); |
|---|
| 293 | 284 | return refs; |
|---|