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.setContentType = function(type) { $scope.contentType = type }
227 $scope.setContentType('text/plain');
229 $scope.useHatchPrinting = function() {
230 return egCore.hatch.usePrinting();
233 $scope.hatchIsOpen = function() {
234 return egCore.hatch.hatchAvailable;
237 $scope.getPrinterByAttr = function(attr, value) {
239 angular.forEach($scope.printers, function(p) {
240 if (p[attr] == value) printer = p;
245 $scope.resetPrinterSettings = function(context) {
246 $scope.printConfig[context] = {
248 printer : $scope.defaultPrinter ? $scope.defaultPrinter.name : null,
255 $scope.savePrinterSettings = function(context) {
256 return egCore.hatch.setPrintConfig(
257 context, $scope.printConfig[context]);
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);
268 function loadPrinterOptions(name) {
269 egCore.hatch.getPrinterOptions(name).then(
270 function(options) {$scope.printerOptions = options});
273 $scope.setPrinter = function(name) {
274 $scope.printConfig[$scope.context].printer = name;
275 loadPrinterOptions(name);
278 $scope.testPrint = function(withDialog) {
279 if ($scope.contentType == 'text/plain') {
281 context : $scope.context,
282 content_type : $scope.contentType,
283 content : $scope.textPrintContent,
284 show_dialog : withDialog
288 context : $scope.context,
289 content_type : $scope.contentType,
290 content : $scope.htmlPrintContent,
292 value1 : 'Value One',
293 value2 : 'Value Two',
294 date_value : '2015-02-04T14:04:34-0400'
296 show_dialog : withDialog
301 // Load startup data....
302 // Don't bother talking to Hatch if it's not there.
303 if (!egCore.hatch.hatchAvailable) return;
305 // fetch info on all remote printers
306 egCore.hatch.getPrinters()
307 .then(function(printers) {
308 $scope.printers = printers;
310 var def = $scope.getPrinterByAttr('is-default', true);
311 if (!def && printers.length) def = printers[0];
314 $scope.defaultPrinter = def;
315 loadPrinterOptions(def.name);
319 ['default','receipt','label','mail','offline'],
321 egCore.hatch.getPrintConfig(ctx).then(function(conf) {
323 $scope.printConfig[ctx] = conf;
325 $scope.resetPrinterSettings(ctx);
334 .controller('PrintTemplatesCtrl',
335 ['$scope','$q','egCore','ngToast',
336 function($scope , $q , egCore , ngToast) {
339 template_name : 'bills_current',
340 template_output : '',
341 template_context : 'default'
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.
350 first_given_name : 'Slow',
351 second_given_name : 'Joe',
352 family_name : 'Jones',
354 barcode : '30393830393'
358 street1 : '123 Apple Rd',
367 title : 'Traveling Pants!!',
368 author : 'Jane Jones',
373 barcode : '33434322323',
378 'title' : 'Test Title'
383 name : 'General Collection'
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'
394 phone_notify : '111-222-3333',
395 sms_notify : '111-222-3333',
396 email_notify : 'user@example.org',
397 request_time : new Date().toISOString(),
405 holds_address : seed_addr
410 holds_address : seed_addr
412 source_send_time : new Date().toISOString(),
413 target_copy : seed_copy
416 $scope.preview_scope = {
421 xact_start : new Date().toISOString(),
423 xact_type : 'circulation',
424 last_billing_type : 'Overdue materials',
426 last_payment_note : 'Test Note 1',
427 last_payment_type : 'cash_payment',
433 xact_start : new Date().toISOString(),
435 xact_type : 'circulation',
436 last_billing_type : 'Overdue materials',
438 last_payment_note : 'Test Note 2',
439 last_payment_type : 'credit_payment',
447 copies : [ seed_copy ],
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
462 due_date : new Date().toISOString(),
465 title : seed_record.title,
466 author : seed_record.author
474 title : seed_record.title
478 previous_balance : 8.45,
479 payment_total : 2.00,
480 payment_applied : 2.00,
484 payment_type : 'cash_payment',
485 payment_note : 'Here is a payment note',
487 create_date : new Date().toISOString(),
488 title : 'Test Note Title',
490 value : 'This patron is super nice!'
493 transit : seed_transit,
494 transits : [ seed_transit ],
495 title : seed_record.title,
496 author : seed_record.author,
499 dest_location : egCore.idl.toHash(egCore.org.get(egCore.auth.user().ws_ou())),
500 dest_address : seed_addr,
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'
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'
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'
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]}
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;
536 // today, staff, current_location, etc.
537 egCore.print.fleshPrintScope($scope.preview_scope);
539 $scope.template_changed = function() {
540 $scope.print.load_failed = false;
541 egCore.print.getPrintTemplate($scope.print.template_name)
544 $scope.print.template_content = html;
545 console.log('set template content');
548 $scope.print.template_content = '';
549 $scope.print.load_failed = true;
552 egCore.print.getPrintTemplateContext($scope.print.template_name)
553 .then(function(template_context) {
554 $scope.print.template_context = template_context;
558 $scope.save_locally = function() {
559 egCore.print.storePrintTemplate(
560 $scope.print.template_name,
561 $scope.print.template_content
563 egCore.print.storePrintTemplateContext(
564 $scope.print.template_name,
565 $scope.print.template_context
569 $scope.exportable_templates = function() {
572 var deferred = $q.defer();
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;
581 promises.push(egCore.hatch.getItem(key).then(function(value) {
582 contexts[key.replace('eg.print.template_context.', '')] = value;
586 $q.all(promises).then(function() {
587 if (Object.keys(templates).length) {
589 templates: templates,
593 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
598 return deferred.promise;
601 $scope.imported_print_templates = { data : '' };
602 $scope.$watch('imported_print_templates.data', function(newVal, oldVal) {
603 if (newVal && newVal != oldVal) {
605 var data = JSON.parse(newVal);
606 angular.forEach(data.templates, function(template_content, template_name) {
607 egCore.print.storePrintTemplate(template_name, template_content);
609 angular.forEach(data.contexts, function(template_context, template_name) {
610 egCore.print.storePrintTemplateContext(template_name, template_context);
612 $scope.template_changed(); // refresh
613 ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
615 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_IMPORT);
620 $scope.template_changed(); // load the default
624 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
625 return function(scope, element, attrs) {
628 return scope.$eval(attrs.content);
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;
640 $compile(element.contents())(print_scope);
646 .controller('StoredPrefsCtrl',
647 ['$scope','$q','egCore','egConfirmDialog',
648 function($scope , $q , egCore , egConfirmDialog) {
649 console.log('StoredPrefsCtrl');
651 $scope.setContext = function(ctx) {
652 $scope.context = ctx;
654 $scope.setContext('local');
656 // grab the edit perm
657 $scope.userHasDeletePerm = false;
658 egCore.perm.hasPermHere('ADMIN_WORKSTATION')
659 .then(function(bool) { $scope.userHasDeletePerm = bool });
663 function refreshKeys() {
664 $scope.keys = {local : [], remote : []};
666 if (egCore.hatch.hatchAvailable) {
667 egCore.hatch.getRemoteKeys().then(
668 function(keys) { $scope.keys.remote = keys.sort() })
671 // local calls are non-async
672 $scope.keys.local = egCore.hatch.getLocalKeys();
676 $scope.selectKey = function(key) {
677 $scope.currentKey = key;
678 $scope.currentKeyContent = null;
680 if ($scope.context == 'local') {
681 $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
683 egCore.hatch.getRemoteItem(key)
684 .then(function(content) {
685 $scope.currentKeyContent = content
690 $scope.getCurrentKeyContent = function() {
691 return JSON.stringify($scope.currentKeyContent, null, 2);
694 $scope.removeKey = function(key) {
695 egConfirmDialog.open(
696 egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
699 if ($scope.context == 'local') {
700 egCore.hatch.removeLocalItem(key);
703 egCore.hatch.removeItem(key)
704 .then(function() { refreshKeys() });
707 cancel : function() {} // user canceled, nothing to do
713 .controller('WSRegCtrl',
714 ['$scope','$q','$window','$location','egCore','egAlertDialog','workstationSvc',
715 function($scope , $q , $window , $location , egCore , egAlertDialog , workstationSvc) {
717 var all_workstations = [];
718 var reg_perm_orgs = [];
720 $scope.page_loaded = false;
721 $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
722 $scope.wsOrgChanged = function(org) { $scope.contextOrg = org; }
724 console.log('set context org to ' + $scope.contextOrg);
726 // fetch workstation reg perms
727 egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
728 .then(function(orgList) {
729 reg_perm_orgs = orgList;
731 // hide orgs in the context org selector where this login
732 // does not have the reg_ws perm
733 $scope.wsOrgHidden = function(id) {
734 return reg_perm_orgs.indexOf(id) == -1;
737 // fetch the locally stored workstation data
739 return workstationSvc.get_all()
741 }).then(function(all) {
742 all_workstations = all || [];
743 $scope.workstations =
744 all_workstations.map(function(w) { return w.name });
745 return workstationSvc.get_default()
747 // fetch the default workstation
748 }).then(function(def) {
749 $scope.defaultWS = def;
750 $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
752 // Handle any URL commands.
754 var remove = $location.search().remove;
756 console.log('Removing WS via URL request: ' + remove);
757 return $scope.remove_ws(remove).then(
758 function() { $scope.page_loaded = true; });
760 $scope.page_loaded = true;
763 $scope.get_ws_label = function(ws) {
764 return ws == $scope.defaultWS ?
765 egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
768 $scope.set_default_ws = function(name) {
769 delete $scope.removing_ws;
770 $scope.defaultWS = name;
771 workstationSvc.set_default(name);
774 $scope.cant_have_users =
775 function (id) { return !egCore.org.CanHaveUsers(id); };
776 $scope.cant_have_volumes =
777 function (id) { return !egCore.org.CanHaveVolumes(id); };
779 // Log out and return to login page with selected WS
780 $scope.use_now = function() {
781 egCore.auth.logout();
782 $window.location.href = $location
784 .search({ws : $scope.selectedWS})
788 $scope.can_delete_ws = function(name) {
789 var ws = all_workstations.filter(
790 function(ws) { return ws.name == name })[0];
791 return ws && reg_perm_orgs.indexOf(ws.owning_lib) != -1;
794 $scope.remove_ws = function(remove_me) {
795 $scope.removing_ws = remove_me;
797 // Perm is used to disable Remove button in UI, but have to check
798 // again here in case we're removing a WS based on URL params.
799 if (!$scope.can_delete_ws(remove_me)) return $q.when();
801 $scope.is_removing = true;
802 return workstationSvc.remove_workstation(remove_me)
805 all_workstations = all_workstations.filter(
806 function(ws) { return ws.name != remove_me });
808 $scope.workstations = $scope.workstations.filter(
809 function(ws) { return ws != remove_me });
811 if ($scope.selectedWS == remove_me)
812 $scope.selectedWS = $scope.workstations[0];
814 if ($scope.defaultWS == remove_me)
815 $scope.defaultWS = '';
817 $scope.is_removing = false;
821 $scope.register_ws = function() {
822 delete $scope.removing_ws;
825 $scope.contextOrg.shortname() + '-' + $scope.newWSName;
827 if ($scope.workstations.indexOf(full_name) > -1) {
828 // avoid duplicate local registrations
829 return egAlertDialog.open(egCore.strings.WS_USED);
832 $scope.is_registering = true;
833 workstationSvc.register_workstation(
834 $scope.newWSName, full_name,
835 $scope.contextOrg.id()
837 ).then(function(new_ws) {
838 $scope.workstations.push(new_ws.name);
839 all_workstations.push(new_ws);
840 $scope.is_registering = false;
842 if (!$scope.selectedWS) {
843 $scope.selectedWS = new_ws.name;
845 if (!$scope.defaultWS) {
846 return $scope.set_default_ws(new_ws.name);
848 $scope.newWSName = '';
853 .controller('HatchCtrl',
855 function($scope , egCore) {
856 var hatch = egCore.hatch; // convenience
858 $scope.hatch_available = hatch.hatchAvailable;
859 $scope.hatch_printing = hatch.usePrinting();
860 $scope.hatch_settings = hatch.useSettings();
861 $scope.hatch_offline = hatch.useOffline();
863 // Apply Hatch settings as changes occur in the UI.
865 $scope.$watch('hatch_printing', function(newval) {
866 if (typeof newval != 'boolean') return;
867 hatch.setLocalItem('eg.hatch.enable.printing', newval);
870 $scope.$watch('hatch_settings', function(newval) {
871 if (typeof newval != 'boolean') return;
872 hatch.setLocalItem('eg.hatch.enable.settings', newval);
875 $scope.$watch('hatch_offline', function(newval) {
876 if (typeof newval != 'boolean') return;
877 hatch.setLocalItem('eg.hatch.enable.offline', newval);