moved vandelay into the new TT infrastructure. still using dtd for now.
[working/Evergreen.git] / Open-ILS / web / js / ui / default / vandelay / vandelay.js
1 /* ---------------------------------------------------------------------------
2 # Copyright (C) 2008  Georgia Public Library Service
3 # Bill Erickson <erickson@esilibrary.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # --------------------------------------------------------------------------- */
15 dojo.require("dojo.parser");
16 dojo.require("dojo.io.iframe"); 
17 dojo.require("dijit.ProgressBar"); 
18 dojo.require("dijit.form.FilteringSelect"); 
19 dojo.require("dijit.layout.ContentPane");
20 dojo.require("dijit.layout.TabContainer");
21 dojo.require("dijit.layout.LayoutContainer");
22 dojo.require('dijit.form.Button');
23 dojo.require('dijit.form.CheckBox');
24 dojo.require('dijit.Toolbar');
25 dojo.require('dijit.Tooltip');
26 dojo.require('dijit.Menu');
27 dojo.require("dijit.Dialog");
28 dojo.require("dojo.cookie");
29 dojo.require('dojox.grid.DataGrid');
30 dojo.require("dojo.data.ItemFileReadStore");
31 dojo.require('dojo.date.locale');
32 dojo.require('dojo.date.stamp');
33 dojo.require("fieldmapper.Fieldmapper");
34 dojo.require("fieldmapper.dojoData");
35 dojo.require('openils.CGI');
36 dojo.require('openils.User');
37 dojo.require('openils.Event');
38 dojo.require('openils.Util');
39 dojo.require('openils.MarcXPathParser');
40 dojo.require('openils.widget.GridColumnPicker');
41
42
43 var globalDivs = [
44     'vl-generic-progress',
45     'vl-generic-progress-with-total',
46     'vl-marc-upload-div',
47     'vl-queue-div',
48     'vl-match-div',
49     'vl-marc-html-div',
50     'vl-queue-select-div',
51     'vl-marc-upload-status-div',
52     'vl-attr-editor-div',
53     'vl-marc-export-div'
54 ];
55
56 var authtoken;
57 var VANDELAY_URL = '/vandelay-upload';
58 var bibAttrDefs = [];
59 var authAttrDefs = [];
60 var queuedRecords = [];
61 var queuedRecordsMap = {};
62 var bibAttrsFetched = false;
63 var authAttrsFetched = false;
64 var attrDefMap = {}; // maps attr def code names to attr def ids
65 var currentType;
66 var currentQueueId = null;
67 var userCache = {};
68 var currentMatchedRecords; // set of loaded matched bib records
69 var currentOverlayRecordsMap; // map of import record to overlay record
70 var currentOverlayRecordsMapGid; // map of import record to overlay record grid id
71 var currentImportRecId; // when analyzing matches, this is the current import record
72 var userBibQueues = []; // only non-complete queues
73 var userAuthQueues = []; // only non-complete queues
74 var allUserBibQueues;
75 var allUserAuthQueues;
76 var selectableGridRecords;
77 var cgi = new openils.CGI();
78 var vlQueueGridColumePicker = {};
79 var vlBibSources = [];
80
81 /**
82   * Grab initial data
83   */
84 function vlInit() {
85     authtoken = openils.User.authtoken;
86     var initNeeded = 5; // how many async responses do we need before we're init'd 
87     var initCount = 0; // how many async reponses we've received
88
89     openils.Util.registerEnterHandler(
90         vlQueueDisplayPage.domNode, function(){retrieveQueuedRecords();});
91     openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
92
93     function checkInitDone() {
94         initCount++;
95         if(initCount == initNeeded)
96             runStartupCommands();
97     }
98
99     // Fetch the bib and authority attribute definitions 
100     vlFetchBibAttrDefs(function () { checkInitDone(); });
101     vlFetchAuthAttrDefs(function () { checkInitDone(); });
102
103     vlRetrieveQueueList('bib', null, 
104         function(list) {
105             allUserBibQueues = list;
106             for(var i = 0; i < allUserBibQueues.length; i++) {
107                 if(allUserBibQueues[i].complete() == 'f')
108                     userBibQueues.push(allUserBibQueues[i]);
109             }
110             checkInitDone();
111         }
112     );
113
114     vlRetrieveQueueList('auth', null, 
115         function(list) {
116             allUserAuthQueues = list;
117             for(var i = 0; i < allUserAuthQueues.length; i++) {
118                 if(allUserAuthQueues[i].complete() == 'f')
119                     userAuthQueues.push(allUserAuthQueues[i]);
120             }
121             checkInitDone();
122         }
123     );
124
125     fieldmapper.standardRequest(
126         ['open-ils.permacrud', 'open-ils.permacrud.search.cbs.atomic'],
127         {   async: true,
128             params: [authtoken, {id:{"!=":null}}, {order_by:{cbs:'id'}}],
129             oncomplete : function(r) {
130                 vlBibSources = openils.Util.readResponse(r, false, true);
131                 checkInitDone();
132             }
133         }
134     );
135
136     vlAttrEditorInit();
137 }
138
139
140 openils.Util.addOnLoad(vlInit);
141
142
143 // fetch the bib and authority attribute definitions
144
145 function vlFetchBibAttrDefs(postcomplete) {
146     bibAttrDefs = [];
147     fieldmapper.standardRequest(
148         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
149         {   async: true,
150             params: [authtoken, {id:{'!=':null}}],
151             onresponse: function(r) {
152                 var def = r.recv().content(); 
153                 if(e = openils.Event.parse(def[0])) 
154                     return alert(e);
155                 bibAttrDefs.push(def);
156             },
157             oncomplete: function() {
158                 bibAttrDefs = bibAttrDefs.sort(
159                     function(a, b) {
160                         if(a.id() > b.id()) return 1;
161                         if(a.id() < b.id()) return -1;
162                         return 0;
163                     }
164                 );
165                 postcomplete();
166             }
167         }
168     );
169 }
170
171 function vlFetchAuthAttrDefs(postcomplete) {
172     authAttrDefs = [];
173     fieldmapper.standardRequest(
174         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
175         {   async: true,
176             params: [authtoken, {id:{'!=':null}}],
177             onresponse: function(r) {
178                 var def = r.recv().content(); 
179                 if(e = openils.Event.parse(def[0])) 
180                     return alert(e);
181                 authAttrDefs.push(def);
182             },
183             oncomplete: function() {
184                 authAttrDefs = authAttrDefs.sort(
185                     function(a, b) {
186                         if(a.id() > b.id()) return 1;
187                         if(a.id() < b.id()) return -1;
188                         return 0;
189                     }
190                 );
191                 postcomplete();
192             }
193         }
194     );
195 }
196
197 function vlRetrieveQueueList(type, filter, onload) {
198     type = (type == 'bib') ? type : 'authority';
199     fieldmapper.standardRequest(
200         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
201         {   async: true,
202             params: [authtoken, null, filter],
203             oncomplete: function(r) {
204                 var list = r.recv().content();
205                 if(e = openils.Event.parse(list[0]))
206                     return alert(e);
207                 onload(list);
208             }
209         }
210     );
211
212 }
213
214 function displayGlobalDiv(id) {
215     for(var i = 0; i < globalDivs.length; i++) {
216         try {
217             dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
218         } catch(e) {
219             alert('please define div ' + globalDivs[i]);
220         }
221     }
222     dojo.style(dojo.byId(id),'display','block');
223
224     openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
225     openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
226     openils.Util.removeCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
227     openils.Util.removeCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
228
229     switch(id) {
230         case 'vl-marc-export-div':
231             openils.Util.addCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
232             break;
233         case 'vl-marc-upload-div':
234             openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
235             break;
236         case 'vl-queue-select-div':
237             openils.Util.addCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
238             break;
239         case 'vl-attr-editor-div':
240             openils.Util.addCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
241             break;
242     }
243 }
244
245 function runStartupCommands() {
246     currentQueueId = cgi.param('qid');
247     currentType = cgi.param('qtype');
248     dojo.style('vl-nav-bar', 'visibility', 'visible');
249     if(currentQueueId)
250         return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
251     vlShowUploadForm();
252 }
253
254 /**
255   * asynchronously upload a file of MARC records
256   */
257 function uploadMARC(onload){
258     dojo.byId('vl-upload-status-count').innerHTML = '0';
259     dojo.byId('vl-ses-input').value = authtoken;
260     displayGlobalDiv('vl-marc-upload-status-div');
261     dojo.io.iframe.send({
262         url: VANDELAY_URL,
263         method: "post",
264         handleAs: "html",
265         form: dojo.byId('vl-marc-upload-form'),
266         handle: function(data,ioArgs){
267             var content = data.documentElement.textContent;
268             onload(content);
269         }
270     });
271 }       
272
273 /**
274   * Creates a new vandelay queue
275   */
276 function createQueue(queueName, type, onload) {
277     var name = (type=='bib') ? 'bib' : 'authority';
278     var method = 'open-ils.vandelay.'+ name +'_queue.create'
279     fieldmapper.standardRequest(
280         ['open-ils.vandelay', method],
281         {   async: true,
282             params: [authtoken, queueName, null, name],
283             oncomplete : function(r) {
284                 var queue = r.recv().content();
285                 if(e = openils.Event.parse(queue)) 
286                     return alert(e);
287                 onload(queue);
288             }
289         }
290     );
291 }
292
293 /**
294   * Tells vandelay to pull a batch of records from the cache and explode them
295   * out into the vandelay tables
296   */
297 function processSpool(key, queueId, type, onload) {
298     fieldmapper.standardRequest(
299         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
300         {   async: true,
301             params: [authtoken, key, queueId],
302             onresponse : function(r) {
303                 var resp = r.recv().content();
304                 if(e = openils.Event.parse(resp)) 
305                     return alert(e);
306                 dojo.byId('vl-upload-status-count').innerHTML = resp;
307             },
308             oncomplete : function(r) {onload();}
309         }
310     );
311 }
312
313 function retrieveQueuedRecords(type, queueId, onload) {
314     displayGlobalDiv('vl-generic-progress');
315     queuedRecords = [];
316     queuedRecordsMap = {};
317     currentOverlayRecordsMap = {};
318     currentOverlayRecordsMapGid = {};
319     selectableGridRecords = {};
320     //resetVlQueueGridLayout();
321
322     if(!type) type = currentType;
323     if(!queueId) queueId = currentQueueId;
324     if(!onload) onload = handleRetrieveRecords;
325
326     var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
327     if(vlQueueGridShowMatches.checked)
328         method = method.replace('records', 'records.matches');
329
330     var sel = dojo.byId('vl-queue-display-limit-selector');
331     var limit = parseInt(sel.options[sel.selectedIndex].value);
332     var offset = limit * parseInt(vlQueueDisplayPage.attr('value')-1);
333
334     var params =  [authtoken, queueId, {clear_marc: 1, offset: offset, limit: limit}];
335     if(vlQueueGridShowNonImport.checked)
336         params[2].non_imported = 1;
337
338     fieldmapper.standardRequest(
339         ['open-ils.vandelay', method],
340         {   async: true,
341             params: params,
342             /*
343             onresponse: function(r) {
344                 console.log("ONREPONSE");
345                 var rec = r.recv().content();
346                 if(e = openils.Event.parse(rec))
347                     return alert(e);
348                 console.log("got record " + rec.id());
349                 queuedRecords.push(rec);
350                 queuedRecordsMap[rec.id()] = rec;
351             },
352             */
353             oncomplete: function(r){
354                 var recs = r.recv().content();
355                 if(e = openils.Event.parse(recs[0]))
356                     return alert(e);
357                 for(var i = 0; i < recs.length; i++) {
358                     var rec = recs[i];
359                     queuedRecords.push(rec);
360                     queuedRecordsMap[rec.id()] = rec;
361                 }
362                 onload();
363             }
364         }
365     );
366 }
367
368 function vlLoadMatchUI(recId) {
369     displayGlobalDiv('vl-generic-progress');
370     var matches = queuedRecordsMap[recId].matches();
371     var records = [];
372     currentImportRecId = recId;
373     for(var i = 0; i < matches.length; i++)
374         records.push(matches[i].eg_record());
375
376     var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
377     var params = [records];
378     if(currentType == 'auth') {
379         retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
380         parmas = [authtoken, records, {clear_marc:1}];
381     }
382
383     fieldmapper.standardRequest(
384         retrieve,
385         {   async: true,
386             params:params,
387             oncomplete: function(r) {
388                 var recs = r.recv().content();
389                 if(e = openils.Event.parse(recs))
390                     return alert(e);
391
392                 /* ui mangling */
393                 displayGlobalDiv('vl-match-div');
394                 resetVlMatchGridLayout();
395                 currentMatchedRecords = recs;
396                 vlMatchGrid.setStructure(vlMatchGridLayout);
397
398                 // build the data store of records with match information
399                 var dataStore = bre.toStoreData(recs, null, 
400                     {virtualFields:['dest_matchpoint', 'src_matchpoint', '_id']});
401                 dataStore.identifier = '_id';
402
403                 var matchSeenMap = {};
404
405                 for(var i = 0; i < dataStore.items.length; i++) {
406                     var item = dataStore.items[i];
407                     item._id = i; // just need something unique
408                     for(var j = 0; j < matches.length; j++) {
409                         var match = matches[j];
410                         if(match.eg_record() == item.id && !matchSeenMap[match.id()]) {
411                             item.dest_matchpoint = match.field_type();
412                             var attr = getRecAttrFromMatch(queuedRecordsMap[recId], match);
413                             item.src_matchpoint = getRecAttrDefFromAttr(attr, currentType).code();
414                             matchSeenMap[match.id()] = 1;
415                             break;
416                         }
417                     }
418                 }
419
420                 // now populate the grid
421                 vlPopulateMatchGrid(vlMatchGrid, dataStore);
422             }
423         }
424     );
425 }
426
427 function vlPopulateMatchGrid(grid, data) {
428     var store = new dojo.data.ItemFileReadStore({data:data});
429     grid.setStore(store);
430     grid.update();
431 }
432
433 function showMe(id) {
434     dojo.style(dojo.byId(id), 'display', 'block');
435 }
436 function hideMe(id) {
437     dojo.style(dojo.byId(id), 'display', 'none');
438 }
439
440
441 function vlLoadMARCHtml(recId, inCat, oncomplete) {
442     dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
443     displayGlobalDiv('vl-generic-progress');
444     var api;
445     var params = [recId, 1];
446
447     if(inCat) {
448         hideMe('vl-marc-html-edit-button'); // don't show marc editor button
449         dijit.byId('vl-marc-html-edit-button').onClick = function(){}
450         api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
451         if(currentType == 'auth')
452             api = ['open-ils.search', 'open-ils.search.authority.to_html'];
453     } else {
454         showMe('vl-marc-html-edit-button'); // plug in the marc editor button
455         dijit.byId('vl-marc-html-edit-button').onClick = 
456             function() {vlLoadMarcEditor(currentType, recId, oncomplete);};
457         params = [authtoken, recId];
458         api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
459         if(currentType == 'auth')
460             api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
461     }
462
463     fieldmapper.standardRequest(
464         api, 
465         {   async: true,
466             params: params,
467             oncomplete: function(r) {
468             displayGlobalDiv('vl-marc-html-div');
469                 var html = r.recv().content();
470                 dojo.byId('vl-marc-record-html').innerHTML = html;
471             }
472         }
473     );
474 }
475
476
477 /*
478 function getRecMatchesFromAttrCode(rec, attrCode) {
479     var matches = [];
480     var attr = getRecAttrFromCode(rec, attrCode);
481     for(var j = 0; j < rec.matches().length; j++) {
482         var match = rec.matches()[j];
483         if(match.matched_attr() == attr.id()) 
484             matches.push(match);
485     }
486     return matches;
487 }
488 */
489
490 function getRecAttrFromMatch(rec, match) {
491     for(var i = 0; i < rec.attributes().length; i++) {
492         var attr = rec.attributes()[i];
493         if(attr.id() == match.matched_attr())
494             return attr;
495     }
496 }
497
498 function getRecAttrDefFromAttr(attr, type) {
499     var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
500     for(var i = 0; i < defs.length; i++) {
501         var def = defs[i];
502         if(def.id() == attr.field())
503             return def;
504     }
505 }
506
507 function getRecAttrFromCode(rec, attrCode) {
508     var defId = attrDefMap[currentType][attrCode];
509     var attrs = rec.attributes();
510     for(var i = 0; i < attrs.length; i++) {
511         var attr = attrs[i];
512         if(attr.field() == defId) 
513             return attr;
514     }
515     return null;
516 }
517
518 function vlGetViewMatches(rowIdx, item) {
519     if(!item) return '';
520     var id = this.grid.store.getValue(item, 'id');
521     var rec = queuedRecordsMap[id];
522     if(rec.matches().length > 0)
523         return this.value.replace('RECID', id);
524     return '';
525 }
526
527 function getAttrValue(rowIdx, item) {
528     if(!item) return '';
529     var attrCode = this.field.split('.')[1];
530     var rec = queuedRecordsMap[this.grid.store.getValue(item, 'id')];
531     var attr = getRecAttrFromCode(rec, attrCode);
532     return (attr) ? attr.attr_value() : '';
533 }
534
535 function vlGetDateTimeField(rowIdx, item) {
536     if(!item) return '';
537     var value = this.grid.store.getValue(item, this.field);
538     if(!value) return '';
539     var date = dojo.date.stamp.fromISOString(value);
540     return dojo.date.locale.format(date, {selector:'date'});
541 }
542
543 function vlGetCreator(rowIdx, item) {
544     if(!item) return '';
545     var id = this.grid.store.getValue(item, 'creator');
546     if(userCache[id])
547         return userCache[id].usrname();
548     var user = fieldmapper.standardRequest(
549         ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
550     if(e = openils.Event.parse(user))
551         return alert(e);
552     userCache[id] = user;
553     return user.usrname();
554 }
555
556 function vlGetViewMARC(rowIdx, item) {
557     if(!item) return '';
558     return this.value.replace('RECID', this.grid.store.getValue(item, 'id'));
559 }
560
561 function vlGetOverlayTargetSelector(rowIdx, item) {
562     if(!item) return '';
563     var _id = this.grid.store.getValue(item, '_id');
564     var id = this.grid.store.getValue(item, 'id');
565     var value = this.value.replace(/GRIDID/g, _id);
566     value = value.replace(/RECID/g, currentImportRecId);
567     value = value.replace(/ID/g, id);
568     if(_id == currentOverlayRecordsMapGid[currentImportRecId])
569         return value.replace('/>', 'checked="checked"/>');
570     return value;
571 }
572
573 /**
574   * see if the user has enabled overlays for the current match set and, 
575   * if so, map the current import record to the overlay target.
576   */
577 function vlHandleOverlayTargetSelected(recId, gridId) {
578     var noneSelected = true;
579     var checkboxes = dojo.query('[name=vl-overlay-target-'+currentImportRecId+']');
580     for(var i = 0; i < checkboxes.length; i++) {
581         var checkbox = checkboxes[i];
582         var matchRecId = checkbox.getAttribute('match');
583         var gid = checkbox.getAttribute('gridid');
584         if(checkbox.checked) {
585             if(matchRecId == recId && gid == gridId) {
586                 noneSelected = false;
587                 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
588                 currentOverlayRecordsMapGid[currentImportRecId] = gid;
589                 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
590                 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
591             } else {
592                 checkbox.checked = false;
593             }
594         }
595     }
596
597     if(noneSelected) {
598         delete currentOverlayRecordsMap[currentImportRecId];
599         delete currentOverlayRecordsMapGid[currentImportRecId];
600         dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
601         dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = '';
602     }
603 }
604
605 var valLastQueueType = null;
606 function buildRecordGrid(type) {
607     displayGlobalDiv('vl-queue-div');
608
609     if(type == 'bib') {
610         openils.Util.show('vl-bib-queue-grid-wrapper');
611         openils.Util.hide('vl-auth-queue-grid-wrapper');
612         vlQueueGrid = vlBibQueueGrid;
613         vlQueueGridMenu = vlBibQueueGridMenu;
614     } else {
615         openils.Util.show('vl-auth-queue-grid-wrapper');
616         openils.Util.hide('vl-bib-queue-grid-wrapper');
617         vlQueueGrid = vlAuthQueueGrid;
618         vlQueueGridMenu = vlAuthQueueGridMenu;
619     }
620
621     if(valLastQueueType != type) {
622         valLastQueueType = type;
623         resetVlQueueGridLayout();
624         var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
625         attrDefMap[type] = {};
626         for(var i = 0; i < defs.length; i++) {
627             var def = defs[i]
628             attrDefMap[type][def.code()] = def.id();
629             var col = {
630                 name:def.description(), 
631                 field:'attr.' + def.code(),
632                 get: getAttrValue,
633                 selectableColumn:true
634             };
635             vlQueueGridLayout[0].cells[0].push(col);
636         }
637     }
638
639     var storeData;
640     if(type == 'bib')
641         storeData = vqbr.toStoreData(queuedRecords);
642     else
643         storeData = vqar.toStoreData(queuedRecords);
644
645     var store = new dojo.data.ItemFileReadStore({data:storeData});
646     vlQueueGrid.setStore(store);
647
648     if(vlQueueGridColumePicker[type]) {
649         vlQueueGrid.update();
650     } else {
651         vlQueueGrid.attr('structure', vlQueueGridLayout);
652         vlQueueGridMenu.init({
653             grid : vlQueueGrid, 
654             authtoken : authtoken, 
655             prefix : 'vandelay.queue.'+type
656         });
657         vlQueueGridMenu.load();
658         vlQueueGridColumePicker[type] = vlQueueGridMenu;
659     }
660 }
661
662 function vlQueueGridPrevPage() {
663     var page = parseInt(vlQueueDisplayPage.getValue());
664     if(page < 2) return;
665     vlQueueDisplayPage.setValue(page - 1);
666     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
667 }
668
669 function vlQueueGridNextPage() {
670     vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
671     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
672 }
673
674 function vlDeleteQueue(type, queueId, onload) {
675     fieldmapper.standardRequest(
676         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
677         {   async: true,
678             params: [authtoken, queueId],
679             oncomplete: function(r) {
680                 var resp = r.recv().content();
681                 if(e = openils.Event.parse(resp))
682                     return alert(e);
683                 onload();
684             }
685         }
686     );
687 }
688
689
690 function vlQueueGridDrawSelectBox(rowIdx, item) {
691     if(!item) return '';
692     var id = this.grid.store.getValue(item, 'id');
693     var domId = 'vl-record-list-selected-' + id;
694     selectableGridRecords[domId] = id;
695     return "<div><input type='checkbox' id='"+domId+"'/></div>";
696 }
697
698 function vlSelectAllQueueGridRecords() {
699     for(var id in selectableGridRecords) 
700         dojo.byId(id).checked = true;
701 }
702 function vlSelectNoQueueGridRecords() {
703     for(var id in selectableGridRecords) 
704         dojo.byId(id).checked = false;
705 }
706 function vlToggleQueueGridSelect() {
707     if(dojo.byId('vl-queue-grid-row-selector').checked)
708         vlSelectAllQueueGridRecords();
709     else
710         vlSelectNoQueueGridRecords();
711 }
712
713 var handleRetrieveRecords = function() {
714     buildRecordGrid(currentType);
715     vlFetchQueueSummary(currentQueueId, currentType, 
716         function(summary) {
717             dojo.byId('vl-queue-summary-name').innerHTML = summary.queue.name();
718             dojo.byId('vl-queue-summary-total-count').innerHTML = summary.total +'';
719             dojo.byId('vl-queue-summary-import-count').innerHTML = summary.imported + '';
720         }
721     );
722 }
723
724 function vlFetchQueueSummary(qId, type, onload) {
725     fieldmapper.standardRequest(
726         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.summary.retrieve'],
727         {   async: true,
728             params: [authtoken, qId],
729             oncomplete : function(r) {
730                 var summary = r.recv().content();
731                 if(e = openils.Event.parse(summary))
732                     return alert(e);
733                 return onload(summary);
734             }
735         }
736     );
737 }
738     
739
740 function vlImportSelectedRecords() {
741     displayGlobalDiv('vl-generic-progress-with-total');
742     var records = [];
743
744     for(var id in selectableGridRecords) {
745         if(dojo.byId(id).checked) {
746             var recId = selectableGridRecords[id];
747             var rec = queuedRecordsMap[recId];
748             if(!rec.import_time()) 
749                 records.push(recId);
750         }
751     }
752
753     fieldmapper.standardRequest(
754         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
755         {   async: true,
756             params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
757             onresponse: function(r) {
758                 var resp = r.recv().content();
759                 if(e = openils.Event.parse(resp))
760                     return alert(e);
761                 if(resp.complete) {
762                     return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
763                 } else {
764                     vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
765                 }
766             }, 
767         }
768     );
769 }
770
771 function vlImportAllRecords() {
772     vlImportRecordQueue(currentType, currentQueueId, false,
773         function(){displayGlobalDiv('vl-queue-div');});
774 }
775
776 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
777     displayGlobalDiv('vl-generic-progress-with-total');
778     var method = 'open-ils.vandelay.bib_queue.import';
779     if(noMatchOnly)
780         method = method.replace('import', 'nomatch.import');
781     if(type == 'auth')
782         method = method.replace('bib', 'auth');
783
784     fieldmapper.standardRequest(
785         ['open-ils.vandelay', method],
786         {   async: true,
787             params: [authtoken, queueId],
788             onresponse: function(r) {
789                 var resp = r.recv().content();
790                 if(e = openils.Event.parse(resp))
791                     return alert(e);
792                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
793             },
794             oncomplete: function() {onload();}
795         }
796     );
797 }
798
799
800 /**
801   * Create queue, upload MARC, process spool, load the newly created queue 
802   */
803 function batchUpload() {
804     var queueName = dijit.byId('vl-queue-name').getValue();
805     currentType = dijit.byId('vl-record-type').getValue();
806
807     var handleProcessSpool = function() {
808         if(vlUploadQueueAutoImport.checked) {
809             vlImportRecordQueue(currentType, currentQueueId, true,  
810                 function() {
811                     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
812                 }
813             );
814         } else {
815             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
816         }
817     }
818
819     var handleUploadMARC = function(key) {
820         dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
821         processSpool(key, currentQueueId, currentType, handleProcessSpool);
822     };
823
824     var handleCreateQueue = function(queue) {
825         currentQueueId = queue.id();
826         uploadMARC(handleUploadMARC);
827     };
828     
829     if(vlUploadQueueSelector.getValue() && !queueName) {
830         currentQueueId = vlUploadQueueSelector.getValue();
831         uploadMARC(handleUploadMARC);
832     } else {
833         createQueue(queueName, currentType, handleCreateQueue);
834     }
835 }
836
837
838 function vlFleshQueueSelect(selector, type) {
839     var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
840     selector.store = new dojo.data.ItemFileReadStore({data:data});
841     selector.setValue(null);
842     selector.setDisplayedValue('');
843     if(data[0])
844         selector.setValue(data[0].id());
845 }
846
847 function vlShowUploadForm() {
848     displayGlobalDiv('vl-marc-upload-div');
849     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
850     vlUploadSourceSelector.store = 
851         new dojo.data.ItemFileReadStore({data:cbs.toStoreData(vlBibSources, 'source')});
852     vlUploadSourceSelector.setValue(vlBibSources[0].id());
853 }
854
855 function vlShowQueueSelect() {
856     displayGlobalDiv('vl-queue-select-div');
857     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
858 }
859
860 function vlFetchQueueFromForm() {
861     currentType = vlQueueSelectType.getValue();
862     currentQueueId = vlQueueSelectQueueList.getValue();
863     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
864 }
865
866 function vlOpenMarcEditWindow(rec, postReloadHTMLHandler) {
867     /*
868         To run in Firefox directly, must set signed.applets.codebase_principal_support
869         to true in about:config
870     */
871     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
872     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
873
874     function onsave(r) {
875         // after the record is saved, reload the HTML display
876         var stat = r.recv().content();
877         if(e = openils.Event.parse(stat))
878             return alert(e);
879         alert(dojo.byId('vl-marc-edit-complete-label').innerHTML);
880         win.close();
881         vlLoadMARCHtml(rec.id(), false, postReloadHTMLHandler);
882     }
883
884     win.xulG = {
885         record : {marc : rec.marc()},
886         save : {
887             label: dojo.byId('vl-marc-edit-save-label').innerHTML,
888             func: function(xmlString) {
889                 var method = 'open-ils.permacrud.update.' + rec.classname;
890                 rec.marc(xmlString);
891                 fieldmapper.standardRequest(
892                     ['open-ils.permacrud', method],
893                     {   async: true,
894                         params: [authtoken, rec],
895                         oncomplete: onsave
896                     }
897                 );
898             },
899         }
900     };
901 }
902
903 function vlLoadMarcEditor(type, recId, postReloadHTMLHandler) {
904     var method = 'open-ils.permacrud.search.vqbr';
905     if(currentType != 'bib')
906         method = method.replace(/vqbr/,'vqar');
907
908     fieldmapper.standardRequest(
909         ['open-ils.permacrud', method],
910         {   async: true, 
911             params: [authtoken, {id : recId}],
912             oncomplete: function(r) {
913                 var rec = r.recv().content();
914                 if(e = openils.Event.parse(rec))
915                     return alert(e);
916                 vlOpenMarcEditWindow(rec, postReloadHTMLHandler);
917             }
918         }
919     );
920 }
921
922
923
924 //------------------------------------------------------------
925 // attribute editors
926
927 // attribute-editor global variables
928
929 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
930 var ATTR_EDIT_ID = null;                // id of current 'edit' attribute
931 var ATTR_EDIT_GROUP = 'bib';            // bib-attrs or auth-attrs
932
933 function vlAttrEditorInit() {
934     // set up tooltips on the edit form
935     connectTooltip('attr-editor-tags'); 
936     connectTooltip('attr-editor-subfields'); 
937 }
938
939 function vlShowAttrEditor() {
940     displayGlobalDiv('vl-attr-editor-div');
941     loadAttrEditorGrid();
942     idHide('vl-generic-progress');
943 }
944
945 function setAttrEditorGroup(groupName) {
946     // put us into 'bib'-attr or 'auth'-attr mode.
947     if (ATTR_EDIT_GROUP != groupName) {
948         ATTR_EDIT_GROUP = groupName;
949         loadAttrEditorGrid();
950     }
951 }
952
953 function onAttrEditorOpen() {
954     // the "bars" have the create/update/cancel/etc. buttons.
955     var create_bar = document.getElementById('attr-editor-create-bar');
956     var update_bar = document.getElementById('attr-editor-update-bar');
957     if (ATTR_EDITOR_IN_UPDATE_MODE) {
958         update_bar.style.display='table-row';
959         create_bar.style.display='none';
960         // hide the dropdown-button
961         idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
962     } else {
963         dijit.byId('attr-editor-dialog').reset();
964         create_bar.style.display='table-row';
965         update_bar.style.display='none';
966     }
967 }
968
969 function onAttrEditorClose() {
970     // reset the form to a "create" form. (We may have borrowed it for editing.)
971     ATTR_EDITOR_IN_UPDATE_MODE = false;
972     // show the dropdown-button
973     idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
974 }
975
976 function loadAttrEditorGrid() {
977     var _data = (ATTR_EDIT_GROUP == 'auth') ? 
978         vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
979
980     var store = new dojo.data.ItemFileReadStore({data:_data});
981     attrEditorGrid.setStore(store);
982     dojo.connect(attrEditorGrid, 'onRowDblClick', onAttrEditorClick);
983     attrEditorGrid.update();
984 }
985
986 function attrGridGetTag(n, item) {
987     // grid helper: return the tags from the row's xpath column.
988     return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).tags;
989 }
990
991 function attrGridGetSubfield(n, item) {
992     // grid helper: return the subfields from the row's xpath column.
993     return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).subfields;
994 }
995
996 function onAttrEditorClick() {
997     var row = this.getItem(this.focus.rowIndex);
998     ATTR_EDIT_ID = this.store.getValue(row, 'id');
999     ATTR_EDITOR_IN_UPDATE_MODE = true;
1000
1001     // populate the popup editor.
1002     dijit.byId('attr-editor-code').attr('value', this.store.getValue(row, 'code'));
1003     dijit.byId('attr-editor-description').attr('value', this.store.getValue(row, 'description'));
1004     var parsed_xpath = xpathParser.parse(this.store.getValue(row, 'xpath'));
1005     dijit.byId('attr-editor-tags').attr('value', parsed_xpath.tags);
1006     dijit.byId('attr-editor-subfields').attr('value', parsed_xpath.subfields);
1007     dijit.byId('attr-editor-identifier').attr('value', this.store.getValue(row, 'ident'));
1008     dijit.byId('attr-editor-xpath').attr('value', this.store.getValue(row, 'xpath'));
1009     dijit.byId('attr-editor-remove').attr('value', this.store.getValue(row, 'remove'));
1010
1011     // set up UI for editing
1012     dojo.byId('vl-create-attr-editor-button').click();
1013 }
1014
1015 function vlSaveAttrDefinition(data) {
1016     idHide('vl-attr-editor-div');
1017     idShow('vl-generic-progress');
1018
1019     data.id = ATTR_EDIT_ID;
1020
1021     // this ought to honour custom xpaths, but overwrite xpaths
1022     // derived from tags/subfields.
1023     if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
1024         var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
1025         data.xpath = _xpath;
1026     }
1027
1028     // build up our permacrud params. Key variables here are
1029     // "create or update" and "bib or auth".
1030
1031     var isAuth   = (ATTR_EDIT_GROUP == 'auth');
1032     var isCreate = (ATTR_EDIT_ID == null);
1033     var rad      = isAuth ? new vqarad() : new vqbrad() ;
1034     var method   = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.') 
1035         + (isAuth ? 'vqarad' : 'vqbrad');
1036     var _data    = rad.fromStoreItem(data);
1037
1038     _data.ischanged(1);
1039
1040     fieldmapper.standardRequest(
1041         ['open-ils.permacrud', method],
1042         {   async: true,
1043             params: [authtoken, _data ],
1044             onresponse: function(r) { },
1045             oncomplete: function(r) {
1046                 attrEditorFetchAttrDefs(vlShowAttrEditor);
1047                 ATTR_EDIT_ID = null;
1048             },
1049             onerror: function(r) {
1050                 alert('vlSaveAttrDefinition comms error: ' + r);
1051             }
1052         }
1053     );
1054 }
1055
1056 function attrEditorFetchAttrDefs(callback) {
1057     var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
1058     return fn(callback);
1059 }
1060
1061 function vlAttrDelete() {
1062     idHide('vl-attr-editor-div');
1063     idShow('vl-generic-progress');
1064
1065     var isAuth = (ATTR_EDIT_GROUP == 'auth');
1066     var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
1067     var rad    = isAuth ? new vqarad() : new vqbrad() ;
1068     fieldmapper.standardRequest(
1069         ['open-ils.permacrud', method],
1070         {   async: true,
1071             params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
1072             oncomplete: function() {
1073                 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
1074                 attrEditorFetchAttrDefs(vlShowAttrEditor);
1075                 ATTR_EDIT_ID = null;
1076             },
1077             onerror: function(r) {
1078                 alert('vlAttrDelete comms error: ' + r);
1079             }
1080         }
1081     );
1082 }
1083
1084 // ------------------------------------------------------------
1085 // utilities for attribute editors
1086
1087 // dom utilities (maybe dojo does these, and these should be replaced)
1088
1089 function idStyle(obId, k, v)    { document.getElementById(obId).style[k] = v;   }
1090 function idShow(obId)           { idStyle(obId, 'display', 'block');            }
1091 function idHide(obId)           { idStyle(obId, 'display' , 'none');            }
1092
1093 function connectTooltip(fieldId) {
1094     // Given an element id, look up a tooltip element in the doc (same
1095     // id with a '-tip' suffix) and associate the two. Maybe dojo has
1096     // a better way to do this?
1097     var fld = dojo.byId(fieldId);
1098     var tip = dojo.byId(fieldId + '-tip');
1099     dojo.connect(fld, 'onfocus', function(evt) {
1100                      dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1101     dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1102 }
1103
1104 // xpath utilities
1105
1106 var xpathParser = new openils.MarcXPathParser();
1107
1108 function tagAndSubFieldsToXpath(tags, subfields) {
1109     // given tags, and subfields, build up an XPath.
1110     try {
1111         var parts = {
1112             'tags':tags.match(/[\d]+/g), 
1113             'subfields':subfields.match(/[a-zA-z]/g) };
1114         return xpathParser.compile(parts);
1115     } catch (err) {
1116         return {'parts':null, 'tags':null, 'error':err};
1117     }
1118 }
1119
1120 function looksLikeDerivedXpath(path) {
1121     // Does this path look like it was derived from tags and subfields?
1122     var parsed = xpathParser.parse(path);
1123     if (parsed.tags == null) 
1124         return false;
1125     var compiled = xpathParser.compile(parsed);
1126     return (path == compiled);
1127 }
1128
1129 // amazing xpath-util unit-tests
1130 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]'))     alert('vandelay xpath-utility error');
1131 if ( looksLikeDerivedXpath('ba-boo-ba-boo!'))                   alert('vandelay xpath-utility error');