refactoring so marcxml_to_brn is its own method
[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.results.records[
115                                                                                 obj.controller.view.marc_import.getAttribute('retrieve_id')
116                                                                         ].brn
117                                                                 );
118                                                         },
119                                                 ],
120                                                 'asc_search' : [
121                                                         ['command'],
122                                                         function() {
123                                                                 obj.asc_search_async();
124                                                         },
125                                                 ],
126                                                 'raw_string' : [
127                                                         ['render'],
128                                                         function(e){
129                                                                 return function(){
130                                                                         e.addEventListener(
131                                                                                 'keypress',
132                                                                                 function(ev) {
133                                                                                         if (ev.keyCode && ev.keyCode == 13) {
134                                                                                                 obj.raw_search_async();
135                                                                                         }
136                                                                                 },
137                                                                                 false
138                                                                         );
139                                                                 };
140                                                         }
141                                                 ],
142                                                 'raw_search' : [
143                                                         ['command'],
144                                                         function() {
145                                                                 obj.raw_search_async();
146                                                         },
147                                                 ],
148                                                 'menu_placeholder' : [
149                                                         ['render'],
150                                                         function(e) {
151                                                                 return function() {
152                                                                         util.widgets.remove_children(e);
153                                                                         var ml = util.widgets.make_menulist( [
154                                                                                 [ 'System Defaults', 0 ],
155                                                                                 [ 'Custom', 1 ],
156                                                                         ] );
157                                                                         ml.setAttribute('flex','1');
158                                                                         e.appendChild(ml);
159                                                                         /* An experiment with virtual events.  I could just use a named function
160                                                                            instead of defining one inline, and then call that once to set things
161                                                                            up, and let the event handler call it the rest of the time. */
162                                                                         ml.addEventListener(
163                                                                                 'set_server_details',
164                                                                                 function(ev) { 
165                                                                                         /* FIXME - get these values from server */
166                                                                                         switch(ev.target.value) {
167                                                                                                 case 0: case '0':
168                                                                                                         obj.controller.view.server.value = 'zcat.oclc.org';
169                                                                                                         obj.controller.view.server.disabled = true;
170                                                                                                         obj.controller.view.database.value = 'OLUCWorldCat';
171                                                                                                         obj.controller.view.database.disabled = true;
172                                                                                                         obj.controller.view.port.value = '210';
173                                                                                                         obj.controller.view.port.disabled = true;
174                                                                                                         obj.controller.view.username.value = '****';
175                                                                                                         obj.controller.view.username.disabled = true;
176                                                                                                         obj.controller.view.password.value = '****';
177                                                                                                         obj.controller.view.password.disabled = true;
178                                                                                                         obj.controller.view.raw_string.value = 'DISABLED';
179                                                                                                         obj.controller.view.raw_string.disabled = true;
180                                                                                                         obj.controller.view.raw_search.disabled = true;
181                                                                                                         obj.controller.view.asc_id.value = '';
182                                                                                                         obj.controller.view.asc_id.disabled = false;
183                                                                                                         obj.controller.view.asc_search.disabled = false;
184                                                                                                 break;
185                                                                                                 default:
186                                                                                                         obj.controller.view.server.disabled = false;
187                                                                                                         obj.controller.view.database.disabled = false;
188                                                                                                         obj.controller.view.server.disabled = false;
189                                                                                                         obj.controller.view.port.disabled = false;
190                                                                                                         obj.controller.view.username.value = '';
191                                                                                                         obj.controller.view.username.disabled = false;
192                                                                                                         obj.controller.view.password.value = '';
193                                                                                                         obj.controller.view.password.disabled = false;
194                                                                                                         obj.controller.view.raw_string.value = '';
195                                                                                                         obj.controller.view.raw_string.disabled = false;
196                                                                                                         obj.controller.view.raw_search.disabled = false;
197                                                                                                         obj.controller.view.asc_id.value = 'DISABLED';
198                                                                                                         obj.controller.view.asc_id.disabled = true;
199                                                                                                         obj.controller.view.asc_search.disabled = true;
200                                                                                                 break;
201                                                                                         }
202                                                                                 },
203                                                                                 false
204                                                                         );
205                                                                         ml.addEventListener(
206                                                                                 'command',
207                                                                                 function(ev) { util.widgets.dispatch('set_server_details', ev.target); },
208                                                                                 false
209                                                                         );
210                                                                         setTimeout( function() { util.widgets.dispatch('set_server_details',ml); }, 0 );
211                                                                 }
212                                                         }
213                                                 ],
214                                         }
215                                 }
216                         );
217
218                         obj.controller.render();
219
220                 } catch(E) {
221                         this.error.sdump('D_ERROR','cat.z3950.init: ' + E + '\n');
222                 }
223         },
224
225         'store_disable_search_buttons' : function() {
226                 var obj = this;
227                 JSAN.use('util.widgets');
228                 util.widgets.store_disable(
229                         obj.controller.view.asc_search,
230                         obj.controller.view.raw_search
231                 );
232                 util.widgets.disable(
233                         obj.controller.view.asc_search,
234                         obj.controller.view.raw_search
235                 );
236         },
237
238         'restore_enable_search_buttons' : function() {
239                 var obj = this;
240                 JSAN.use('util.widgets');
241                 util.widgets.restore_disable(
242                         obj.controller.view.asc_search,
243                         obj.controller.view.raw_search
244                 );
245         },
246
247         'asc_search_async' : function() {
248                 try {
249                         var obj = this;
250                         var search = obj.controller.view.asc_id.value;
251                         obj.error.sdump('D_TRACE','search string: ' + search);
252                         JSAN.use('util.widgets');
253                         util.widgets.remove_children( obj.controller.view.result_message );
254                         obj.controller.view.result_message.appendChild(
255                                 document.createTextNode( 'Searching...' )
256                         );
257                         obj.store_disable_search_buttons();
258                         obj.network.simple_request(
259                                 'FM_BRN_RETRIEVE_VIA_Z3950_TCN',
260                                 [ obj.session, search ],
261                                 function(req) {
262                                         obj.handle_results(req.getResultObject())
263                                         obj.restore_enable_search_buttons();
264                                 }
265                         );
266                 } catch(E) {
267                         this.error.sdump('D_ERROR',E);
268                         alert(E);
269                 }
270
271         },
272
273         'raw_search_async' : function() {
274                 try {
275                         var obj = this;
276                         var search = obj.controller.view.raw_string.value;
277                         obj.error.sdump('D_TRACE','search string: ' + search);
278                         JSAN.use('util.widgets');
279                         util.widgets.remove_children( obj.controller.view.result_message );
280                         obj.controller.view.result_message.appendChild(
281                                 document.createTextNode( 'Searching...' )
282                         );
283                         obj.store_disable_search_buttons();
284                         obj.network.simple_request(
285                                 'FM_BRN_RETRIEVE_VIA_Z3950_RAW',
286                                 [ 
287                                         obj.session, 
288                                         obj.controller.view.server.value, 
289                                         obj.controller.view.port.value, 
290                                         obj.controller.view.database.value, 
291                                         search, 
292                                         obj.controller.view.username.value, 
293                                         obj.controller.view.password.value 
294                                 ],
295                                 function(req) {
296                                         obj.handle_results(req.getResultObject())
297                                         obj.restore_enable_search_buttons();
298                                 }
299                         );
300                 } catch(E) {
301                         this.error.sdump('D_ERROR',E);
302                         alert(E);
303                 }
304         },
305
306         'handle_results' : function(results) {
307                 var obj = this;
308                 JSAN.use('util.widgets');
309                 util.widgets.remove_children( obj.controller.view.result_message );
310                 if (results == null) {
311                         obj.controller.view.result_message.appendChild(
312                                 document.createTextNode( 'Server Error: ' + api.FM_BRN_RETRIEVE_VIA_Z3950_TCN.method + ' returned null' )
313                         );
314                         return;
315                 }
316                 if (results.count) {
317                         obj.controller.view.result_message.appendChild(
318                                 document.createTextNode( results.count + (results.count == 1 ? ' result ' : ' results ') + 'found. ' )
319                         );
320                 }
321                 if (results.records) {
322                         obj.controller.view.result_message.appendChild(
323                                 document.createTextNode( results.records.length + (results.records.length == 1 ? ' result' : ' results') + ' retrieved. ')
324                         );
325                         obj.results = results;
326                         obj.list.clear(); obj.controller.view.marc_import.disabled = true;
327                         for (var i = 0; i < obj.results.records.length; i++) {
328                                 obj.list.append(
329                                         {
330                                                 'retrieve_id' : i,
331                                                 'row' : {
332                                                         'my' : {
333                                                                 'mvr' : function(a){return a;}(obj.results.records[i].mvr),
334                                                         }
335                                                 }
336                                         }
337                                 );
338                         }
339                 } else {
340                         obj.controller.view.result_message.appendChild(
341                                 document.createTextNode( 'Too many results to retrieve. ')
342                         );
343                 }
344         },
345
346         'spawn_marc_editor' : function(my_brn) {
347                 var obj = this;
348                 xulG.new_tab(
349                         xulG.url_prefix(urls.XUL_MARC_EDIT) + '?session=' + window.escape(obj.session), 
350                         { 'tab_name' : 'MARC Editor' }, { 'import_tree' : my_brn } 
351                 );
352         },
353
354 }
355
356 dump('exiting cat.z3950.js\n');