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 .factory('workstationSvc',
51 ['$q','$timeout','$location','egCore','egConfirmDialog',
52 function($q , $timeout , $location , egCore , egConfirmDialog) {
56 service.get_all = function() {
57 return egCore.hatch.getItem('eg.workstation.all')
58 .then(function(all) { return all || [] });
61 service.get_default = function() {
62 return egCore.hatch.getItem('eg.workstation.default');
65 service.set_default = function(name) {
66 return egCore.hatch.setItem('eg.workstation.default', name);
69 service.register_workstation = function(base_name, name, org_id) {
70 return service.register_ws_api(base_name, name, org_id)
71 .then(function(ws_id) {
72 return service.track_new_ws(ws_id, name, org_id);
76 service.register_ws_api =
77 function(base_name, name, org_id, override, deferred) {
78 if (!deferred) deferred = $q.defer();
80 var method = 'open-ils.actor.workstation.register';
81 if (override) method += '.override';
84 'open-ils.actor', method, egCore.auth.token(), name, org_id)
86 .then(function(resp) {
88 if (evt = egCore.evt.parse(resp)) {
89 console.log('register returned ' + evt.toString());
91 if (evt.textcode == 'WORKSTATION_NAME_EXISTS' && !override) {
94 egCore.strings.WS_EXISTS, base_name, {
96 service.register_ws_api(
97 base_name, name, org_id, true, deferred)
99 cancel : function() {deferred.reject()}
104 alert(evt.toString());
108 console.log('Resolving register promise with: ' + resp);
109 deferred.resolve(resp);
113 return deferred.promise;
116 service.track_new_ws = function(ws_id, ws_name, owning_lib) {
117 console.log('Tracking newly created WS with ID ' + ws_id);
118 var new_ws = {id : ws_id, name : ws_name, owning_lib : owning_lib};
120 return service.get_all()
121 .then(function(all) {
123 return egCore.hatch.setItem('eg.workstation.all', all)
124 .then(function() { return new_ws });
128 // Remove all traces of the workstation locally.
129 // This does not remove the WS from the server.
130 service.remove_workstation = function(name) {
131 console.debug('Removing workstation: ' + name);
133 return egCore.hatch.getItem('eg.workstation.all')
135 // remove from list of all workstations
136 .then(function(all) {
138 var keep = all.filter(function(ws) {return ws.name != name});
139 return egCore.hatch.setItem('eg.workstation.all', keep)
143 return service.get_default()
145 }).then(function(def) {
147 console.debug('Removing default workstation: ' + name);
148 return egCore.hatch.removeItem('eg.workstation.default');
157 .controller('SplashCtrl',
158 ['$scope','$window','$location','egCore','egConfirmDialog',
159 function($scope , $window , $location , egCore , egConfirmDialog) {
161 // ---------------------
163 $scope.hatchURL = egCore.hatch.hatchURL();
164 $scope.hatchRequired =
165 egCore.hatch.getLocalItem('eg.hatch.required');
167 $scope.updateHatchRequired = function() {
168 egCore.hatch.setLocalItem(
169 'eg.hatch.required', $scope.hatchRequired);
172 $scope.updateHatchURL = function() {
173 egCore.hatch.setLocalItem(
174 'eg.hatch.url', $scope.hatchURL);
177 egCore.hatch.getItem('eg.audio.disable').then(function(val) {
178 $scope.disable_sound = val;
181 $scope.apply_sound = function() {
182 if ($scope.disable_sound) {
183 egCore.hatch.setItem('eg.audio.disable', true);
185 egCore.hatch.removeItem('eg.audio.disable');
189 $scope.test_audio = function(sound) {
190 egCore.audio.play(sound);
195 .controller('PrintConfigCtrl',
197 function($scope , egCore) {
198 console.log('PrintConfigCtrl');
200 $scope.actionPending = false;
201 $scope.isTestView = false;
203 $scope.setContext = function(ctx) {
204 $scope.context = ctx;
205 $scope.isTestView = false;
206 $scope.actionPending = false;
208 $scope.setContext('default');
210 $scope.getPrinterByAttr = function(attr, value) {
212 angular.forEach($scope.printers, function(p) {
213 if (p[attr] == value) printer = p;
218 $scope.currentPrinter = function() {
219 if ($scope.printConfig && $scope.printConfig[$scope.context]) {
220 return $scope.getPrinterByAttr(
221 'name', $scope.printConfig[$scope.context].printer
226 // fetch info on all remote printers
227 egCore.hatch.getPrinters()
228 .then(function(printers) {
229 $scope.printers = printers;
230 $scope.defaultPrinter =
231 $scope.getPrinterByAttr('is-default', true);
233 .then(function() { return egCore.hatch.getPrintConfig() })
234 .then(function(config) {
235 $scope.printConfig = config;
238 if ($scope.defaultPrinter) {
239 pname = $scope.defaultPrinter.name;
241 } else if ($scope.printers.length == 1) {
242 // if the OS does not report a default printer, but only
243 // one printer is available, treat it as the default.
244 pname = $scope.printers[0].name;
247 // apply the default printer to every context which has
248 // no printer configured.
250 ['default','receipt','label','mail','offline'],
252 if (!$scope.printConfig[ctx]) {
253 $scope.printConfig[ctx] = {
262 $scope.printerConfString = function() {
263 if ($scope.printConfigError) return $scope.printConfigError;
264 if (!$scope.printConfig) return;
265 if (!$scope.printConfig[$scope.context]) return;
266 return JSON.stringify(
267 $scope.printConfig[$scope.context], undefined, 2);
270 $scope.resetConfig = function() {
271 $scope.actionPending = true;
272 $scope.printConfigError = null;
273 $scope.printConfig[$scope.context] = {
274 context : $scope.context
277 if ($scope.defaultPrinter) {
278 $scope.printConfig[$scope.context].printer =
279 $scope.defaultPrinter.name;
282 egCore.hatch.setPrintConfig($scope.printConfig)
283 .finally(function() {$scope.actionPending = false});
286 $scope.configurePrinter = function() {
287 $scope.printConfigError = null;
288 $scope.actionPending = true;
289 egCore.hatch.configurePrinter(
291 $scope.printConfig[$scope.context].printer
294 function(config) {$scope.printConfig = config},
295 function(error) {$scope.printConfigError = error}
297 .finally(function() {$scope.actionPending = false});
300 $scope.setPrinter = function(name) {
301 $scope.printConfig[$scope.context].printer = name;
305 $scope.setContentType = function(type) { $scope.contentType = type }
307 $scope.testPrint = function(withDialog) {
308 if ($scope.contentType == 'text/plain') {
310 context : $scope.context,
311 content_type : $scope.contentType,
312 content : $scope.textPrintContent,
313 show_dialog : withDialog
317 context : $scope.context,
318 content_type : $scope.contentType,
319 content : $scope.htmlPrintContent,
321 value1 : 'Value One',
322 value2 : 'Value Two',
323 date_value : '2015-02-04T14:04:34-0400'
325 show_dialog : withDialog
330 $scope.setContentType('text/plain');
334 .controller('PrintTemplatesCtrl',
335 ['$scope','$q','egCore',
336 function($scope , $q , egCore) {
339 template_name : 'bills_current',
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 phone_notify : '111-222-3333',
378 sms_notify : '111-222-3333',
379 email_notify : 'user@example.org',
380 request_time : new Date().toISOString()
384 $scope.preview_scope = {
389 xact_start : new Date().toISOString(),
391 xact_type : 'circulation',
392 last_billing_type : 'Overdue materials',
394 last_payment_note : 'Test Note 1',
400 xact_start : new Date().toISOString(),
402 xact_type : 'circulation',
403 last_billing_type : 'Overdue materials',
405 last_payment_note : 'Test Note 2',
414 due_date : new Date().toISOString(),
415 target_copy : seed_copy,
416 title : seed_record.title
420 previous_balance : 8.45,
421 payment_total : 2.00,
422 payment_applied : 2.00,
426 payment_type : 'cash_payment',
427 payment_note : 'Here is a payment note',
429 create_date : new Date().toISOString(),
430 title : 'Test Note Title',
432 value : 'This patron is super nice!'
439 holds_address : seed_addr
441 target_copy : seed_copy
443 title : seed_record.title,
444 author : seed_record.author,
445 patron : egCore.idl.toHash(egCore.auth.user()),
449 {hold : one_hold, title : 'Some Title 1', author : 'Some Author 1'},
450 {hold : one_hold, title : 'Some Title 2', author : 'Some Author 2'},
451 {hold : one_hold, title : 'Some Title 3', author : 'Some Author 3'}
455 $scope.preview_scope.payments = [
456 {amount : 1.00, xact : $scope.preview_scope.transactions[0]},
457 {amount : 1.00, xact : $scope.preview_scope.transactions[1]}
459 $scope.preview_scope.payments[0].xact.title = 'Hali Bote Azikaban de tao fan';
460 $scope.preview_scope.payments[0].xact.copy_barcode = '334343434';
461 $scope.preview_scope.payments[1].xact.title = seed_record.title;
462 $scope.preview_scope.payments[1].xact.copy_barcode = seed_copy.barcode;
464 // today, staff, current_location, etc.
465 egCore.print.fleshPrintScope($scope.preview_scope);
467 $scope.template_changed = function() {
468 $scope.print.load_failed = false;
469 egCore.print.getPrintTemplate($scope.print.template_name)
472 $scope.print.template_content = html;
473 console.log('set template content');
476 $scope.print.template_content = '';
477 $scope.print.load_failed = true;
482 $scope.save_locally = function() {
483 egCore.print.storePrintTemplate(
484 $scope.print.template_name,
485 $scope.print.template_content
489 $scope.template_changed(); // load the default
493 .directive('egPrintTemplateOutput', ['$compile',function($compile) {
494 return function(scope, element, attrs) {
497 return scope.$eval(attrs.content);
500 // create an isolate scope and copy the print context
501 // data into the new scope.
502 // TODO: see also print security concerns in egHatch
503 var result = element.html(value);
504 var context = scope.$eval(attrs.context);
505 var print_scope = scope.$new(true);
506 angular.forEach(context, function(val, key) {
507 print_scope[key] = val;
509 $compile(element.contents())(print_scope);
515 .controller('StoredPrefsCtrl',
516 ['$scope','$q','egCore','egConfirmDialog',
517 function($scope , $q , egCore , egConfirmDialog) {
518 console.log('StoredPrefsCtrl');
520 $scope.setContext = function(ctx) {
521 $scope.context = ctx;
523 $scope.setContext('local');
525 // grab the edit perm
526 $scope.userHasDeletePerm = false;
527 egCore.perm.hasPermHere('ADMIN_WORKSTATION')
528 .then(function(bool) { $scope.userHasDeletePerm = bool });
532 function refreshKeys() {
533 $scope.keys = {local : [], remote : []};
535 egCore.hatch.getRemoteKeys().then(
536 function(keys) { $scope.keys.remote = keys.sort() })
538 // local calls are non-async
539 $scope.keys.local = egCore.hatch.getLocalKeys();
543 $scope.selectKey = function(key) {
544 $scope.currentKey = key;
545 $scope.currentKeyContent = null;
547 if ($scope.context == 'local') {
548 $scope.currentKeyContent = egCore.hatch.getLocalItem(key);
550 egCore.hatch.getRemoteItem(key)
551 .then(function(content) {
552 $scope.currentKeyContent = content
557 $scope.getCurrentKeyContent = function() {
558 return JSON.stringify($scope.currentKeyContent, null, 2);
561 $scope.removeKey = function(key) {
562 egConfirmDialog.open(
563 egCore.strings.PREFS_REMOVE_KEY_CONFIRM, '',
566 if ($scope.context == 'local') {
567 egCore.hatch.removeLocalItem(key);
570 egCore.hatch.removeItem(key)
571 .then(function() { refreshKeys() });
574 cancel : function() {} // user canceled, nothing to do
580 .controller('WSRegCtrl',
581 ['$scope','$q','$window','$location','egCore','egAlertDialog','workstationSvc',
582 function($scope , $q , $window , $location , egCore , egAlertDialog , workstationSvc) {
584 var all_workstations = [];
585 var reg_perm_orgs = [];
587 $scope.page_loaded = false;
588 $scope.contextOrg = egCore.org.get(egCore.auth.user().ws_ou());
589 $scope.wsOrgChanged = function(org) { $scope.contextOrg = org; }
591 console.log('set context org to ' + $scope.contextOrg);
593 // fetch workstation reg perms
594 egCore.perm.hasPermAt('REGISTER_WORKSTATION', true)
595 .then(function(orgList) {
596 reg_perm_orgs = orgList;
598 // hide orgs in the context org selector where this login
599 // does not have the reg_ws perm
600 $scope.wsOrgHidden = function(id) {
601 return reg_perm_orgs.indexOf(id) == -1;
604 // fetch the locally stored workstation data
606 return workstationSvc.get_all()
608 }).then(function(all) {
609 all_workstations = all || [];
610 $scope.workstations =
611 all_workstations.map(function(w) { return w.name });
612 return workstationSvc.get_default()
614 // fetch the default workstation
615 }).then(function(def) {
616 $scope.defaultWS = def;
617 $scope.activeWS = $scope.selectedWS = egCore.auth.workstation() || def;
619 // Handle any URL commands.
621 var remove = $location.search().remove;
623 console.log('Removing WS via URL request: ' + remove);
624 return $scope.remove_ws(remove).then(
625 function() { $scope.page_loaded = true; });
627 $scope.page_loaded = true;
630 $scope.get_ws_label = function(ws) {
631 return ws == $scope.defaultWS ?
632 egCore.strings.$replace(egCore.strings.DEFAULT_WS_LABEL, {ws:ws}) : ws;
635 $scope.set_default_ws = function(name) {
636 delete $scope.removing_ws;
637 $scope.defaultWS = name;
638 workstationSvc.set_default(name);
641 $scope.cant_have_users =
642 function (id) { return !egCore.org.CanHaveUsers(id); };
643 $scope.cant_have_volumes =
644 function (id) { return !egCore.org.CanHaveVolumes(id); };
646 // Log out and return to login page with selected WS
647 $scope.use_now = function() {
648 egCore.auth.logout();
649 $window.location.href = $location
651 .search({ws : $scope.selectedWS})
655 $scope.can_delete_ws = function(name) {
656 var ws = all_workstations.filter(
657 function(ws) { return ws.name == name })[0];
658 return ws && reg_perm_orgs.indexOf(ws.owning_lib);
661 $scope.remove_ws = function(remove_me) {
662 $scope.removing_ws = remove_me;
664 // Perm is used to disable Remove button in UI, but have to check
665 // again here in case we're removing a WS based on URL params.
666 if (!$scope.can_delete_ws(remove_me)) return $q.when();
668 $scope.is_removing = true;
669 return workstationSvc.remove_workstation(remove_me)
672 all_workstations = all_workstations.filter(
673 function(ws) { return ws.name != remove_me });
675 $scope.workstations = $scope.workstations.filter(
676 function(ws) { return ws != remove_me });
678 if ($scope.selectedWS == remove_me)
679 $scope.selectedWS = $scope.workstations[0];
681 if ($scope.defaultWS == remove_me)
682 $scope.defaultWS = '';
684 $scope.is_removing = false;
688 $scope.register_ws = function() {
689 delete $scope.removing_ws;
692 $scope.contextOrg.shortname() + '-' + $scope.newWSName;
694 if ($scope.workstations.indexOf(full_name) > -1) {
695 // avoid duplicate local registrations
696 return egAlertDialog.open(egCore.strings.WS_USED);
699 $scope.is_registering = true;
700 workstationSvc.register_workstation(
701 $scope.newWSName, full_name,
702 $scope.contextOrg.id()
704 ).then(function(new_ws) {
705 $scope.workstations.push(new_ws.name);
706 all_workstations.push(new_ws);
707 $scope.is_registering = false;
709 if (!$scope.selectedWS) {
710 $scope.selectedWS = new_ws.name;
712 if (!$scope.defaultWS) {
713 return $scope.set_default_ws(new_ws.name);
715 $scope.newWSName = '';