]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/admin/workstation/app.js
LP#1402797 Do not allow workstations as org units that cannot have user
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / admin / workstation / app.js
1 /**
2  * App to drive the base page. 
3  * Login Form
4  * Splash Page
5  */
6
7 angular.module('egWorkstationAdmin', 
8     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod'])
9
10 .config(['$routeProvider','$locationProvider','$compileProvider', 
11  function($routeProvider , $locationProvider , $compileProvider) {
12
13     $locationProvider.html5Mode(true);
14     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); 
15     var resolver = {delay : function(egStartup) {return egStartup.go()}};
16
17     $routeProvider.when('/admin/workstation/print/config', {
18         templateUrl: './admin/workstation/t_print_config',
19         controller: 'PrintConfigCtrl',
20         resolve : resolver
21     });
22
23     $routeProvider.when('/admin/workstation/print/templates', {
24         templateUrl: './admin/workstation/t_print_templates',
25         controller: 'PrintTemplatesCtrl',
26         resolve : resolver
27     });
28
29     $routeProvider.when('/admin/workstation/stored_prefs', {
30         templateUrl: './admin/workstation/t_stored_prefs',
31         controller: 'StoredPrefsCtrl',
32         resolve : resolver
33     });
34
35
36     // default page 
37     $routeProvider.otherwise({
38         templateUrl : './admin/workstation/t_splash',
39         controller : 'SplashCtrl',
40         resolve : resolver
41     });
42 }])
43
44 .controller('SplashCtrl',
45        ['$scope','$window','$location','egCore','egConfirmDialog',
46 function($scope , $window , $location , egCore , egConfirmDialog) {
47
48     var allWorkstations = [];
49     var permMap = {};
50     $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
51
52     egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
53     .then(function(orgList) { 
54
55         // hide orgs in the context org selector where this login
56         // does not have the reg_ws perm
57         $scope.wsOrgHidden = function(id) {
58             return orgList.indexOf(id) == -1;
59         }
60         $scope.userHasRegPerm = 
61             orgList.indexOf($scope.contextOrg.id()) > -1;
62     });
63
64     // fetch the stored WS info
65     egCore.hatch.getItem('eg.workstation.all')
66     .then(function(all) {
67         allWorkstations = all || [];
68         $scope.workstations = 
69             allWorkstations.map(function(w) { return w.name });
70         return egCore.hatch.getItem('eg.workstation.default');
71     })
72     .then(function(def) { 
73         $scope.defaultWS = def;
74         $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
75     });
76
77     $scope.getWSLabel = function(ws) {
78         return ws == $scope.defaultWS ? 
79             egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
80     }
81
82     $scope.setDefaultWS = function() {
83         egCore.hatch.setItem(
84             'eg.workstation.default', $scope.selectedWS)
85         .then(function() { $scope.defaultWS = $scope.selectedWS });
86     }
87
88     $scope.cant_have_users = function (id) { return !egCore.org.CanHaveUsers(id); };
89
90     // redirect the user to the login page using the current
91     // workstation as the workstation URL param
92     $scope.useWS = function() {
93         $window.location.href = $location
94             .path('/login')
95             .search({ws : $scope.selectedWS})
96             .absUrl();
97     }
98
99     $scope.registerWS = function() {
100         register_workstation(
101             $scope.newWSName,
102             $scope.contextOrg.shortname() + '-' + $scope.newWSName,
103             $scope.contextOrg.id()
104         );
105     }
106
107     function register_workstation(base_name, name, org_id, override) {
108
109         var method = 'open-ils.actor.workstation.register';
110         if (override) method += '.override';
111
112         egCore.net.request(
113             'open-ils.actor', method, egCore.auth.token(), name, org_id)
114
115         .then(function(resp) {
116             if (evt = egCore.evt.parse(resp)) {
117                 console.log('register returned ' + evt.toString());
118
119                 if (evt.textcode == 'WORKSTATION_NAME_EXISTS' && !override) {
120                     egConfirmDialog.open(
121                         egCore.strings.WS_EXISTS, base_name, {  
122                             ok : function() {
123                                 register_workstation(base_name, name, org_id, true);
124                             },
125                             cancel : function() {} 
126                         }
127                     );
128
129                 } else {
130                     // TODO: provide permission error display
131                     alert(evt.toString());
132                 }
133             } else if (resp) {
134                 $scope.workstations.push(name);
135
136                 allWorkstations.push({   
137                     id : resp,
138                     name : name,
139                     owning_lib : org_id
140                 });
141
142                 egCore.hatch.setItem(
143                     'eg.workstation.all', allWorkstations)
144                 .then(function() {
145                     if (allWorkstations.length == 1) {
146                         // first one registerd, also mark it as the default
147                         $scope.selectedWS = name;
148                         $scope.setDefaultWS();
149                     }
150                 });
151             }
152         });
153     }
154
155     $scope.wsOrgChanged = function(org) {
156         $scope.contextOrg = org;
157     }
158
159     // ---------------------
160     // Hatch Configs
161     $scope.hatchURL = egCore.hatch.hatchURL();
162     $scope.hatchRequired = 
163         egCore.hatch.getLocalItem('eg.hatch.required');
164
165     $scope.updateHatchRequired = function() {
166         egCore.hatch.setLocalItem(
167             'eg.hatch.required', $scope.hatchRequired);
168     }
169
170     $scope.updateHatchURL = function() {
171         egCore.hatch.setLocalItem(
172             'eg.hatch.url', $scope.hatchURL);
173     }
174 }])
175
176 .controller('PrintConfigCtrl',
177        ['$scope','egCore',
178 function($scope , egCore) {
179     console.log('PrintConfigCtrl');
180
181     $scope.actionPending = false;
182     $scope.isTestView = false;
183
184     $scope.setContext = function(ctx) { 
185         $scope.context = ctx; 
186         $scope.isTestView = false;
187         $scope.actionPending = false;
188     }
189     $scope.setContext('default');
190
191     $scope.getPrinterByAttr = function(attr, value) {
192         var printer;
193         angular.forEach($scope.printers, function(p) {
194             if (p[attr] == value) printer = p;
195         });
196         return printer;
197     }
198
199     $scope.currentPrinter = function() {
200         if ($scope.printConfig && $scope.printConfig[$scope.context]) {
201             return $scope.getPrinterByAttr(
202                 'name', $scope.printConfig[$scope.context].printer
203             );
204         }
205     }
206
207     // fetch info on all remote printers
208     egCore.hatch.getPrinters()
209     .then(function(printers) { 
210         $scope.printers = printers;
211         $scope.defaultPrinter = 
212             $scope.getPrinterByAttr('is-default', true);
213     })
214     .then(function() { return egCore.hatch.getPrintConfig() })
215     .then(function(config) {
216         $scope.printConfig = config;
217
218         var pname = '';
219         if ($scope.defaultPrinter) {
220             pname = $scope.defaultPrinter.name;
221
222         } else if ($scope.printers.length == 1) {
223             // if the OS does not report a default printer, but only
224             // one printer is available, treat it as the default.
225             pname = $scope.printers[0].name;
226         }
227
228         // apply the default printer to every context which has
229         // no printer configured.
230         angular.forEach(
231             ['default','receipt','label','mail','offline'],
232             function(ctx) {
233                 if (!$scope.printConfig[ctx]) {
234                     $scope.printConfig[ctx] = {
235                         context : ctx,
236                         printer : pname
237                     }
238                 }
239             }
240         );
241     });
242
243     $scope.printerConfString = function() {
244         if ($scope.printConfigError) return $scope.printConfigError;
245         if (!$scope.printConfig) return;
246         if (!$scope.printConfig[$scope.context]) return;
247         return JSON.stringify(
248             $scope.printConfig[$scope.context], undefined, 2);
249     }
250
251     $scope.resetConfig = function() {
252         $scope.actionPending = true;
253         $scope.printConfigError = null;
254         $scope.printConfig[$scope.context] = {
255             context : $scope.context
256         }
257         
258         if ($scope.defaultPrinter) {
259             $scope.printConfig[$scope.context].printer = 
260                 $scope.defaultPrinter.name;
261         }
262
263         egCore.hatch.setPrintConfig($scope.printConfig)
264         .finally(function() {$scope.actionPending = false});
265     }
266
267     $scope.configurePrinter = function() {
268         $scope.printConfigError = null;
269         $scope.actionPending = true;
270         egCore.hatch.configurePrinter(
271             $scope.context,
272             $scope.printConfig[$scope.context].printer
273         )
274         .then(
275             function(config) {$scope.printConfig = config},
276             function(error) {$scope.printConfigError = error}
277         )
278         .finally(function() {$scope.actionPending = false});
279     }
280
281     $scope.setPrinter = function(name) {
282         $scope.printConfig[$scope.context].printer = name;
283     }
284
285     // for testing
286     $scope.setContentType = function(type) { $scope.contentType = type }
287
288     $scope.testPrint = function(withDialog) {
289         if ($scope.contentType == 'text/plain') {
290             egCore.print.print({
291                 context : $scope.context, 
292                 content_type : $scope.contentType, 
293                 content : $scope.textPrintContent,
294                 show_dialog : withDialog
295             });
296         } else {
297             egCore.print.print({
298                 context : $scope.context,
299                 content_type : $scope.contentType, 
300                 content : $scope.htmlPrintContent, 
301                 scope : {
302                     value1 : 'Value One', 
303                     value2 : 'Value Two',
304                     date_value : '2015-02-04T14:04:34-0400'
305                 },
306                 show_dialog : withDialog
307             });
308         }
309     }
310
311     $scope.setContentType('text/plain');
312
313 }])
314
315 .controller('PrintTemplatesCtrl',
316        ['$scope','$q','egCore',
317 function($scope , $q , egCore) {
318
319     $scope.print = {
320         template_name : 'bills_current',
321         template_output : ''
322     };
323
324     // print preview scope data
325     // TODO: consider moving the template-specific bits directly
326     // into the templates or storing template- specific script files
327     // alongside the templates.
328     // NOTE: A lot of this data can be shared across templates.
329     var seed_user = {
330         first_given_name : 'Slow',
331         second_given_name : 'Joe',
332         family_name : 'Jones',
333         card : {
334             barcode : '30393830393'
335         }
336     }
337     var seed_addr = {
338         street1 : '123 Apple Rd',
339         street2 : 'Suite B',
340         city : 'Anywhere',
341         state : 'XX',
342         country : 'US',
343         post_code : '12345'
344     }
345
346     var seed_record = {
347         title : 'Traveling Pants!!',
348         author : 'Jane Jones',
349         isbn : '1231312123'
350     };
351
352     var seed_copy = {
353         barcode : '33434322323'
354     }
355
356     var one_hold = {
357         behind_desk : 'f',
358         phone_notify : '111-222-3333',
359         sms_notify : '111-222-3333',
360         email_notify : 'user@example.org',
361         request_time : new Date().toISOString()
362     }
363
364
365     $scope.preview_scope = {
366         //bills
367         transactions : [
368             {
369                 id : 1,
370                 xact_start : new Date().toISOString(),
371                 summary : {
372                     xact_type : 'circulation',
373                     last_billing_type : 'Overdue materials',
374                     total_owed : 1.50,
375                     last_payment_note : 'Test Note 1',
376                     total_paid : 0.50,
377                     balance_owed : 1.00
378                 }
379             }, {
380                 id : 2,
381                 xact_start : new Date().toISOString(),
382                 summary : {
383                     xact_type : 'circulation',
384                     last_billing_type : 'Overdue materials',
385                     total_owed : 2.50,
386                     last_payment_note : 'Test Note 2',
387                     total_paid : 0.50,
388                     balance_owed : 2.00
389                 }
390             }
391         ],
392
393         circulations : [
394             {   
395                 due_date : new Date().toISOString(), 
396                 target_copy : seed_copy,
397                 title : seed_record.title
398             },
399         ],
400
401         previous_balance : 8.45,
402         payment_total : 2.00,
403         payment_applied : 2.00,
404         new_balance : 6.45,
405         amount_voided : 0,
406         change_given : 0,
407         payment_type : 'cash_payment',
408         payment_note : 'Here is a payment note',
409         note : {
410             create_date : new Date().toISOString(), 
411             title : 'Test Note Title',
412             usr : seed_user,
413             value : 'This patron is super nice!'
414         },
415
416         transit : {
417             dest : {
418                 name : 'Library X',
419                 shortname : 'LX',
420                 holds_address : seed_addr
421             },
422             target_copy : seed_copy
423         },
424         title : seed_record.title,
425         author : seed_record.author,
426         patron : egCore.idl.toHash(egCore.auth.user()),
427         address : seed_addr,
428         hold : one_hold,
429         holds : [
430             {hold : one_hold, title : 'Some Title 1', author : 'Some Author 1'},
431             {hold : one_hold, title : 'Some Title 2', author : 'Some Author 2'},
432             {hold : one_hold, title : 'Some Title 3', author : 'Some Author 3'}
433         ]
434     }
435
436     $scope.preview_scope.payments = [
437         {amount : 1.00, xact : $scope.preview_scope.transactions[0]}, 
438         {amount : 1.00, xact : $scope.preview_scope.transactions[1]}
439     ]
440     $scope.preview_scope.payments[0].xact.title = 'Hali Bote Azikaban de tao fan';
441     $scope.preview_scope.payments[0].xact.copy_barcode = '334343434';
442     $scope.preview_scope.payments[1].xact.title = seed_record.title;
443     $scope.preview_scope.payments[1].xact.copy_barcode = seed_copy.barcode;
444
445     // today, staff, current_location, etc.
446     egCore.print.fleshPrintScope($scope.preview_scope);
447
448     $scope.template_changed = function() {
449         $scope.print.load_failed = false;
450         egCore.print.getPrintTemplate($scope.print.template_name)
451         .then(
452             function(html) { 
453                 $scope.print.template_content = html;
454                 console.log('set template content');
455             },
456             function() {
457                 $scope.print.template_content = '';
458                 $scope.print.load_failed = true;
459             }
460         );
461     }
462
463     $scope.save_locally = function() {
464         egCore.hatch.storePrintTemplate(
465             $scope.print.template_name,
466             $scope.print.template_content
467         );
468     }
469
470     $scope.template_changed(); // load the default
471 }])
472
473 // 
474 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
475     return function(scope, element, attrs) {
476         scope.$watch(
477             function(scope) {
478                 return scope.$eval(attrs.content);
479             },
480             function(value) {
481                 // create an isolate scope and copy the print context
482                 // data into the new scope.
483                 // TODO: see also print security concerns in egHatch
484                 var result = element.html(value);
485                 var context = scope.$eval(attrs.context);
486                 var print_scope = scope.$new(true);
487                 angular.forEach(context, function(val, key) {
488                     print_scope[key] = val;
489                 })
490                 $compile(element.contents())(print_scope);
491             }
492         );
493     };
494 }])
495
496 .controller('StoredPrefsCtrl',
497        ['$scope','$q','egCore','egConfirmDialog',
498 function($scope , $q , egCore , egConfirmDialog) {
499     console.log('StoredPrefsCtrl');
500
501     $scope.setContext = function(ctx) {
502         $scope.context = ctx;
503     }
504     $scope.setContext('local');
505
506     // grab the edit perm
507     $scope.userHasDeletePerm = false;
508     egCore.perm.hasPermHere('ADMIN_WORKSTATION')
509     .then(function(bool) { $scope.userHasDeletePerm = bool });
510
511     // fetch the keys
512
513     function refreshKeys() {
514         $scope.keys = {local : [], remote : []};
515
516         egCore.hatch.getRemoteKeys().then(
517             function(keys) { $scope.keys.remote = keys.sort() })
518     
519         // local calls are non-async
520         $scope.keys.local = egCore.hatch.getLocalKeys();
521     }
522     refreshKeys();
523
524     $scope.selectKey = function(key) {
525         $scope.currentKey = key;
526         $scope.currentKeyContent = null;
527
528         if ($scope.context == 'local') {
529             $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
530         } else {
531             egCore.hatch.getRemoteItem(key)
532             .then(function(content) {
533                 $scope.currentKeyContent = content
534             });
535         }
536     }
537
538     $scope.getCurrentKeyContent = function() {
539         return JSON.stringify($scope.currentKeyContent, null, 2);
540     }
541
542     $scope.removeKey = function(key) {
543         egConfirmDialog.open(
544             egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
545             {   deleteKey : key,
546                 ok : function() {
547                     if ($scope.context == 'local') {
548                         egCore.hatch.removeLocalItem(key);
549                         refreshKeys();
550                     } else {
551                         egCore.hatch.removeItem(key)
552                         .then(function() { refreshKeys() });
553                     }
554                 },
555                 cancel : function() {} // user canceled, nothing to do
556             }
557         );
558     }
559 }])