Roberto Sánchez
2013-12-30 86a407cd3fcbefb16b00ea9ef10bd80f2b0e0a0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(function() {
   'use strict';
   var app = angular.module('app', [ 'ngRoute', 'ngAnimate' ]);
   app.directive('catalogField', function() {
       return {
         restrict: 'A', // only activate on element attribute
         require: '?ngModel', // get a hold of NgModelController
         link: function(scope, element, attrs, ngModel) {
           if(!ngModel) return; // do nothing if no ng-model
           // TODO: Replace the hard-coded form ID by the appropiate dynamic field
           scope.catalogForm[attrs.name] = scope.catalogForm['{{field.name}}'];
           scope.catalogForm[attrs.name].$name = attrs.name; 
         }   
       };
     });
   
   app.factory('Catalogs', function($http) {
       var CatalogsService = {}
       CatalogsService.list = function(initFn) {
           $http.get('/js/catalogs.json').success(function(data){
                   console.log(data);
                   CatalogsService.data = data;
                   initFn();
               })
           return CatalogsService;
       }
       CatalogsService.getName = function(index) {
           return CatalogsService.data ? CatalogsService.data[index].name : '';
       }
       CatalogsService.getMetadata = function(index) {
           return CatalogsService.data ? CatalogsService.data[index] : {};
       }
       return CatalogsService;
       
   });
   
   app.controller('CatalogsCtrl', [ '$scope', '$http', 'Catalogs' ,
           function($scope, $http, catalogs) {
               $scope.formu = {};
               $scope.catalogIndex = 0;
               $scope.catalogs = catalogs.list(function() {
                   $scope.catalogMetadata = catalogs.getMetadata($scope.catalogIndex);
               }); //['Applications','License types','Users','Organizations','System params'];
               $scope.catalogMetadata = {};
               $scope.selectCatalog = function(index, $event) {
                   $scope.catalogIndex = index;
                   $scope.catalogMetadata = catalogs.getMetadata($scope.catalogIndex);
                   console.log($event);
               }
           } ]);
   app.controller('CatalogFormCtrl', [ '$scope', '$http', 'Catalogs',
                                       
           function($scope, $http, menuController, Catalogs) {
               $scope.showForm = false;
               $scope.scope = $scope;
               console.log('Form: currentCatalog:'+ $scope.cataLogIndex);
               $scope.editNew = function() {
                   $scope.showForm = true;
                   $scope.isNew = true;
                   //$scope.formu = {};
               }
               $scope.edit = function() {
                   $scope.showForm = true;
                   $scope.isNew = false;
                   // TODO: Load in formu values for Form
                   //$scope.formu = {};
               }
               $scope.cancel = function() {
                   $scope.showForm = false;
               }
               
               $scope.saveCatalog = function() {
                   if ($scope.catalogForm.$invalid) {
                       alert(JSON.stringify($scope.catalogForm))
                   }
               }
           } ]);
   app.controller('CatalogListCtrl', [ '$scope', '$http',
           function($scope, $http) {
               console.log('List: currentCatalog: '+ $scope.currentCatalog);
           } ]);
})();