(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/:id', { id : '@id' }), user : $resource('user/:id', { id : '@username' }), organization : $resource( 'organization/:id', { id : '@id' }), licensetype : $resource( 'licensetype/:id', { id : '@id' }) } var _metadata = null; var _current = null; var _list = function() { return $http.get('js/catalogs.json') .success(function(data) { _metadata = data; }) } this.init = function() { if (_metadata) { console.debug('Catalogs already initilizated'); var defer = $q.defer(); defer.resolve(_metadata); return defer.promise; } 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 resources[res]; } 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, catalog) { catalog = catalog || _current; if (!catalog) throw new Error('There is no current catalog selected'); var index = -1; if (typeof key === 'string') { for (var i = catalog.fields.length - 1; i >= 0 && catalog.fields[i].name !== key; i--); index = i; } else { index = key; // In this case key === field position } return index === -1 ? {} : catalog.fields[index]; } /*********************************************** * Catalog resource operations on server * **********************************************/ function _success(response) { console.debug('$resource action success') console.log(_current); } 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.get = function(id, _onsuccess, _onfail) { if (!_current) { throw new Error('There is no current catalog selected'); } var resource = this.getResource(); return resource.get({id: id}, _onsuccess, _onfail); } this.remove = function(data) { return this.getResource().remove({}, data, _success, _fail) } this.query = function() { var list = this.getResource().query(); list.$promise.then(_success, _fail); return list; } 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(callback, refsFields) { if (!refsFields || refsFields.length === 0) { if (!_current) { throw new Error('There is no current catalog selected'); } refsFields = []; _current.fields.forEach(function(f) { if (f.resource) { refsFields.push(f); } }); } var that = this; var promises = [] var refs = [] refsFields.forEach(function(f) { var resource = that .getResource(f.resource); refs[f.name] = resource.query({}, _success, _fail); promises.push(refs[f.name].$promise); }); $q.all(promises) .then(function() { refsFields.forEach(function(rf) { var cat = that.getResource(rf.resource); var pk = that.getPk(that.getMetadata(rf.resource)) //console.log('PK field for ' + rf.name + ' is ' + pk) var comboData = [] refs[rf.name].forEach(function(row) { comboData.push({ id : row[pk], code : row.code, label : row.label || row.name || row.code || row.first_name + ' ' + (row.last_name || '') }); }) refs[rf.name] = comboData; }); // Next lines are to load special catalogs with predefined values, just like user roles _current && _current.fields.forEach(function(f) { if (f.values) refs[f.name] = f.values; }); callback(refs); }) } } ]) })();