]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/eframe.js
ae26e7f74af884987e5a0b80b38ce35fec113e7a
[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
22             // called after egFrameEmbedLoader, during link phase
23             afterload : '@',
24
25             // for tweaking height
26             saveSpace : '@',
27             minHeight : '=?',
28
29             // to display button for displaying embedded page
30             // in a new tab
31             allowEscape : '=?'
32         },
33
34         templateUrl : './share/t_eframe',
35
36         link: function (scope, element, attrs) {
37             scope.autoresize = 'autoresize' in attrs;
38             scope.showIframe = true;
39             // well, I *might* embed XUL; in any event, this gives a way
40             // for things like Dojo widgets to detect whether they are
41             // running in an eframe before the frame load has finished.
42             window.IEMBEDXUL = true;
43             element.find('iframe').on(
44                 'load',
45                 function() {
46                     scope.egEmbedFrameLoader(this);
47                     if (scope.afterload) this.contentWindow[scope.afterload]();
48                 }
49             );
50         },
51
52         controller : 
53                    ['$scope','$window','$location','$q','$timeout','egCore',
54             function($scope , $window , $location , $q , $timeout , egCore) {
55
56             $scope.save_space = $scope.saveSpace ? $scope.saveSpace : 300;
57             // Set the initial iframe height to just under the window height.
58             // leave room for the navbar, padding, margins, etc.
59             $scope.height = $window.outerHeight - $scope.save_space;
60             if ($scope.minHeight && $scope.height < $scope.minHeight) {
61                 $scope.height = $scope.minHeight;
62             }
63
64             // browser client doesn't use cookies, so we don't load the
65             // (at the time of writing, quite limited) angular.cookies
66             // module.  We could load something, but this seems to work
67             // well enough for setting the auth cookie (at least, until 
68             // it doesn't).
69             //
70             // note: document.cookie is smart enough to leave unreferenced
71             // cookies alone, so contrary to how this might look, it's not 
72             // deleting other cookies (anoncache, etc.)
73             
74             // delete any existing ses cookie
75             $window.document.cookie = "ses=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
76             // push our authtoken in
77             $window.document.cookie = 'ses=' + egCore.auth.token() + '; path=/; secure'
78
79             // $location has functions for modifying paths and search,
80             // but they all assume you are staying within the angular
81             // app, which we are not.  Build the URLs by hand.
82             function open_tab(path) {
83                 var url = 'https://' + $window.location.hostname + 
84                     egCore.env.basePath + path;
85                 console.debug('egEmbedFrame opening tab ' + url);
86                 $window.open(url, '_blank').focus();
87             }
88
89             // define our own xulG functions to be inserted into the
90             // iframe.  NOTE: window-level functions are bad.  Though
91             // there is probably a way, I was unable to correctly wire
92             // up the iframe onload handler within the controller or link
93             // funcs.  In any event, the code below is meant as a stop-gap
94             // for porting dojo, etc. apps to angular apps and should
95             // eventually go away.
96             // NOTE: catalog integration is not a stop-gap
97
98             $scope.egEmbedFrameLoader = function(iframe) {
99
100                 $scope.frame = {dom:iframe};
101                 $scope.iframe = iframe;
102
103                 if ($scope.autoresize) {
104                     iFrameResize({}, $scope.iframe);
105                 } else {
106                     // Reset the iframe height to the final content height.
107                     if ($scope.height < $scope.iframe.contentWindow.document.body.scrollHeight)
108                         $scope.height = $scope.iframe.contentWindow.document.body.scrollHeight;
109                 }
110
111                 var page = $scope.iframe.contentWindow.location.href;
112                 console.debug('egEmbedFrameLoader(): ' + page);
113
114                 // reload ifram page w/o reloading the entire UI
115                 $scope.reload = function() {
116                     $scope.iframe.contentWindow.location.replace(
117                         $scope.iframe.contentWindow.location);
118                 }
119
120                 $scope.style = function() {
121                     return 'height:' + $scope.height + 'px';
122                 }
123
124                 // tell the iframe'd window its inside the staff client
125                 $scope.iframe.contentWindow.IAMXUL = true;
126
127                 // also tell it it's inside the browser client, which 
128                 // may be needed in a few special cases.
129                 $scope.iframe.contentWindow.IAMBROWSER /* hear me roar */ = true; 
130
131                 // XUL has a dump() function which is occasinally called 
132                 // from embedded browsers.
133                 $scope.iframe.contentWindow.dump = function(msg) {
134                     console.debug('egEmbedFrame:dump(): ' + msg);
135                 }
136
137                 // Adjust the height again if the iframe loads the openils.Util Dojo module
138                 $timeout(function () {
139                     if ($scope.autoresize) return; // let iframe-resizer handle it
140                     if ($scope.iframe.contentWindow.openils && $scope.iframe.contentWindow.openils.Util) {
141
142                         // HACK! for patron reg page
143                         var e = $scope.iframe.contentWindow.document.getElementById('myForm');
144                         var extra = 50;
145                         
146                         // HACK! for vandelay
147                         if (!e) {
148                             e = $scope.iframe.contentWindow.document.getElementById('vl-body-wrapper');
149                             extra = 10000;
150                         }
151
152                         if (!e) {
153                             e = $scope.iframe.contentWindow.document.body;
154                             extra = 0;
155                         }
156
157                         if ($scope.height < e.scrollHeight + extra) {
158                             $scope.iframe.contentWindow.openils.Util.addOnLoad( function() {
159                                 var old_height = $scope.height;
160                                 $scope.height = e.scrollHeight + extra;
161                                 $scope.$apply();
162                             });
163                         }
164                     }
165                 });
166
167                 // define a few commonly used stock xulG handlers. 
168                 
169                 $scope.iframe.contentWindow.xulG = {
170                     // patron search
171                     spawn_search : function(search) {
172                         open_tab('/circ/patron/search?search=' 
173                             + encodeURIComponent(js2JSON(search)));
174                     },
175
176                     // edit an existing user
177                     spawn_editor : function(info) {
178                         if (info.usr) {
179                             open_tab('/circ/patron/register/edit/' + info.usr);
180                         
181                         } else if (info.clone) {
182                             // FIXME: The save-and-clone operation in the
183                             // patron editor results in this action.  
184                             // For some reason, this specific function results
185                             // in a new browser window opening instead of a 
186                             // browser tab.  Possibly this is caused by the 
187                             // fact that the action occurs as a result of a
188                             // button click instead of an href.  *shrug*.
189                             // It's obnoxious.
190                             open_tab('/circ/patron/register/clone/' + info.clone);
191                         } 
192                     },
193
194                     // open a user account
195                     new_patron_tab : function(tab_info, usr_info) {
196                         open_tab('/circ/patron/' + usr_info.id + '/checkout');
197                     },
198
199                     get_barcode_and_settings_async : function(barcode, only_settings) {
200                         if (!barcode) return $q.reject();
201                         var deferred = $q.defer();
202
203                         var barcode_promise = $q.when(barcode);
204                         if (!only_settings) {
205
206                             // first verify / locate the barcode
207                             barcode_promise = egCore.net.request(
208                                 'open-ils.actor',
209                                 'open-ils.actor.get_barcodes',
210                                 egCore.auth.token(), 
211                                 egCore.auth.user().ws_ou(), 'actor', barcode
212                             ).then(function(resp) {
213
214                                 if (!resp || egCore.evt.parse(resp) || !resp.length) {
215                                     console.error('user not found: ' + barcode);
216                                     deferred.reject();
217                                     return null;
218                                 } 
219
220                                 resp = resp[0];
221                                 return barcode = resp.barcode;
222                             });
223                         }
224
225                         barcode_promise.then(function(barcode) {
226                             if (!barcode) return;
227
228                             return egCore.net.request(
229                                 'open-ils.actor',
230                                 'open-ils.actor.user.fleshed.retrieve_by_barcode',
231                                 egCore.auth.token(), barcode);
232
233                         }).then(function(user) {
234                             if (!user) return null;
235
236                             if (e = egCore.evt.parse(user)) {
237                                 console.error('user fetch failed : ' + e.toString());
238                                 deferred.reject();
239                                 return null;
240                             }
241
242                             // copied more or less directly from XUL menu.js
243                             var settings = {};
244                             for(var i = 0; i < user.settings().length; i++) {
245                                 settings[user.settings()[i].name()] = 
246                                     JSON2js(user.settings()[i].value());
247                             }
248
249                             if(!settings['opac.default_phone'] && user.day_phone()) 
250                                 settings['opac.default_phone'] = user.day_phone();
251                             if(!settings['opac.hold_notify'] && settings['opac.hold_notify'] !== '') 
252                                 settings['opac.hold_notify'] = 'email:phone';
253
254                             // Taken from patron/util.js format_name
255                             // FIXME: I18n
256                             var patron_name = 
257                                 ( user.prefix() ? user.prefix() + ' ' : '') +
258                                 user.family_name() + ', ' +
259                                 user.first_given_name() + ' ' +
260                                 ( user.second_given_name() ? user.second_given_name() + ' ' : '' ) +
261                                 ( user.suffix() ? user.suffix() : '');
262
263                             deferred.resolve({
264                                 "barcode": barcode, 
265                                 "pickup_lib": user.home_ou(), 
266                                 "settings" : settings, 
267                                 "user_email" : user.email(), 
268                                 "patron_name" : patron_name
269                             });
270                         });
271
272                         return deferred.promise;
273                     }
274                 }
275
276                 if ($scope.handlers) {
277                     $scope.handlers.reload = $scope.reload;
278                     angular.forEach($scope.handlers, function(val, key) {
279                         console.log('eframe applying xulG handlers: ' + key);
280                         $scope.iframe.contentWindow.xulG[key] = val;
281                     });
282                 }
283
284                 if ($scope.onchange) $scope.onchange(page);
285             }
286
287             // open a new tab with the embedded URL
288             $scope.escapeEmbed = function() {
289                 $scope.showIframe = false;
290                 $window.open($scope.iframe.contentWindow.location, '_blank').focus();
291             }
292             $scope.restoreEmbed = function() {
293                 $scope.showIframe = true;
294                 $scope.reload();
295             }
296         }]
297     }
298 })
299
300