]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/admin/workstation/app.js
LP2061136 - Stamping 1405 DB upgrade script
[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?|mailto|blob):/); 
15     var resolver = {delay : function(egStartup) {return egStartup.go()}};
16
17     $routeProvider.when('/admin/workstation/workstations', {
18         templateUrl: './admin/workstation/t_workstations',
19         controller: 'WSRegCtrl',
20         resolve : resolver
21     });
22
23     $routeProvider.when('/admin/workstation/print/config', {
24         templateUrl: './admin/workstation/t_print_config',
25         controller: 'PrintConfigCtrl',
26         resolve : resolver
27     });
28
29     $routeProvider.when('/admin/workstation/print/templates', {
30         templateUrl: './admin/workstation/t_print_templates',
31         controller: 'PrintTemplatesCtrl',
32         resolve : resolver
33     });
34
35     $routeProvider.when('/admin/workstation/stored_prefs', {
36         templateUrl: './admin/workstation/t_stored_prefs',
37         controller: 'StoredPrefsCtrl',
38         resolve : resolver
39     });
40
41     $routeProvider.when('/admin/workstation/tests', {
42         templateUrl: './admin/workstation/t_tests',
43         controller: 'testsCtrl',
44         resolve : resolver
45     });
46     
47     // default page 
48     $routeProvider.otherwise({
49         templateUrl : './admin/workstation/t_splash',
50         controller : 'SplashCtrl',
51         resolve : resolver
52     });
53 }])
54
55 .config(['ngToastProvider', function(ngToastProvider) {
56   ngToastProvider.configure({
57     verticalPosition: 'bottom',
58     animation: 'fade'
59   });
60 }])
61
62 .factory('workstationSvc',
63        ['$q','$timeout','$location','egCore','egConfirmDialog',
64 function($q , $timeout , $location , egCore , egConfirmDialog) {
65     
66     var service = {};
67
68     service.get_all = function() {
69         return egCore.hatch.getWorkstations()
70         .then(function(all) { return all || [] });
71     }
72
73     service.get_default = function() {
74         return egCore.hatch.getDefaultWorkstation();
75     }
76
77     service.set_default = function(name) {
78         return egCore.hatch.setDefaultWorkstation(name);
79     }
80
81     service.register_workstation = function(base_name, name, org_id) {
82         return service.register_ws_api(base_name, name, org_id)
83         .then(function(ws_id) {
84             return service.track_new_ws(ws_id, name, org_id);
85         });
86     };
87
88     service.register_ws_api = 
89         function(base_name, name, org_id, override, deferred) {
90         if (!deferred) deferred = $q.defer();
91
92         var method = 'open-ils.actor.workstation.register';
93         if (override) method += '.override';
94
95         egCore.net.request(
96             'open-ils.actor', method, egCore.auth.token(), name, org_id)
97
98         .then(function(resp) {
99
100             if (evt = egCore.evt.parse(resp)) {
101                 console.log('register returned ' + evt.toString());
102
103                 if (evt.textcode == 'WORKSTATION_NAME_EXISTS' && !override) {
104
105                     egConfirmDialog.open(
106                         egCore.strings.WS_EXISTS, base_name, {  
107                             ok : function() {
108                                 service.register_ws_api(
109                                     base_name, name, org_id, true, deferred)
110                             },
111                             cancel : function() {
112                                 deferred.reject();
113                             }
114                         }
115                     );
116
117                 } else {
118                     alert(evt.toString());
119                     deferred.reject();
120                 }
121             } else if (resp) {
122                 console.log('Resolving register promise with: ' + resp);
123                 deferred.resolve(resp);
124             }
125         });
126
127         return deferred.promise;
128     }
129
130     service.track_new_ws = function(ws_id, ws_name, owning_lib) {
131         console.log('Tracking newly created WS with ID ' + ws_id);
132         var new_ws = {id : ws_id, name : ws_name, owning_lib : owning_lib};
133
134         return service.get_all()
135         .then(function(all) {
136             all.push(new_ws);
137             return egCore.hatch.setWorkstations(all)
138             .then(function() { return new_ws });
139         });
140     }
141
142     // Remove all traces of the workstation locally.
143     // This does not remove the WS from the server.
144     service.remove_workstation = function(name) {
145         console.debug('Removing workstation: ' + name);
146
147         return egCore.hatch.getWorkstations()
148
149         // remove from list of all workstations
150         .then(function(all) {
151             if (!all) all = [];
152             var keep = all.filter(function(ws) {return ws.name != name});
153             return egCore.hatch.setWorkstations(keep);
154
155         }).then(function() { 
156
157             return service.get_default()
158
159         }).then(function(def) {
160             if (def == name) {
161                 console.debug('Removing default workstation: ' + name);
162                 return egCore.hatch.removeDefaultWorkstation();
163             }
164         });
165     }
166
167     return service;
168 }])
169
170
171 .controller('SplashCtrl',
172        ['$scope','$window','$location','egCore','egConfirmDialog',
173 function($scope , $window , $location , egCore , egConfirmDialog) {
174
175     egCore.hatch.getItem('eg.audio.disable').then(function(val) {
176         $scope.disable_sound = val;
177     });
178
179     egCore.hatch.getItem('eg.orgselect.show_combined_names').then(function(val) {
180         $scope.orgselect_combo_names = val;
181     });
182
183     egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
184         $scope.search_lib = egCore.org.get(val);
185     });
186     $scope.handle_search_lib_changed = function(org) {
187         egCore.hatch.setItem('eg.search.search_lib', org.id());
188     };
189
190     egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
191         $scope.pref_lib = egCore.org.get(val);
192     });
193     $scope.handle_pref_lib_changed = function(org) {
194         egCore.hatch.setItem('eg.search.pref_lib', org.id());
195     };
196
197     $scope.adv_pane = 'advanced'; // default value if not explicitly set
198     egCore.hatch.getItem('eg.search.adv_pane').then(function(val) {
199         $scope.adv_pane = val;
200     });
201     $scope.$watch('adv_pane', function(newVal, oldVal) {
202         if (typeof newVal != 'undefined' && newVal != oldVal) {
203             egCore.hatch.setItem('eg.search.adv_pane', newVal);
204         }
205     });
206
207     $scope.apply_sound = function() {
208         if ($scope.disable_sound) {
209             egCore.hatch.setItem('eg.audio.disable', true);
210         } else {
211             egCore.hatch.removeItem('eg.audio.disable');
212         }
213     }
214
215     $scope.apply_orgselect_combob_names = function() {
216         if ($scope.orgselect_combo_names) {
217             egCore.hatch.setItem('eg.orgselect.show_combined_names', true);
218         } else {
219             egCore.hatch.removeItem('eg.orgselect.show_combined_names');
220         }
221     }
222
223     $scope.test_audio = function(sound) {
224         egCore.audio.play(sound);
225     }
226
227 }])
228
229 .controller('PrintConfigCtrl',
230        ['$scope','egCore',
231 function($scope , egCore) {
232
233     $scope.printConfig = {};
234     $scope.setContext = function(ctx) { 
235         $scope.context = ctx; 
236         $scope.isTestView = false;
237     }
238     $scope.setContext('default');
239
240     $scope.setContentType = function(type) { $scope.contentType = type }
241     $scope.setContentType('text/plain');
242
243     var hatchPrinting = false;
244     egCore.hatch.usePrinting().then(function(answer) {
245         hatchPrinting = answer;
246     });
247
248     $scope.useHatchPrinting = function() {
249         return hatchPrinting;
250     }
251
252     $scope.hatchIsOpen = function() {
253         return egCore.hatch.hatchAvailable;
254     }
255
256     $scope.getPrinterByAttr = function(attr, value) {
257         var printer;
258         angular.forEach($scope.printers, function(p) {
259             if (p[attr] == value) printer = p;
260         });
261         return printer;
262     }
263
264     $scope.resetPrinterSettings = function(context) {
265         $scope.printConfig[context] = {
266             context : context,
267             printer : $scope.defaultPrinter ? $scope.defaultPrinter.name : null,
268             autoMargins : true, 
269             allPages : true,
270             pageRanges : []
271         };
272     }
273
274     $scope.savePrinterSettings = function(context) {
275         return egCore.hatch.setPrintConfig(
276             context, $scope.printConfig[context]);
277     }
278
279     $scope.printerConfString = function() {
280         if ($scope.printConfigError) return $scope.printConfigError;
281         if (!$scope.printConfig) return;
282         if (!$scope.printConfig[$scope.context]) return;
283         return JSON.stringify(
284             $scope.printConfig[$scope.context], undefined, 2);
285     }
286
287     function loadPrinterOptions(name) {
288         if (name == 'hatch_file_writer' || name == 'hatch_browser_printing') {
289             $scope.printerOptions = {};
290         } else {
291             egCore.hatch.getPrinterOptions(name).then(
292                 function(options) {$scope.printerOptions = options});
293         }
294     }
295
296     $scope.setPrinter = function(name) {
297         $scope.printConfig[$scope.context].printer = name;
298         loadPrinterOptions(name);
299     }
300
301     $scope.testPrint = function(withDialog) {
302         if ($scope.contentType == 'text/plain') {
303             egCore.print.print({
304                 context : $scope.context, 
305                 content_type : $scope.contentType, 
306                 content : $scope.textPrintContent,
307                 show_dialog : withDialog
308             });
309         } else {
310             egCore.print.print({
311                 context : $scope.context,
312                 content_type : $scope.contentType, 
313                 content : $scope.htmlPrintContent, 
314                 scope : {
315                     value1 : 'Value One', 
316                     value2 : 'Value Two',
317                     date_value : '2015-02-04T14:04:34-0400'
318                 },
319                 show_dialog : withDialog
320             });
321         }
322     }
323
324     $scope.useFileWriter = function() {
325         return (
326             $scope.printConfig[$scope.context] &&
327             $scope.printConfig[$scope.context].printer == 'hatch_file_writer'
328         );
329     }
330
331     $scope.useBrowserPrinting = function() {
332         return (
333             $scope.printConfig[$scope.context] &&
334             $scope.printConfig[$scope.context].printer == 'hatch_browser_printing'
335         );
336     }
337
338
339     // Load startup data....
340     // Don't bother talking to Hatch if it's not there.
341     if (!egCore.hatch.hatchAvailable) return;
342
343     // fetch info on all remote printers
344     egCore.hatch.getPrinters()
345     .then(function(printers) { 
346         $scope.printers = printers;
347
348         printers.push({
349             // We need a static name for saving configs.
350             // Human-friendly label is set in the template.
351             name: 'hatch_file_writer' 
352         });
353
354         printers.push({name: 'hatch_browser_printing'});
355
356         var def = $scope.getPrinterByAttr('is-default', true);
357         if (!def && printers.length) def = printers[0];
358
359         if (def) {
360             $scope.defaultPrinter = def;
361             loadPrinterOptions(def.name);
362         }
363     }).then(function() {
364         angular.forEach(
365             ['default','receipt','label','mail','offline'],
366             function(ctx) {
367                 egCore.hatch.getPrintConfig(ctx).then(function(conf) {
368                     if (conf) {
369                         $scope.printConfig[ctx] = conf;
370                     } else {
371                         $scope.resetPrinterSettings(ctx);
372                     }
373                 });
374             }
375         );
376     });
377
378 }])
379
380 .controller('PrintTemplatesCtrl',
381        ['$scope','$q','egCore','ngToast',
382 function($scope , $q , egCore , ngToast) {
383
384     $scope.print = {
385         template_name : 'bills_current',
386         template_output : '',
387         template_context : 'default'
388     };
389
390     // print preview scope data
391     // TODO: consider moving the template-specific bits directly
392     // into the templates or storing template- specific script files
393     // alongside the templates.
394     // NOTE: A lot of this data can be shared across templates.
395     var seed_user = {
396         prefix : 'Mr',
397         first_given_name : 'Joseph',
398         second_given_name : 'Martin',
399         family_name : 'Jones',
400         suffix : 'III',
401         pref_first_given_name : 'Martin',
402         pref_second_given_name : 'Joe',
403         pref_family_name : 'Smith',
404         card : {
405             barcode : '30393830393'
406         },
407         money_summary : {
408             balance_owed : 4, // This is currently how these values are returned to the client
409             total_billed : '5.00',
410             total_paid : '1.00'
411         },
412         expire_date : '2020-12-31',
413         alias : 'Joey J.',
414         has_email : true,
415         has_phone : false,
416         dob : '1980-01-01T00:00:00-8:00',
417         juvenile : 'f',
418         usrname : '30393830393',
419         day_phone : '111-222-3333',
420         evening_phone : '222-333-1111',
421         other_phone : '333-111-2222',
422         email : 'user@example.com',
423         home_ou : {name: function() {return 'BR1'}},
424         profile : {name: function() {return 'Patrons'}},
425         net_access_level : {name: function() {return 'Filtered'}},
426         active : 't',
427         barred : 'f',
428         master_account : 'f',
429         claims_returned_count : '0',
430         claims_never_checked_out_count : '0',
431         ident_type: {name: function() {return 'Drivers License'}},
432         ident_value: '11332445',
433         ident_type2: {name: function() {return 'Other'}},
434         ident_value2 : '55442211',
435         addresses : [],
436         stat_cat_entries : [
437             {
438                 stat_cat : {'name' : 'Favorite Donut'},
439                 'stat_cat_entry' : 'Maple'
440             }, {
441                 stat_cat : {'name' : 'Favorite Book'},
442                 'stat_cat_entry' : 'Beasts Made of Night'
443             }
444         ],
445         surveys : [
446             {
447                 survey : {
448                     'id' : function() {return '1'},
449                     'description' : function() {return 'Voter Registration'},
450                 },
451                 responses : [
452                     {
453                         'answer_date' : function() {return '2020-12-31'},
454                         question : function() {return {'question' : function() {return 'Would you like to register to vote today?'}}},
455                         answer : function() {return {'answer' : function() {return 'Already registered'}}}
456                     }
457                 ]
458             }
459         ]
460     }
461
462     var seed_addr = {
463         address_type : 'MAILING',
464         street1 : '123 Apple Rd',
465         street2 : 'Suite B',
466         city : 'Anywhere',
467         county : 'Great County',
468         state : 'XX',
469         country : 'US',
470         post_code : '12345',
471         valid : 't',
472         within_city_limits: 't'
473     }
474
475     seed_user.addresses.push(seed_addr);
476
477     var seed_record = {
478         title : 'Traveling Pants!!',
479         author : 'Jane Jones',
480         isbn : '1231312123'
481     };
482
483     var seed_copy = {
484         barcode : '33434322323',
485         status : {
486             name : 'In transit'
487             },
488         call_number : {
489             label : '636.8 JON',
490             record : {
491                 simple_record : {
492                     'title' : 'Test Title'
493                 }
494             },
495             owning_lib : {
496                 name : 'Ankers Memorial Library',
497                 shortname : 'Ankers'
498             }
499         },
500         circ_modifier : {
501                 name : 'Book'
502                 },
503         location : {
504             name : 'General Collection'
505         },
506         status : {
507             name : 'In Transit'
508         },
509         // flattened versions for item status template
510         // TODO - make this go away
511         'call_number.label' : '636.8 JON',
512         'call_number.record.simple_record.title' : 'Test Title',
513         'location.name' : 'General Collection',
514         'call_number.owning_lib.name' : 'Ankers Memorial Library',
515         'call_number.owning_lib.shortname' : 'Ankers',
516         'location.name' : 'General Collection'
517     }
518
519     var one_hold = {
520         behind_desk : 'f',
521         phone_notify : '111-222-3333',
522         sms_notify : '111-222-3333',
523         email_notify : 'user@example.org',
524         request_time : new Date().toISOString(),
525         hold_type : 'T',
526         shelf_expire_time : new Date().toISOString()
527     }
528
529     var seed_transit = {
530         source : {
531             name : 'Library Y',
532             shortname : 'LY',
533             holds_address : seed_addr
534         },
535         dest : {
536             name : 'Library X',
537             shortname : 'LX',
538             holds_address : seed_addr
539         },
540         source_send_time : new Date().toISOString(),
541         target_copy : seed_copy
542     }
543
544     $scope.preview_scope = {
545         //bills
546         transactions : [
547             {
548                 id : 1,
549                 xact_start : new Date().toISOString(),
550                 xact_finish : new Date().toISOString(),
551                 call_number : {
552                     label : "spindler",
553                     prefix : "biography",
554                     suffix : "Closed Stacks",
555                     owning_lib : {
556                         name : "Mineola Public Library",
557                         shortname : "Mineola"
558                     }
559                 },
560                 summary : {
561                     xact_type : 'circulation',
562                     last_billing_type : 'Overdue materials',
563                     total_owed : 1.50,
564                     last_payment_note : 'Test Note 1',
565                     last_payment_type : 'cash_payment',
566                     last_payment_ts : new Date().toISOString(),
567                     total_paid : 0.50,
568                     balance_owed : 1.00
569                 }
570             }, {
571                 id : 2,
572                 xact_start : new Date().toISOString(),
573                 xact_finish : new Date().toISOString(),
574                 call_number : {
575                         label : "796.6 WEI",
576                         prefix : "",
577                         suffix : "REF",
578                         owning_lib : {
579                            name : "Rogers Reading Room",
580                            shortname : "Rogers"
581                                      }
582                                },
583                 summary : {
584                     xact_type : 'circulation',
585                     last_billing_type : 'Overdue materials',
586                     total_owed : 2.50,
587                     last_payment_note : 'Test Note 2',
588                     last_payment_type : 'credit_payment',
589                     last_payment_ts : new Date().toISOString(),
590                     total_paid : 0.50,
591                     balance_owed : 2.00
592                 }
593             }
594         ],
595
596         copy : seed_copy,
597         copies : [ seed_copy ],
598
599         checkins : [
600             {
601                 due_date : new Date().toISOString(),
602                 circ_lib : 1,
603                 duration : '7 days',
604                 target_copy : seed_copy,
605                 copy_barcode : seed_copy.barcode,
606                 call_number : seed_copy.call_number,
607                 title : seed_record.title
608             },
609         ],
610
611         circulations : [
612             {
613                 circ : {
614                     due_date : new Date().toISOString(),
615                     circ_lib : 1,
616                     duration : '7 days',
617                     renewal_remaining : 2
618                 },
619                 copy : seed_copy,
620                 title : seed_record.title,
621                 author : seed_record.author,
622                 call_number : seed_copy.call_number
623             }
624         ],
625
626         patron_money : {
627             balance_owed : 5.01,
628             total_owed : 10.12,
629             total_paid : 5.11
630         },
631
632         in_house_uses : [
633             {
634                 num_uses : 3,
635                 copy : seed_copy,
636                 title : seed_record.title
637             }
638         ],
639
640         previous_balance : 8.45,
641         payment_total : 2.00,
642         payment_applied : 2.00,
643         new_balance : 6.45,
644         amount_voided : 0,
645         change_given : 0,
646         payment_type : 'cash_payment',
647         payment_note : 'Here is a payment note',
648         approval_code : 'CH1234567',
649         note : {
650             create_date : new Date().toISOString(), 
651             title : 'Test Note Title',
652             usr : seed_user,
653             value : 'This patron is super nice!'
654         },
655
656         transit : seed_transit,
657         transits : [ seed_transit ],
658         title : seed_record.title,
659         author : seed_record.author,
660         patron : seed_user,
661         address : seed_addr,
662         dest_location : egCore.idl.toHash(egCore.org.get(egCore.auth.user().ws_ou())),
663         dest_courier_code : 'ABC 123',
664         dest_address : seed_addr,
665         source_address : seed_addr,
666                 source_location : egCore.idl.toHash(egCore.org.get(egCore.auth.user().ws_ou())),
667         hold : one_hold,
668         holds : [
669             {
670                 hold : one_hold, title : 'Some Title 1', author : 'Some Author 1',
671                 volume : { label : '646.4 SOM' }, copy : seed_copy,
672                 part : { label : 'v. 1' },
673                 patron_barcode : 'S52802662',
674                 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane',
675                 status_string : 'Ready for Pickup'
676             },
677             {
678                 hold : one_hold, title : 'Some Title 2', author : 'Some Author 2',
679                 volume : { label : '646.4 SOM' }, copy : seed_copy,
680                 part : { label : 'v. 1' },
681                 patron_barcode : 'S52802662',
682                 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane',
683                 status_string : 'Ready for Pickup'
684             },
685             {
686                 hold : one_hold, title : 'Some Title 3', author : 'Some Author 3',
687                 volume : { label : '646.4 SOM' }, copy : seed_copy,
688                 part : { label : 'v. 1' },
689                 patron_barcode : 'S52802662',
690                 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane',
691                 status_string : 'Canceled'
692             }
693         ]
694     }
695
696     $scope.preview_scope.payments = [
697         {amount : 1.00, xact : $scope.preview_scope.transactions[0]}, 
698         {amount : 1.00, xact : $scope.preview_scope.transactions[1]}
699     ]
700     $scope.preview_scope.payments[0].xact.title = 'Hali Bote Azikaban de tao fan';
701     $scope.preview_scope.payments[0].xact.copy_barcode = '334343434';
702     $scope.preview_scope.payments[1].xact.title = seed_record.title;
703     $scope.preview_scope.payments[1].xact.copy_barcode = seed_copy.barcode;
704
705     // today, staff, current_location, etc.
706     egCore.print.fleshPrintScope($scope.preview_scope);
707
708     $scope.template_changed = function() {
709         $scope.print.load_failed = false;
710         egCore.print.getPrintTemplate($scope.print.template_name)
711         .then(
712             function(html) { 
713                 $scope.print.template_content = html;
714                 console.log('set template content');
715             },
716             function() {
717                 $scope.print.template_content = '';
718                 $scope.print.load_failed = true;
719             }
720         );
721         egCore.print.getPrintTemplateContext($scope.print.template_name)
722         .then(function(template_context) {
723             $scope.print.template_context = template_context;
724         });
725     }
726
727     $scope.reset_to_default = function() {
728         egCore.print.removePrintTemplate(
729             $scope.print.template_name
730         );
731         egCore.print.removePrintTemplateContext(
732             $scope.print.template_name
733         );
734         $scope.template_changed();
735     }
736
737     $scope.save_locally = function() {
738         egCore.print.storePrintTemplate(
739             $scope.print.template_name,
740             $scope.print.template_content
741         );
742         egCore.print.storePrintTemplateContext(
743             $scope.print.template_name,
744             $scope.print.template_context
745         );
746     }
747
748     $scope.exportable_templates = function() {
749         var templates = {};
750         var contexts = {};
751         var deferred = $q.defer();
752         var promises = [];
753         egCore.hatch.getKeys('eg.print.template').then(function(keys) {
754             angular.forEach(keys, function(key) {
755                 if (key.match(/^eg\.print\.template\./)) {
756                     promises.push(egCore.hatch.getItem(key).then(function(value) {
757                         templates[key.replace('eg.print.template.', '')] = value;
758                     }));
759                 } else {
760                     promises.push(egCore.hatch.getItem(key).then(function(value) {
761                         contexts[key.replace('eg.print.template_context.', '')] = value;
762                     }));
763                 }
764             });
765             $q.all(promises).then(function() {
766                 if (Object.keys(templates).length) {
767                     deferred.resolve({
768                         templates: templates,
769                         contexts: contexts
770                     });
771                 } else {
772                     ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
773                     deferred.reject();
774                 }
775             });
776         });
777         return deferred.promise;
778     }
779
780     $scope.imported_print_templates = { data : '' };
781     $scope.$watch('imported_print_templates.data', function(newVal, oldVal) {
782         if (newVal && newVal != oldVal) {
783             try {
784                 var data = JSON.parse(newVal);
785                 angular.forEach(data.templates, function(template_content, template_name) {
786                     egCore.print.storePrintTemplate(template_name, template_content);
787                 });
788                 angular.forEach(data.contexts, function(template_context, template_name) {
789                     egCore.print.storePrintTemplateContext(template_name, template_context);
790                 });
791                 $scope.template_changed(); // refresh
792                 ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
793             } catch (E) {
794                 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_IMPORT);
795             }
796         }
797     });
798
799     $scope.template_changed(); // load the default
800 }])
801
802 // 
803 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
804     return function(scope, element, attrs) {
805         scope.$watch(
806             function(scope) {
807                 return scope.$eval(attrs.content);
808             },
809             function(value) {
810                 // create an isolate scope and copy the print context
811                 // data into the new scope.
812                 // TODO: see also print security concerns in egHatch
813                 var result = element.html(value);
814                 var context = scope.$eval(attrs.context);
815                 var print_scope = scope.$new(true);
816                 angular.forEach(context, function(val, key) {
817                     print_scope[key] = val;
818                 })
819                 $compile(element.contents())(print_scope);
820             }
821         );
822     };
823 }])
824
825 .controller('StoredPrefsCtrl',
826        ['$scope','$q','egCore','egConfirmDialog',
827 function($scope , $q , egCore , egConfirmDialog) {
828     console.log('StoredPrefsCtrl');
829
830     $scope.setContext = function(ctx) {
831         $scope.context = ctx;
832     }
833     $scope.setContext('local');
834
835     // grab the edit perm
836     $scope.userHasDeletePerm = false;
837     egCore.perm.hasPermHere('ADMIN_WORKSTATION')
838     .then(function(bool) { $scope.userHasDeletePerm = bool });
839
840     // fetch the keys
841
842     function refreshKeys() {
843         $scope.keys = {local : [], remote : [], server_workstation: []};
844
845         if (egCore.hatch.hatchAvailable) {
846             egCore.hatch.getRemoteKeys().then(
847                 function(keys) { $scope.keys.remote = keys.sort() })
848         }
849     
850         // local calls are non-async
851         $scope.keys.local = egCore.hatch.getLocalKeys();
852
853         egCore.hatch.getServerKeys(null, {workstation_only: true}).then(
854             function(keys) {$scope.keys.server_workstation = keys});
855     }
856     refreshKeys();
857
858     $scope.selectKey = function(key) {
859         $scope.currentKey = key;
860         $scope.currentKeyContent = null;
861
862         if ($scope.context == 'local') {
863             $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
864         } else if ($scope.context == 'remote') {
865             egCore.hatch.getRemoteItem(key)
866             .then(function(content) {
867                 $scope.currentKeyContent = content
868             });
869         } else if ($scope.context == 'server_workstation') {
870             egCore.hatch.getServerItem(key).then(function(content) {
871                 $scope.currentKeyContent = content;
872             });
873         }
874     }
875
876     $scope.getCurrentKeyContent = function() {
877         return JSON.stringify($scope.currentKeyContent, null, 2);
878     }
879
880     $scope.removeKey = function(key) {
881         egConfirmDialog.open(
882             egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
883             {   deleteKey : key,
884                 ok : function() {
885                     if ($scope.context == 'local') {
886                         egCore.hatch.removeLocalItem(key);
887                         refreshKeys();
888                     } else if ($scope.context == 'remote') {
889                         // Honor requests to remove items from Hatch even
890                         // when Hatch is configured for data storage.
891                         egCore.hatch.removeRemoteItem(key)
892                         .then(function() { refreshKeys() });
893                     } else if ($scope.context == 'server_workstation') {
894                         egCore.hatch.removeServerItem(key)
895                         .then(function() { refreshKeys() });
896                     }
897                 },
898                 cancel : function() {} // user canceled, nothing to do
899             }
900         );
901     }
902 }])
903
904 .controller('WSRegCtrl',
905        ['$scope','$q','$window','$location','egCore','egAlertDialog','workstationSvc',
906 function($scope , $q , $window , $location , egCore , egAlertDialog , workstationSvc) {
907
908     var all_workstations = [];
909     var reg_perm_orgs = [];
910
911     $scope.page_loaded = false;
912     $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
913     $scope.wsOrgChanged = function(org) { $scope.contextOrg = org; }
914
915     console.log('set context org to ' + $scope.contextOrg);
916
917     egCore.hatch.hostname().then(function(name) {
918         $scope.newWSName = name || '';
919     });
920
921     // fetch workstation reg perms
922     egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
923     .then(function(orgList) { 
924         reg_perm_orgs = orgList;
925
926         // hide orgs in the context org selector where this login
927         // does not have the reg_ws perm or the org can't have users
928         $scope.wsOrgHidden = function(id) {
929             return reg_perm_orgs.indexOf(id) == -1
930                 || $scope.cant_have_users(id);
931         }
932
933     // fetch the locally stored workstation data
934     }).then(function() {
935         return workstationSvc.get_all()
936         
937     }).then(function(all) {
938         all_workstations = all || [];
939         $scope.workstations = 
940             all_workstations.map(function(w) { return w.name });
941         return workstationSvc.get_default()
942
943     // fetch the default workstation
944     }).then(function(def) { 
945         $scope.defaultWS = def;
946         $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
947
948     // Handle any URL commands.
949     }).then(function() {
950         var remove = $location.search().remove;
951          if (remove) {
952             console.log('Removing WS via URL request: ' + remove);
953             return $scope.remove_ws(remove).then(
954                 function() { $scope.page_loaded = true; });
955         }
956         $scope.page_loaded = true;
957     });
958
959     $scope.get_ws_label = function(ws) {
960         return ws == $scope.defaultWS ? 
961             egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
962     }
963
964     $scope.set_default_ws = function(name) {
965         delete $scope.removing_ws;
966         $scope.defaultWS = name;
967         workstationSvc.set_default(name);
968     }
969
970     $scope.cant_have_users = 
971         function (id) { return !egCore.org.CanHaveUsers(id); };
972     $scope.cant_have_volumes = 
973         function (id) { return !egCore.org.CanHaveVolumes(id); };
974
975     // Log out and return to login page with selected WS 
976     $scope.use_now = function() {
977         egCore.auth.logout();
978         $window.location.href = $location
979             .path('/login')
980             .search({ws : $scope.selectedWS})
981             .absUrl();
982     }
983
984     $scope.can_delete_ws = function(name) {
985         var ws = all_workstations.filter(
986             function(ws) { return ws.name == name })[0];
987         return ws && reg_perm_orgs.indexOf(ws.owning_lib) != -1;
988     }
989
990     $scope.remove_ws = function(remove_me) {
991         $scope.removing_ws = remove_me;
992
993         // Perm is used to disable Remove button in UI, but have to check
994         // again here in case we're removing a WS based on URL params.
995         if (!$scope.can_delete_ws(remove_me)) return $q.when();
996
997         $scope.is_removing = true;
998         return workstationSvc.remove_workstation(remove_me)
999         .then(function() {
1000
1001             all_workstations = all_workstations.filter(
1002                 function(ws) { return ws.name != remove_me });
1003
1004             $scope.workstations = $scope.workstations.filter(
1005                 function(ws) { return ws != remove_me });
1006
1007             if ($scope.selectedWS == remove_me) 
1008                 $scope.selectedWS = $scope.workstations[0];
1009
1010             if ($scope.defaultWS == remove_me) 
1011                 $scope.defaultWS = '';
1012
1013             $scope.is_removing = false;
1014         });
1015     }
1016
1017     $scope.register_ws = function() {
1018         delete $scope.removing_ws;
1019
1020         var full_name = 
1021             $scope.contextOrg.shortname() + '-' + $scope.newWSName;
1022
1023         if ($scope.workstations.indexOf(full_name) > -1) {
1024             // avoid duplicate local registrations
1025             return egAlertDialog.open(egCore.strings.WS_USED);
1026         }
1027
1028         $scope.is_registering = true;
1029         workstationSvc.register_workstation(
1030             $scope.newWSName, full_name,
1031             $scope.contextOrg.id()
1032
1033         ).then(function(new_ws) {
1034             $scope.workstations.push(new_ws.name);
1035             all_workstations.push(new_ws);  
1036             $scope.is_registering = false;
1037
1038             if (!$scope.selectedWS) {
1039                 $scope.selectedWS = new_ws.name;
1040             }
1041             if (!$scope.defaultWS) {
1042                 return $scope.set_default_ws(new_ws.name);
1043             }
1044             $scope.newWSName = '';
1045         }, function(err) {
1046             $scope.is_registering = false;
1047         });
1048     }
1049 }])
1050
1051 /*
1052  * Home of the Latency tester
1053  * */
1054 .controller('testsCtrl', ['$scope', '$location', 'egCore', function($scope, $location, egCore) {
1055     $scope.hostname = $location.host();
1056
1057     $scope.tests = [];
1058     $scope.clearTestData = function(){
1059         $scope.tests = [];
1060         numPings = 0;
1061     }
1062
1063     $scope.isTesting = false;
1064     $scope.avrg = 0; // avrg latency
1065     $scope.canCopyCommand = document.queryCommandSupported('copy');
1066     var numPings = 0;
1067     // initially fetch first 10 (gets a decent average)
1068
1069     function calcAverage(){
1070
1071         if ($scope.tests.length == 0) return 0;
1072
1073         if ($scope.tests.length == 1) return $scope.tests[0].l;
1074
1075         var sum = 0;
1076         angular.forEach($scope.tests, function(t){
1077             sum += t.l;
1078         });
1079
1080         return sum / $scope.tests.length;
1081     }
1082
1083     function ping(){
1084         $scope.isTesting = true;
1085         var t = Date.now();
1086         return egCore.net.request(
1087             "open-ils.pcrud", "opensrf.system.echo", "ping"
1088         ).then(function(resp){
1089             var t2 = Date.now();
1090             var latency = t2 - t;
1091             $scope.tests.push({t: new Date(t), l: latency});
1092             console.log("Start: " + t + " and end: " + t2);
1093             console.log("Latency: " + latency);
1094             console.log(resp);
1095         }).then(function(){
1096             $scope.avrg = calcAverage();
1097             numPings++;
1098             $scope.isTesting = false;
1099         });
1100     }
1101
1102     $scope.testLatency = function(){
1103
1104         if (numPings >= 10){
1105             ping(); // just ping once after the initial ten
1106         } else {
1107             ping()
1108                 .then($scope.testLatency)
1109                 .then(function(){
1110                     if (numPings == 9){
1111                         $scope.tests.shift(); // toss first result
1112                         $scope.avrg = calcAverage();
1113                     }
1114                 });
1115         }
1116     }
1117
1118     $scope.copyTests = function(){
1119
1120         var lNode = document.querySelector('#pingData');
1121         var r = document.createRange();
1122         r.selectNode(lNode);
1123         var sel = window.getSelection();
1124         sel.removeAllRanges();
1125         sel.addRange(r);
1126         document.execCommand('copy');
1127     }
1128
1129 }])
1130
1131