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