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',
41 $routeProvider.when('/admin/workstation/hatch', {
42 templateUrl: './admin/workstation/t_hatch',
43 controller: 'HatchCtrl',
48 $routeProvider.otherwise({
49 templateUrl : './admin/workstation/t_splash',
50 controller : 'SplashCtrl',
55 .config(['ngToastProvider', function(ngToastProvider) {
56 ngToastProvider.configure({
57 verticalPosition: 'bottom',
62 .factory('workstationSvc',
63 ['$q','$timeout','$location','egCore','egConfirmDialog',
64 function($q , $timeout , $location , egCore , egConfirmDialog) {
68 service.get_all = function() {
69 return egCore.hatch.getItem('eg.workstation.all')
70 .then(function(all) { return all || [] });
73 service.get_default = function() {
74 return egCore.hatch.getItem('eg.workstation.default');
77 service.set_default = function(name) {
78 return egCore.hatch.setItem('eg.workstation.default', name);
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);
88 service.register_ws_api =
89 function(base_name, name, org_id, override, deferred) {
90 if (!deferred) deferred = $q.defer();
92 var method = 'open-ils.actor.workstation.register';
93 if (override) method += '.override';
96 'open-ils.actor', method, egCore.auth.token(), name, org_id)
98 .then(function(resp) {
100 if (evt = egCore.evt.parse(resp)) {
101 console.log('register returned ' + evt.toString());
103 if (evt.textcode == 'WORKSTATION_NAME_EXISTS' && !override) {
105 egConfirmDialog.open(
106 egCore.strings.WS_EXISTS, base_name, {
108 service.register_ws_api(
109 base_name, name, org_id, true, deferred)
111 cancel : function() {deferred.reject()}
116 alert(evt.toString());
120 console.log('Resolving register promise with: ' + resp);
121 deferred.resolve(resp);
125 return deferred.promise;
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};
132 return service.get_all()
133 .then(function(all) {
135 return egCore.hatch.setItem('eg.workstation.all', all)
136 .then(function() { return new_ws });
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);
145 return egCore.hatch.getItem('eg.workstation.all')
147 // remove from list of all workstations
148 .then(function(all) {
150 var keep = all.filter(function(ws) {return ws.name != name});
151 return egCore.hatch.setItem('eg.workstation.all', keep)
155 return service.get_default()
157 }).then(function(def) {
159 console.debug('Removing default workstation: ' + name);
160 return egCore.hatch.removeItem('eg.workstation.default');
169 .controller('SplashCtrl',
170 ['$scope','$window','$location','egCore','egConfirmDialog',
171 function($scope , $window , $location , egCore , egConfirmDialog) {
173 egCore.hatch.getItem('eg.audio.disable').then(function(val) {
174 $scope.disable_sound = val;
177 egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
178 $scope.search_lib = egCore.org.get(val);
180 $scope.handle_search_lib_changed = function(org) {
181 egCore.hatch.setItem('eg.search.search_lib', org.id());
184 egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
185 $scope.pref_lib = egCore.org.get(val);
187 $scope.handle_pref_lib_changed = function(org) {
188 egCore.hatch.setItem('eg.search.pref_lib', org.id());
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;
195 $scope.$watch('adv_pane', function(newVal, oldVal) {
196 if (typeof newVal != 'undefined' && newVal != oldVal) {
197 egCore.hatch.setItem('eg.search.adv_pane', newVal);
201 $scope.apply_sound = function() {
202 if ($scope.disable_sound) {
203 egCore.hatch.setItem('eg.audio.disable', true);
205 egCore.hatch.removeItem('eg.audio.disable');
209 $scope.test_audio = function(sound) {
210 egCore.audio.play(sound);
215 .controller('PrintConfigCtrl',
217 function($scope , egCore) {
219 $scope.printConfig = {};
220 $scope.setContext = function(ctx) {
221 $scope.context = ctx;
222 $scope.isTestView = false;
224 $scope.setContext('default');
226 $scope.useHatchPrinting = function() {
227 return egCore.hatch.usePrinting();
230 $scope.hatchIsOpen = function() {
231 return egCore.hatch.hatchAvailable;
234 $scope.getPrinterByAttr = function(attr, value) {
236 angular.forEach($scope.printers, function(p) {
237 if (p[attr] == value) printer = p;
242 // fetch info on all remote printers
243 egCore.hatch.getPrinters()
244 .then(function(printers) {
245 $scope.printers = printers;
247 var def = $scope.getPrinterByAttr('is-default', true);
248 if (!def && printers.length) def = printers[0];
251 $scope.defaultPrinter = def;
252 loadPrinterOptions(def.name);
256 ['default','receipt','label','mail','offline'],
258 egCore.hatch.getPrintConfig(ctx).then(function(conf) {
260 $scope.printConfig[ctx] = conf;
262 $scope.resetPrinterSettings(ctx);
269 $scope.resetPrinterSettings = function(context) {
270 $scope.printConfig[context] = {
272 printer : $scope.defaultPrinter ? $scope.defaultPrinter.name : null,
279 $scope.savePrinterSettings = function(context) {
280 return egCore.hatch.setPrintConfig(
281 context, $scope.printConfig[context]);
284 $scope.printerConfString = function() {
285 if ($scope.printConfigError) return $scope.printConfigError;
286 if (!$scope.printConfig) return;
287 if (!$scope.printConfig[$scope.context]) return;
288 return JSON.stringify(
289 $scope.printConfig[$scope.context], undefined, 2);
292 function loadPrinterOptions(name) {
293 egCore.hatch.getPrinterOptions(name).then(
294 function(options) {$scope.printerOptions = options});
297 $scope.setPrinter = function(name) {
298 $scope.printConfig[$scope.context].printer = name;
299 loadPrinterOptions(name);
303 $scope.setContentType = function(type) { $scope.contentType = type }
305 $scope.testPrint = function(withDialog) {
306 if ($scope.contentType == 'text/plain') {
308 context : $scope.context,
309 content_type : $scope.contentType,
310 content : $scope.textPrintContent,
311 show_dialog : withDialog
315 context : $scope.context,
316 content_type : $scope.contentType,
317 content : $scope.htmlPrintContent,
319 value1 : 'Value One',
320 value2 : 'Value Two',
321 date_value : '2015-02-04T14:04:34-0400'
323 show_dialog : withDialog
328 $scope.setContentType('text/plain');
332 .controller('PrintTemplatesCtrl',
333 ['$scope','$q','egCore','ngToast',
334 function($scope , $q , egCore , ngToast) {
337 template_name : 'bills_current',
338 template_output : '',
339 template_context : 'default'
342 // print preview scope data
343 // TODO: consider moving the template-specific bits directly
344 // into the templates or storing template- specific script files
345 // alongside the templates.
346 // NOTE: A lot of this data can be shared across templates.
348 first_given_name : 'Slow',
349 second_given_name : 'Joe',
350 family_name : 'Jones',
352 barcode : '30393830393'
356 street1 : '123 Apple Rd',
365 title : 'Traveling Pants!!',
366 author : 'Jane Jones',
371 barcode : '33434322323',
376 'title' : 'Test Title'
381 name : 'General Collection'
383 // flattened versions for item status template
384 // TODO - make this go away
385 'call_number.label' : '636.8 JON',
386 'call_number.record.simple_record.title' : 'Test Title',
387 'location.name' : 'General Colleciton'
392 phone_notify : '111-222-3333',
393 sms_notify : '111-222-3333',
394 email_notify : 'user@example.org',
395 request_time : new Date().toISOString(),
403 holds_address : seed_addr
408 holds_address : seed_addr
410 source_send_time : new Date().toISOString(),
411 target_copy : seed_copy
414 $scope.preview_scope = {
419 xact_start : new Date().toISOString(),
421 xact_type : 'circulation',
422 last_billing_type : 'Overdue materials',
424 last_payment_note : 'Test Note 1',
425 last_payment_type : 'cash_payment',
431 xact_start : new Date().toISOString(),
433 xact_type : 'circulation',
434 last_billing_type : 'Overdue materials',
436 last_payment_note : 'Test Note 2',
437 last_payment_type : 'credit_payment',
445 copies : [ seed_copy ],
449 due_date : new Date().toISOString(),
450 target_copy : seed_copy,
451 copy_barcode : seed_copy.barcode,
452 call_number : seed_copy.call_number,
453 title : seed_record.title
460 due_date : new Date().toISOString(),
463 title : seed_record.title,
464 author : seed_record.author
472 title : seed_record.title
476 previous_balance : 8.45,
477 payment_total : 2.00,
478 payment_applied : 2.00,
482 payment_type : 'cash_payment',
483 payment_note : 'Here is a payment note',
485 create_date : new Date().toISOString(),
486 title : 'Test Note Title',
488 value : 'This patron is super nice!'
491 transit : seed_transit,
492 transits : [ seed_transit ],
493 title : seed_record.title,
494 author : seed_record.author,
497 dest_location : egCore.idl.toHash(egCore.org.get(egCore.auth.user().ws_ou())),
498 dest_address : seed_addr,
502 hold : one_hold, title : 'Some Title 1', author : 'Some Author 1',
503 volume : { label : '646.4 SOM' }, copy : seed_copy,
504 part : { label : 'v. 1' },
505 patron_barcode : 'S52802662',
506 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
509 hold : one_hold, title : 'Some Title 2', author : 'Some Author 2',
510 volume : { label : '646.4 SOM' }, copy : seed_copy,
511 part : { label : 'v. 1' },
512 patron_barcode : 'S52802662',
513 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
516 hold : one_hold, title : 'Some Title 3', author : 'Some Author 3',
517 volume : { label : '646.4 SOM' }, copy : seed_copy,
518 part : { label : 'v. 1' },
519 patron_barcode : 'S52802662',
520 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
525 $scope.preview_scope.payments = [
526 {amount : 1.00, xact : $scope.preview_scope.transactions[0]},
527 {amount : 1.00, xact : $scope.preview_scope.transactions[1]}
529 $scope.preview_scope.payments[0].xact.title = 'Hali Bote Azikaban de tao fan';
530 $scope.preview_scope.payments[0].xact.copy_barcode = '334343434';
531 $scope.preview_scope.payments[1].xact.title = seed_record.title;
532 $scope.preview_scope.payments[1].xact.copy_barcode = seed_copy.barcode;
534 // today, staff, current_location, etc.
535 egCore.print.fleshPrintScope($scope.preview_scope);
537 $scope.template_changed = function() {
538 $scope.print.load_failed = false;
539 egCore.print.getPrintTemplate($scope.print.template_name)
542 $scope.print.template_content = html;
543 console.log('set template content');
546 $scope.print.template_content = '';
547 $scope.print.load_failed = true;
550 egCore.print.getPrintTemplateContext($scope.print.template_name)
551 .then(function(template_context) {
552 $scope.print.template_context = template_context;
556 $scope.save_locally = function() {
557 egCore.print.storePrintTemplate(
558 $scope.print.template_name,
559 $scope.print.template_content
561 egCore.print.storePrintTemplateContext(
562 $scope.print.template_name,
563 $scope.print.template_context
567 $scope.exportable_templates = function() {
570 var deferred = $q.defer();
572 egCore.hatch.getKeys('eg.print.template').then(function(keys) {
573 angular.forEach(keys, function(key) {
574 if (key.match(/^eg\.print\.template\./)) {
575 promises.push(egCore.hatch.getItem(key).then(function(value) {
576 templates[key.replace('eg.print.template.', '')] = value;
579 promises.push(egCore.hatch.getItem(key).then(function(value) {
580 contexts[key.replace('eg.print.template_context.', '')] = value;
584 $q.all(promises).then(function() {
585 if (Object.keys(templates).length) {
587 templates: templates,
591 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
596 return deferred.promise;
599 $scope.imported_print_templates = { data : '' };
600 $scope.$watch('imported_print_templates.data', function(newVal, oldVal) {
601 if (newVal && newVal != oldVal) {
603 var data = JSON.parse(newVal);
604 angular.forEach(data.templates, function(template_content, template_name) {
605 egCore.print.storePrintTemplate(template_name, template_content);
607 angular.forEach(data.contexts, function(template_context, template_name) {
608 egCore.print.storePrintTemplateContext(template_name, template_context);
610 $scope.template_changed(); // refresh
611 ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
613 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_IMPORT);
618 $scope.template_changed(); // load the default
622 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
623 return function(scope, element, attrs) {
626 return scope.$eval(attrs.content);
629 // create an isolate scope and copy the print context
630 // data into the new scope.
631 // TODO: see also print security concerns in egHatch
632 var result = element.html(value);
633 var context = scope.$eval(attrs.context);
634 var print_scope = scope.$new(true);
635 angular.forEach(context, function(val, key) {
636 print_scope[key] = val;
638 $compile(element.contents())(print_scope);
644 .controller('StoredPrefsCtrl',
645 ['$scope','$q','egCore','egConfirmDialog',
646 function($scope , $q , egCore , egConfirmDialog) {
647 console.log('StoredPrefsCtrl');
649 $scope.setContext = function(ctx) {
650 $scope.context = ctx;
652 $scope.setContext('local');
654 // grab the edit perm
655 $scope.userHasDeletePerm = false;
656 egCore.perm.hasPermHere('ADMIN_WORKSTATION')
657 .then(function(bool) { $scope.userHasDeletePerm = bool });
661 function refreshKeys() {
662 $scope.keys = {local : [], remote : []};
664 egCore.hatch.getRemoteKeys().then(
665 function(keys) { $scope.keys.remote = keys.sort() })
667 // local calls are non-async
668 $scope.keys.local = egCore.hatch.getLocalKeys();
672 $scope.selectKey = function(key) {
673 $scope.currentKey = key;
674 $scope.currentKeyContent = null;
676 if ($scope.context == 'local') {
677 $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
679 egCore.hatch.getRemoteItem(key)
680 .then(function(content) {
681 $scope.currentKeyContent = content
686 $scope.getCurrentKeyContent = function() {
687 return JSON.stringify($scope.currentKeyContent, null, 2);
690 $scope.removeKey = function(key) {
691 egConfirmDialog.open(
692 egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
695 if ($scope.context == 'local') {
696 egCore.hatch.removeLocalItem(key);
699 egCore.hatch.removeItem(key)
700 .then(function() { refreshKeys() });
703 cancel : function() {} // user canceled, nothing to do
709 .controller('WSRegCtrl',
710 ['$scope','$q','$window','$location','egCore','egAlertDialog','workstationSvc',
711 function($scope , $q , $window , $location , egCore , egAlertDialog , workstationSvc) {
713 var all_workstations = [];
714 var reg_perm_orgs = [];
716 $scope.page_loaded = false;
717 $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
718 $scope.wsOrgChanged = function(org) { $scope.contextOrg = org; }
720 console.log('set context org to ' + $scope.contextOrg);
722 // fetch workstation reg perms
723 egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
724 .then(function(orgList) {
725 reg_perm_orgs = orgList;
727 // hide orgs in the context org selector where this login
728 // does not have the reg_ws perm
729 $scope.wsOrgHidden = function(id) {
730 return reg_perm_orgs.indexOf(id) == -1;
733 // fetch the locally stored workstation data
735 return workstationSvc.get_all()
737 }).then(function(all) {
738 all_workstations = all || [];
739 $scope.workstations =
740 all_workstations.map(function(w) { return w.name });
741 return workstationSvc.get_default()
743 // fetch the default workstation
744 }).then(function(def) {
745 $scope.defaultWS = def;
746 $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
748 // Handle any URL commands.
750 var remove = $location.search().remove;
752 console.log('Removing WS via URL request: ' + remove);
753 return $scope.remove_ws(remove).then(
754 function() { $scope.page_loaded = true; });
756 $scope.page_loaded = true;
759 $scope.get_ws_label = function(ws) {
760 return ws == $scope.defaultWS ?
761 egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
764 $scope.set_default_ws = function(name) {
765 delete $scope.removing_ws;
766 $scope.defaultWS = name;
767 workstationSvc.set_default(name);
770 $scope.cant_have_users =
771 function (id) { return !egCore.org.CanHaveUsers(id); };
772 $scope.cant_have_volumes =
773 function (id) { return !egCore.org.CanHaveVolumes(id); };
775 // Log out and return to login page with selected WS
776 $scope.use_now = function() {
777 egCore.auth.logout();
778 $window.location.href = $location
780 .search({ws : $scope.selectedWS})
784 $scope.can_delete_ws = function(name) {
785 var ws = all_workstations.filter(
786 function(ws) { return ws.name == name })[0];
787 return ws && reg_perm_orgs.indexOf(ws.owning_lib) != -1;
790 $scope.remove_ws = function(remove_me) {
791 $scope.removing_ws = remove_me;
793 // Perm is used to disable Remove button in UI, but have to check
794 // again here in case we're removing a WS based on URL params.
795 if (!$scope.can_delete_ws(remove_me)) return $q.when();
797 $scope.is_removing = true;
798 return workstationSvc.remove_workstation(remove_me)
801 all_workstations = all_workstations.filter(
802 function(ws) { return ws.name != remove_me });
804 $scope.workstations = $scope.workstations.filter(
805 function(ws) { return ws != remove_me });
807 if ($scope.selectedWS == remove_me)
808 $scope.selectedWS = $scope.workstations[0];
810 if ($scope.defaultWS == remove_me)
811 $scope.defaultWS = '';
813 $scope.is_removing = false;
817 $scope.register_ws = function() {
818 delete $scope.removing_ws;
821 $scope.contextOrg.shortname() + '-' + $scope.newWSName;
823 if ($scope.workstations.indexOf(full_name) > -1) {
824 // avoid duplicate local registrations
825 return egAlertDialog.open(egCore.strings.WS_USED);
828 $scope.is_registering = true;
829 workstationSvc.register_workstation(
830 $scope.newWSName, full_name,
831 $scope.contextOrg.id()
833 ).then(function(new_ws) {
834 $scope.workstations.push(new_ws.name);
835 all_workstations.push(new_ws);
836 $scope.is_registering = false;
838 if (!$scope.selectedWS) {
839 $scope.selectedWS = new_ws.name;
841 if (!$scope.defaultWS) {
842 return $scope.set_default_ws(new_ws.name);
844 $scope.newWSName = '';
849 .controller('HatchCtrl',
851 function($scope , egCore) {
852 var hatch = egCore.hatch; // convenience
854 $scope.hatch_available = hatch.hatchAvailable;
855 $scope.hatch_printing = hatch.usePrinting();
856 $scope.hatch_settings = hatch.useSettings();
857 $scope.hatch_offline = hatch.useOffline();
859 // Apply Hatch settings as changes occur in the UI.
861 $scope.$watch('hatch_printing', function(newval) {
862 if (typeof newval != 'boolean') return;
863 hatch.setLocalItem('eg.hatch.enable.printing', newval);
866 $scope.$watch('hatch_settings', function(newval) {
867 if (typeof newval != 'boolean') return;
868 hatch.setLocalItem('eg.hatch.enable.settings', newval);
871 $scope.$watch('hatch_offline', function(newval) {
872 if (typeof newval != 'boolean') return;
873 hatch.setLocalItem('eg.hatch.enable.offline', newval);