1 angular.module('egCoreMod')
4 * Iframe container for (mostly legacy) embedded interfaces
6 .directive('egEmbedFrame', function() {
11 // URL to load in the embed iframe
14 // optional hash of functions which augment or override
15 // the stock xulG functions defined below.
19 // called after onload of each new iframe page
22 // called after egFrameEmbedLoader, during link phase
25 // for tweaking height
29 // to display button for displaying embedded page
34 templateUrl : './share/t_eframe',
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(
46 scope.egEmbedFrameLoader(this);
47 if (scope.afterload) this.contentWindow[scope.afterload]();
53 ['$scope','$window','$location','$q','$timeout','egCore',
54 function($scope , $window , $location , $q , $timeout , egCore) {
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;
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
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.)
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'
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();
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
98 $scope.egEmbedFrameLoader = function(iframe) {
100 $scope.frame = {dom:iframe};
101 $scope.iframe = iframe;
103 if ($scope.autoresize) {
104 iFrameResize({}, $scope.iframe);
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;
111 var page = $scope.iframe.contentWindow.location.href;
112 console.debug('egEmbedFrameLoader(): ' + page);
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);
120 $scope.style = function() {
121 return 'height:' + $scope.height + 'px';
124 // tell the iframe'd window its inside the staff client
125 $scope.iframe.contentWindow.IAMXUL = true;
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;
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);
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) {
142 // HACK! for patron reg page
143 var e = $scope.iframe.contentWindow.document.getElementById('myForm');
146 // HACK! for vandelay
148 e = $scope.iframe.contentWindow.document.getElementById('vl-body-wrapper');
153 e = $scope.iframe.contentWindow.document.body;
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;
167 // define a few commonly used stock xulG handlers.
169 $scope.iframe.contentWindow.xulG = {
171 spawn_search : function(search) {
172 open_tab('/circ/patron/search?search='
173 + encodeURIComponent(js2JSON(search)));
176 // edit an existing user
177 spawn_editor : function(info) {
179 open_tab('/circ/patron/register/edit/' + info.usr);
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*.
190 open_tab('/circ/patron/register/clone/' + info.clone);
194 // open a user account
195 new_patron_tab : function(tab_info, usr_info) {
196 open_tab('/circ/patron/' + usr_info.id + '/checkout');
199 get_barcode_and_settings_async : function(barcode, only_settings) {
200 if (!barcode) return $q.reject();
201 var deferred = $q.defer();
203 var barcode_promise = $q.when(barcode);
204 if (!only_settings) {
206 // first verify / locate the barcode
207 barcode_promise = egCore.net.request(
209 'open-ils.actor.get_barcodes',
211 egCore.auth.user().ws_ou(), 'actor', barcode
212 ).then(function(resp) {
214 if (!resp || egCore.evt.parse(resp) || !resp.length) {
215 console.error('user not found: ' + barcode);
221 return barcode = resp.barcode;
225 barcode_promise.then(function(barcode) {
226 if (!barcode) return;
228 return egCore.net.request(
230 'open-ils.actor.user.fleshed.retrieve_by_barcode',
231 egCore.auth.token(), barcode);
233 }).then(function(user) {
234 if (!user) return null;
236 if (e = egCore.evt.parse(user)) {
237 console.error('user fetch failed : ' + e.toString());
242 egCore.org.settings(['circ.staff_placed_holds_fallback_to_ws_ou'])
243 .then(function(auth_usr_aous){
245 // copied more or less directly from XUL menu.js
247 for(var i = 0; i < user.settings().length; i++) {
248 settings[user.settings()[i].name()] =
249 JSON2js(user.settings()[i].value());
252 // find applicable YAOUSes for staff-placed holds
253 var requestor = egCore.auth.user();
254 var pickup_lib = user.home_ou(); // default to home ou
255 if (requestor.id() !== user.id()){
256 // this is a staff-placed hold, optionally default to ws ou
257 if (auth_usr_aous['circ.staff_placed_holds_fallback_to_ws_ou']){
258 pickup_lib = requestor.ws_ou();
262 if(!settings['opac.default_phone'] && user.day_phone())
263 settings['opac.default_phone'] = user.day_phone();
264 if(!settings['opac.hold_notify'] && settings['opac.hold_notify'] !== '')
265 settings['opac.hold_notify'] = 'email:phone';
267 // Taken from patron/util.js format_name
270 ( user.prefix() ? user.prefix() + ' ' : '') +
271 user.family_name() + ', ' +
272 user.first_given_name() + ' ' +
273 ( user.second_given_name() ? user.second_given_name() + ' ' : '' ) +
274 ( user.suffix() ? user.suffix() : '');
278 "pickup_lib": pickup_lib,
279 "settings" : settings,
280 "user_email" : user.email(),
281 "patron_name" : patron_name
286 return deferred.promise;
290 if ($scope.handlers) {
291 $scope.handlers.reload = $scope.reload;
292 angular.forEach($scope.handlers, function(val, key) {
293 console.log('eframe applying xulG handlers: ' + key);
294 $scope.iframe.contentWindow.xulG[key] = val;
298 if ($scope.onchange) $scope.onchange(page);
301 // open a new tab with the embedded URL
302 $scope.escapeEmbed = function() {
303 $scope.showIframe = false;
304 $window.open($scope.iframe.contentWindow.location, '_blank').focus();
306 $scope.restoreEmbed = function() {
307 $scope.showIframe = true;