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