]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/transits/list.js
webstaff: transit list: don't reset form
[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         };
109         if ($scope.transit_direction == 'to') { filter['dest'] = $scope.context_org.id(); }
110         if ($scope.transit_direction == 'from') { filter['source'] = $scope.context_org.id(); }
111
112         egCore.pcrud.search('atc',
113             filter, {
114                 'flesh' : 5,
115                 // atc -> target_copy       -> call_number -> record -> simple_record
116                 // atc -> hold_transit_copy -> hold        -> usr    -> card
117                 'flesh_fields' : {
118                     'atc' : ['target_copy','dest','source','hold_transit_copy'],
119                     'acp' : ['call_number','location','circ_lib'],
120                     'acn' : ['record'],
121                     'bre' : ['simple_record'],
122                     'ahtc' : ['hold'],
123                     'ahr' : ['usr'],
124                     'au' : ['card']
125                 },
126                 'select' : { 'bre' : ['id'] }
127             }
128         ).then(
129             deferred.resolve, null, 
130             function(transit) {
131                 transits[offset + recv_index++] = transit;
132                 deferred.notify(transit);
133             }
134         );
135
136         return deferred.promise;
137     }
138
139     $scope.context_org = egCore.org.get(egCore.auth.user().ws_ou());
140     $scope.$watch('context_org', function(newVal, oldVal) {
141         if (newVal && newVal != oldVal) refresh_page();
142     });
143     $scope.$watch('transit_direction', function(newVal, oldVal) {
144         if (newVal && newVal != oldVal) refresh_page();
145     });
146     $scope.$watch('dates.start_date', function(newVal, oldVal) {
147         if (newVal && newVal != oldVal) refresh_page();
148     });
149     $scope.$watch('dates.end_date', function(newVal, oldVal) {
150         if (newVal && newVal != oldVal) refresh_page();
151     });
152 }])
153