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
24 templateUrl : './share/t_eframe',
26 link: function (scope, element, attrs) {
27 element.find('iframe').on(
29 function() {scope.egEmbedFrameLoader(this)}
34 ['$scope','$window','$location','$q','$timeout','egCore',
35 function($scope , $window , $location , $q , $timeout , egCore) {
37 $scope.save_space = $scope.saveSpace ? $scope.saveSpace : 300;
38 // Set the iframe height to just under the window height.
39 // leave room for the navbar, padding, margins, etc.
40 $scope.height = $window.outerHeight - $scope.save_space;
42 // browser client doesn't use cookies, so we don't load the
43 // (at the time of writing, quite limited) angular.cookies
44 // module. We could load something, but this seems to work
45 // well enough for setting the auth cookie (at least, until
48 // note: document.cookie is smart enough to leave unreferenced
49 // cookies alone, so contrary to how this might look, it's not
50 // deleting other cookies (anoncache, etc.)
52 // delete any existing ses cookie
53 $window.document.cookie = "ses=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
54 // push our authtoken in
55 $window.document.cookie = 'ses=' + egCore.auth.token() + '; path=/; secure'
57 // $location has functions for modifying paths and search,
58 // but they all assume you are staying within the angular
59 // app, which we are not. Build the URLs by hand.
60 function open_tab(path) {
61 var url = 'https://' + $window.location.hostname +
62 egCore.env.basePath + path;
63 console.debug('egEmbedFrame opening tab ' + url);
64 $window.open(url, '_blank').focus();
67 // define our own xulG functions to be inserted into the
68 // iframe. NOTE: window-level functions are bad. Though
69 // there is probably a way, I was unable to correctly wire
70 // up the iframe onload handler within the controller or link
71 // funcs. In any event, the code below is meant as a stop-gap
72 // for porting dojo, etc. apps to angular apps and should
73 // eventually go away.
74 // NOTE: catalog integration is not a stop-gap
76 $scope.egEmbedFrameLoader = function(iframe) {
78 $scope.frame = {dom:iframe};
80 var page = iframe.contentWindow.location.href;
81 console.debug('egEmbedFrameLoader(): ' + page);
83 // reload ifram page w/o reloading the entire UI
84 $scope.reload = function() {
85 iframe.contentWindow.location.replace(
86 iframe.contentWindow.location);
89 // tell the iframe'd window its inside the staff client
90 iframe.contentWindow.IAMXUL = true;
92 // also tell it it's inside the browser client, which
93 // may be needed in a few special cases.
94 iframe.contentWindow.IAMBROWSER /* hear me roar */ = true;
96 // XUL has a dump() function which is occasinally called
97 // from embedded browsers.
98 iframe.contentWindow.dump = function(msg) {
99 console.debug('egEmbedFrame:dump(): ' + msg);
102 // define a few commonly used stock xulG handlers.
104 iframe.contentWindow.xulG = {
106 spawn_search : function(search) {
107 open_tab('/circ/patron/search?search='
108 + encodeURIComponent(js2JSON(search)));
111 // edit an existing user
112 spawn_editor : function(info) {
114 open_tab('/circ/patron/register/edit/' + info.usr);
116 } else if (info.clone) {
117 // FIXME: The save-and-clone operation in the
118 // patron editor results in this action.
119 // For some reason, this specific function results
120 // in a new browser window opening instead of a
121 // browser tab. Possibly this is caused by the
122 // fact that the action occurs as a result of a
123 // button click instead of an href. *shrug*.
125 open_tab('/circ/patron/register/clone/' + info.clone);
129 // open a user account
130 new_patron_tab : function(tab_info, usr_info) {
131 open_tab('/circ/patron/' + usr_info.id + '/checkout');
134 get_barcode_and_settings_async : function(barcode, only_settings) {
135 if (!barcode) return $q.reject();
136 var deferred = $q.defer();
138 var barcode_promise = $q.when(barcode);
139 if (!only_settings) {
141 // first verify / locate the barcode
142 barcode_promise = egCore.net.request(
144 'open-ils.actor.get_barcodes',
146 egCore.auth.user().ws_ou(), 'actor', barcode
147 ).then(function(resp) {
149 if (!resp || egCore.evt.parse(resp) || !resp.length) {
150 console.error('user not found: ' + barcode);
156 return barcode = resp.barcode;
160 barcode_promise.then(function(barcode) {
161 if (!barcode) return;
163 return egCore.net.request(
165 'open-ils.actor.user.fleshed.retrieve_by_barcode',
166 egCore.auth.token(), barcode);
168 }).then(function(user) {
169 if (!user) return null;
171 if (e = egCore.evt.parse(user)) {
172 console.error('user fetch failed : ' + e.toString());
177 // copied more or less directly from XUL menu.js
179 for(var i = 0; i < user.settings().length; i++) {
180 settings[user.settings()[i].name()] =
181 JSON2js(user.settings()[i].value());
184 if(!settings['opac.default_phone'] && user.day_phone())
185 settings['opac.default_phone'] = user.day_phone();
186 if(!settings['opac.hold_notify'] && settings['opac.hold_notify'] !== '')
187 settings['opac.hold_notify'] = 'email:phone';
189 // Taken from patron/util.js format_name
192 ( user.prefix() ? user.prefix() + ' ' : '') +
193 user.family_name() + ', ' +
194 user.first_given_name() + ' ' +
195 ( user.second_given_name() ? user.second_given_name() + ' ' : '' ) +
196 ( user.suffix() ? user.suffix() : '');
200 "settings" : settings,
201 "user_email" : user.email(),
202 "patron_name" : patron_name
206 return deferred.promise;
210 if ($scope.handlers) {
211 $scope.handlers.reload = $scope.reload;
212 angular.forEach($scope.handlers, function(val, key) {
213 iframe.contentWindow.xulG[key] = val;
217 if ($scope.onchange) $scope.onchange(page);