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