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