From 59cdd2b7ebceae94fbecdb1eeb46a969666dc88f Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Fri, 10 Jan 2014 12:25:27 +0000
Subject: [PATCH] #394 feature - Added all catalog resources with refereced fields

---
 securis/src/main/resources/static/js/admin.js |  172 ++++++++++++---------------------------------------------
 1 files changed, 37 insertions(+), 135 deletions(-)

diff --git a/securis/src/main/resources/static/js/admin.js b/securis/src/main/resources/static/js/admin.js
index 6b4ecec..39ba6b4 100644
--- a/securis/src/main/resources/static/js/admin.js
+++ b/securis/src/main/resources/static/js/admin.js
@@ -1,7 +1,7 @@
 (function() {
 	'use strict';
 
-	var app = angular.module('app', [ 'ngRoute', 'ngAnimate', 'ngResource', 'toaster' ]);
+	var app = angular.module('app', [ 'ngRoute', 'ngAnimate', 'ngResource', 'toaster', 'catalogs' ]);
 
 	app.directive(
 					'catalogField',
@@ -22,124 +22,34 @@
 						};
 					});
 
-	app.factory('Catalogs', function($http, $resource, toaster) {
-		var CatalogsService = {
-			resources : {
-				application : $resource('/application/:appId', {
-					appId : '@id'
-				}, {
-					update : {
-						method : "PUT"
-					}
-				}),
-				user : $resource('/user/:userId', {
-					userId : '@id'
-				}, {
-					update : {
-						method : "PUT"
-					}
-				}),
-				licensetype : $resource('/licensetype/:licenseTypeId', {
-					licenseTypeId : '@id'
-				}, {
-					update : {
-						method : "PUT"
-					}
-				})
-
-			},
-			list : function(initFn) {
-				$http.get('/js/catalogs.json').success(function(data) {
-					console.log(data);
-					CatalogsService.data = data;
-					initFn();
-				})
-				return CatalogsService;
-			},
-			getName : function(index) {
-				return CatalogsService.data ? CatalogsService.data[index].name
-						: '';
-			},
-			getResource : function(index) {
-				return CatalogsService.data ? CatalogsService.data[index].resource
-						: '';
-			},
-			getMetadata : function(index) {
-				return CatalogsService.data ? CatalogsService.data[index] : {};
-			},
-			save: function(catalog, data) {
-				var resource = CatalogsService.resources[catalog.toLowerCase()];
-				function success(data) {
-					console.log('success')
-					console.log(data)
-					toaster.pop('success', "Data saved sucessfully in " + catalog);
-				}
-				function fail(data, status) {
-					var errorMsg = {500: 'Server error', 404: 'Item to modify was not found'}[data.status]
-					toaster.pop('error', "Error saving data in " + catalog, errorMsg);
-					console.log('error')
-					console.error(data)
-					console.error(status)
-				}
-				if (data.id && data.id !== '')
-					return resource.update(data, success, fail)
-				else
-					return resource.save(data, success, fail)
-			},
-			remove: function(catalog, data) {
-				var resource = CatalogsService.resources[catalog.toLowerCase()];
-				function success(data) {
-					console.log('success')
-					console.log(data)
-				}
-				function fail(data, status) {
-					console.log('error')
-					console.error(data)
-					console.error(status)
-				}
-				return resource.remove({}, data, success, fail)
-			},
-			query: function(catalog) {
-				console.log('HI catalog ???? ' + catalog);
-				var resource = CatalogsService.resources[catalog];
-				function success(data) {
-					console.log('success')
-					console.log(data)
-				}
-				function fail(data, status) {
-					console.log('error')
-					console.error(data)
-					console.error(status)
-				}
-				return resource.query({}, success, fail);
-			}
-		}
-
-		return CatalogsService;
-
-	});
-
 	app.controller('CatalogsCtrl', [
 			'$scope',
 			'$http',
 			'Catalogs',
 			function($scope, $http, Catalogs) {
-				$scope.showForm = true;
+				$scope.showForm = false;
 				$scope.isNew = false;
 				$scope.formu = {};
 				$scope.catalogIndex = 0;
-				$scope.catalogs = Catalogs.list(function() {
-					$scope.catalogMetadata = Catalogs.getMetadata($scope.catalogIndex);
-					$scope.list = Catalogs.query(Catalogs.getResource($scope.catalogIndex));
-				}); 
-
 				$scope.catalogMetadata = {};
-				$scope.selectCatalog = function(index, $event) {
-					$scope.catalogIndex = index;
-					$scope.catalogMetadata = Catalogs.getMetadata($scope.catalogIndex);
-					$scope.list = Catalogs.query(Catalogs.getResource($scope.catalogIndex));
-					console.log($event);
+				$scope.catalogsList = null;
+				$scope.list = null;
+				
+				var _changeCatalog = function(index) {
+					if (!$scope.catalogsList) $scope.catalogsList = Catalogs.getList(); // catalog list is also in index.data
+					if (typeof index === 'number') $scope.catalogIndex = index;
+					Catalogs.setCurrent($scope.catalogIndex);
+					$scope.catalogMetadata = Catalogs.getMetadata();
+					$scope.list = Catalogs.query();
+					$scope.refs = {}
+					Catalogs.loadRefs($scope.refs)
+					console.log($scope.refs)
 				}
+				
+				Catalogs.init().then(_changeCatalog); 
+
+				$scope.selectCatalog = _changeCatalog;
+				
 				$scope.edit = function(data) {
 					$scope.showForm = true;
 					$scope.isNew = false;
@@ -149,15 +59,16 @@
 					}
 					console.log('$scope.edit')
 					console.log($scope.formu)
+					$('#'+ Catalogs.getFFF()).focus();
+
 					
 				}
 				$scope.delete = function(data) {
 					BootstrapDialog.confirm('The record will be deleted, are you sure?', function(result){
 			            if(result) {
-							var catalogName = Catalogs.getResource($scope.catalogIndex);
-							var promise = Catalogs.remove(catalogName, data).$promise;
+							var promise = Catalogs.remove(data).$promise;
 							promise.then(function(data) {
-								$scope.list = Catalogs.query(catalogName);
+								$scope.list = Catalogs.query();
 							});
 			            }
 			        });
@@ -190,7 +101,7 @@
 				}
 				
 				$scope.editNew = function() {
-					$('#name').focus();
+					$('#'+ Catalogs.getFFF()).focus();
 					$scope.$parent.showForm = true;
 					$scope.$parent.isNew = true;
 					$scope.$parent.formu = {};
@@ -203,20 +114,17 @@
 					if ($scope.catalogForm.$invalid) {
 						alert(JSON.stringify($scope.catalogForm))
 					} else {
-						var catalogName = Catalogs.getResource($scope.catalogIndex);
-						var promise = Catalogs.save(catalogName, $scope.formu).$promise;
+						var promise = Catalogs.save($scope.formu).$promise;
 						promise.then(function(data, otro) {
 							if ($scope.isNew) {
 								$scope.$parent.formu = {}
-								$('#name').focus();
+								$('#'+ Catalogs.getFFF()).focus();
 							} else {
 								$scope.cancel();
 							}
-							$scope.$parent.list = Catalogs.query(catalogName);
-						}, function(error, otro) {
-							console.log('then error');
+							$scope.$parent.list = Catalogs.query();
+						}, function(error) {
 							console.log(error);
-							console.log(otro);							
 						});
 						
 					}
@@ -225,24 +133,18 @@
 
 	app.controller('CatalogListCtrl', [ '$scope', '$http', '$filter', 'Catalogs', 
 			function($scope, $http, $filter, Catalogs) {
-				console.log('List: currentCatalog: ' + $scope.currentCatalog);
-				var _indexOfField = function(name) {
-					if (!$scope.catalogMetadata) return -1;
-					for (var i = $scope.catalogMetadata.fields.length - 1; i >= 0 && $scope.catalogMetadata.fields[i].name !== name; i--);
-					return i;
-				}
-				
-				$scope.print = function(name, value) {
-					var index = _indexOfField(name);
-					if (index === -1) return value;
-					var type = $scope.catalogMetadata.fields[index].type;
-					
-					return type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value;
+
+				$scope.print = function(name, row) {
+					var value = row[name];
+					var type = Catalogs.getField(name).type;
+					var printedValue = type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value;
+					if (printedValue !== value) // this line is a work around to allow search in formatted fields
+						row['_display_'+name] = printedValue;
+					return printedValue;
 				}
 				
 				$scope.display = function(name) {
-					var index = _indexOfField(name);
-					return index === -1 ? '' : $scope.catalogMetadata.fields[index].display;
+					return Catalogs.getField(name).display;
 				}
 
 			} ]);

--
Gitblit v1.3.2