]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/curbside/directives/staged_manager.js
LP#1879983: AngularJS staff interface for curbside pickup
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / curbside / directives / staged_manager.js
1 angular.module('egCurbsideAppDep')
2
3 .directive('egCurbsideStagedManager', function() {
4     return {
5         transclude: true,
6         restrict:   'E',
7         scope: { },
8         templateUrl: './circ/curbside/t_staged_manager',
9         controller:
10        ['$scope','$q','egCurbsideCoreSvc','egCore','egGridDataProvider','egProgressDialog',
11         '$uibModal','$timeout','$location','egConfirmDialog','ngToast','$interval',
12 function($scope , $q , egCurbsideCoreSvc , egCore , egGridDataProvider , egProgressDialog ,
13          $uibModal , $timeout , $location , egConfirmDialog , ngToast , $interval) {
14
15     $scope.gridControls = {};
16
17     $scope.wasHandled = {};
18     $scope.refreshNeeded = false;
19
20     latestTime = undefined;
21     var checkRefresh = undefined;
22     function startRefreshCheck() {
23         if (!angular.isDefined(checkRefresh)) {
24             checkRefresh = $interval(function() {
25                 egCurbsideCoreSvc.get_latest_staged().then(function(latest) {
26                     if (angular.isDefined(latest)) {
27                         if (angular.isDefined(latestTime) && latestTime != latest) {
28                             $scope.refreshNeeded = true;
29                             stopRefreshCheck();
30                         }
31                         latestTime = latest;
32                     }
33                 });
34             }, 15000);
35         }
36     }
37     function stopRefreshCheck() {
38         if (angular.isDefined(checkRefresh)) {
39             $interval.cancel(checkRefresh);
40             checkRefresh = undefined;
41         }
42     }
43     this.$onInit = function() {
44         startRefreshCheck();
45     }
46     this.$onDestroy = function() {
47         stopRefreshCheck();
48     }
49
50     $scope.gridDataProvider = egGridDataProvider.instance({
51         get : function(offset, count) {
52             $scope.wasHandled = {};
53             $scope.refreshNeeded = false;
54             startRefreshCheck();
55             return egCurbsideCoreSvc.get_staged(offset, count);
56         }
57     });
58
59     $scope.refresh_staged = function() {
60         $scope.gridControls.refresh();
61     }
62
63     $scope.gridCellHandlers = { };
64     $scope.gridCellHandlers.mark_arrived = function(id) {
65         egCurbsideCoreSvc.mark_arrived(id).then(function(resp) {
66             if (evt = egCore.evt.parse(resp)) {
67                 ngToast.danger(egCore.strings.$replace(
68                     egCore.strings.FAILED_CURBSIDE_MARK_ARRIVED,
69                     { slot_id : id, evt_code : evt.code }
70                 ));
71                 return;
72             } 
73             if (!angular.isDefined(resp)) {
74                 ngToast.warning(egCore.strings.$replace(
75                     egCore.strings.NOTFOUND_CURBSIDE_MARK_ARRIVED,
76                     { slot_id : id }
77                 ));
78                 return;
79             }
80             ngToast.success(egCore.strings.$replace(
81                 egCore.strings.SUCCESS_CURBSIDE_MARK_ARRIVED,
82                 { slot_id : id }
83             ));
84             $scope.wasHandled[id] = true;
85             $timeout(function() { $scope.refresh_staged() }, 500);
86         });
87     }
88     $scope.gridCellHandlers.mark_unstaged = function(id) {
89         egCurbsideCoreSvc.mark_unstaged(id).then(function(resp) {
90             if (evt = egCore.evt.parse(resp)) {
91                 ngToast.danger(egCore.strings.$replace(
92                     egCore.strings.FAILED_CURBSIDE_MARK_UNSTAGED,
93                     { slot_id : id, evt_code : evt.code }
94                 ));
95                 return;
96             } 
97             if (!angular.isDefined(resp)) {
98                 ngToast.warning(egCore.strings.$replace(
99                     egCore.strings.NOTFOUND_CURBSIDE_MARK_UNSTAGED,
100                     { slot_id : id }
101                 ));
102                 return;
103             }
104             ngToast.success(egCore.strings.$replace(
105                 egCore.strings.SUCCESS_CURBSIDE_MARK_UNSTAGED,
106                 { slot_id : id }
107             ));
108             $scope.wasHandled[id] = true;
109             $timeout(function() { $scope.refresh_staged() }, 500);
110         });
111     }
112     $scope.gridCellHandlers.mark_delivered = function(id) {
113         egProgressDialog.open();
114         egCurbsideCoreSvc.mark_delivered(id).then(function(resp) {
115             egProgressDialog.close();
116             if (evt = egCore.evt.parse(resp)) {
117                 ngToast.danger(egCore.strings.$replace(
118                     egCore.strings.FAILED_CURBSIDE_MARK_DELIVERED,
119                     { slot_id : id, evt_code : evt.code }
120                 ));
121                 return;
122             }
123             if (!angular.isDefined(resp)) {
124                 ngToast.warning(egCore.strings.$replace(
125                     egCore.strings.NOTFOUND_CURBSIDE_MARK_DELIVERED,
126                     { slot_id : id }
127                 ));
128                 return;
129             }
130             ngToast.success(egCore.strings.$replace(
131                 egCore.strings.SUCCESS_CURBSIDE_MARK_DELIVERED,
132                 { slot_id : id }
133             ));
134             $scope.wasHandled[id] = true;
135             $timeout(function() { $scope.refresh_staged() }, 500);
136         });
137     }
138     $scope.gridCellHandlers.wasHandled = function(id) {
139         return $scope.wasHandled[id];
140     }
141     $scope.gridCellHandlers.patronIsBlocked = function(usr) {
142         return egCurbsideCoreSvc.patron_blocked(usr);
143     }
144
145 }]}});