2 * App to drive the base page.
7 angular.module('egWorkstationAdmin',
8 ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod'])
10 .config(['$routeProvider','$locationProvider','$compileProvider',
11 function($routeProvider , $locationProvider , $compileProvider) {
13 $locationProvider.html5Mode(true);
14 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/);
15 var resolver = {delay : function(egStartup) {return egStartup.go()}};
17 $routeProvider.when('/admin/workstation/workstations', {
18 templateUrl: './admin/workstation/t_workstations',
19 controller: 'WSRegCtrl',
23 $routeProvider.when('/admin/workstation/print/config', {
24 templateUrl: './admin/workstation/t_print_config',
25 controller: 'PrintConfigCtrl',
29 $routeProvider.when('/admin/workstation/print/templates', {
30 templateUrl: './admin/workstation/t_print_templates',
31 controller: 'PrintTemplatesCtrl',
35 $routeProvider.when('/admin/workstation/stored_prefs', {
36 templateUrl: './admin/workstation/t_stored_prefs',
37 controller: 'StoredPrefsCtrl',
43 $routeProvider.otherwise({
44 templateUrl : './admin/workstation/t_splash',
45 controller : 'SplashCtrl',
50 .config(['ngToastProvider', function(ngToastProvider) {
51 ngToastProvider.configure({
52 verticalPosition: 'bottom',
57 .factory('workstationSvc',
58 ['$q','$timeout','$location','egCore','egConfirmDialog',
59 function($q , $timeout , $location , egCore , egConfirmDialog) {
63 service.get_all = function() {
64 return egCore.hatch.getItem('eg.workstation.all')
65 .then(function(all) { return all || [] });
68 service.get_default = function() {
69 return egCore.hatch.getItem('eg.workstation.default');
72 service.set_default = function(name) {
73 return egCore.hatch.setItem('eg.workstation.default', name);
76 service.register_workstation = function(base_name, name, org_id) {
77 return service.register_ws_api(base_name, name, org_id)
78 .then(function(ws_id) {
79 return service.track_new_ws(ws_id, name, org_id);
83 service.register_ws_api =
84 function(base_name, name, org_id, override, deferred) {
85 if (!deferred) deferred = $q.defer();
87 var method = 'open-ils.actor.workstation.register';
88 if (override) method += '.override';
91 'open-ils.actor', method, egCore.auth.token(), name, org_id)
93 .then(function(resp) {
95 if (evt = egCore.evt.parse(resp)) {
96 console.log('register returned ' + evt.toString());
98 if (evt.textcode == 'WORKSTATION_NAME_EXISTS' && !override) {
100 egConfirmDialog.open(
101 egCore.strings.WS_EXISTS, base_name, {
103 service.register_ws_api(
104 base_name, name, org_id, true, deferred)
106 cancel : function() {deferred.reject()}
111 alert(evt.toString());
115 console.log('Resolving register promise with: ' + resp);
116 deferred.resolve(resp);
120 return deferred.promise;
123 service.track_new_ws = function(ws_id, ws_name, owning_lib) {
124 console.log('Tracking newly created WS with ID ' + ws_id);
125 var new_ws = {id : ws_id, name : ws_name, owning_lib : owning_lib};
127 return service.get_all()
128 .then(function(all) {
130 return egCore.hatch.setItem('eg.workstation.all', all)
131 .then(function() { return new_ws });
135 // Remove all traces of the workstation locally.
136 // This does not remove the WS from the server.
137 service.remove_workstation = function(name) {
138 console.debug('Removing workstation: ' + name);
140 return egCore.hatch.getItem('eg.workstation.all')
142 // remove from list of all workstations
143 .then(function(all) {
145 var keep = all.filter(function(ws) {return ws.name != name});
146 return egCore.hatch.setItem('eg.workstation.all', keep)
150 return service.get_default()
152 }).then(function(def) {
154 console.debug('Removing default workstation: ' + name);
155 return egCore.hatch.removeItem('eg.workstation.default');
164 .controller('SplashCtrl',
165 ['$scope','$window','$location','egCore','egConfirmDialog',
166 function($scope , $window , $location , egCore , egConfirmDialog) {
168 // ---------------------
170 $scope.hatchURL = egCore.hatch.hatchURL();
171 $scope.hatchRequired =
172 egCore.hatch.getLocalItem('eg.hatch.required');
174 $scope.updateHatchRequired = function() {
175 egCore.hatch.setLocalItem(
176 'eg.hatch.required', $scope.hatchRequired);
179 $scope.updateHatchURL = function() {
180 egCore.hatch.setLocalItem(
181 'eg.hatch.url', $scope.hatchURL);
184 egCore.hatch.getItem('eg.audio.disable').then(function(val) {
185 $scope.disable_sound = val;
188 egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
189 $scope.search_lib = egCore.org.get(val);
191 $scope.handle_search_lib_changed = function(org) {
192 egCore.hatch.setItem('eg.search.search_lib', org.id());
195 egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
196 $scope.pref_lib = egCore.org.get(val);
198 $scope.handle_pref_lib_changed = function(org) {
199 egCore.hatch.setItem('eg.search.pref_lib', org.id());
202 $scope.adv_pane = 'advanced'; // default value if not explicitly set
203 egCore.hatch.getItem('eg.search.adv_pane').then(function(val) {
204 $scope.adv_pane = val;
206 $scope.$watch('adv_pane', function(newVal, oldVal) {
207 if (newVal != oldVal) {
208 egCore.hatch.setItem('eg.search.adv_pane', newVal);
212 $scope.apply_sound = function() {
213 if ($scope.disable_sound) {
214 egCore.hatch.setItem('eg.audio.disable', true);
216 egCore.hatch.removeItem('eg.audio.disable');
220 $scope.test_audio = function(sound) {
221 egCore.audio.play(sound);
226 .controller('PrintConfigCtrl',
228 function($scope , egCore) {
229 console.log('PrintConfigCtrl');
231 $scope.actionPending = false;
232 $scope.isTestView = false;
234 $scope.setContext = function(ctx) {
235 $scope.context = ctx;
236 $scope.isTestView = false;
237 $scope.actionPending = false;
239 $scope.setContext('default');
241 $scope.getPrinterByAttr = function(attr, value) {
243 angular.forEach($scope.printers, function(p) {
244 if (p[attr] == value) printer = p;
249 $scope.currentPrinter = function() {
250 if ($scope.printConfig && $scope.printConfig[$scope.context]) {
251 return $scope.getPrinterByAttr(
252 'name', $scope.printConfig[$scope.context].printer
257 // fetch info on all remote printers
258 egCore.hatch.getPrinters()
259 .then(function(printers) {
260 $scope.printers = printers;
261 $scope.defaultPrinter =
262 $scope.getPrinterByAttr('is-default', true);
264 .then(function() { return egCore.hatch.getPrintConfig() })
265 .then(function(config) {
266 $scope.printConfig = config;
269 if ($scope.defaultPrinter) {
270 pname = $scope.defaultPrinter.name;
272 } else if ($scope.printers.length == 1) {
273 // if the OS does not report a default printer, but only
274 // one printer is available, treat it as the default.
275 pname = $scope.printers[0].name;
278 // apply the default printer to every context which has
279 // no printer configured.
281 ['default','receipt','label','mail','offline'],
283 if (!$scope.printConfig[ctx]) {
284 $scope.printConfig[ctx] = {
293 $scope.printerConfString = function() {
294 if ($scope.printConfigError) return $scope.printConfigError;
295 if (!$scope.printConfig) return;
296 if (!$scope.printConfig[$scope.context]) return;
297 return JSON.stringify(
298 $scope.printConfig[$scope.context], undefined, 2);
301 $scope.resetConfig = function() {
302 $scope.actionPending = true;
303 $scope.printConfigError = null;
304 $scope.printConfig[$scope.context] = {
305 context : $scope.context
308 if ($scope.defaultPrinter) {
309 $scope.printConfig[$scope.context].printer =
310 $scope.defaultPrinter.name;
313 egCore.hatch.setPrintConfig($scope.printConfig)
314 .finally(function() {$scope.actionPending = false});
317 $scope.configurePrinter = function() {
318 $scope.printConfigError = null;
319 $scope.actionPending = true;
320 egCore.hatch.configurePrinter(
322 $scope.printConfig[$scope.context].printer
325 function(config) {$scope.printConfig = config},
326 function(error) {$scope.printConfigError = error}
328 .finally(function() {$scope.actionPending = false});
331 $scope.setPrinter = function(name) {
332 $scope.printConfig[$scope.context].printer = name;
336 $scope.setContentType = function(type) { $scope.contentType = type }
338 $scope.testPrint = function(withDialog) {
339 if ($scope.contentType == 'text/plain') {
341 context : $scope.context,
342 content_type : $scope.contentType,
343 content : $scope.textPrintContent,
344 show_dialog : withDialog
348 context : $scope.context,
349 content_type : $scope.contentType,
350 content : $scope.htmlPrintContent,
352 value1 : 'Value One',
353 value2 : 'Value Two',
354 date_value : '2015-02-04T14:04:34-0400'
356 show_dialog : withDialog
361 $scope.setContentType('text/plain');
365 .controller('PrintTemplatesCtrl',
366 ['$scope','$q','egCore','ngToast',
367 function($scope , $q , egCore , ngToast) {
370 template_name : 'bills_current',
371 template_output : '',
372 template_context : 'default'
375 // print preview scope data
376 // TODO: consider moving the template-specific bits directly
377 // into the templates or storing template- specific script files
378 // alongside the templates.
379 // NOTE: A lot of this data can be shared across templates.
381 first_given_name : 'Slow',
382 second_given_name : 'Joe',
383 family_name : 'Jones',
385 barcode : '30393830393'
389 street1 : '123 Apple Rd',
398 title : 'Traveling Pants!!',
399 author : 'Jane Jones',
404 barcode : '33434322323',
409 'title' : 'Test Title'
414 name : 'General Collection'
416 // flattened versions for item status template
417 // TODO - make this go away
418 'call_number.label' : '636.8 JON',
419 'call_number.record.simple_record.title' : 'Test Title',
420 'location.name' : 'General Colleciton'
425 phone_notify : '111-222-3333',
426 sms_notify : '111-222-3333',
427 email_notify : 'user@example.org',
428 request_time : new Date().toISOString(),
436 holds_address : seed_addr
441 holds_address : seed_addr
443 source_send_time : new Date().toISOString(),
444 target_copy : seed_copy
447 $scope.preview_scope = {
452 xact_start : new Date().toISOString(),
454 xact_type : 'circulation',
455 last_billing_type : 'Overdue materials',
457 last_payment_note : 'Test Note 1',
458 last_payment_type : 'cash_payment',
464 xact_start : new Date().toISOString(),
466 xact_type : 'circulation',
467 last_billing_type : 'Overdue materials',
469 last_payment_note : 'Test Note 2',
470 last_payment_type : 'credit_payment',
478 copies : [ seed_copy ],
482 due_date : new Date().toISOString(),
483 target_copy : seed_copy,
484 copy_barcode : seed_copy.barcode,
485 call_number : seed_copy.call_number,
486 title : seed_record.title
493 due_date : new Date().toISOString(),
496 title : seed_record.title,
497 author : seed_record.author
505 title : seed_record.title
509 previous_balance : 8.45,
510 payment_total : 2.00,
511 payment_applied : 2.00,
515 payment_type : 'cash_payment',
516 payment_note : 'Here is a payment note',
518 create_date : new Date().toISOString(),
519 title : 'Test Note Title',
521 value : 'This patron is super nice!'
524 transit : seed_transit,
525 transits : [ seed_transit ],
526 title : seed_record.title,
527 author : seed_record.author,
530 dest_location : egCore.idl.toHash(egCore.org.get(egCore.auth.user().ws_ou())),
531 dest_address : seed_addr,
535 hold : one_hold, title : 'Some Title 1', author : 'Some Author 1',
536 volume : { label : '646.4 SOM' }, copy : seed_copy,
537 part : { label : 'v. 1' },
538 patron_barcode : 'S52802662',
539 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
542 hold : one_hold, title : 'Some Title 2', author : 'Some Author 2',
543 volume : { label : '646.4 SOM' }, copy : seed_copy,
544 part : { label : 'v. 1' },
545 patron_barcode : 'S52802662',
546 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
549 hold : one_hold, title : 'Some Title 3', author : 'Some Author 3',
550 volume : { label : '646.4 SOM' }, copy : seed_copy,
551 part : { label : 'v. 1' },
552 patron_barcode : 'S52802662',
553 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
558 $scope.preview_scope.payments = [
559 {amount : 1.00, xact : $scope.preview_scope.transactions[0]},
560 {amount : 1.00, xact : $scope.preview_scope.transactions[1]}
562 $scope.preview_scope.payments[0].xact.title = 'Hali Bote Azikaban de tao fan';
563 $scope.preview_scope.payments[0].xact.copy_barcode = '334343434';
564 $scope.preview_scope.payments[1].xact.title = seed_record.title;
565 $scope.preview_scope.payments[1].xact.copy_barcode = seed_copy.barcode;
567 // today, staff, current_location, etc.
568 egCore.print.fleshPrintScope($scope.preview_scope);
570 $scope.template_changed = function() {
571 $scope.print.load_failed = false;
572 egCore.print.getPrintTemplate($scope.print.template_name)
575 $scope.print.template_content = html;
576 console.log('set template content');
579 $scope.print.template_content = '';
580 $scope.print.load_failed = true;
583 egCore.print.getPrintTemplateContext($scope.print.template_name)
584 .then(function(template_context) {
585 $scope.print.template_context = template_context;
589 $scope.save_locally = function() {
590 egCore.print.storePrintTemplate(
591 $scope.print.template_name,
592 $scope.print.template_content
594 egCore.print.storePrintTemplateContext(
595 $scope.print.template_name,
596 $scope.print.template_context
600 $scope.exportable_templates = function() {
603 var deferred = $q.defer();
605 egCore.hatch.getKeys('eg.print.template').then(function(keys) {
606 angular.forEach(keys, function(key) {
607 if (key.match(/^eg\.print\.template\./)) {
608 promises.push(egCore.hatch.getItem(key).then(function(value) {
609 templates[key.replace('eg.print.template.', '')] = value;
612 promises.push(egCore.hatch.getItem(key).then(function(value) {
613 contexts[key.replace('eg.print.template_context.', '')] = value;
617 $q.all(promises).then(function() {
618 if (Object.keys(templates).length) {
620 templates: templates,
624 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
629 return deferred.promise;
632 $scope.imported_print_templates = { data : '' };
633 $scope.$watch('imported_print_templates.data', function(newVal, oldVal) {
634 if (newVal && newVal != oldVal) {
636 var data = JSON.parse(newVal);
637 angular.forEach(data.templates, function(template_content, template_name) {
638 egCore.print.storePrintTemplate(template_name, template_content);
640 angular.forEach(data.contexts, function(template_context, template_name) {
641 egCore.print.storePrintTemplateContext(template_name, template_context);
643 $scope.template_changed(); // refresh
644 ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
646 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_IMPORT);
651 $scope.template_changed(); // load the default
655 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
656 return function(scope, element, attrs) {
659 return scope.$eval(attrs.content);
662 // create an isolate scope and copy the print context
663 // data into the new scope.
664 // TODO: see also print security concerns in egHatch
665 var result = element.html(value);
666 var context = scope.$eval(attrs.context);
667 var print_scope = scope.$new(true);
668 angular.forEach(context, function(val, key) {
669 print_scope[key] = val;
671 $compile(element.contents())(print_scope);
677 .controller('StoredPrefsCtrl',
678 ['$scope','$q','egCore','egConfirmDialog',
679 function($scope , $q , egCore , egConfirmDialog) {
680 console.log('StoredPrefsCtrl');
682 $scope.setContext = function(ctx) {
683 $scope.context = ctx;
685 $scope.setContext('local');
687 // grab the edit perm
688 $scope.userHasDeletePerm = false;
689 egCore.perm.hasPermHere('ADMIN_WORKSTATION')
690 .then(function(bool) { $scope.userHasDeletePerm = bool });
694 function refreshKeys() {
695 $scope.keys = {local : [], remote : []};
697 egCore.hatch.getRemoteKeys().then(
698 function(keys) { $scope.keys.remote = keys.sort() })
700 // local calls are non-async
701 $scope.keys.local = egCore.hatch.getLocalKeys();
705 $scope.selectKey = function(key) {
706 $scope.currentKey = key;
707 $scope.currentKeyContent = null;
709 if ($scope.context == 'local') {
710 $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
712 egCore.hatch.getRemoteItem(key)
713 .then(function(content) {
714 $scope.currentKeyContent = content
719 $scope.getCurrentKeyContent = function() {
720 return JSON.stringify($scope.currentKeyContent, null, 2);
723 $scope.removeKey = function(key) {
724 egConfirmDialog.open(
725 egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
728 if ($scope.context == 'local') {
729 egCore.hatch.removeLocalItem(key);
732 egCore.hatch.removeItem(key)
733 .then(function() { refreshKeys() });
736 cancel : function() {} // user canceled, nothing to do
742 .controller('WSRegCtrl',
743 ['$scope','$q','$window','$location','egCore','egAlertDialog','workstationSvc',
744 function($scope , $q , $window , $location , egCore , egAlertDialog , workstationSvc) {
746 var all_workstations = [];
747 var reg_perm_orgs = [];
749 $scope.page_loaded = false;
750 $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
751 $scope.wsOrgChanged = function(org) { $scope.contextOrg = org; }
753 console.log('set context org to ' + $scope.contextOrg);
755 // fetch workstation reg perms
756 egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
757 .then(function(orgList) {
758 reg_perm_orgs = orgList;
760 // hide orgs in the context org selector where this login
761 // does not have the reg_ws perm
762 $scope.wsOrgHidden = function(id) {
763 return reg_perm_orgs.indexOf(id) == -1;
766 // fetch the locally stored workstation data
768 return workstationSvc.get_all()
770 }).then(function(all) {
771 all_workstations = all || [];
772 $scope.workstations =
773 all_workstations.map(function(w) { return w.name });
774 return workstationSvc.get_default()
776 // fetch the default workstation
777 }).then(function(def) {
778 $scope.defaultWS = def;
779 $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
781 // Handle any URL commands.
783 var remove = $location.search().remove;
785 console.log('Removing WS via URL request: ' + remove);
786 return $scope.remove_ws(remove).then(
787 function() { $scope.page_loaded = true; });
789 $scope.page_loaded = true;
792 $scope.get_ws_label = function(ws) {
793 return ws == $scope.defaultWS ?
794 egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
797 $scope.set_default_ws = function(name) {
798 delete $scope.removing_ws;
799 $scope.defaultWS = name;
800 workstationSvc.set_default(name);
803 $scope.cant_have_users =
804 function (id) { return !egCore.org.CanHaveUsers(id); };
805 $scope.cant_have_volumes =
806 function (id) { return !egCore.org.CanHaveVolumes(id); };
808 // Log out and return to login page with selected WS
809 $scope.use_now = function() {
810 egCore.auth.logout();
811 $window.location.href = $location
813 .search({ws : $scope.selectedWS})
817 $scope.can_delete_ws = function(name) {
818 var ws = all_workstations.filter(
819 function(ws) { return ws.name == name })[0];
820 return ws && reg_perm_orgs.indexOf(ws.owning_lib);
823 $scope.remove_ws = function(remove_me) {
824 $scope.removing_ws = remove_me;
826 // Perm is used to disable Remove button in UI, but have to check
827 // again here in case we're removing a WS based on URL params.
828 if (!$scope.can_delete_ws(remove_me)) return $q.when();
830 $scope.is_removing = true;
831 return workstationSvc.remove_workstation(remove_me)
834 all_workstations = all_workstations.filter(
835 function(ws) { return ws.name != remove_me });
837 $scope.workstations = $scope.workstations.filter(
838 function(ws) { return ws != remove_me });
840 if ($scope.selectedWS == remove_me)
841 $scope.selectedWS = $scope.workstations[0];
843 if ($scope.defaultWS == remove_me)
844 $scope.defaultWS = '';
846 $scope.is_removing = false;
850 $scope.register_ws = function() {
851 delete $scope.removing_ws;
854 $scope.contextOrg.shortname() + '-' + $scope.newWSName;
856 if ($scope.workstations.indexOf(full_name) > -1) {
857 // avoid duplicate local registrations
858 return egAlertDialog.open(egCore.strings.WS_USED);
861 $scope.is_registering = true;
862 workstationSvc.register_workstation(
863 $scope.newWSName, full_name,
864 $scope.contextOrg.id()
866 ).then(function(new_ws) {
867 $scope.workstations.push(new_ws.name);
868 all_workstations.push(new_ws);
869 $scope.is_registering = false;
871 if (!$scope.selectedWS) {
872 $scope.selectedWS = new_ws.name;
874 if (!$scope.defaultWS) {
875 return $scope.set_default_ws(new_ws.name);
877 $scope.newWSName = '';