LP#1737800 - add delete action to pending patrons
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / patron / pending.js
1 angular.module('egPendingPatronsApp', 
2     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
3
4 .config(function($routeProvider, $locationProvider, $compileProvider) {
5     $locationProvider.html5Mode(true);
6     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
7         
8     var resolver = {delay : 
9         ['egStartup', function(egStartup) {return egStartup.go()}]}
10
11     $routeProvider.when('/circ/patron/pending/list', {
12         templateUrl: './circ/patron/t_pending_list',
13         controller: 'PendingPatronsCtrl',
14         resolve : resolver
15     });
16
17     $routeProvider.otherwise({redirectTo : '/circ/patron/pending/list'});
18 })
19
20 .controller('PendingPatronsCtrl',
21        ['$scope','$q','$routeParams','$window','$location','egCore','egGridDataProvider',
22 function($scope , $q , $routeParams , $window , $location , egCore , egGridDataProvider) {
23
24     console.log('HERE');
25
26     var pending_patrons = [];
27     var provider = egGridDataProvider.instance({});
28     $scope.grid_data_provider = provider;
29
30     function load_patron(item) {
31         if (angular.isArray(item)) item = item[0];
32         if (!item) return;
33         $window.open(
34             $location.path(
35                 '/circ/patron/register/stage/' + item.user.usrname()).absUrl(),
36             '_blank'
37         ).focus();
38     }
39
40     function delete_patron(sel_pending_users) {
41         if (angular.isArray(sel_pending_users)){
42            var promises = [];
43             angular.forEach(sel_pending_users, function(stgu){
44                 promises.push(egCore.net.request(
45                     'open-ils.actor',
46                     'open-ils.actor.user.stage.delete',
47                     egCore.auth.token(),
48                     stgu.user.row_id()
49                 ));
50             });
51
52             $q.all(promises).then(refresh_page);
53         }
54     }
55
56     $scope.load_patron = function(action, data, items) {
57         load_patron(items);
58     }
59
60     $scope.deletePatron = function(action, data, items) {
61         delete_patron(items);
62     }
63
64     $scope.grid_controls = {
65         activateItem : load_patron,
66         deleteItem : delete_patron
67     }
68
69     function refresh_page() {
70         pending_patrons = [];
71         provider.refresh();
72     }
73
74     if (typeof BroadcastChannel != 'undefined') {
75         // connect 2 bChannel
76         holdings_bChannel = new BroadcastChannel('eg.pending_usr.update');
77         holdings_bChannel.onmessage = function(e){
78             if (e.data && e.data.usr.home_ou == $scope.context_org.id()){
79                 // pending usr was registered, refresh grid!
80                 console.log("Got broadcast from channel eg.pending_usr.update for usr id: " + e.data.usr.id);
81                 refresh_page();
82             }
83         }
84     };
85
86     provider.get = function(offset, count) {
87         var deferred = $q.defer();
88         var recv_index = 0;
89
90         egCore.net.request(
91             'open-ils.actor',
92             'open-ils.actor.user.stage.retrieve.by_org',
93             egCore.auth.token(), $scope.context_org.id()
94
95         ).then(
96             deferred.resolve, null, 
97             function(user) {
98                 user.id = user.user.row_id();
99                 user.user.home_ou(egCore.org.get(user.user.home_ou()));
100
101                 // only one (mailing) address is captured during patron
102                 // self-registration
103                 user.mailing_address = user.mailing_addresses[0];
104                 pending_patrons[offset + recv_index++] = user;
105                 deferred.notify(user);
106             }
107         );
108
109         return deferred.promise;
110     }
111
112     $scope.context_org = egCore.org.get(egCore.auth.user().ws_ou())
113     $scope.$watch('context_org', function(newVal, oldVal) {
114         if (newVal && newVal != oldVal) refresh_page();
115     });
116 }])
117