]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/transits/list.js
changes to transit list interface
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / transits / list.js
1 angular.module('egTransitListApp', 
2     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
3
4 .config(function($routeProvider, $locationProvider, $compileProvider) {
5     $locationProvider.html5Mode(true);
6     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
7
8     var resolver = {delay : 
9         ['egStartup', function(egStartup) {return egStartup.go()}]}
10
11     $routeProvider.when('/circ/transits/list', {
12         templateUrl: './circ/transits/t_list',
13         controller: 'TransitListCtrl',
14         resolve : resolver
15     });
16
17     $routeProvider.otherwise({redirectTo : '/circ/transits/list'});
18 })
19
20 .controller('TransitListCtrl',
21        ['$scope','$q','$routeParams','$window','egCore','egTransits','egGridDataProvider','$uibModal','$timeout',
22 function($scope , $q , $routeParams , $window , egCore , egTransits , egGridDataProvider , $uibModal , $timeout) {
23
24     var transits = [];
25     var provider = egGridDataProvider.instance({});
26     $scope.grid_data_provider = provider;
27     $scope.transit_direction = 'to';
28
29     function init_dates() {
30         // setup date filters
31         var start = new Date(); // midnight this morning
32         start.setHours(0);
33         start.setMinutes(0);
34         var end = new Date(); // near midnight tonight
35         end.setHours(23);
36         end.setMinutes(59);
37         $scope.dates = {
38             start_date : start,
39             end_date : new Date()
40         }
41     }
42     init_dates();
43
44     function date_range() {
45         if ($scope.dates.start_date > $scope.dates.end_date) {
46             var tmp = $scope.dates.start_date;
47             $scope.dates.start_date = $scope.dates.end_date;
48             $scope.dates.end_date = tmp;
49         }
50         $scope.dates.start_date.setHours(0);
51         $scope.dates.start_date.setMinutes(0);
52         $scope.dates.end_date.setHours(23);
53         $scope.dates.end_date.setMinutes(59);
54         try {
55             var start = $scope.dates.start_date.toISOString().replace(/T.*/,'');
56             var end = $scope.dates.end_date.toISOString().replace(/T.*/,'');
57         } catch(E) { // handling empty date widgets; maybe dangerous if something else can happen
58             init_dates();
59             return date_range();
60         }
61         var today = new Date().toISOString().replace(/T.*/,'');
62         if (end == today) end = 'now';
63         return [start, end];
64     }
65
66     function load_item(transits) {
67         if (!transits) return;
68         if (!angular.isArray(transits)) transits = [transits];
69         angular.forEach(transits, function(transit) {
70             $window.open(
71                 egCore.env.basePath + '/cat/item/' +
72                 transit.target_copy().id(),
73                 '_blank'
74             ).focus()
75         });
76     }
77
78     $scope.load_item = function(action, data, transits) {
79         load_item(transits);
80     }
81
82     function abort_transit(transits) {
83         if (!transits) return;
84         if (!angular.isArray(transits)) transits = [transits];
85         if (transits.length == 0) return;
86         egTransits.abort_transits( transits, refresh_page );
87     }
88
89     $scope.abort_transit = function(action, date, transits) {
90         abort_transit(transits);
91     }
92
93     $scope.add_copies_to_bucket = function(transits) {
94         var copy_list = [];
95         angular.forEach($scope.grid_controls.selectedItems(), function(transit) {
96             copy_list.push(transit.target_copy().id());
97         });
98         if (copy_list.length == 0) return;
99
100         // FIXME what follows ought to be refactored into a factory
101         return $uibModal.open({
102             templateUrl: './cat/catalog/t_add_to_bucket',
103             animation: true,
104             size: 'md',
105             controller:
106                    ['$scope','$uibModalInstance',
107             function($scope , $uibModalInstance) {
108
109                 $scope.bucket_id = 0;
110                 $scope.newBucketName = '';
111                 $scope.allBuckets = [];
112
113                 egCore.net.request(
114                     'open-ils.actor',
115                     'open-ils.actor.container.retrieve_by_class.authoritative',
116                     egCore.auth.token(), egCore.auth.user().id(),
117                     'copy', 'staff_client'
118                 ).then(function(buckets) { $scope.allBuckets = buckets; });
119
120                 $scope.add_to_bucket = function() {
121                     var promises = [];
122                     angular.forEach(copy_list, function (cp) {
123                         var item = new egCore.idl.ccbi()
124                         item.bucket($scope.bucket_id);
125                         item.target_copy(cp);
126                         promises.push(
127                             egCore.net.request(
128                                 'open-ils.actor',
129                                 'open-ils.actor.container.item.create',
130                                 egCore.auth.token(), 'copy', item
131                             )
132                         );
133
134                         return $q.all(promises).then(function() {
135                             $uibModalInstance.close();
136                         });
137                     });
138                 }
139
140                 $scope.add_to_new_bucket = function() {
141                     var bucket = new egCore.idl.ccb();
142                     bucket.owner(egCore.auth.user().id());
143                     bucket.name($scope.newBucketName);
144                     bucket.description('');
145                     bucket.btype('staff_client');
146
147                     return egCore.net.request(
148                         'open-ils.actor',
149                         'open-ils.actor.container.create',
150                         egCore.auth.token(), 'copy', bucket
151                     ).then(function(bucket) {
152                         $scope.bucket_id = bucket;
153                         $scope.add_to_bucket();
154                     });
155                 }
156
157                 $scope.cancel = function() {
158                     $uibModalInstance.dismiss();
159                 }
160             }]
161         });
162     }
163
164
165     function gatherSelectedRecordIds () {
166         var rid_list = [];
167         angular.forEach(
168             $scope.grid_controls.selectedItems(),
169             function (item) {
170                 if (rid_list.indexOf(item.target_copy().call_number().record().simple_record().id()) == -1)
171                     rid_list.push(item.target_copy().call_number().record().simple_record().id());
172             }
173         );
174         return rid_list;
175     }
176     function gatherSelectedHoldingsIds (rid) {
177         var cp_id_list = [];
178         angular.forEach(
179             $scope.grid_controls.selectedItems(),
180             function (item) {
181                 if (rid && item.target_copy().call_number().record().simple_record().id() != rid) return;
182                 cp_id_list.push(item.target_copy().id());
183             }
184         );
185         return cp_id_list;
186     }
187
188     var spawnHoldingsEdit = function (hide_vols, hide_copies){
189         angular.forEach(gatherSelectedRecordIds(), function (r) {
190             egCore.net.request(
191                 'open-ils.actor',
192                 'open-ils.actor.anon_cache.set_value',
193                 null, 'edit-these-copies', {
194                     record_id: r,
195                     copies: gatherSelectedHoldingsIds(r),
196                     raw: {},
197                     hide_vols : hide_vols,
198                     hide_copies : hide_copies
199                 }
200             ).then(function(key) {
201                 if (key) {
202                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
203                     $timeout(function() { $window.open(url, '_blank') });
204                 } else {
205                     alert('Could not create anonymous cache key!');
206                 }
207             });
208         });
209     }
210    
211     $scope.edit_copies = function() { 
212         spawnHoldingsEdit(true, false);
213     }
214
215     $scope.grid_controls = {
216         activateItem : load_item
217     }
218
219     function refresh_page() {
220         transits = [];
221         provider.refresh();
222     }
223
224     provider.get = function(offset, count) {
225         var deferred = $q.defer();
226         var recv_index = 0;
227
228         var filter = {
229             'source_send_time' : { 'between' : date_range() },
230             'dest_recv_time'   : null
231         };
232         if ($scope.transit_direction == 'to') { filter['dest'] = $scope.context_org.id(); }
233         if ($scope.transit_direction == 'from') { filter['source'] = $scope.context_org.id(); }
234
235         egCore.pcrud.search('atc',
236             filter, {
237                 'flesh' : 5,
238                 // atc -> target_copy       -> call_number -> record -> simple_record
239                 // atc -> hold_transit_copy -> hold        -> usr    -> card
240                 'flesh_fields' : {
241                     'atc' : ['target_copy','dest','source','hold_transit_copy'],
242                     'acp' : ['call_number','location','circ_lib'],
243                     'acn' : ['record'],
244                     'bre' : ['simple_record'],
245                     'ahtc' : ['hold'],
246                     'ahr' : ['usr'],
247                     'au' : ['card']
248                 },
249                 'select' : { 'bre' : ['id'] }
250             }
251         ).then(
252             deferred.resolve, null, 
253             function(transit) {
254                 transits[offset + recv_index++] = transit;
255                 deferred.notify(transit);
256             }
257         );
258
259         return deferred.promise;
260     }
261
262     $scope.context_org = egCore.org.get(egCore.auth.user().ws_ou());
263     $scope.$watch('context_org', function(newVal, oldVal) {
264         if (newVal && newVal != oldVal) refresh_page();
265     });
266     $scope.$watch('transit_direction', function(newVal, oldVal) {
267         if (newVal && newVal != oldVal) refresh_page();
268     });
269     $scope.$watch('dates.start_date', function(newVal, oldVal) {
270         if (newVal && newVal != oldVal) refresh_page();
271     });
272     $scope.$watch('dates.end_date', function(newVal, oldVal) {
273         if (newVal && newVal != oldVal) refresh_page();
274     });
275 }])
276