(function() { 'use strict'; /* * Catalogs module */ angular.module('catalogs', ['ngResource']) .service('Catalogs', ['$rootScope', '$http', '$resource', '$q', function ($rootScope, $http, $resource, $q) { var resources = { application : $resource('/application/:appId', { appId : '@id' }), user : $resource('/user/:userId', { userId : '@username' }), organization : $resource('/organization/:orgId', { orgId : '@id' }), licensetype : $resource('/licensetype/:licenseTypeId', { licenseTypeId : '@id' }) } var _metadata = null; var _current = null; var _list = function() { return $http.get('/js/catalogs.json').success(function(data) { _metadata = data; }) } this.init = function() { return _list(); } this.getList = function() { return _metadata; } this.getName = function(index) { if (index === undefined) return _current ? _current.name : ''; return _metadata ? _metadata[index].name : ''; } this.getResource = function(res) { if (res === undefined) return _current ? resources[_current.resource] : null; return _current ? resources[res] : null; } this.getPk = function(catalogMetadata) { if (!catalogMetadata) catalogMetadata = _current; for(var i = 0; i < catalogMetadata.fields.length; i++) if (catalogMetadata.fields[i].pk) return catalogMetadata.fields[i].name; return null; } /** * Returns catalog metadata * @param index: Return current catalog if undefined, if string It find the catalog by resoource name if number it find it by position */ this.getMetadata = function(index) { if (!_metadata) throw new Error('There is no catalog metadata info'); if (index === undefined) return _current; if (typeof index === 'string') { for (var i = _metadata.length - 1; i >= 0 && _metadata[i].resource !== index; i--); index = i; } return _metadata[index]; } this.setCurrent = function(index) { if (!_metadata) throw new Error('There is no catalog metadata info'); if (index === undefined) _current = null; else _current = _metadata[index]; } /******************************************** * Catalog fields methods * ********************************************/ /** * Returns the first field in form that should get the focus. We find the first field that is not read only */ this.getFFF = this.getFirstFocusableField = function() { if (!_current) throw new Error('There is no current catalog selected'); for(var i = 0; i < _current.fields.length; i++) if (!_current.fields[i].readOnly) return _current.fields[i].name; return null; } /** * Find the field by name or position */ this.getField = function(key) { if (!_current) throw new Error('There is no current catalog selected'); var index = -1; if (typeof key === 'string') { for (var i = _current.fields.length - 1; i >= 0 && _current.fields[i].name !== key; i--); index = i; } else { index = key; // In this case key === field position } return index === -1 ? {} : _current.fields[index]; } /******************************************** * Catalog resource operations on server * ********************************************/ function _success(response) { console.log('$resource') console.log(response) } function _fail(response) { console.error('Error trying to get data, HTTP error code: ' + response.status) } this.save = function(data) { if (!_current) throw new Error('There is no current catalog selected'); var resource = this.getResource(); return resource.save(data, _success, _fail); } this.remove = function(data) { return this.getResource().remove({}, data, _success, _fail) } this.query = function() { return this.getResource().query({}, _success, _fail); } this.refreshRef = function(refs, res, preloadedData) { // We check if there is some field for the resource passed as parameter var field = (function() { for (var i = _current.fields.length - 1; i >= 0; i--) { if (_current.fields[i].resource === res) return _current.fields[i]; } return null; })(); // If field for that resource is not found there is nothing to refresh if (!field) return; var resource = this.getResource(res); var data = preloadedData || resource.query({}, _success, _fail); var that = this; data.$promise.then(function(responseData) { var pk = that.getPk(that.getMetadata(field.resource)) var comboData = [] responseData.forEach(function(row) { comboData.push({ id: row[pk], label: row.label || row.name || row.code || row.first_name + ' ' + row.last_name }); }) refs[field.name] = comboData; }) } this.loadRefs = function(refs) { if (!_current) throw new Error('There is no current catalog selected'); var refsFields = []; _current.fields.forEach(function(f) { if (f.resource) refsFields.push(f) }); var that = this; var promises = [] refsFields.forEach(function(f) { var resource = that.getResource(f.resource); refs[f.name] = resource.query({}, _success, _fail); promises.push(refs[f.name].$promise); }); console.log('promises: ' + promises.length + ' ') console.log(promises) $q.all(promises).then(function() { for (var k in refs) { var field = that.getField(k); var pk = that.getPk(that.getMetadata(field.resource)) console.log('PK field for ' + k + ' is ' + pk) var comboData = [] refs[k].forEach(function(row) { console.log('field.resource !== _current.resource: ' + field.resource +' '+ _current.resource) comboData.push({ id: row[pk], label: row.label || row.name || row.code || row.first_name + ' ' + (row.last_name || '') }); }) refs[k] = comboData; console.log('Ready for combo for ' + k) console.log(comboData); } _current.fields.forEach(function(f) { if (f.values) refs[f.name] = f.values; }); }) console.log(refs); return refs; } }]) })();