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',
27 ['$scope','$window','$location','$q','$timeout','egCore',
28 function($scope , $window , $location , $q , $timeout , egCore) {
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;
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
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.)
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'
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();
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) {
70 $scope.frame = iframe;
72 var page = iframe.contentWindow.location.href;
73 console.debug('egEmbedFrameLoader(): ' + page);
75 // reload ifram page w/o reloading the entire UI
76 $scope.reload = function() {
77 iframe.contentWindow.location.replace(
78 iframe.contentWindow.location);
81 // tell the iframe'd window its inside the staff client
82 iframe.contentWindow.IAMXUL = true;
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;
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);
94 // define a few commonly used stock xulG handlers.
96 iframe.contentWindow.xulG = {
98 spawn_search : function(search) {
99 open_tab('/circ/patron/search?search='
100 + encodeURIComponent(js2JSON(search)));
103 // edit an existing user
104 spawn_editor : function(info) {
106 open_tab('/circ/patron/register/edit/' + info.usr);
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*.
117 open_tab('/circ/patron/register/clone/' + info.clone);
121 // open a user account
122 new_patron_tab : function(tab_info, usr_info) {
123 open_tab('/circ/patron/' + usr_info.id + '/checkout');
126 get_barcode_and_settings_async : function(barcode, only_settings) {
127 if (!barcode) return $q.reject();
128 var deferred = $q.defer();
130 var barcode_promise = $q.when(barcode);
131 if (!only_settings) {
133 // first verify / locate the barcode
134 barcode_promise = egCore.net.request(
136 'open-ils.actor.get_barcodes',
138 egCore.auth.user().ws_ou(), 'actor', barcode
139 ).then(function(resp) {
141 if (!resp || egCore.evt.parse(resp) || !resp.length) {
142 console.error('user not found: ' + barcode);
148 return barcode = resp.barcode;
152 barcode_promise.then(function(barcode) {
153 if (!barcode) return;
155 return egCore.net.request(
157 'open-ils.actor.user.fleshed.retrieve_by_barcode',
158 egCore.auth.token(), barcode);
160 }).then(function(user) {
161 if (!user) return null;
163 if (e = egCore.evt.parse(user)) {
164 console.error('user fetch failed : ' + e.toString());
169 // copied more or less directly from XUL menu.js
171 for(var i = 0; i < user.settings().length; i++) {
172 settings[user.settings()[i].name()] =
173 JSON2js(user.settings()[i].value());
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';
181 // Taken from patron/util.js format_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() : '');
192 "settings" : settings,
193 "user_email" : user.email(),
194 "patron_name" : patron_name
198 return deferred.promise;
202 if ($scope.handlers) {
203 $scope.handlers.reload = $scope.reload;
204 angular.forEach($scope.handlers, function(val, key) {
205 iframe.contentWindow.xulG[key] = val;
209 if ($scope.onchange) $scope.onchange(page);