Web Client: Make Patron Email Clickable
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / in_house_use / app.js
1 angular.module('egInHouseUseApp', 
2     ['ngRoute', '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 })
9
10 .controller('InHouseUseCtrl',
11        ['$scope','egCore','egGridDataProvider','egConfirmDialog', 'egAlertDialog',
12 function($scope,  egCore,  egGridDataProvider , egConfirmDialog, egAlertDialog) {
13
14     var countCap;
15     var countMax;
16
17     egCore.startup.go().then(function() {
18
19         // grab our non-cat types after startup
20         egCore.pcrud.search('cnct', 
21             {owning_lib : 
22                 egCore.org.fullPath(egCore.auth.user().ws_ou(), true)},
23             null, {atomic : true}
24         ).then(function(list) { 
25             egCore.env.absorbList(list, 'cnct');
26             $scope.nonCatTypes = list 
27         });
28
29         // org settings for max and warning in-house-use counts
30         
31         egCore.org.settings([
32             'ui.circ.in_house_use.entry_cap',
33             'ui.circ.in_house_use.entry_warn',
34             'circ.in_house_use.copy_alert',
35             'circ.in_house_use.checkin_alert'
36         ]).then(function(set) {
37             countWarn = set['ui.circ.in_house_use.entry_warn'] || 20;
38             $scope.countMax = countMax = 
39                 set['ui.circ.in_house_use.entry_cap'] || 99;
40             $scope.copyAlert = copyAlert =
41                 set['circ.in_house_use.copy_alert'] || false;
42             $scope.checkinAlert = checkinAlert =
43                 set['circ.in_house_use.checkin_alert'] || false;
44         });
45     });
46
47     $scope.useFocus = true;
48     $scope.args = {noncat_type : 'barcode', num_uses : 1, needsCountWarnModal: false };
49     var checkouts = [];
50
51     var provider = egGridDataProvider.instance({});
52     provider.get = function(offset, count) {
53         return provider.arrayNotifier(checkouts, offset, count);
54     }
55     $scope.gridDataProvider = provider;
56
57     // currently selected non-cat type
58     $scope.selectedNcType = function() {
59         if (!egCore.env.cnct) return null; // too soon
60         var type = egCore.env.cnct.map[$scope.args.noncat_type];
61         return type ? type.name() : null;
62     }
63
64     $scope.onNumUsesChanged = function(){
65         $scope.args.needsCountWarnModal = countWarn < $scope.args.num_uses;
66     }
67
68     $scope.checkout = function(args){
69         if ($scope.args.needsCountWarnModal) {
70             // show modal to allow warning/confirmation
71             egConfirmDialog.open(egCore.strings.CONFIRM_IN_HOUSE_NUM_USES_COUNT_TITLE, '',
72                 { num_uses: $scope.args.num_uses }
73             ).result.then(function(){
74                 $scope.args.needsCountWarnModal = false
75                 $scope.checkoutStart(args)
76             });
77         } else {
78             $scope.checkoutStart(args);
79         }
80     }
81
82     $scope.checkoutStart = function(args) {
83         $scope.copyNotFound = false;
84
85         var coArgs = {
86             count : args.num_uses,
87             'location' : egCore.auth.user().ws_ou()
88         };
89
90         if (args.noncat_type == 'barcode') {
91
92             egCore.pcrud.search('acp',
93                 {barcode : args.barcode, deleted : 'f'},
94                 {   flesh : 3, 
95                     flesh_fields : {
96                         acp : ['call_number','location'],
97                         acn : ['record', 'prefix', 'suffix'],
98                         bre : ['simple_record']
99                     },
100                     select : { bre : ['id'] } // avoid fleshing MARC
101                 }
102             ).then(function(copy) {
103
104                 if (!copy) {
105                     egCore.audio.play('error.in_house.copy_not_found');
106                     $scope.copyNotFound = true;
107                     return;
108                 }
109
110                 coArgs.copyid = copy.id();
111
112                 // LP1507807: Display the copy alert if the setting is on.
113                 if ($scope.copyAlert && copy.alert_message()) {
114                     egAlertDialog.open(copy.alert_message()).result;
115                 }
116
117                 // LP1507807: Display the location alert if the setting is on.
118                 if ($scope.checkinAlert && copy.location().checkin_alert() == 't') {
119                     egAlertDialog.open(egCore.strings.LOCATION_ALERT_MSG, {copy: copy}).result;
120                 }
121
122                 performCheckout(
123                     'open-ils.circ.in_house_use.create',
124                     coArgs, {copy:copy}
125                 );
126             });
127
128         } else {
129             coArgs.non_cat_type = args.noncat_type;
130             performCheckout(
131                 'open-ils.circ.non_cat_in_house_use.create',
132                 coArgs, {title : $scope.selectedNcType()}
133             );
134         }
135         $scope.args.barcode='';
136     }
137
138     function performCheckout(method, args, data) {
139
140         // FIXME: make this API stream
141         egCore.net.request(
142             'open-ils.circ', method, egCore.auth.token(), args
143
144         ).then(function(resp) {
145             if (evt = egCore.evt.parse(resp)) {
146                 egCore.audio.play('error.in_house');
147                 return alert(evt);
148             }
149
150             egCore.audio.play('success.in_house');
151
152             var item = {num_uses : resp.length};
153             item.copy = data.copy;
154             item.title = data.title || 
155                 data.copy.call_number().record().simple_record().title();
156             item.index = checkouts.length;
157
158             checkouts.unshift(item);
159             provider.refresh();
160         });
161     }
162
163     $scope.print_list = function() {
164         var print_data = { in_house_uses : [] };
165
166         if (checkouts.length == 0) return $q.when();
167
168         angular.forEach(checkouts, function(ihu) {
169             print_data.in_house_uses.push({
170                 num_uses : ihu.num_uses,
171                 copy : egCore.idl.toHash(ihu.copy),
172                 title : ihu.title
173             })
174         });
175
176         return egCore.print.print({
177             template : 'in_house_use_list',
178             scope : print_data
179         });
180     }
181
182 }])