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