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