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.hatchRequired =
171 egCore.hatch.getLocalItem('eg.hatch.required');
173 $scope.updateHatchRequired = function() {
174 egCore.hatch.setLocalItem(
175 'eg.hatch.required', $scope.hatchRequired);
178 egCore.hatch.getItem('eg.audio.disable').then(function(val) {
179 $scope.disable_sound = val;
182 egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
183 $scope.search_lib = egCore.org.get(val);
185 $scope.handle_search_lib_changed = function(org) {
186 egCore.hatch.setItem('eg.search.search_lib', org.id());
189 egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
190 $scope.pref_lib = egCore.org.get(val);
192 $scope.handle_pref_lib_changed = function(org) {
193 egCore.hatch.setItem('eg.search.pref_lib', org.id());
196 $scope.adv_pane = 'advanced'; // default value if not explicitly set
197 egCore.hatch.getItem('eg.search.adv_pane').then(function(val) {
198 $scope.adv_pane = val;
200 $scope.$watch('adv_pane', function(newVal, oldVal) {
201 if (newVal != oldVal) {
202 egCore.hatch.setItem('eg.search.adv_pane', newVal);
206 $scope.apply_sound = function() {
207 if ($scope.disable_sound) {
208 egCore.hatch.setItem('eg.audio.disable', true);
210 egCore.hatch.removeItem('eg.audio.disable');
214 $scope.test_audio = function(sound) {
215 egCore.audio.play(sound);
220 .controller('PrintConfigCtrl',
222 function($scope , egCore) {
223 console.log('PrintConfigCtrl');
225 $scope.actionPending = false;
226 $scope.isTestView = false;
228 $scope.setContext = function(ctx) {
229 $scope.context = ctx;
230 $scope.isTestView = false;
231 $scope.actionPending = false;
233 $scope.setContext('default');
235 $scope.getPrinterByAttr = function(attr, value) {
237 angular.forEach($scope.printers, function(p) {
238 if (p[attr] == value) printer = p;
243 $scope.currentPrinter = function() {
244 if ($scope.printConfig && $scope.printConfig[$scope.context]) {
245 return $scope.getPrinterByAttr(
246 'name', $scope.printConfig[$scope.context].printer
251 // fetch info on all remote printers
252 egCore.hatch.getPrinters()
253 .then(function(printers) {
254 $scope.printers = printers;
255 $scope.defaultPrinter =
256 $scope.getPrinterByAttr('is-default', true);
258 .then(function() { return egCore.hatch.getPrintConfig() })
259 .then(function(config) {
260 $scope.printConfig = config;
263 if ($scope.defaultPrinter) {
264 pname = $scope.defaultPrinter.name;
266 } else if ($scope.printers.length == 1) {
267 // if the OS does not report a default printer, but only
268 // one printer is available, treat it as the default.
269 pname = $scope.printers[0].name;
272 // apply the default printer to every context which has
273 // no printer configured.
275 ['default','receipt','label','mail','offline'],
277 if (!$scope.printConfig[ctx]) {
278 $scope.printConfig[ctx] = {
287 $scope.printerConfString = function() {
288 if ($scope.printConfigError) return $scope.printConfigError;
289 if (!$scope.printConfig) return;
290 if (!$scope.printConfig[$scope.context]) return;
291 return JSON.stringify(
292 $scope.printConfig[$scope.context], undefined, 2);
295 $scope.resetConfig = function() {
296 $scope.actionPending = true;
297 $scope.printConfigError = null;
298 $scope.printConfig[$scope.context] = {
299 context : $scope.context
302 if ($scope.defaultPrinter) {
303 $scope.printConfig[$scope.context].printer =
304 $scope.defaultPrinter.name;
307 egCore.hatch.setPrintConfig($scope.printConfig)
308 .finally(function() {$scope.actionPending = false});
311 $scope.setPrinter = function(name) {
312 $scope.printConfig[$scope.context].printer = name;
316 $scope.setContentType = function(type) { $scope.contentType = type }
318 $scope.testPrint = function(withDialog) {
319 if ($scope.contentType == 'text/plain') {
321 context : $scope.context,
322 content_type : $scope.contentType,
323 content : $scope.textPrintContent,
324 show_dialog : withDialog
328 context : $scope.context,
329 content_type : $scope.contentType,
330 content : $scope.htmlPrintContent,
332 value1 : 'Value One',
333 value2 : 'Value Two',
334 date_value : '2015-02-04T14:04:34-0400'
336 show_dialog : withDialog
341 $scope.setContentType('text/plain');
345 .controller('PrintTemplatesCtrl',
346 ['$scope','$q','egCore','ngToast',
347 function($scope , $q , egCore , ngToast) {
350 template_name : 'bills_current',
351 template_output : '',
352 template_context : 'default'
355 // print preview scope data
356 // TODO: consider moving the template-specific bits directly
357 // into the templates or storing template- specific script files
358 // alongside the templates.
359 // NOTE: A lot of this data can be shared across templates.
361 first_given_name : 'Slow',
362 second_given_name : 'Joe',
363 family_name : 'Jones',
365 barcode : '30393830393'
369 street1 : '123 Apple Rd',
378 title : 'Traveling Pants!!',
379 author : 'Jane Jones',
384 barcode : '33434322323',
389 'title' : 'Test Title'
394 name : 'General Collection'
396 // flattened versions for item status template
397 // TODO - make this go away
398 'call_number.label' : '636.8 JON',
399 'call_number.record.simple_record.title' : 'Test Title',
400 'location.name' : 'General Colleciton'
405 phone_notify : '111-222-3333',
406 sms_notify : '111-222-3333',
407 email_notify : 'user@example.org',
408 request_time : new Date().toISOString(),
416 holds_address : seed_addr
421 holds_address : seed_addr
423 source_send_time : new Date().toISOString(),
424 target_copy : seed_copy
427 $scope.preview_scope = {
432 xact_start : new Date().toISOString(),
434 xact_type : 'circulation',
435 last_billing_type : 'Overdue materials',
437 last_payment_note : 'Test Note 1',
438 last_payment_type : 'cash_payment',
444 xact_start : new Date().toISOString(),
446 xact_type : 'circulation',
447 last_billing_type : 'Overdue materials',
449 last_payment_note : 'Test Note 2',
450 last_payment_type : 'credit_payment',
458 copies : [ seed_copy ],
462 due_date : new Date().toISOString(),
463 target_copy : seed_copy,
464 copy_barcode : seed_copy.barcode,
465 call_number : seed_copy.call_number,
466 title : seed_record.title
473 due_date : new Date().toISOString(),
476 title : seed_record.title,
477 author : seed_record.author
485 title : seed_record.title
489 previous_balance : 8.45,
490 payment_total : 2.00,
491 payment_applied : 2.00,
495 payment_type : 'cash_payment',
496 payment_note : 'Here is a payment note',
498 create_date : new Date().toISOString(),
499 title : 'Test Note Title',
501 value : 'This patron is super nice!'
504 transit : seed_transit,
505 transits : [ seed_transit ],
506 title : seed_record.title,
507 author : seed_record.author,
510 dest_location : egCore.idl.toHash(egCore.org.get(egCore.auth.user().ws_ou())),
511 dest_address : seed_addr,
515 hold : one_hold, title : 'Some Title 1', author : 'Some Author 1',
516 volume : { label : '646.4 SOM' }, copy : seed_copy,
517 part : { label : 'v. 1' },
518 patron_barcode : 'S52802662',
519 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
522 hold : one_hold, title : 'Some Title 2', author : 'Some Author 2',
523 volume : { label : '646.4 SOM' }, copy : seed_copy,
524 part : { label : 'v. 1' },
525 patron_barcode : 'S52802662',
526 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
529 hold : one_hold, title : 'Some Title 3', author : 'Some Author 3',
530 volume : { label : '646.4 SOM' }, copy : seed_copy,
531 part : { label : 'v. 1' },
532 patron_barcode : 'S52802662',
533 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
538 $scope.preview_scope.payments = [
539 {amount : 1.00, xact : $scope.preview_scope.transactions[0]},
540 {amount : 1.00, xact : $scope.preview_scope.transactions[1]}
542 $scope.preview_scope.payments[0].xact.title = 'Hali Bote Azikaban de tao fan';
543 $scope.preview_scope.payments[0].xact.copy_barcode = '334343434';
544 $scope.preview_scope.payments[1].xact.title = seed_record.title;
545 $scope.preview_scope.payments[1].xact.copy_barcode = seed_copy.barcode;
547 // today, staff, current_location, etc.
548 egCore.print.fleshPrintScope($scope.preview_scope);
550 $scope.template_changed = function() {
551 $scope.print.load_failed = false;
552 egCore.print.getPrintTemplate($scope.print.template_name)
555 $scope.print.template_content = html;
556 console.log('set template content');
559 $scope.print.template_content = '';
560 $scope.print.load_failed = true;
563 egCore.print.getPrintTemplateContext($scope.print.template_name)
564 .then(function(template_context) {
565 $scope.print.template_context = template_context;
569 $scope.save_locally = function() {
570 egCore.print.storePrintTemplate(
571 $scope.print.template_name,
572 $scope.print.template_content
574 egCore.print.storePrintTemplateContext(
575 $scope.print.template_name,
576 $scope.print.template_context
580 $scope.exportable_templates = function() {
583 var deferred = $q.defer();
585 egCore.hatch.getKeys('eg.print.template').then(function(keys) {
586 angular.forEach(keys, function(key) {
587 if (key.match(/^eg\.print\.template\./)) {
588 promises.push(egCore.hatch.getItem(key).then(function(value) {
589 templates[key.replace('eg.print.template.', '')] = value;
592 promises.push(egCore.hatch.getItem(key).then(function(value) {
593 contexts[key.replace('eg.print.template_context.', '')] = value;
597 $q.all(promises).then(function() {
598 if (Object.keys(templates).length) {
600 templates: templates,
604 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
609 return deferred.promise;
612 $scope.imported_print_templates = { data : '' };
613 $scope.$watch('imported_print_templates.data', function(newVal, oldVal) {
614 if (newVal && newVal != oldVal) {
616 var data = JSON.parse(newVal);
617 angular.forEach(data.templates, function(template_content, template_name) {
618 egCore.print.storePrintTemplate(template_name, template_content);
620 angular.forEach(data.contexts, function(template_context, template_name) {
621 egCore.print.storePrintTemplateContext(template_name, template_context);
623 $scope.template_changed(); // refresh
624 ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
626 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_IMPORT);
631 $scope.template_changed(); // load the default
635 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
636 return function(scope, element, attrs) {
639 return scope.$eval(attrs.content);
642 // create an isolate scope and copy the print context
643 // data into the new scope.
644 // TODO: see also print security concerns in egHatch
645 var result = element.html(value);
646 var context = scope.$eval(attrs.context);
647 var print_scope = scope.$new(true);
648 angular.forEach(context, function(val, key) {
649 print_scope[key] = val;
651 $compile(element.contents())(print_scope);
657 .controller('StoredPrefsCtrl',
658 ['$scope','$q','egCore','egConfirmDialog',
659 function($scope , $q , egCore , egConfirmDialog) {
660 console.log('StoredPrefsCtrl');
662 $scope.setContext = function(ctx) {
663 $scope.context = ctx;
665 $scope.setContext('local');
667 // grab the edit perm
668 $scope.userHasDeletePerm = false;
669 egCore.perm.hasPermHere('ADMIN_WORKSTATION')
670 .then(function(bool) { $scope.userHasDeletePerm = bool });
674 function refreshKeys() {
675 $scope.keys = {local : [], remote : []};
677 egCore.hatch.getRemoteKeys().then(
678 function(keys) { $scope.keys.remote = keys.sort() })
680 // local calls are non-async
681 $scope.keys.local = egCore.hatch.getLocalKeys();
685 $scope.selectKey = function(key) {
686 $scope.currentKey = key;
687 $scope.currentKeyContent = null;
689 if ($scope.context == 'local') {
690 $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
692 egCore.hatch.getRemoteItem(key)
693 .then(function(content) {
694 $scope.currentKeyContent = content
699 $scope.getCurrentKeyContent = function() {
700 return JSON.stringify($scope.currentKeyContent, null, 2);
703 $scope.removeKey = function(key) {
704 egConfirmDialog.open(
705 egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
708 if ($scope.context == 'local') {
709 egCore.hatch.removeLocalItem(key);
712 egCore.hatch.removeItem(key)
713 .then(function() { refreshKeys() });
716 cancel : function() {} // user canceled, nothing to do
722 .controller('WSRegCtrl',
723 ['$scope','$q','$window','$location','egCore','egAlertDialog','workstationSvc',
724 function($scope , $q , $window , $location , egCore , egAlertDialog , workstationSvc) {
726 var all_workstations = [];
727 var reg_perm_orgs = [];
729 $scope.page_loaded = false;
730 $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
731 $scope.wsOrgChanged = function(org) { $scope.contextOrg = org; }
733 console.log('set context org to ' + $scope.contextOrg);
735 // fetch workstation reg perms
736 egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
737 .then(function(orgList) {
738 reg_perm_orgs = orgList;
740 // hide orgs in the context org selector where this login
741 // does not have the reg_ws perm
742 $scope.wsOrgHidden = function(id) {
743 return reg_perm_orgs.indexOf(id) == -1;
746 // fetch the locally stored workstation data
748 return workstationSvc.get_all()
750 }).then(function(all) {
751 all_workstations = all || [];
752 $scope.workstations =
753 all_workstations.map(function(w) { return w.name });
754 return workstationSvc.get_default()
756 // fetch the default workstation
757 }).then(function(def) {
758 $scope.defaultWS = def;
759 $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
761 // Handle any URL commands.
763 var remove = $location.search().remove;
765 console.log('Removing WS via URL request: ' + remove);
766 return $scope.remove_ws(remove).then(
767 function() { $scope.page_loaded = true; });
769 $scope.page_loaded = true;
772 $scope.get_ws_label = function(ws) {
773 return ws == $scope.defaultWS ?
774 egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
777 $scope.set_default_ws = function(name) {
778 delete $scope.removing_ws;
779 $scope.defaultWS = name;
780 workstationSvc.set_default(name);
783 $scope.cant_have_users =
784 function (id) { return !egCore.org.CanHaveUsers(id); };
785 $scope.cant_have_volumes =
786 function (id) { return !egCore.org.CanHaveVolumes(id); };
788 // Log out and return to login page with selected WS
789 $scope.use_now = function() {
790 egCore.auth.logout();
791 $window.location.href = $location
793 .search({ws : $scope.selectedWS})
797 $scope.can_delete_ws = function(name) {
798 var ws = all_workstations.filter(
799 function(ws) { return ws.name == name })[0];
800 return ws && reg_perm_orgs.indexOf(ws.owning_lib) != -1;
803 $scope.remove_ws = function(remove_me) {
804 $scope.removing_ws = remove_me;
806 // Perm is used to disable Remove button in UI, but have to check
807 // again here in case we're removing a WS based on URL params.
808 if (!$scope.can_delete_ws(remove_me)) return $q.when();
810 $scope.is_removing = true;
811 return workstationSvc.remove_workstation(remove_me)
814 all_workstations = all_workstations.filter(
815 function(ws) { return ws.name != remove_me });
817 $scope.workstations = $scope.workstations.filter(
818 function(ws) { return ws != remove_me });
820 if ($scope.selectedWS == remove_me)
821 $scope.selectedWS = $scope.workstations[0];
823 if ($scope.defaultWS == remove_me)
824 $scope.defaultWS = '';
826 $scope.is_removing = false;
830 $scope.register_ws = function() {
831 delete $scope.removing_ws;
834 $scope.contextOrg.shortname() + '-' + $scope.newWSName;
836 if ($scope.workstations.indexOf(full_name) > -1) {
837 // avoid duplicate local registrations
838 return egAlertDialog.open(egCore.strings.WS_USED);
841 $scope.is_registering = true;
842 workstationSvc.register_workstation(
843 $scope.newWSName, full_name,
844 $scope.contextOrg.id()
846 ).then(function(new_ws) {
847 $scope.workstations.push(new_ws.name);
848 all_workstations.push(new_ws);
849 $scope.is_registering = false;
851 if (!$scope.selectedWS) {
852 $scope.selectedWS = new_ws.name;
854 if (!$scope.defaultWS) {
855 return $scope.set_default_ws(new_ws.name);
857 $scope.newWSName = '';