]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/eframe.js
webstaff: Make space saving configurable on embedded iframes
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / eframe.js
1 angular.module('egCoreMod')
2
3 /*
4  * Iframe container for (mostly legacy) embedded interfaces
5  */
6 .directive('egEmbedFrame', function() {
7     return {
8         restrict : 'AE',
9         replace : true,
10         scope : {
11             // URL to load in the embed iframe
12             url : '=',
13
14             // optional hash of functions which augment or override 
15             // the stock xulG functions defined below.
16             handlers : '=',
17             frame : '=',
18
19             // called after onload of each new iframe page
20             onchange : '=',
21             saveSpace : '@',
22         },
23
24         templateUrl : './share/t_eframe',
25
26         controller : 
27                    ['$scope','$window','$location','$q','$timeout','egCore',
28             function($scope , $window , $location , $q , $timeout , egCore) {
29
30             $scope.save_space = $scope.saveSpace ? $scope.saveSpace : 300;
31             // Set the iframe height to just under the window height.
32             // leave room for the navbar, padding, margins, etc.
33             $scope.height = $window.outerHeight - $scope.save_space;
34
35             // browser client doesn't use cookies, so we don't load the
36             // (at the time of writing, quite limited) angular.cookies
37             // module.  We could load something, but this seems to work
38             // well enough for setting the auth cookie (at least, until 
39             // it doesn't).
40             //
41             // note: document.cookie is smart enough to leave unreferenced
42             // cookies alone, so contrary to how this might look, it's not 
43             // deleting other cookies (anoncache, etc.)
44             
45             // delete any existing ses cookie
46             $window.document.cookie = "ses=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
47             // push our authtoken in
48             $window.document.cookie = 'ses=' + egCore.auth.token() + '; path=/; secure'
49
50             // $location has functions for modifying paths and search,
51             // but they all assume you are staying within the angular
52             // app, which we are not.  Build the URLs by hand.
53             function open_tab(path) {
54                 var url = 'https://' + $window.location.hostname + 
55                     egCore.env.basePath + path;
56                 console.debug('egEmbedFrame opening tab ' + url);
57                 $window.open(url, '_blank').focus();
58             }
59
60             // define our own xulG functions to be inserted into the
61             // iframe.  NOTE: window-level functions are bad.  Though
62             // there is probably a way, I was unable to correctly wire
63             // up the iframe onload handler within the controller or link
64             // funcs.  In any event, the code below is meant as a stop-gap
65             // for porting dojo, etc. apps to angular apps and should
66             // eventually go away.
67             // NOTE: catalog integration is not a stop-gap
68             $window.egEmbedFrameLoader = function(iframe) {
69
70                 $scope.frame = iframe;
71
72                 var page = iframe.contentWindow.location.href;
73                 console.debug('egEmbedFrameLoader(): ' + page);
74
75                 // reload ifram page w/o reloading the entire UI
76                 $scope.reload = function() {
77                     iframe.contentWindow.location.replace(
78                         iframe.contentWindow.location);
79                 }
80
81                 // tell the iframe'd window its inside the staff client
82                 iframe.contentWindow.IAMXUL = true;
83
84                 // also tell it it's inside the browser client, which 
85                 // may be needed in a few special cases.
86                 iframe.contentWindow.IAMBROWSER /* hear me roar */ = true; 
87
88                 // XUL has a dump() function which is occasinally called 
89                 // from embedded browsers.
90                 iframe.contentWindow.dump = function(msg) {
91                     console.debug('egEmbedFrame:dump(): ' + msg);
92                 }
93
94                 // define a few commonly used stock xulG handlers. 
95                 
96                 iframe.contentWindow.xulG = {
97                     // patron search
98                     spawn_search : function(search) {
99                         open_tab('/circ/patron/search?search=' 
100                             + encodeURIComponent(js2JSON(search)));
101                     },
102
103                     // edit an existing user
104                     spawn_editor : function(info) {
105                         if (info.usr) {
106                             open_tab('/circ/patron/register/edit/' + info.usr);
107                         
108                         } else if (info.clone) {
109                             // FIXME: The save-and-clone operation in the
110                             // patron editor results in this action.  
111                             // For some reason, this specific function results
112                             // in a new browser window opening instead of a 
113                             // browser tab.  Possibly this is caused by the 
114                             // fact that the action occurs as a result of a
115                             // button click instead of an href.  *shrug*.
116                             // It's obnoxious.
117                             open_tab('/circ/patron/register/clone/' + info.clone);
118                         } 
119                     },
120
121                     // open a user account
122                     new_patron_tab : function(tab_info, usr_info) {
123                         open_tab('/circ/patron/' + usr_info.id + '/checkout');
124                     },
125
126                     get_barcode_and_settings_async : function(barcode, only_settings) {
127                         if (!barcode) return $q.reject();
128                         var deferred = $q.defer();
129
130                         var barcode_promise = $q.when(barcode);
131                         if (!only_settings) {
132
133                             // first verify / locate the barcode
134                             barcode_promise = egCore.net.request(
135                                 'open-ils.actor',
136                                 'open-ils.actor.get_barcodes',
137                                 egCore.auth.token(), 
138                                 egCore.auth.user().ws_ou(), 'actor', barcode
139                             ).then(function(resp) {
140
141                                 if (!resp || egCore.evt.parse(resp) || !resp.length) {
142                                     console.error('user not found: ' + barcode);
143                                     deferred.reject();
144                                     return null;
145                                 } 
146
147                                 resp = resp[0];
148                                 return barcode = resp.barcode;
149                             });
150                         }
151
152                         barcode_promise.then(function(barcode) {
153                             if (!barcode) return;
154
155                             return egCore.net.request(
156                                 'open-ils.actor',
157                                 'open-ils.actor.user.fleshed.retrieve_by_barcode',
158                                 egCore.auth.token(), barcode);
159
160                         }).then(function(user) {
161                             if (!user) return null;
162
163                             if (e = egCore.evt.parse(user)) {
164                                 console.error('user fetch failed : ' + e.toString());
165                                 deferred.reject();
166                                 return null;
167                             }
168
169                             // copied more or less directly from XUL menu.js
170                             var settings = {};
171                             for(var i = 0; i < user.settings().length; i++) {
172                                 settings[user.settings()[i].name()] = 
173                                     JSON2js(user.settings()[i].value());
174                             }
175
176                             if(!settings['opac.default_phone'] && user.day_phone()) 
177                                 settings['opac.default_phone'] = user.day_phone();
178                             if(!settings['opac.hold_notify'] && settings['opac.hold_notify'] !== '') 
179                                 settings['opac.hold_notify'] = 'email:phone';
180
181                             // Taken from patron/util.js format_name
182                             // FIXME: I18n
183                             var patron_name = 
184                                 ( user.prefix() ? user.prefix() + ' ' : '') +
185                                 user.family_name() + ', ' +
186                                 user.first_given_name() + ' ' +
187                                 ( user.second_given_name() ? user.second_given_name() + ' ' : '' ) +
188                                 ( user.suffix() ? user.suffix() : '');
189
190                             deferred.resolve({
191                                 "barcode": barcode, 
192                                 "settings" : settings, 
193                                 "user_email" : user.email(), 
194                                 "patron_name" : patron_name
195                             });
196                         });
197
198                         return deferred.promise;
199                     }
200                 }
201
202                 if ($scope.handlers) {
203                     $scope.handlers.reload = $scope.reload;
204                     angular.forEach($scope.handlers, function(val, key) {
205                         iframe.contentWindow.xulG[key] = val;
206                     });
207                 }
208
209                 if ($scope.onchange) $scope.onchange(page);
210             }
211         }]
212     }
213 })
214
215