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) {
224 $scope.printConfig = {};
225 $scope.setContext = function(ctx) {
226 $scope.context = ctx;
227 $scope.isTestView = false;
229 $scope.setContext('default');
231 $scope.hatchNotConnected = function() {
232 return !egCore.hatch.hatchAvailable;
235 $scope.getPrinterByAttr = function(attr, value) {
237 angular.forEach($scope.printers, function(p) {
238 if (p[attr] == value) printer = p;
243 // fetch info on all remote printers
244 egCore.hatch.getPrinters()
245 .then(function(printers) {
246 $scope.printers = printers;
248 var def = $scope.getPrinterByAttr('is-default', true);
249 if (!def && printers.length) def = printers[0];
252 $scope.defaultPrinter = def;
253 loadPrinterOptions(def.name);
257 ['default','receipt','label','mail','offline'],
259 egCore.hatch.getPrintConfig(ctx).then(function(conf) {
261 $scope.printConfig[ctx] = conf;
263 $scope.resetPrinterSettings(ctx);
270 $scope.resetPrinterSettings = function(context) {
271 $scope.printConfig[context] = {
273 printer : $scope.defaultPrinter ? $scope.defaultPrinter.name : null,
280 $scope.savePrinterSettings = function(context) {
281 return egCore.hatch.setPrintConfig(
282 context, $scope.printConfig[context]);
285 $scope.printerConfString = function() {
286 if ($scope.printConfigError) return $scope.printConfigError;
287 if (!$scope.printConfig) return;
288 if (!$scope.printConfig[$scope.context]) return;
289 return JSON.stringify(
290 $scope.printConfig[$scope.context], undefined, 2);
293 function loadPrinterOptions(name) {
294 egCore.hatch.getPrinterOptions(name).then(
295 function(options) {$scope.printerOptions = options});
298 $scope.setPrinter = function(name) {
299 $scope.printConfig[$scope.context].printer = name;
300 loadPrinterOptions(name);
304 $scope.setContentType = function(type) { $scope.contentType = type }
306 $scope.testPrint = function(withDialog) {
307 if ($scope.contentType == 'text/plain') {
309 context : $scope.context,
310 content_type : $scope.contentType,
311 content : $scope.textPrintContent,
312 show_dialog : withDialog
316 context : $scope.context,
317 content_type : $scope.contentType,
318 content : $scope.htmlPrintContent,
320 value1 : 'Value One',
321 value2 : 'Value Two',
322 date_value : '2015-02-04T14:04:34-0400'
324 show_dialog : withDialog
329 $scope.setContentType('text/plain');
333 .controller('PrintTemplatesCtrl',
334 ['$scope','$q','egCore','ngToast',
335 function($scope , $q , egCore , ngToast) {
338 template_name : 'bills_current',
339 template_output : '',
340 template_context : 'default'
343 // print preview scope data
344 // TODO: consider moving the template-specific bits directly
345 // into the templates or storing template- specific script files
346 // alongside the templates.
347 // NOTE: A lot of this data can be shared across templates.
349 first_given_name : 'Slow',
350 second_given_name : 'Joe',
351 family_name : 'Jones',
353 barcode : '30393830393'
357 street1 : '123 Apple Rd',
366 title : 'Traveling Pants!!',
367 author : 'Jane Jones',
372 barcode : '33434322323',
377 'title' : 'Test Title'
382 name : 'General Collection'
384 // flattened versions for item status template
385 // TODO - make this go away
386 'call_number.label' : '636.8 JON',
387 'call_number.record.simple_record.title' : 'Test Title',
388 'location.name' : 'General Colleciton'
393 phone_notify : '111-222-3333',
394 sms_notify : '111-222-3333',
395 email_notify : 'user@example.org',
396 request_time : new Date().toISOString(),
404 holds_address : seed_addr
409 holds_address : seed_addr
411 source_send_time : new Date().toISOString(),
412 target_copy : seed_copy
415 $scope.preview_scope = {
420 xact_start : new Date().toISOString(),
422 xact_type : 'circulation',
423 last_billing_type : 'Overdue materials',
425 last_payment_note : 'Test Note 1',
426 last_payment_type : 'cash_payment',
432 xact_start : new Date().toISOString(),
434 xact_type : 'circulation',
435 last_billing_type : 'Overdue materials',
437 last_payment_note : 'Test Note 2',
438 last_payment_type : 'credit_payment',
446 copies : [ seed_copy ],
450 due_date : new Date().toISOString(),
451 target_copy : seed_copy,
452 copy_barcode : seed_copy.barcode,
453 call_number : seed_copy.call_number,
454 title : seed_record.title
461 due_date : new Date().toISOString(),
464 title : seed_record.title,
465 author : seed_record.author
473 title : seed_record.title
477 previous_balance : 8.45,
478 payment_total : 2.00,
479 payment_applied : 2.00,
483 payment_type : 'cash_payment',
484 payment_note : 'Here is a payment note',
486 create_date : new Date().toISOString(),
487 title : 'Test Note Title',
489 value : 'This patron is super nice!'
492 transit : seed_transit,
493 transits : [ seed_transit ],
494 title : seed_record.title,
495 author : seed_record.author,
498 dest_location : egCore.idl.toHash(egCore.org.get(egCore.auth.user().ws_ou())),
499 dest_address : seed_addr,
503 hold : one_hold, title : 'Some Title 1', author : 'Some Author 1',
504 volume : { label : '646.4 SOM' }, copy : seed_copy,
505 part : { label : 'v. 1' },
506 patron_barcode : 'S52802662',
507 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
510 hold : one_hold, title : 'Some Title 2', author : 'Some Author 2',
511 volume : { label : '646.4 SOM' }, copy : seed_copy,
512 part : { label : 'v. 1' },
513 patron_barcode : 'S52802662',
514 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
517 hold : one_hold, title : 'Some Title 3', author : 'Some Author 3',
518 volume : { label : '646.4 SOM' }, copy : seed_copy,
519 part : { label : 'v. 1' },
520 patron_barcode : 'S52802662',
521 patron_alias : 'XYZ', patron_last : 'Smith', patron_first : 'Jane'
526 $scope.preview_scope.payments = [
527 {amount : 1.00, xact : $scope.preview_scope.transactions[0]},
528 {amount : 1.00, xact : $scope.preview_scope.transactions[1]}
530 $scope.preview_scope.payments[0].xact.title = 'Hali Bote Azikaban de tao fan';
531 $scope.preview_scope.payments[0].xact.copy_barcode = '334343434';
532 $scope.preview_scope.payments[1].xact.title = seed_record.title;
533 $scope.preview_scope.payments[1].xact.copy_barcode = seed_copy.barcode;
535 // today, staff, current_location, etc.
536 egCore.print.fleshPrintScope($scope.preview_scope);
538 $scope.template_changed = function() {
539 $scope.print.load_failed = false;
540 egCore.print.getPrintTemplate($scope.print.template_name)
543 $scope.print.template_content = html;
544 console.log('set template content');
547 $scope.print.template_content = '';
548 $scope.print.load_failed = true;
551 egCore.print.getPrintTemplateContext($scope.print.template_name)
552 .then(function(template_context) {
553 $scope.print.template_context = template_context;
557 $scope.save_locally = function() {
558 egCore.print.storePrintTemplate(
559 $scope.print.template_name,
560 $scope.print.template_content
562 egCore.print.storePrintTemplateContext(
563 $scope.print.template_name,
564 $scope.print.template_context
568 $scope.exportable_templates = function() {
571 var deferred = $q.defer();
573 egCore.hatch.getKeys('eg.print.template').then(function(keys) {
574 angular.forEach(keys, function(key) {
575 if (key.match(/^eg\.print\.template\./)) {
576 promises.push(egCore.hatch.getItem(key).then(function(value) {
577 templates[key.replace('eg.print.template.', '')] = value;
580 promises.push(egCore.hatch.getItem(key).then(function(value) {
581 contexts[key.replace('eg.print.template_context.', '')] = value;
585 $q.all(promises).then(function() {
586 if (Object.keys(templates).length) {
588 templates: templates,
592 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_EXPORT);
597 return deferred.promise;
600 $scope.imported_print_templates = { data : '' };
601 $scope.$watch('imported_print_templates.data', function(newVal, oldVal) {
602 if (newVal && newVal != oldVal) {
604 var data = JSON.parse(newVal);
605 angular.forEach(data.templates, function(template_content, template_name) {
606 egCore.print.storePrintTemplate(template_name, template_content);
608 angular.forEach(data.contexts, function(template_context, template_name) {
609 egCore.print.storePrintTemplateContext(template_name, template_context);
611 $scope.template_changed(); // refresh
612 ngToast.create(egCore.strings.PRINT_TEMPLATES_SUCCESS_IMPORT);
614 ngToast.warning(egCore.strings.PRINT_TEMPLATES_FAIL_IMPORT);
619 $scope.template_changed(); // load the default
623 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
624 return function(scope, element, attrs) {
627 return scope.$eval(attrs.content);
630 // create an isolate scope and copy the print context
631 // data into the new scope.
632 // TODO: see also print security concerns in egHatch
633 var result = element.html(value);
634 var context = scope.$eval(attrs.context);
635 var print_scope = scope.$new(true);
636 angular.forEach(context, function(val, key) {
637 print_scope[key] = val;
639 $compile(element.contents())(print_scope);
645 .controller('StoredPrefsCtrl',
646 ['$scope','$q','egCore','egConfirmDialog',
647 function($scope , $q , egCore , egConfirmDialog) {
648 console.log('StoredPrefsCtrl');
650 $scope.setContext = function(ctx) {
651 $scope.context = ctx;
653 $scope.setContext('local');
655 // grab the edit perm
656 $scope.userHasDeletePerm = false;
657 egCore.perm.hasPermHere('ADMIN_WORKSTATION')
658 .then(function(bool) { $scope.userHasDeletePerm = bool });
662 function refreshKeys() {
663 $scope.keys = {local : [], remote : []};
665 egCore.hatch.getRemoteKeys().then(
666 function(keys) { $scope.keys.remote = keys.sort() })
668 // local calls are non-async
669 $scope.keys.local = egCore.hatch.getLocalKeys();
673 $scope.selectKey = function(key) {
674 $scope.currentKey = key;
675 $scope.currentKeyContent = null;
677 if ($scope.context == 'local') {
678 $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
680 egCore.hatch.getRemoteItem(key)
681 .then(function(content) {
682 $scope.currentKeyContent = content
687 $scope.getCurrentKeyContent = function() {
688 return JSON.stringify($scope.currentKeyContent, null, 2);
691 $scope.removeKey = function(key) {
692 egConfirmDialog.open(
693 egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
696 if ($scope.context == 'local') {
697 egCore.hatch.removeLocalItem(key);
700 egCore.hatch.removeItem(key)
701 .then(function() { refreshKeys() });
704 cancel : function() {} // user canceled, nothing to do
710 .controller('WSRegCtrl',
711 ['$scope','$q','$window','$location','egCore','egAlertDialog','workstationSvc',
712 function($scope , $q , $window , $location , egCore , egAlertDialog , workstationSvc) {
714 var all_workstations = [];
715 var reg_perm_orgs = [];
717 $scope.page_loaded = false;
718 $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
719 $scope.wsOrgChanged = function(org) { $scope.contextOrg = org; }
721 console.log('set context org to ' + $scope.contextOrg);
723 // fetch workstation reg perms
724 egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
725 .then(function(orgList) {
726 reg_perm_orgs = orgList;
728 // hide orgs in the context org selector where this login
729 // does not have the reg_ws perm
730 $scope.wsOrgHidden = function(id) {
731 return reg_perm_orgs.indexOf(id) == -1;
734 // fetch the locally stored workstation data
736 return workstationSvc.get_all()
738 }).then(function(all) {
739 all_workstations = all || [];
740 $scope.workstations =
741 all_workstations.map(function(w) { return w.name });
742 return workstationSvc.get_default()
744 // fetch the default workstation
745 }).then(function(def) {
746 $scope.defaultWS = def;
747 $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
749 // Handle any URL commands.
751 var remove = $location.search().remove;
753 console.log('Removing WS via URL request: ' + remove);
754 return $scope.remove_ws(remove).then(
755 function() { $scope.page_loaded = true; });
757 $scope.page_loaded = true;
760 $scope.get_ws_label = function(ws) {
761 return ws == $scope.defaultWS ?
762 egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
765 $scope.set_default_ws = function(name) {
766 delete $scope.removing_ws;
767 $scope.defaultWS = name;
768 workstationSvc.set_default(name);
771 $scope.cant_have_users =
772 function (id) { return !egCore.org.CanHaveUsers(id); };
773 $scope.cant_have_volumes =
774 function (id) { return !egCore.org.CanHaveVolumes(id); };
776 // Log out and return to login page with selected WS
777 $scope.use_now = function() {
778 egCore.auth.logout();
779 $window.location.href = $location
781 .search({ws : $scope.selectedWS})
785 $scope.can_delete_ws = function(name) {
786 var ws = all_workstations.filter(
787 function(ws) { return ws.name == name })[0];
788 return ws && reg_perm_orgs.indexOf(ws.owning_lib) != -1;
791 $scope.remove_ws = function(remove_me) {
792 $scope.removing_ws = remove_me;
794 // Perm is used to disable Remove button in UI, but have to check
795 // again here in case we're removing a WS based on URL params.
796 if (!$scope.can_delete_ws(remove_me)) return $q.when();
798 $scope.is_removing = true;
799 return workstationSvc.remove_workstation(remove_me)
802 all_workstations = all_workstations.filter(
803 function(ws) { return ws.name != remove_me });
805 $scope.workstations = $scope.workstations.filter(
806 function(ws) { return ws != remove_me });
808 if ($scope.selectedWS == remove_me)
809 $scope.selectedWS = $scope.workstations[0];
811 if ($scope.defaultWS == remove_me)
812 $scope.defaultWS = '';
814 $scope.is_removing = false;
818 $scope.register_ws = function() {
819 delete $scope.removing_ws;
822 $scope.contextOrg.shortname() + '-' + $scope.newWSName;
824 if ($scope.workstations.indexOf(full_name) > -1) {
825 // avoid duplicate local registrations
826 return egAlertDialog.open(egCore.strings.WS_USED);
829 $scope.is_registering = true;
830 workstationSvc.register_workstation(
831 $scope.newWSName, full_name,
832 $scope.contextOrg.id()
834 ).then(function(new_ws) {
835 $scope.workstations.push(new_ws.name);
836 all_workstations.push(new_ws);
837 $scope.is_registering = false;
839 if (!$scope.selectedWS) {
840 $scope.selectedWS = new_ws.name;
842 if (!$scope.defaultWS) {
843 return $scope.set_default_ws(new_ws.name);
845 $scope.newWSName = '';