From 79121484b7e6f721f5435a102018152a164ed655 Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Wed, 22 Jan 2014 18:55:29 +0000
Subject: [PATCH] #395 feature - Implemented pack section

---
 securis/src/main/resources/static/js/catalogs.js |  163 +++++++++++++++++++++++++----------------------------
 1 files changed, 77 insertions(+), 86 deletions(-)

diff --git a/securis/src/main/resources/static/js/catalogs.js b/securis/src/main/resources/static/js/catalogs.js
index 7dc6614..5f244ac 100644
--- a/securis/src/main/resources/static/js/catalogs.js
+++ b/securis/src/main/resources/static/js/catalogs.js
@@ -2,8 +2,8 @@
 	'use strict';
 
 	/*
-	 * Catalogs module
-	 */
+     * Catalogs module
+     */
 
 	angular
 			.module('catalogs', [ 'ngResource' ])
@@ -44,6 +44,12 @@
 											})
 								}
 								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() {
@@ -59,7 +65,7 @@
 									if (res === undefined)
 										return _current ? resources[_current.resource]
 												: null;
-									return _current ? resources[res] : null;
+									return resources[res];
 								}
 								this.getPk = function(catalogMetadata) {
 									if (!catalogMetadata)
@@ -72,14 +78,14 @@
 									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
-								 */
+                                 * 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(
@@ -105,14 +111,14 @@
 										_current = _metadata[index];
 								}
 								/***********************************************
-								 * Catalog fields methods *
-								 **********************************************/
+                                 * Catalog fields methods *
+                                 **********************************************/
 
 								/**
-								 * Returns the first field in form that should
-								 * get the focus. We find the first field that
-								 * is not read only
-								 */
+                                 * 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(
@@ -126,16 +132,17 @@
 								}
 
 								/**
-								 * Find the field by name or position
-								 */
-								this.getField = function(key) {
-									if (!_current)
+                                 * 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 = _current.fields.length - 1; i >= 0
-												&& _current.fields[i].name !== key; i--)
+										for (var i = catalog.fields.length - 1; i >= 0
+												&& catalog.fields[i].name !== key; i--)
 											;
 										index = i;
 									} else {
@@ -144,12 +151,12 @@
 									}
 
 									return index === -1 ? {}
-											: _current.fields[index];
+											: catalog.fields[index];
 								}
 
 								/***********************************************
-								 * Catalog resource operations on server *
-								 **********************************************/
+                                 * Catalog resource operations on server *
+                                 **********************************************/
 
 								function _success(response) {
 									console.log('$resource')
@@ -214,16 +221,18 @@
 										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)
-
-									});
+								this.loadRefs = function(refs, 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 = []
@@ -238,56 +247,38 @@
 									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;
-																});
-													})
+									$q.all(promises)
+									  .then(function() {
+											    for(var i in refsFields) {
+												//for ( var k in refs) {
+											        var rf = refsFields[i];
+													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],
+																			label : row.label
+																					|| row.name
+																					|| row.code
+																					|| row.first_name
+																					+ ' '
+																					+ (row.last_name || '')
+																		});
+															})
+													refs[rf.name] = comboData;
+													console.log('Ready for combo for ' + rf.name)
+													console.log(comboData);
+												}
+											    _current && _current.fields.forEach(function(f) {
+															if (f.values)
+																refs[f.name] = f.values;
+														});
+											})
 
 									console.log(refs);
 									return refs;

--
Gitblit v1.3.2