3f233a7c386f7e77a9807b336753575f93ea2a5b
[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
17         service.changeOperator = function(calledFromNavbar, failedRequest) {
18                 var _op_changed = false;
19         $uibModal.open({
20             templateUrl: './share/t_opchange',
21             controller:
22                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
23                 $scope.args = {username : '', password : '', type : 'temp'};
24                 $scope.displayTypeField = calledFromNavbar;
25                 $scope.title = egStrings.OP_CHANGE_TITLE;
26                 if(failedRequest) {
27                     $scope.title = failedRequest.perm_evt.desc + ": "
28                         + failedRequest.perm_evt.ilsperm;
29                     $scope.message = egStrings.OP_CHANGE_PERM_MESSAGE;
30                     console.log($scope.message);
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) return;
38             args.type = args.type || 'temp';
39             args.workstation = egAuth.workstation();
40             egAuth.opChange(args).then(
41                 function() {
42                     _op_changed = true;
43                     if(failedRequest) {
44                         console.log(js2JSON(failedRequest));
45                         egNet.request(
46                             failedRequest.service,
47                             failedRequest.method,
48                             egAuth.token(),
49                             failedRequest.params[1]
50                         ).then(service.changeOperatorUndo());
51                     } else {
52                         ngToast.create(egStrings.OP_CHANGE_SUCCESS);
53                     }
54                 },
55                 function() {
56                     ngToast.warning(egStrings.OP_CHANGE_FAILURE);
57                 }
58             );
59         });
60         return _op_changed;
61     }
62
63     service.changeOperatorUndo = function() {
64         egAuth.opChangeUndo();
65         var _op_changed = false;
66         ngToast.create(egStrings.OP_CHANGE_SUCCESS);
67         return _op_changed;
68     }
69
70     //Check for any permission failure broadcasts. then call changeOperator and retry the action
71     $rootScope.$on('egNetPermFailure', function(args, request_info) {
72         var op_changed = service.changeOperator(false, request_info);
73     })
74
75         return service;
76 }])