tweaks
[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                                                                                 [ 'OCLC', 'oclc' ],
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 'oclc':
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                                                                         obj.controller.view.server_menu = ml;
217                                                                 }
218                                                         }
219                                                 ],
220                                         }
221                                 }
222                         );
223
224                         obj.controller.render();
225
226                 } catch(E) {
227                         this.error.sdump('D_ERROR','cat.z3950.init: ' + E + '\n');
228                 }
229         },
230
231         'store_disable_search_buttons' : function() {
232                 var obj = this;
233                 JSAN.use('util.widgets');
234                 util.widgets.store_disable(
235                         obj.controller.view.asc_search,
236                         obj.controller.view.raw_search
237                 );
238                 util.widgets.disable(
239                         obj.controller.view.asc_search,
240                         obj.controller.view.raw_search
241                 );
242         },
243
244         'restore_enable_search_buttons' : function() {
245                 var obj = this;
246                 JSAN.use('util.widgets');
247                 util.widgets.restore_disable(
248                         obj.controller.view.asc_search,
249                         obj.controller.view.raw_search
250                 );
251         },
252
253         'asc_search_async' : function() {
254                 try {
255                         var obj = this;
256                         var search = obj.controller.view.asc_id.value;
257                         obj.error.sdump('D_TRACE','search string: ' + search);
258                         JSAN.use('util.widgets');
259                         util.widgets.remove_children( obj.controller.view.result_message );
260                         obj.controller.view.result_message.appendChild(
261                                 document.createTextNode( 'Searching...' )
262                         );
263                         obj.store_disable_search_buttons();
264                         var params;
265                         if (!Number(obj.controller.view.server_menu.value)) {
266                                 params = [ obj.session, search, obj.controller.view.server_menu.value ];
267                         } else {
268                                 params = [ obj.session, search, 'oclc' ];
269                         }
270                         obj.network.simple_request(
271                                 'FM_BLOB_RETRIEVE_VIA_Z3950_TCN',
272                                 [ obj.session, search, 'oclc' ],
273                                 function(req) {
274                                         obj.handle_results(req.getResultObject())
275                                         obj.restore_enable_search_buttons();
276                                 }
277                         );
278                 } catch(E) {
279                         this.error.sdump('D_ERROR',E);
280                         alert(E);
281                 }
282
283         },
284
285         'raw_search_async' : function() {
286                 try {
287                         var obj = this;
288                         var search = obj.controller.view.raw_string.value;
289                         obj.error.sdump('D_TRACE','search string: ' + search);
290                         JSAN.use('util.widgets');
291                         util.widgets.remove_children( obj.controller.view.result_message );
292                         obj.controller.view.result_message.appendChild(
293                                 document.createTextNode( 'Searching...' )
294                         );
295                         obj.store_disable_search_buttons();
296                         if (!Number(obj.controller.view.server_menu.value)) {
297                                 params = [ 
298                                         obj.session, 
299                                         {
300                                                 'search':search,
301                                                 'service':obj.controller.view.server_menu.value,
302                                         }
303                                 ];
304                         } else {
305                                 params = [ 
306                                         obj.session, 
307                                         {
308                                                 'search':search,
309                                                 'server':obj.controller.view.server.value, 
310                                                 'prt':obj.controller.view.port.value, 
311                                                 'db':obj.controller.view.database.value, 
312                                                 'username':obj.controller.view.username.value, 
313                                                 'password':obj.controller.view.password.value 
314                                         }
315
316                                 ];
317                         }
318                         obj.network.simple_request(
319                                 'FM_BLOB_RETRIEVE_VIA_Z3950_RAW',
320                                 params,
321                                 function(req) {
322                                         obj.handle_results(req.getResultObject())
323                                         obj.restore_enable_search_buttons();
324                                 }
325                         );
326                 } catch(E) {
327                         this.error.sdump('D_ERROR',E);
328                         alert(E);
329                 }
330         },
331
332         'handle_results' : function(results) {
333                 var obj = this;
334                 JSAN.use('util.widgets');
335                 util.widgets.remove_children( obj.controller.view.result_message );
336                 if (results == null) {
337                         obj.controller.view.result_message.appendChild(
338                                 document.createTextNode( 'Server Error: ' + api.FM_BLOB_RETRIEVE_VIA_Z3950_TCN.method + ' returned null' )
339                         );
340                         return;
341                 }
342                 if (results.count) {
343                         obj.controller.view.result_message.appendChild(
344                                 document.createTextNode( results.count + (results.count == 1 ? ' result ' : ' results ') + 'found. ' )
345                         );
346                 }
347                 if (results.records) {
348                         obj.controller.view.result_message.appendChild(
349                                 document.createTextNode( results.records.length + (results.records.length == 1 ? ' result' : ' results') + ' retrieved. ')
350                         );
351                         obj.results = results;
352                         obj.list.clear(); obj.controller.view.marc_import.disabled = true;
353                         for (var i = 0; i < obj.results.records.length; i++) {
354                                 obj.list.append(
355                                         {
356                                                 'retrieve_id' : i,
357                                                 'row' : {
358                                                         'my' : {
359                                                                 'mvr' : function(a){return a;}(obj.results.records[i].mvr),
360                                                         }
361                                                 }
362                                         }
363                                 );
364                         }
365                 } else {
366                         obj.controller.view.result_message.appendChild(
367                                 document.createTextNode( 'Too many results to retrieve. ')
368                         );
369                 }
370         },
371
372         'spawn_marc_editor' : function(my_brn) {
373                 var obj = this;
374                 xulG.new_tab(
375                         xulG.url_prefix(urls.XUL_MARC_EDIT) + '?session=' + window.escape(obj.session), 
376                         { 'tab_name' : 'MARC Editor' }, { 'import_tree' : my_brn } 
377                 );
378         },
379
380 }
381
382 dump('exiting cat.z3950.js\n');