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