]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/op_change.js
LP1615805 No inputs after submit in patron search (AngularJS)
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / op_change.js
1 angular.module('egCoreMod')
2
3 /**
4  * egPermLoginDialog.open(
5  *  open("some message goes {{here}}", {
6  *  here : 'foo', ok : function() {}, cancel : function() {}},
7  *  'OK', 'Cancel');
8  */
9 .factory('egOpChange',
10
11        ['$uibModal','$interpolate', '$rootScope', '$q', 'egAuth', 'egStrings', 'egNet', 'ngToast',
12 function($uibModal, $interpolate, $rootScope, $q, egAuth, egStrings, egNet, ngToast) {
13
14     var service = {};
15
16     // Returns a promise resolved upon successful op-change.
17     // Rejected otherwise.
18     service.changeOperator = function(permEvt) {
19         return $uibModal.open({
20             templateUrl: './share/t_opchange',
21             backdrop: 'static',
22             controller:
23                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
24                 $scope.args = {username : '', password : '', type : 'temp'};
25                 $scope.title = egStrings.OP_CHANGE_TITLE;
26                 if (permEvt) {
27                     $scope.title = permEvt.desc + ": " + permEvt.ilsperm;
28                     $scope.message = egStrings.OP_CHANGE_PERM_MESSAGE;
29                 } else {
30                     $scope.displayTypeField = true;
31                 }
32                 $scope.focus = true;
33                 $scope.ok = function() { $uibModalInstance.close($scope.args) }
34                 $scope.cancel = function () { $uibModalInstance.dismiss() }
35             }]
36         }).result.then(function (args) {
37             if (!args || !args.username || !args.password)
38                 return $q.reject();
39
40             args.type = args.type || 'temp';
41             args.workstation = egAuth.workstation();
42             return egAuth.opChange(args).then(
43                 function() {
44                     console.debug('op-change succeeded');
45                     if (permEvt) {
46                         ngToast.create(egStrings.PERM_OP_CHANGE_SUCCESS);
47                     } else {
48                         ngToast.create(egStrings.OP_CHANGE_SUCCESS);
49                     }
50                 },
51                 function() {
52                     console.debug('op-change failed');
53                     if (permEvt) {
54                         ngToast.warning(egStrings.PERM_OP_CHANGE_FAILURE);
55                     } else {
56                         ngToast.warning(egStrings.OP_CHANGE_FAILURE);
57                     }
58                 }
59             );
60         });
61     }
62
63     // Returns a promise resolved on successful op-change undo.
64     service.changeOperatorUndo = function(hideToast) {
65         return egAuth.opChangeUndo().then(
66             function() {
67                 console.debug('op-change undo succeeded');
68                 if (!hideToast) ngToast.create(egStrings.OP_CHANGE_SUCCESS);
69             },
70             function() {
71                 console.debug('op-change undo failed');
72                 if (!hideToast) ngToast.warning(egStrings.OP_CHANGE_FAILURE);
73             }
74         );
75     }
76
77     // Tell egNet to use our permission failure handler,
78     // since we know how to launch a login override dialog.
79     //
80     // 1. Launch the change-operator dialog
81     // 2. If op-change succeeds, re-do the failed request using the
82     //    op-change'd authtoken.
83     // 3. Undo the op-change.
84     //
85     // Returns a promise resolved along with the re-ran request.
86     egNet.handlePermFailure = function(request) {
87         console.debug("perm override required for "+request.method);
88
89         return service.changeOperator(request.evt).then(function() {
90
91             return egNet.requestWithParamList(
92                 request.service,
93                 request.method,
94                 // original params, but replace the failed authtoken
95                 // with the op-change'd authtoken
96                 [egAuth.token()].concat(request.params.splice(1))
97
98             )['finally'](function() {
99                 // always undo the operator change after a perm override.
100                 // well, unless inside a UI "batch" like Renew All
101                 if (!window.oils_inside_batch) {
102                     console.debug("clearing op-change after perm override redo");
103                     window.oils_op_change_within_batch = false;
104                     service.changeOperatorUndo(true);
105                 } else {
106                     // up to the batch caller to call changeOperatorUndo
107                     console.debug("persisting op-change after perm override redo");
108                     window.oils_op_change_within_batch = true;
109                     // this is an even kludgier use of window-scoped variables
110                     window.oils_op_change_undo_func = function() {
111                         console.debug("clearing op-change after perm override redo");
112                         service.changeOperatorUndo(true);
113                     }
114                     window.oils_op_change_toast_func = function() {
115                         ngToast.create(egStrings.PERM_OP_CHANGE_PERSIST);
116                     }
117                 }
118             });
119         });
120     }
121
122     return service;
123 }])