]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/admin/workstation/app.js
LP 1648922: Hide orgs that can't have users in workstation registration.
[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/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     // 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.getItem('eg.workstation.all')
70         .then(function(all) { return all || [] });
71     }
72
73     service.get_default = function() {
74         return egCore.hatch.getItem('eg.workstation.default');
75     }
76
77     service.set_default = function(name) {
78         return egCore.hatch.setItem('eg.workstation.default', 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() {deferred.reject()} 
112                         }
113                     );
114
115                 } else {
116                     alert(evt.toString());
117                     deferred.reject();
118                 }
119             } else if (resp) {
120                 console.log('Resolving register promise with: ' + resp);
121                 deferred.resolve(resp);
122             }
123         });
124
125         return deferred.promise;
126     }
127
128     service.track_new_ws = function(ws_id, ws_name, owning_lib) {
129         console.log('Tracking newly created WS with ID ' + ws_id);
130         var new_ws = {id : ws_id, name : ws_name, owning_lib : owning_lib};
131
132         return service.get_all()
133         .then(function(all) {
134             all.push(new_ws);
135             return egCore.hatch.setItem('eg.workstation.all', all)
136             .then(function() { return new_ws });
137         });
138     }
139
140     // Remove all traces of the workstation locally.
141     // This does not remove the WS from the server.
142     service.remove_workstation = function(name) {
143         console.debug('Removing workstation: ' + name);
144
145         return egCore.hatch.getItem('eg.workstation.all')
146
147         // remove from list of all workstations
148         .then(function(all) {
149             if (!all) all = [];
150             var keep = all.filter(function(ws) {return ws.name != name});
151             return egCore.hatch.setItem('eg.workstation.all', keep)
152
153         }).then(function() { 
154
155             return service.get_default()
156
157         }).then(function(def) {
158             if (def == name) {
159                 console.debug('Removing default workstation: ' + name);
160                 return egCore.hatch.removeItem('eg.workstation.default');
161             }
162         });
163     }
164
165     return service;
166 }])
167
168
169 .controller('SplashCtrl',
170        ['$scope','$window','$location','egCore','egConfirmDialog',
171 function($scope , $window , $location , egCore , egConfirmDialog) {
172
173     egCore.hatch.getItem('eg.audio.disable').then(function(val) {
174         $scope.disable_sound = val;
175     });
176
177     egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
178         $scope.search_lib = egCore.org.get(val);
179     });
180     $scope.handle_search_lib_changed = function(org) {
181         egCore.hatch.setItem('eg.search.search_lib', org.id());
182     };
183
184     egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
185         $scope.pref_lib = egCore.org.get(val);
186     });
187     $scope.handle_pref_lib_changed = function(org) {
188         egCore.hatch.setItem('eg.search.pref_lib', org.id());
189     };
190
191     $scope.adv_pane = 'advanced'; // default value if not explicitly set
192     egCore.hatch.getItem('eg.search.adv_pane').then(function(val) {
193         $scope.adv_pane = val;
194     });
195     $scope.$watch('adv_pane', function(newVal, oldVal) {
196         if (typeof newVal != 'undefined' && newVal != oldVal) {
197             egCore.hatch.setItem('eg.search.adv_pane', newVal);
198         }
199     });
200
201     $scope.apply_sound = function() {
202         if ($scope.disable_sound) {
203             egCore.hatch.setItem('eg.audio.disable', true);
204         } else {
205             egCore.hatch.removeItem('eg.audio.disable');
206         }
207     }
208
209     $scope.test_audio = function(sound) {
210         egCore.audio.play(sound);
211     }
212
213 }])
214
215 .controller('PrintConfigCtrl',
216        ['$scope','egCore',
217 function($scope , egCore) {
218
219     $scope.printConfig = {};
220     $scope.setContext = function(ctx) { 
221         $scope.context = ctx; 
222         $scope.isTestView = false;
223     }
224     $scope.setContext('default');
225
226     $scope.setContentType = function(type) { $scope.contentType = type }
227     $scope.setContentType('text/plain');
228
229     $scope.useHatchPrinting = function() {
230         return egCore.hatch.usePrinting();
231     }
232
233     $scope.hatchIsOpen = function() {
234         return egCore.hatch.hatchAvailable;
235     }
236
237     $scope.getPrinterByAttr = function(attr, value) {
238         var printer;
239         angular.forEach($scope.printers, function(p) {
240             if (p[attr] == value) printer = p;
241         });
242         return printer;
243     }
244
245     $scope.resetPrinterSettings = function(context) {
246         $scope.printConfig[context] = {
247             context : context,
248             printer : $scope.defaultPrinter ? $scope.defaultPrinter.name : null,
249             autoMargins : true, 
250             allPages : true,
251             pageRanges : []
252         };
253     }
254
255     $scope.savePrinterSettings = function(context) {
256         return egCore.hatch.setPrintConfig(
257             context, $scope.printConfig[context]);
258     }
259
260     $scope.printerConfString = function() {
261         if ($scope.printConfigError) return $scope.printConfigError;
262         if (!$scope.printConfig) return;
263         if (!$scope.printConfig[$scope.context]) return;
264         return JSON.stringify(
265             $scope.printConfig[$scope.context], undefined, 2);
266     }
267
268     function loadPrinterOptions(name) {
269         egCore.hatch.getPrinterOptions(name).then(
270             function(options) {$scope.printerOptions = options});
271     }
272
273     $scope.setPrinter = function(name) {
274         $scope.printConfig[$scope.context].printer = name;
275         loadPrinterOptions(name);
276     }
277
278     $scope.testPrint = function(withDialog) {
279         if ($scope.contentType == 'text/plain') {
280             egCore.print.print({
281                 context : $scope.context, 
282                 content_type : $scope.contentType, 
283                 content : $scope.textPrintContent,
284                 show_dialog : withDialog
285             });
286         } else {
287             egCore.print.print({
288                 context : $scope.context,
289                 content_type : $scope.contentType, 
290                 content : $scope.htmlPrintContent, 
291                 scope : {
292                     value1 : 'Value One', 
293                     value2 : 'Value Two',
294                     date_value : '2015-02-04T14:04:34-0400'
295                 },
296                 show_dialog : withDialog
297             });
298         }
299     }
300
301     // Load startup data....
302     // Don't bother talking to Hatch if it's not there.
303     if (!egCore.hatch.hatchAvailable) return;
304
305     // fetch info on all remote printers
306     egCore.hatch.getPrinters()
307     .then(function(printers) { 
308         $scope.printers = printers;
309
310         var def = $scope.getPrinterByAttr('is-default', true);
311         if (!def && printers.length) def = printers[0];
312
313         if (def) {
314             $scope.defaultPrinter = def;
315             loadPrinterOptions(def.name);
316         }
317     }).then(function() {
318         angular.forEach(
319             ['default','receipt','label','mail','offline'],
320             function(ctx) {
321                 egCore.hatch.getPrintConfig(ctx).then(function(conf) {
322                     if (conf) {
323                         $scope.printConfig[ctx] = conf;
324                     } else {
325                         $scope.resetPrinterSettings(ctx);
326                     }
327                 });
328             }
329         );
330     });
331
332 }])
333
334 .controller('PrintTemplatesCtrl',
335        ['$scope','$q','egCore','ngToast',
336 function($scope , $q , egCore , ngToast) {
337
338     $scope.print = {
339         template_name : 'bills_current',
340         template_output : '',
341         template_context : 'default'
342     };
343
344     // print preview scope data
345     // TODO: consider moving the template-specific bits directly
346     // into the templates or storing template- specific script files
347     // alongside the templates.
348     // NOTE: A lot of this data can be shared across templates.
349     var seed_user = {
350         first_given_name : 'Slow',
351         second_given_name : 'Joe',
352         family_name : 'Jones',
353         card : {
354             barcode : '30393830393'
355         }
356     }
357     var seed_addr = {
358         street1 : '123 Apple Rd',
359         street2 : 'Suite B',
360         city : 'Anywhere',
361         state : 'XX',
362         country : 'US',
363         post_code : '12345'
364     }
365
366     var seed_record = {
367         title : 'Traveling Pants!!',
368         author : 'Jane Jones',
369         isbn : '1231312123'
370     };
371
372     var seed_copy = {
373         barcode : '33434322323',
374         call_number : {
375             label : '636.8 JON',
376             record : {
377                 simple_record : {
378                     'title' : 'Test Title'
379                 }
380             }
381         },
382         location : {
383             name : 'General Collection'
384         },
385         // flattened versions for item status template
386         // TODO - make this go away
387         'call_number.label' : '636.8 JON',
388         'call_number.record.simple_record.title' : 'Test Title',
389         'location.name' : 'General Colleciton'
390     }
391
392     var one_hold = {
393         behind_desk : 'f',
394         phone_notify : '111-222-3333',
395         sms_notify : '111-222-3333',
396         email_notify : 'user@example.org',
397         request_time : new Date().toISOString(),
398         hold_type : 'T'
399     }
400
401     var seed_transit = {
402         source : {
403             name : 'Library Y',
404             shortname : 'LY',
405             holds_address : seed_addr
406         },
407         dest : {
408             name : 'Library X',
409             shortname : 'LX',
410             holds_address : seed_addr
411         },
412         source_send_time : new Date().toISOString(),
413         target_copy : seed_copy
414     }
415
416     $scope.preview_scope = {
417         //bills
418         transactions : [
419             {
420                 id : 1,
421                 xact_start : new Date().toISOString(),
422                 summary : {
423                     xact_type : 'circulation',
424                     last_billing_type : 'Overdue materials',
425                     total_owed : 1.50,
426                     last_payment_note : 'Test Note 1',
427                     last_payment_type : 'cash_payment',
428                     total_paid : 0.50,
429                     balance_owed : 1.00
430                 }
431             }, {
432                 id : 2,
433                 xact_start : new Date().toISOString(),
434                 summary : {
435                     xact_type : 'circulation',
436                     last_billing_type : 'Overdue materials',
437                     total_owed : 2.50,
438                     last_payment_note : 'Test Note 2',
439                     last_payment_type : 'credit_payment',
440                     total_paid : 0.50,
441                     balance_owed : 2.00
442                 }
443             }
444         ],
445
446         copy : seed_copy,
447         copies : [ seed_copy ],
448
449         checkins : [
450             {
451                 due_date : new Date().toISOString(),
452                 target_copy : seed_copy,
453                 copy_barcode : seed_copy.barcode,
454                 call_number : seed_copy.call_number,
455                 title : seed_record.title
456             },
457         ],
458
459         circulations : [
460             {
461                 circ : {
462                     due_date : new Date().toISOString(),
463                 },
464                 copy : seed_copy,
465                 title : seed_record.title,
466                 author : seed_record.author
467             },
468         ],
469
470         in_house_uses : [
471             {
472                 num_uses : 3,
473                 copy : seed_copy,
474                 title : seed_record.title
475             }
476         ],
477
478         previous_balance : 8.45,
479         payment_total : 2.00,
480         payment_applied : 2.00,
481         new_balance : 6.45,
482         amount_voided : 0,
483         change_given : 0,
484         payment_type : 'cash_payment',
485         payment_note : 'Here is a payment note',
486         note : {
487             create_date : new Date().toISOString(), 
488             title : 'Test Note Title',
489             usr : seed_user,
490             value : 'This patron is super nice!'
491         },
492
493         transit : seed_transit,
494         transits : [ seed_transit ],
495         title : seed_record.title,
496         author : seed_record.author,
497         patron : seed_user,
498         address : seed_addr,
499         dest_location : egCore.idl.toHash(egCore.org.get(egCore.auth.user().ws_ou())),
500         dest_address : seed_addr,
501         hold : one_hold,
502         holds : [
503             {
504                 hold : one_hold, title : 'Some Title 1', author : 'Some Author 1',
505                 volume : { label : '646.4 SOM' }, copy : seed_copy,
506                 part : { label : 'v. 1' },
507                 patron_barcode : 'S52802662',
508                 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
509             },
510             {
511                 hold : one_hold, title : 'Some Title 2', author : 'Some Author 2',
512                 volume : { label : '646.4 SOM' }, copy : seed_copy,
513                 part : { label : 'v. 1' },
514                 patron_barcode : 'S52802662',
515                 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
516             },
517             {
518                 hold : one_hold, title : 'Some Title 3', author : 'Some Author 3',
519                 volume : { label : '646.4 SOM' }, copy : seed_copy,
520                 part : { label : 'v. 1' },
521                 patron_barcode : 'S52802662',
522                 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
523             }
524         ]
525     }
526
527     $scope.preview_scope.payments = [
528         {amount : 1.00, xact : $scope.preview_scope.transactions[0]}, 
529         {amount : 1.00, xact : $scope.preview_scope.transactions[1]}
530     ]
531     $scope.preview_scope.payments[0].xact.title = 'Hali Bote Azikaban de tao fan';
532     $scope.preview_scope.payments[0].xact.copy_barcode = '334343434';
533     $scope.preview_scope.payments[1].xact.title = seed_record.title;
534     $scope.preview_scope.payments[1].xact.copy_barcode = seed_copy.barcode;
535
536     // today, staff, current_location, etc.
537     egCore.print.fleshPrintScope($scope.preview_scope);
538
539     $scope.template_changed = function() {
540         $scope.print.load_failed = false;
541         egCore.print.getPrintTemplate($scope.print.template_name)
542         .then(
543             function(html) { 
544                 $scope.print.template_content = html;
545                 console.log('set template content');
546             },
547             function() {
548                 $scope.print.template_content = '';
549                 $scope.print.load_failed = true;
550             }
551         );
552         egCore.print.getPrintTemplateContext($scope.print.template_name)
553         .then(function(template_context) {
554             $scope.print.template_context = template_context;
555         });
556     }
557
558     $scope.save_locally = function() {
559         egCore.print.storePrintTemplate(
560             $scope.print.template_name,
561             $scope.print.template_content
562         );
563         egCore.print.storePrintTemplateContext(
564             $scope.print.template_name,
565             $scope.print.template_context
566         );
567     }
568
569     $scope.exportable_templates = function() {
570         var templates = {};
571         var contexts = {};
572         var deferred = $q.defer();
573         var promises = [];
574         egCore.hatch.getKeys('eg.print.template').then(function(keys) {
575             angular.forEach(keys, function(key) {
576                 if (key.match(/^eg\.print\.template\./)) {
577                     promises.push(egCore.hatch.getItem(key).then(function(value) {
578                         templates[key.replace('eg.print.template.', '')] = value;
579                     }));
580                 } else {
581                     promises.push(egCore.hatch.getItem(key).then(function(value) {
582                         contexts[key.replace('eg.print.template_context.', '')] = value;
583                     }));
584                 }
585             });
586             $q.all(promises).then(function() {
587                 if (Object.keys(templates).length) {
588                     deferred.resolve({
589                         templates: templates,
590                         contexts: contexts
591                     });
592                 } else {
593                     ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
594                     deferred.reject();
595                 }
596             });
597         });
598         return deferred.promise;
599     }
600
601     $scope.imported_print_templates = { data : '' };
602     $scope.$watch('imported_print_templates.data', function(newVal, oldVal) {
603         if (newVal && newVal != oldVal) {
604             try {
605                 var data = JSON.parse(newVal);
606                 angular.forEach(data.templates, function(template_content, template_name) {
607                     egCore.print.storePrintTemplate(template_name, template_content);
608                 });
609                 angular.forEach(data.contexts, function(template_context, template_name) {
610                     egCore.print.storePrintTemplateContext(template_name, template_context);
611                 });
612                 $scope.template_changed(); // refresh
613                 ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
614             } catch (E) {
615                 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_IMPORT);
616             }
617         }
618     });
619
620     $scope.template_changed(); // load the default
621 }])
622
623 // 
624 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
625     return function(scope, element, attrs) {
626         scope.$watch(
627             function(scope) {
628                 return scope.$eval(attrs.content);
629             },
630             function(value) {
631                 // create an isolate scope and copy the print context
632                 // data into the new scope.
633                 // TODO: see also print security concerns in egHatch
634                 var result = element.html(value);
635                 var context = scope.$eval(attrs.context);
636                 var print_scope = scope.$new(true);
637                 angular.forEach(context, function(val, key) {
638                     print_scope[key] = val;
639                 })
640                 $compile(element.contents())(print_scope);
641             }
642         );
643     };
644 }])
645
646 .controller('StoredPrefsCtrl',
647        ['$scope','$q','egCore','egConfirmDialog',
648 function($scope , $q , egCore , egConfirmDialog) {
649     console.log('StoredPrefsCtrl');
650
651     $scope.setContext = function(ctx) {
652         $scope.context = ctx;
653     }
654     $scope.setContext('local');
655
656     // grab the edit perm
657     $scope.userHasDeletePerm = false;
658     egCore.perm.hasPermHere('ADMIN_WORKSTATION')
659     .then(function(bool) { $scope.userHasDeletePerm = bool });
660
661     // fetch the keys
662
663     function refreshKeys() {
664         $scope.keys = {local : [], remote : []};
665
666         if (egCore.hatch.hatchAvailable) {
667             egCore.hatch.getRemoteKeys().then(
668                 function(keys) { $scope.keys.remote = keys.sort() })
669         }
670     
671         // local calls are non-async
672         $scope.keys.local = egCore.hatch.getLocalKeys();
673     }
674     refreshKeys();
675
676     $scope.selectKey = function(key) {
677         $scope.currentKey = key;
678         $scope.currentKeyContent = null;
679
680         if ($scope.context == 'local') {
681             $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
682         } else {
683             egCore.hatch.getRemoteItem(key)
684             .then(function(content) {
685                 $scope.currentKeyContent = content
686             });
687         }
688     }
689
690     $scope.getCurrentKeyContent = function() {
691         return JSON.stringify($scope.currentKeyContent, null, 2);
692     }
693
694     $scope.removeKey = function(key) {
695         egConfirmDialog.open(
696             egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
697             {   deleteKey : key,
698                 ok : function() {
699                     if ($scope.context == 'local') {
700                         egCore.hatch.removeLocalItem(key);
701                         refreshKeys();
702                     } else {
703                         egCore.hatch.removeItem(key)
704                         .then(function() { refreshKeys() });
705                     }
706                 },
707                 cancel : function() {} // user canceled, nothing to do
708             }
709         );
710     }
711 }])
712
713 .controller('WSRegCtrl',
714        ['$scope','$q','$window','$location','egCore','egAlertDialog','workstationSvc',
715 function($scope , $q , $window , $location , egCore , egAlertDialog , workstationSvc) {
716
717     var all_workstations = [];
718     var reg_perm_orgs = [];
719
720     $scope.page_loaded = false;
721     $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
722     $scope.wsOrgChanged = function(org) { $scope.contextOrg = org; }
723
724     console.log('set context org to ' + $scope.contextOrg);
725
726     // fetch workstation reg perms
727     egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
728     .then(function(orgList) { 
729         reg_perm_orgs = orgList;
730
731         // hide orgs in the context org selector where this login
732         // does not have the reg_ws perm or the org can't have users
733         $scope.wsOrgHidden = function(id) {
734             return reg_perm_orgs.indexOf(id) == -1
735                 || $scope.cant_have_users(id);
736         }
737
738     // fetch the locally stored workstation data
739     }).then(function() {
740         return workstationSvc.get_all()
741         
742     }).then(function(all) {
743         all_workstations = all || [];
744         $scope.workstations = 
745             all_workstations.map(function(w) { return w.name });
746         return workstationSvc.get_default()
747
748     // fetch the default workstation
749     }).then(function(def) { 
750         $scope.defaultWS = def;
751         $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
752
753     // Handle any URL commands.
754     }).then(function() {
755         var remove = $location.search().remove;
756          if (remove) {
757             console.log('Removing WS via URL request: ' + remove);
758             return $scope.remove_ws(remove).then(
759                 function() { $scope.page_loaded = true; });
760         }
761         $scope.page_loaded = true;
762     });
763
764     $scope.get_ws_label = function(ws) {
765         return ws == $scope.defaultWS ? 
766             egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
767     }
768
769     $scope.set_default_ws = function(name) {
770         delete $scope.removing_ws;
771         $scope.defaultWS = name;
772         workstationSvc.set_default(name);
773     }
774
775     $scope.cant_have_users = 
776         function (id) { return !egCore.org.CanHaveUsers(id); };
777     $scope.cant_have_volumes = 
778         function (id) { return !egCore.org.CanHaveVolumes(id); };
779
780     // Log out and return to login page with selected WS 
781     $scope.use_now = function() {
782         egCore.auth.logout();
783         $window.location.href = $location
784             .path('/login')
785             .search({ws : $scope.selectedWS})
786             .absUrl();
787     }
788
789     $scope.can_delete_ws = function(name) {
790         var ws = all_workstations.filter(
791             function(ws) { return ws.name == name })[0];
792         return ws && reg_perm_orgs.indexOf(ws.owning_lib) != -1;
793     }
794
795     $scope.remove_ws = function(remove_me) {
796         $scope.removing_ws = remove_me;
797
798         // Perm is used to disable Remove button in UI, but have to check
799         // again here in case we're removing a WS based on URL params.
800         if (!$scope.can_delete_ws(remove_me)) return $q.when();
801
802         $scope.is_removing = true;
803         return workstationSvc.remove_workstation(remove_me)
804         .then(function() {
805
806             all_workstations = all_workstations.filter(
807                 function(ws) { return ws.name != remove_me });
808
809             $scope.workstations = $scope.workstations.filter(
810                 function(ws) { return ws != remove_me });
811
812             if ($scope.selectedWS == remove_me) 
813                 $scope.selectedWS = $scope.workstations[0];
814
815             if ($scope.defaultWS == remove_me) 
816                 $scope.defaultWS = '';
817
818             $scope.is_removing = false;
819         });
820     }
821
822     $scope.register_ws = function() {
823         delete $scope.removing_ws;
824
825         var full_name = 
826             $scope.contextOrg.shortname() + '-' + $scope.newWSName;
827
828         if ($scope.workstations.indexOf(full_name) > -1) {
829             // avoid duplicate local registrations
830             return egAlertDialog.open(egCore.strings.WS_USED);
831         }
832
833         $scope.is_registering = true;
834         workstationSvc.register_workstation(
835             $scope.newWSName, full_name,
836             $scope.contextOrg.id()
837
838         ).then(function(new_ws) {
839             $scope.workstations.push(new_ws.name);
840             all_workstations.push(new_ws);  
841             $scope.is_registering = false;
842
843             if (!$scope.selectedWS) {
844                 $scope.selectedWS = new_ws.name;
845             }
846             if (!$scope.defaultWS) {
847                 return $scope.set_default_ws(new_ws.name);
848             }
849             $scope.newWSName = '';
850         });
851     }
852 }])
853
854 .controller('HatchCtrl',
855        ['$scope','egCore','ngToast',
856 function($scope , egCore , ngToast) {
857     var hatch = egCore.hatch;  // convenience
858
859     $scope.hatch_available = hatch.hatchAvailable;
860     $scope.hatch_printing = hatch.usePrinting();
861     $scope.hatch_settings = hatch.useSettings();
862     $scope.hatch_offline  = hatch.useOffline();
863
864     // Apply Hatch settings as changes occur in the UI.
865     
866     $scope.$watch('hatch_printing', function(newval) {
867         if (typeof newval != 'boolean') return;
868         hatch.setLocalItem('eg.hatch.enable.printing', newval);
869     });
870
871     $scope.$watch('hatch_settings', function(newval) {
872         if (typeof newval != 'boolean') return;
873         hatch.setLocalItem('eg.hatch.enable.settings', newval);
874     });
875
876     $scope.$watch('hatch_offline', function(newval) {
877         if (typeof newval != 'boolean') return;
878         hatch.setLocalItem('eg.hatch.enable.offline', newval);
879     });
880
881     $scope.copy_to_hatch = function() {
882         hatch.copySettingsToHatch().then(
883             function() {
884                 ngToast.create(egCore.strings.HATCH_SETTINGS_MIGRATION_SUCCESS)},
885             function() {
886                 ngToast.warning(egCore.strings.HATCH_SETTINGS_MIGRATION_FAILURE)}
887         );
888     }
889
890     $scope.copy_to_local = function() {
891         hatch.copySettingsToLocal().then(
892             function() {
893                 ngToast.create(egCore.strings.HATCH_SETTINGS_MIGRATION_SUCCESS)},
894             function() {
895                 ngToast.warning(egCore.strings.HATCH_SETTINGS_MIGRATION_FAILURE)}
896         );
897     }
898
899 }])
900
901