Roberto Sánchez
2014-01-10 59cdd2b7ebceae94fbecdb1eeb46a969666dc88f
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(function() {
   'use strict';
   var app = angular.module('app', [ 'ngRoute', 'ngAnimate', 'ngResource', 'toaster', 'catalogs' ]);
   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.controller('CatalogsCtrl', [
           '$scope',
           '$http',
           'Catalogs',
           function($scope, $http, Catalogs) {
               $scope.showForm = false;
               $scope.isNew = false;
               $scope.formu = {};
               $scope.catalogIndex = 0;
               $scope.catalogMetadata = {};
               $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;
                   $scope.formu = {}
                   for (var k in data) {
                       if (k.indexOf('$') !== 0) $scope.formu[k] = data[k]
                   }
                   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 promise = Catalogs.remove(data).$promise;
                           promise.then(function(data) {
                               $scope.list = Catalogs.query();
                           });
                       }
                   });
                   $scope.showForm = false;
                   $scope.isNew = false;
                   // TODO: Load in formu values for Form
                   // $scope.formu = {};
               }
           } ]);
   app.controller('CatalogFormCtrl', [ '$scope', '$http', 'toaster', 'Catalogs',
           function($scope, $http, toaster, Catalogs) {
               $scope.scope = $scope;
               console.log('Form: currentCatalog:' + $scope.cataLogIndex);
               
               $scope.inputType = function(field) {
                   
                   if (field.readOnly && field.type === 'date')
                       return 'readonly_date';
                   if (field.readOnly)
                       return 'readonly';
                   if (field.type === 'select')
                       return 'select';
                   if (!field.multiline)
                       return 'normal';
                   if (field.multiline)
                       return 'textarea';
               }
               
               $scope.editNew = function() {
                   $('#'+ Catalogs.getFFF()).focus();
                   $scope.$parent.showForm = true;
                   $scope.$parent.isNew = true;
                   $scope.$parent.formu = {};
               }
               $scope.cancel = function() {
                   $scope.$parent.showForm = false;
               }
               $scope.saveCatalog = function() {
                   if ($scope.catalogForm.$invalid) {
                       alert(JSON.stringify($scope.catalogForm))
                   } else {
                       var promise = Catalogs.save($scope.formu).$promise;
                       promise.then(function(data, otro) {
                           if ($scope.isNew) {
                               $scope.$parent.formu = {}
                               $('#'+ Catalogs.getFFF()).focus();
                           } else {
                               $scope.cancel();
                           }
                           $scope.$parent.list = Catalogs.query();
                       }, function(error) {
                           console.log(error);
                       });
                       
                   }
               }
           } ]);
   app.controller('CatalogListCtrl', [ '$scope', '$http', '$filter', 'Catalogs', 
           function($scope, $http, $filter, Catalogs) {
               $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) {
                   return Catalogs.getField(name).display;
               }
           } ]);
})();