LP#1676608: copy alert and suppression matrix
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / admin / local / app.js
1 angular.module('egLocalAdmin',
2     ['ngRoute', 'ui.bootstrap', 'egCoreMod','egUiMod','egGridMod'])
3
4 .config(['$routeProvider','$locationProvider','$compileProvider', 
5  function($routeProvider , $locationProvider , $compileProvider) {
6
7     $locationProvider.html5Mode(true);
8     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); 
9         
10     var resolver = {delay : function(egStartup) {return egStartup.go()}};
11
12     var eframe_template = 
13         '<eg-embed-frame allow-escape="true" min-height="min_height" url="local_admin_url" handlers="funcs"></eg-embed-frame>';
14
15     // non-conify routes come first
16     $routeProvider.when('/admin/local/money/cash_reports', {
17         template: eframe_template,
18         controller: 'EmbedXHTMLCtl', // non-conify
19         resolve : resolver
20     });
21    
22     // non-conify routes come first
23     $routeProvider.when('/admin/local/actor/closed_dates', {
24         template: eframe_template,
25         controller: 'EmbedXHTMLCtl', // non-conify
26         resolve : resolver
27     });
28     
29     // non-conify routes come first
30     $routeProvider.when('/admin/local/asset/copy_locations', {
31         template: eframe_template,
32         controller: 'EmbedXHTMLCtl', // non-conify
33         resolve : resolver
34     });
35
36     // non-conify routes come first
37     $routeProvider.when('/admin/local/asset/org_unit_settings', {
38         template: eframe_template,
39         controller: 'EmbedXHTMLCtl', // non-conify
40         resolve : resolver
41     });
42
43     $routeProvider.when('/admin/local/config/non_cat_types', {
44         template: eframe_template,
45         controller: 'EmbedXHTMLCtl', // non-conify
46         resolve : resolver
47     });
48
49     $routeProvider.when('/admin/local/asset/stat_cat_editor', {
50         template: eframe_template,
51         controller: 'EmbedXHTMLCtl', // non-conify
52         resolve : resolver
53     });
54
55     $routeProvider.when('/admin/local/config/copy_alert_types', {
56         templateUrl: './admin/local/t_grid_editor',
57         controller: 'AutoGridEditorCtl',
58         fmBase: 'ccat',
59         createEditPrefetch: {
60             ccs : { id : {'!=' : null} }
61         },
62         createDefaults : { 'in_renew' : 'f', 'next_status' : [] },
63         createEditOrgExpand: ['scope_org'],
64         createEditIntarray: ['next_status'],
65         createEditNullableBool : ['in_renew', 'at_circ', 'at_owning']
66     });
67
68     $routeProvider.when('/admin/local/actor/copy_alert_suppress', {
69         templateUrl: './admin/local/t_grid_editor',
70         controller: 'AutoGridEditorCtl',
71         fmBase: 'acas',
72         createEditPrefetch: {
73             ccat : { active: 't' }
74         },
75         createEditOrgExpand: ['org']
76     });
77
78     // Conify page handler
79     $routeProvider.when('/admin/local/:schema/:page', {
80         template: eframe_template,
81         controller: 'EmbedConifyCtl',
82         resolve : resolver
83     });
84
85     // default page 
86     $routeProvider.otherwise({
87         templateUrl : './admin/local/t_splash',
88         resolve : resolver
89     });
90 }])
91
92 .controller('EmbedConifyCtl', 
93        ['$scope','$routeParams','$location','egCore',
94 function($scope , $routeParams , $location , egCore) {
95
96     $scope.funcs = {
97         ses : egCore.auth.token(),
98     }
99
100     var conify_path = '/eg/conify/global/' + 
101         $routeParams.schema + '/' + $routeParams.page;
102
103     $scope.min_height = 800;
104
105     // embed URL must include protocol/domain or it will be loaded via
106     // push-state, resulting in an infinitely nested pages.
107     $scope.local_admin_url = 
108         $location.absUrl().replace(/\/eg\/staff.*/, conify_path);
109
110     console.log('Loading local admin URL: ' + $scope.local_admin_url);
111
112 }])
113
114 .controller('EmbedXHTMLCtl', 
115        ['$scope','$location','egCore','$timeout',
116 function($scope , $location , egCore , $timeout) {
117
118     $scope.funcs = {};
119
120     var xul_base = '/xul/server/admin/';
121     var page_parts = $location.path().split(/\//);
122     var url = xul_base + page_parts[page_parts.length - 1] + '.xhtml';
123
124     // old-school XUL admin UI's only want CGI ses values.
125     url += '?ses=' + egCore.auth.token();
126     
127     console.log('Loading local admin URL: ' + $scope.local_admin_url);
128
129     $scope.local_admin_url = $location.absUrl().replace(/\/.*/, url);
130 }])
131
132 .controller('AutoGridEditorCtl',
133        ['$scope','$route','$location','egCore','$timeout','egConfirmDialog','$uibModal',
134 function($scope , $route , $location , egCore , $timeout , egConfirmDialog , $uibModal) {
135
136     $scope.funcs = {};
137
138     $scope.baseFmClass = $route.current.$$route.fmBase;
139     $scope.createEditPrefetch = $route.current.$$route.createEditPrefetch || {};
140     $scope.createEditOrgExpand = $route.current.$$route.createEditOrgExpand || [];
141     $scope.createEditNullableBool = $route.current.$$route.createEditNullableBool || [];
142     $scope.createEditIntarray = $route.current.$$route.createEditIntarray || [];
143     $scope.createDefaults = $route.current.$$route.createDefaults || {};
144     $scope.gridControls = {
145         setQuery : function(q) {
146             if (q) query = q;
147             return query;
148         },
149         activateItem : function (item) {
150             $scope.editHandler([item])
151         }
152     };
153     $scope.gridControls.setQuery({id : {'!=' : null}});
154
155     function openCreateEditDialog(id) {
156         return $uibModal.open({
157             templateUrl : './admin/local/autoGridEditor/' + $scope.baseFmClass,
158             scope : $scope,
159             controller :
160                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
161                 $scope.creating = id ? false : true;
162                 angular.forEach($scope.$parent.createEditPrefetch, function(where, fmClass) {
163                     egCore.pcrud.search(
164                         fmClass, where, {},
165                         {atomic : true, authoritative : true}
166                     ).then(function(vals) {
167                         $scope[fmClass] = vals;
168                     });
169                 });
170                 if ($scope.creating) {
171                     $scope.record = $scope.createDefaults;
172                 } else {
173                     egCore.pcrud.retrieve($scope.baseFmClass, id).then(function(to_edit) {
174                         $scope.record = egCore.idl.toHash(to_edit);
175                         angular.forEach($scope.createEditOrgExpand, function(ou_field) {
176                             $scope.record[ou_field] = egCore.org.get($scope.record[ou_field]);
177                         });
178                         angular.forEach($scope.createEditIntarray, function(intarray_field) {
179                             if (!($scope.record[intarray_field] == null) && $scope.record[intarray_field] != "") {
180                                 $scope.record[intarray_field] = $scope.record[intarray_field]
181                                                     .replace('{', '')
182                                                     .replace('}', '')
183                                                     .split(',');
184                             } else {
185                                 $scope.record[intarray_field] = [];
186                             }
187                         });
188                     });
189                 }
190                 $scope.ok = function(record) { $uibModalInstance.close(record) };
191                 $scope.cancel = function () { $uibModalInstance.dismiss() }
192             }]
193         });
194     }
195
196     $scope.createHandler = function() {
197         openCreateEditDialog().result.then(function(record) {
198             var newRec = new egCore.idl[$scope.baseFmClass]();
199             angular.forEach(record, function(val, key) {
200                 if (typeof(val) === 'object' && !angular.isArray(val)) {
201                     newRec[key](val.id());
202                 } else {
203                     newRec[key](val);
204                 }
205             });
206             angular.forEach($scope.createEditNullableBool, function(nb_field) {
207                 if (!(record[nb_field] == null) && record[nb_field] == "")
208                     newRec[nb_field](null);
209             });
210             angular.forEach($scope.createEditIntarray, function(intarray_field) {
211                 if (newRec[intarray_field]().length > 0) {
212                     newRec[intarray_field]('{' + newRec[intarray_field]().join(',') + '}');
213                 } else {
214                     newRec[intarray_field](null);
215                 }
216             });
217             return egCore.pcrud.create(newRec);
218         }).then(function(){
219             $scope.gridControls.refresh();
220         });
221     };
222     $scope.editHandler = function(items) {
223         openCreateEditDialog(items[0].id).result.then(function(record) {
224             var editedRec = new egCore.idl[$scope.baseFmClass]();
225             angular.forEach(record, function(val, key) {
226                 if (angular.isObject(val) && !angular.isArray(val)) {
227                     editedRec[key](val.id());
228                 } else {
229                     editedRec[key](val);
230                 }
231             });
232             angular.forEach($scope.createEditNullableBool, function(nb_field) {
233                 if (!(record[nb_field] == null) && record[nb_field] == "")
234                     editedRec[nb_field](null);
235             });
236             angular.forEach($scope.createEditIntarray, function(intarray_field) {
237                 if (editedRec[intarray_field]().length > 0) {
238                     editedRec[intarray_field]('{' + editedRec[intarray_field]().join(',') + '}');
239                 } else {
240                     editedRec[intarray_field](null);
241                 }
242             });
243             return egCore.pcrud.update(editedRec);
244         }).then(function(){
245             $scope.gridControls.refresh();
246         });
247     };
248     $scope.deleteHandler = function(items) {
249         egConfirmDialog.open(
250             egCore.strings.REMOVE_ITEM_CONFIRM,
251             '',
252             {}
253         ).result.then(function() {
254             var ids = items.map(function(s){ return s.id });
255             egCore.pcrud.search(
256                 $scope.baseFmClass, {id : ids}, {},
257                 {atomic : true, authoritative : true}
258             ).then(function(to_delete) {
259                 return egCore.pcrud.remove(to_delete);
260             }).then(function() {
261                 $scope.gridControls.refresh();
262             });
263         });
264     };
265 }])
266