]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/transits/list.js
webstaff: tweaks to transit list
[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',
22 function($scope , $q , $routeParams , $window , egCore , egTransits , egGridDataProvider) {
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.grid_controls = {
94         activateItem : load_item
95     }
96
97     function refresh_page() {
98         transits = [];
99         provider.refresh();
100     }
101
102     provider.get = function(offset, count) {
103         var deferred = $q.defer();
104         var recv_index = 0;
105
106         var filter = {
107             'source_send_time' : { 'between' : date_range() },
108             'dest_recv_time'   : null
109         };
110         if ($scope.transit_direction == 'to') { filter['dest'] = $scope.context_org.id(); }
111         if ($scope.transit_direction == 'from') { filter['source'] = $scope.context_org.id(); }
112
113         egCore.pcrud.search('atc',
114             filter, {
115                 'flesh' : 5,
116                 // atc -> target_copy       -> call_number -> record -> simple_record
117                 // atc -> hold_transit_copy -> hold        -> usr    -> card
118                 'flesh_fields' : {
119                     'atc' : ['target_copy','dest','source','hold_transit_copy'],
120                     'acp' : ['call_number','location','circ_lib'],
121                     'acn' : ['record'],
122                     'bre' : ['simple_record'],
123                     'ahtc' : ['hold'],
124                     'ahr' : ['usr'],
125                     'au' : ['card']
126                 },
127                 'select' : { 'bre' : ['id'] }
128             }
129         ).then(
130             deferred.resolve, null, 
131             function(transit) {
132                 transits[offset + recv_index++] = transit;
133                 deferred.notify(transit);
134             }
135         );
136
137         return deferred.promise;
138     }
139
140     $scope.context_org = egCore.org.get(egCore.auth.user().ws_ou());
141     $scope.$watch('context_org', function(newVal, oldVal) {
142         if (newVal && newVal != oldVal) refresh_page();
143     });
144     $scope.$watch('transit_direction', function(newVal, oldVal) {
145         if (newVal && newVal != oldVal) refresh_page();
146     });
147     $scope.$watch('dates.start_date', function(newVal, oldVal) {
148         if (newVal && newVal != oldVal) refresh_page();
149     });
150     $scope.$watch('dates.end_date', function(newVal, oldVal) {
151         if (newVal && newVal != oldVal) refresh_page();
152     });
153 }])
154