65c77740eacb05f1fbf8d4682a670dd4b3650d9b
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / z3950.js
1 dump('entering cat.z3950.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.z3950 = function (params) {
5         try {
6                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
7                 JSAN.use('util.error'); this.error = new util.error();
8                 JSAN.use('util.network'); this.network = new util.network();
9         } catch(E) {
10                 dump('cat.z3950: ' + E + '\n');
11         }
12 }
13
14 cat.z3950.prototype = {
15
16         'init' : function( params ) {
17
18                 try {
19                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
20                         JSAN.use('util.widgets');
21
22                         var obj = this;
23
24                         obj.session = params['session'];
25
26                         JSAN.use('circ.util');
27                         var columns = circ.util.columns(
28                                 {
29                                         'tcn' : { 'hidden' : false },
30                                         'isbn' : { 'hidden' : false },
31                                         'title' : { 'hidden' : false, 'flex' : '1' },
32                                         'author' : { 'hidden' : false },
33                                         'edition' : { 'hidden' : false },
34                                         'pubdate' : { 'hidden' : false },
35                                         'publisher' : { 'hidden' : false },
36                                 }
37                         );
38
39                         JSAN.use('util.list'); obj.list = new util.list('results');
40                         obj.list.init(
41                                 {
42                                         'columns' : columns,
43                                         'map_row_to_column' : circ.util.std_map_row_to_column(),
44                                         'on_select' : function(ev) {
45                                                 try {
46                                                         JSAN.use('util.functional');
47                                                         var sel = obj.list.retrieve_selection();
48                                                         var list = util.functional.map_list(
49                                                                 sel,
50                                                                 function(o) { return o.getAttribute('retrieve_id'); }
51                                                         );
52                                                         obj.error.sdump('D_TRACE','cat/z3950: selection list = ' + js2JSON(list) );
53                                                         obj.controller.view.marc_import.disabled = false;
54                                                         obj.controller.view.marc_import.setAttribute('retrieve_id',list[0]);
55                                                 } catch(E) {
56                                                         alert('FIXME: ' + E);
57                                                 }
58                                         },
59                                 }
60                         );
61
62
63                         document.getAnonymousNodes(document.getElementById('c1'))[0].addEventListener(
64                                 'mouseup',
65                                 function() {
66                                         util.widgets.click(
67                                                 document.getAnonymousNodes(document.getElementById('c2'))[0]
68                                         );
69                                 }, false
70                         );
71                         document.getAnonymousNodes(document.getElementById('c2'))[0].addEventListener(
72                                 'mouseup',
73                                 function() {
74                                         util.widgets.click(
75                                                 document.getAnonymousNodes(document.getElementById('c1'))[0]
76                                         );
77                                 }, false
78                         );
79
80                         JSAN.use('util.controller'); obj.controller = new util.controller();
81                         obj.controller.init(
82                                 {
83                                         control_map : {
84                                                 'cmd_broken' : [
85                                                         ['command'],
86                                                         function() { alert('Not Yet Implemented'); }
87                                                 ],
88                                                 'result_message' : [['render'],function(e){return function(){};}],
89                                                 'server' : [['render'],function(e){return function(){};}],
90                                                 'database' : [['render'],function(e){return function(){};}],
91                                                 'port' : [['render'],function(e){return function(){};}],
92                                                 'username' : [['render'],function(e){return function(){};}],
93                                                 'password' : [['render'],function(e){return function(){};}],
94                                                 'asc_id' : [
95                                                         ['render'],
96                                                         function(e){
97                                                                 return function(){
98                                                                         e.addEventListener(
99                                                                                 'keypress',
100                                                                                 function(ev) {
101                                                                                         if (ev.keyCode && ev.keyCode == 13) {
102                                                                                                 obj.asc_search_async();
103                                                                                         }
104                                                                                 },
105                                                                                 false
106                                                                         );
107                                                                 };
108                                                         }
109                                                 ],
110                                                 'marc_import' : [
111                                                         ['command'],
112                                                         function() {
113                                                                 obj.spawn_marc_editor(
114                                                                         obj.network.simple_request(
115                                                                                 'FM_BRN_FROM_MARCXML',
116                                                                                 [
117                                                                                         obj.results.records[
118                                                                                                 obj.controller.view.marc_import.getAttribute('retrieve_id')
119                                                                                         ].marcxml
120                                                                                 ]
121                                                                         )
122                                                                 );
123                                                         },
124                                                 ],
125                                                 'asc_search' : [
126                                                         ['command'],
127                                                         function() {
128                                                                 obj.asc_search_async();
129                                                         },
130                                                 ],
131                                                 'raw_string' : [
132                                                         ['render'],
133                                                         function(e){
134                                                                 return function(){
135                                                                         e.addEventListener(
136                                                                                 'keypress',
137                                                                                 function(ev) {
138                                                                                         if (ev.keyCode && ev.keyCode == 13) {
139                                                                                                 obj.raw_search_async();
140                                                                                         }
141                                                                                 },
142                                                                                 false
143                                                                         );
144                                                                 };
145                                                         }
146                                                 ],
147                                                 'raw_search' : [
148                                                         ['command'],
149                                                         function() {
150                                                                 obj.raw_search_async();
151                                                         },
152                                                 ],
153                                                 'menu_placeholder' : [
154                                                         ['render'],
155                                                         function(e) {
156                                                                 return function() {
157                                                                         util.widgets.remove_children(e);
158                                                                         var ml = util.widgets.make_menulist( [
159                                                                                 [ 'System Defaults', 0 ],
160                                                                                 [ 'Custom', 1 ],
161                                                                         ] );
162                                                                         ml.setAttribute('flex','1');
163                                                                         e.appendChild(ml);
164                                                                         /* An experiment with virtual events.  I could just use a named function
165                                                                            instead of defining one inline, and then call that once to set things
166                                                                            up, and let the event handler call it the rest of the time. */
167                                                                         ml.addEventListener(
168                                                                                 'set_server_details',
169                                                                                 function(ev) { 
170                                                                                         /* FIXME - get these values from server */
171                                                                                         switch(ev.target.value) {
172                                                                                                 case 0: case '0':
173                                                                                                         obj.controller.view.server.value = 'zcat.oclc.org';
174                                                                                                         obj.controller.view.server.disabled = true;
175                                                                                                         obj.controller.view.database.value = 'OLUCWorldCat';
176                                                                                                         obj.controller.view.database.disabled = true;
177                                                                                                         obj.controller.view.port.value = '210';
178                                                                                                         obj.controller.view.port.disabled = true;
179                                                                                                         obj.controller.view.username.value = '****';
180                                                                                                         obj.controller.view.username.disabled = true;
181                                                                                                         obj.controller.view.password.value = '****';
182                                                                                                         obj.controller.view.password.disabled = true;
183                                                                                                         obj.controller.view.raw_string.value = 'DISABLED';
184                                                                                                         obj.controller.view.raw_string.disabled = true;
185                                                                                                         obj.controller.view.raw_search.disabled = true;
186                                                                                                         obj.controller.view.asc_id.value = '';
187                                                                                                         obj.controller.view.asc_id.disabled = false;
188                                                                                                         obj.controller.view.asc_search.disabled = false;
189                                                                                                 break;
190                                                                                                 default:
191                                                                                                         obj.controller.view.server.disabled = false;
192                                                                                                         obj.controller.view.database.disabled = false;
193                                                                                                         obj.controller.view.server.disabled = false;
194                                                                                                         obj.controller.view.port.disabled = false;
195                                                                                                         obj.controller.view.username.value = '';
196                                                                                                         obj.controller.view.username.disabled = false;
197                                                                                                         obj.controller.view.password.value = '';
198                                                                                                         obj.controller.view.password.disabled = false;
199                                                                                                         obj.controller.view.raw_string.value = '';
200                                                                                                         obj.controller.view.raw_string.disabled = false;
201                                                                                                         obj.controller.view.raw_search.disabled = false;
202                                                                                                         obj.controller.view.asc_id.value = 'DISABLED';
203                                                                                                         obj.controller.view.asc_id.disabled = true;
204                                                                                                         obj.controller.view.asc_search.disabled = true;
205                                                                                                 break;
206                                                                                         }
207                                                                                 },
208                                                                                 false
209                                                                         );
210                                                                         ml.addEventListener(
211                                                                                 'command',
212                                                                                 function(ev) { util.widgets.dispatch('set_server_details', ev.target); },
213                                                                                 false
214                                                                         );
215                                                                         setTimeout( function() { util.widgets.dispatch('set_server_details',ml); }, 0 );
216                                                                 }
217                                                         }
218                                                 ],
219                                         }
220                                 }
221                         );
222
223                         obj.controller.render();
224
225                 } catch(E) {
226                         this.error.sdump('D_ERROR','cat.z3950.init: ' + E + '\n');
227                 }
228         },
229
230         'store_disable_search_buttons' : function() {
231                 var obj = this;
232                 JSAN.use('util.widgets');
233                 util.widgets.store_disable(
234                         obj.controller.view.asc_search,
235                         obj.controller.view.raw_search
236                 );
237                 util.widgets.disable(
238                         obj.controller.view.asc_search,
239                         obj.controller.view.raw_search
240                 );
241         },
242
243         'restore_enable_search_buttons' : function() {
244                 var obj = this;
245                 JSAN.use('util.widgets');
246                 util.widgets.restore_disable(
247                         obj.controller.view.asc_search,
248                         obj.controller.view.raw_search
249                 );
250         },
251
252         'asc_search_async' : function() {
253                 try {
254                         var obj = this;
255                         var search = obj.controller.view.asc_id.value;
256                         obj.error.sdump('D_TRACE','search string: ' + search);
257                         JSAN.use('util.widgets');
258                         util.widgets.remove_children( obj.controller.view.result_message );
259                         obj.controller.view.result_message.appendChild(
260                                 document.createTextNode( 'Searching...' )
261                         );
262                         obj.store_disable_search_buttons();
263                         obj.network.simple_request(
264                                 'FM_BRN_RETRIEVE_VIA_Z3950_TCN',
265                                 [ obj.session, search ],
266                                 function(req) {
267                                         obj.handle_results(req.getResultObject())
268                                         obj.restore_enable_search_buttons();
269                                 }
270                         );
271                 } catch(E) {
272                         this.error.sdump('D_ERROR',E);
273                         alert(E);
274                 }
275
276         },
277
278         'raw_search_async' : function() {
279                 try {
280                         var obj = this;
281                         var search = obj.controller.view.raw_string.value;
282                         obj.error.sdump('D_TRACE','search string: ' + search);
283                         JSAN.use('util.widgets');
284                         util.widgets.remove_children( obj.controller.view.result_message );
285                         obj.controller.view.result_message.appendChild(
286                                 document.createTextNode( 'Searching...' )
287                         );
288                         obj.store_disable_search_buttons();
289                         obj.network.simple_request(
290                                 'FM_BRN_RETRIEVE_VIA_Z3950_RAW',
291                                 [ 
292                                         obj.session, 
293                                         obj.controller.view.server.value, 
294                                         obj.controller.view.port.value, 
295                                         obj.controller.view.database.value, 
296                                         search, 
297                                         obj.controller.view.username.value, 
298                                         obj.controller.view.password.value 
299                                 ],
300                                 function(req) {
301                                         obj.handle_results(req.getResultObject())
302                                         obj.restore_enable_search_buttons();
303                                 }
304                         );
305                 } catch(E) {
306                         this.error.sdump('D_ERROR',E);
307                         alert(E);
308                 }
309         },
310
311         'handle_results' : function(results) {
312                 var obj = this;
313                 JSAN.use('util.widgets');
314                 util.widgets.remove_children( obj.controller.view.result_message );
315                 if (results == null) {
316                         obj.controller.view.result_message.appendChild(
317                                 document.createTextNode( 'Server Error: ' + api.FM_BRN_RETRIEVE_VIA_Z3950_TCN.method + ' returned null' )
318                         );
319                         return;
320                 }
321                 if (results.count) {
322                         obj.controller.view.result_message.appendChild(
323                                 document.createTextNode( results.count + (results.count == 1 ? ' result ' : ' results ') + 'found. ' )
324                         );
325                 }
326                 if (results.records) {
327                         obj.controller.view.result_message.appendChild(
328                                 document.createTextNode( results.records.length + (results.records.length == 1 ? ' result' : ' results') + ' retrieved. ')
329                         );
330                         obj.results = results;
331                         obj.list.clear(); obj.controller.view.marc_import.disabled = true;
332                         for (var i = 0; i < obj.results.records.length; i++) {
333                                 obj.list.append(
334                                         {
335                                                 'retrieve_id' : i,
336                                                 'row' : {
337                                                         'my' : {
338                                                                 'mvr' : function(a){return a;}(obj.results.records[i].mvr),
339                                                         }
340                                                 }
341                                         }
342                                 );
343                         }
344                 } else {
345                         obj.controller.view.result_message.appendChild(
346                                 document.createTextNode( 'Too many results to retrieve. ')
347                         );
348                 }
349         },
350
351         'spawn_marc_editor' : function(my_brn) {
352                 var obj = this;
353                 xulG.new_tab(
354                         xulG.url_prefix(urls.XUL_MARC_EDIT) + '?session=' + window.escape(obj.session), 
355                         { 'tab_name' : 'MARC Editor' }, { 'import_tree' : my_brn } 
356                 );
357         },
358
359 }
360
361 dump('exiting cat.z3950.js\n');