]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/vandelay/vandelay.js
partially implemented bib-source selector. more to follow soon
[working/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.Util');
39 dojo.require('openils.MarcXPathParser');
40 dojo.require('openils.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 = dojo.cookie('ses') || cgi.param('ses');
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'],
127         {   async: true,
128             params: [authtoken, {id:{"!=":null}}],
129             oncomplete : function(r) {
130                 vlBibSources = openils.Util.readResponse(r, false, true);
131                 checkInitDone();
132             }
133         }
134     );
135
136     vlAttrEditorInit();
137 }
138
139
140 dojo.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 limit = parseInt(vlQueueDisplayLimit.getValue());
331     var sel = dojo.byId('vl-queue-display-limit-selector');
332     var limit = parseInt(sel.options[sel.selectedIndex].value);
333     var offset = limit * parseInt(vlQueueDisplayPage.getValue()-1);
334
335     var params =  [authtoken, queueId, {clear_marc: 1, offset: offset, limit: limit}];
336     if(vlQueueGridShowNonImport.checked)
337         params[2].non_imported = 1;
338
339     fieldmapper.standardRequest(
340         ['open-ils.vandelay', method],
341         {   async: true,
342             params: params,
343             /*
344             onresponse: function(r) {
345                 console.log("ONREPONSE");
346                 var rec = r.recv().content();
347                 if(e = openils.Event.parse(rec))
348                     return alert(e);
349                 console.log("got record " + rec.id());
350                 queuedRecords.push(rec);
351                 queuedRecordsMap[rec.id()] = rec;
352             },
353             */
354             oncomplete: function(r){
355                 var recs = r.recv().content();
356                 if(e = openils.Event.parse(recs[0]))
357                     return alert(e);
358                 for(var i = 0; i < recs.length; i++) {
359                     var rec = recs[i];
360                     queuedRecords.push(rec);
361                     queuedRecordsMap[rec.id()] = rec;
362                 }
363                 onload();
364             }
365         }
366     );
367 }
368
369 function vlLoadMatchUI(recId) {
370     displayGlobalDiv('vl-generic-progress');
371     var matches = queuedRecordsMap[recId].matches();
372     var records = [];
373     currentImportRecId = recId;
374     for(var i = 0; i < matches.length; i++)
375         records.push(matches[i].eg_record());
376
377     var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
378     var params = [records];
379     if(currentType == 'auth') {
380         retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
381         parmas = [authtoken, records, {clear_marc:1}];
382     }
383
384     fieldmapper.standardRequest(
385         retrieve,
386         {   async: true,
387             params:params,
388             oncomplete: function(r) {
389                 var recs = r.recv().content();
390                 if(e = openils.Event.parse(recs))
391                     return alert(e);
392
393                 /* ui mangling */
394                 displayGlobalDiv('vl-match-div');
395                 resetVlMatchGridLayout();
396                 currentMatchedRecords = recs;
397                 if(!vlMatchGrid.structure)
398                     vlMatchGrid.setStructure(vlMatchGridLayout);
399
400                 // build the data store of records with match information
401                 var dataStore = bre.toStoreData(recs, null, 
402                     {virtualFields:['dest_matchpoint', 'src_matchpoint', '_id']});
403                 dataStore.identifier = '_id';
404
405                 var matchSeenMap = {};
406
407                 for(var i = 0; i < dataStore.items.length; i++) {
408                     var item = dataStore.items[i];
409                     item._id = i; // just need something unique
410                     for(var j = 0; j < matches.length; j++) {
411                         var match = matches[j];
412                         if(match.eg_record() == item.id && !matchSeenMap[match.id()]) {
413                             item.dest_matchpoint = match.field_type();
414                             var attr = getRecAttrFromMatch(queuedRecordsMap[recId], match);
415                             item.src_matchpoint = getRecAttrDefFromAttr(attr, currentType).code();
416                             matchSeenMap[match.id()] = 1;
417                             break;
418                         }
419                     }
420                 }
421
422                 // now populate the grid
423                 vlPopulateMatchGrid(vlMatchGrid, dataStore);
424             }
425         }
426     );
427 }
428
429 function vlPopulateMatchGrid(grid, data) {
430     var store = new dojo.data.ItemFileReadStore({data:data});
431     var model = new dojox.grid.data.DojoData(
432         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
433     grid.setModel(model);
434     grid.update();
435 }
436
437 function showMe(id) {
438     dojo.style(dojo.byId(id), 'display', 'block');
439 }
440 function hideMe(id) {
441     dojo.style(dojo.byId(id), 'display', 'none');
442 }
443
444
445 function vlLoadMARCHtml(recId, inCat, oncomplete) {
446     dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
447     displayGlobalDiv('vl-generic-progress');
448     var api;
449     var params = [recId, 1];
450
451     if(inCat) {
452         hideMe('vl-marc-html-edit-button'); // don't show marc editor button
453         dijit.byId('vl-marc-html-edit-button').onClick = function(){}
454         api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
455         if(currentType == 'auth')
456             api = ['open-ils.search', 'open-ils.search.authority.to_html'];
457     } else {
458         showMe('vl-marc-html-edit-button'); // plug in the marc editor button
459         dijit.byId('vl-marc-html-edit-button').onClick = 
460             function() {vlLoadMarcEditor(currentType, recId, oncomplete);};
461         params = [authtoken, recId];
462         api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
463         if(currentType == 'auth')
464             api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
465     }
466
467     fieldmapper.standardRequest(
468         api, 
469         {   async: true,
470             params: params,
471             oncomplete: function(r) {
472             displayGlobalDiv('vl-marc-html-div');
473                 var html = r.recv().content();
474                 dojo.byId('vl-marc-record-html').innerHTML = html;
475             }
476         }
477     );
478 }
479
480
481 /*
482 function getRecMatchesFromAttrCode(rec, attrCode) {
483     var matches = [];
484     var attr = getRecAttrFromCode(rec, attrCode);
485     for(var j = 0; j < rec.matches().length; j++) {
486         var match = rec.matches()[j];
487         if(match.matched_attr() == attr.id()) 
488             matches.push(match);
489     }
490     return matches;
491 }
492 */
493
494 function getRecAttrFromMatch(rec, match) {
495     for(var i = 0; i < rec.attributes().length; i++) {
496         var attr = rec.attributes()[i];
497         if(attr.id() == match.matched_attr())
498             return attr;
499     }
500 }
501
502 function getRecAttrDefFromAttr(attr, type) {
503     var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
504     for(var i = 0; i < defs.length; i++) {
505         var def = defs[i];
506         if(def.id() == attr.field())
507             return def;
508     }
509 }
510
511 function getRecAttrFromCode(rec, attrCode) {
512     var defId = attrDefMap[currentType][attrCode];
513     var attrs = rec.attributes();
514     for(var i = 0; i < attrs.length; i++) {
515         var attr = attrs[i];
516         if(attr.field() == defId) 
517             return attr;
518     }
519     return null;
520 }
521
522 function vlGetViewMatches(rowIdx) {
523     var data = this.grid.model.getRow(rowIdx);
524     if(!data) return '';
525     var rec = queuedRecordsMap[data.id];
526     if(rec.matches().length > 0)
527         return this.value.replace('RECID', data.id);
528     return '';
529 }
530
531 function getAttrValue(rowIdx) {
532     var data = this.grid.model.getRow(rowIdx);
533     if(!data) return '';
534     var attrCode = this.field.split('.')[1];
535     var rec = queuedRecordsMap[data.id];
536     var attr = getRecAttrFromCode(rec, attrCode);
537     return (attr) ? attr.attr_value() : '';
538 }
539
540 function vlGetDateTimeField(rowIdx) {
541     data = this.grid.model.getRow(rowIdx);
542     if(!data) return '';
543     if(!data[this.field]) return '';
544     var date = dojo.date.stamp.fromISOString(data[this.field]);
545     return dojo.date.locale.format(date, {selector:'date'});
546 }
547
548 function vlGetCreator(rowIdx) {
549     data = this.grid.model.getRow(rowIdx);
550     if(!data) return '';
551     var id = data.creator;
552     if(userCache[id])
553         return userCache[id].usrname();
554     var user = fieldmapper.standardRequest(
555         ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
556     if(e = openils.Event.parse(user))
557         return alert(e);
558     userCache[id] = user;
559     return user.usrname();
560 }
561
562 function vlGetViewMARC(rowIdx) {
563     data = this.grid.model.getRow(rowIdx);
564     if(data) 
565         return this.value.replace('RECID', data.id);
566 }
567
568 function vlGetOverlayTargetSelector(rowIdx) {
569     data = this.grid.model.getRow(rowIdx);
570     if(data) {
571         var value = this.value.replace(/GRIDID/g, data._id);
572         value = value.replace(/RECID/g, currentImportRecId);
573         value = value.replace(/ID/g, data.id);
574         if(data._id == currentOverlayRecordsMapGid[currentImportRecId])
575             return value.replace('/>', 'checked="checked"/>');
576         return value;
577     }
578 }
579
580 /**
581   * see if the user has enabled overlays for the current match set and, 
582   * if so, map the current import record to the overlay target.
583   */
584 function vlHandleOverlayTargetSelected(recId, gridId) {
585     var noneSelected = true;
586     var checkboxes = dojo.query('[name=vl-overlay-target-'+currentImportRecId+']');
587     for(var i = 0; i < checkboxes.length; i++) {
588         var checkbox = checkboxes[i];
589         var matchRecId = checkbox.getAttribute('match');
590         var gid = checkbox.getAttribute('gridid');
591         if(checkbox.checked) {
592             if(matchRecId == recId && gid == gridId) {
593                 noneSelected = false;
594                 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
595                 currentOverlayRecordsMapGid[currentImportRecId] = gid;
596                 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
597                 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
598             } else {
599                 checkbox.checked = false;
600             }
601         }
602     }
603
604     if(noneSelected) {
605         delete currentOverlayRecordsMap[currentImportRecId];
606         delete currentOverlayRecordsMapGid[currentImportRecId];
607         dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
608         dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = '';
609     }
610 }
611
612 var valLastQueueType = null;
613 function buildRecordGrid(type) {
614     displayGlobalDiv('vl-queue-div');
615
616     if(valLastQueueType != type) {
617         valLastQueueType = type;
618         resetVlQueueGridLayout();
619         var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
620         attrDefMap[type] = {};
621         for(var i = 0; i < defs.length; i++) {
622             var def = defs[i]
623             attrDefMap[type][def.code()] = def.id();
624             var col = {
625                 name:def.description(), 
626                 field:'attr.' + def.code(),
627                 get: getAttrValue,
628                 selectableColumn:true
629             };
630             vlQueueGridLayout[0].cells[0].push(col);
631         }
632     }
633
634     var storeData;
635     if(type == 'bib')
636         storeData = vqbr.toStoreData(queuedRecords);
637     else
638         storeData = vqar.toStoreData(queuedRecords);
639
640     var store = new dojo.data.ItemFileReadStore({data:storeData});
641     var model = new dojox.grid.data.DojoData(
642         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
643     vlQueueGrid.setModel(model);
644
645
646     if(vlQueueGridColumePicker[type]) {
647         vlQueueGrid.update();
648     } else {
649         vlQueueGridColumePicker[type] = 
650             new openils.GridColumnPicker(vlQueueGridColumePickerDialog, 
651                 vlQueueGrid, vlQueueGridLayout, authtoken, 'vandelay.queue.'+type);
652         vlQueueGridColumePicker[type].load();
653     }
654 }
655
656 function vlQueueGridPrevPage() {
657     var page = parseInt(vlQueueDisplayPage.getValue());
658     if(page < 2) return;
659     vlQueueDisplayPage.setValue(page - 1);
660     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
661 }
662
663 function vlQueueGridNextPage() {
664     vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
665     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
666 }
667
668 function vlDeleteQueue(type, queueId, onload) {
669     fieldmapper.standardRequest(
670         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
671         {   async: true,
672             params: [authtoken, queueId],
673             oncomplete: function(r) {
674                 var resp = r.recv().content();
675                 if(e = openils.Event.parse(resp))
676                     return alert(e);
677                 onload();
678             }
679         }
680     );
681 }
682
683
684 function vlQueueGridDrawSelectBox(rowIdx) {
685     var data = this.grid.model.getRow(rowIdx);
686     if(!data) return '';
687     var domId = 'vl-record-list-selected-' +data.id;
688     selectableGridRecords[domId] = data.id;
689     return "<div><input type='checkbox' id='"+domId+"'/></div>";
690 }
691
692 function vlSelectAllQueueGridRecords() {
693     for(var id in selectableGridRecords) 
694         dojo.byId(id).checked = true;
695 }
696 function vlSelectNoQueueGridRecords() {
697     for(var id in selectableGridRecords) 
698         dojo.byId(id).checked = false;
699 }
700 function vlToggleQueueGridSelect() {
701     if(dojo.byId('vl-queue-grid-row-selector').checked)
702         vlSelectAllQueueGridRecords();
703     else
704         vlSelectNoQueueGridRecords();
705 }
706
707 var handleRetrieveRecords = function() {
708     buildRecordGrid(currentType);
709     vlFetchQueueSummary(currentQueueId, currentType, 
710         function(summary) {
711             dojo.byId('vl-queue-summary-name').innerHTML = summary.queue.name();
712             dojo.byId('vl-queue-summary-total-count').innerHTML = summary.total +'';
713             dojo.byId('vl-queue-summary-import-count').innerHTML = summary.imported + '';
714         }
715     );
716 }
717
718 function vlFetchQueueSummary(qId, type, onload) {
719     fieldmapper.standardRequest(
720         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.summary.retrieve'],
721         {   async: true,
722             params: [authtoken, qId],
723             oncomplete : function(r) {
724                 var summary = r.recv().content();
725                 if(e = openils.Event.parse(summary))
726                     return alert(e);
727                 return onload(summary);
728             }
729         }
730     );
731 }
732     
733
734 function vlImportSelectedRecords() {
735     displayGlobalDiv('vl-generic-progress-with-total');
736     var records = [];
737
738     for(var id in selectableGridRecords) {
739         if(dojo.byId(id).checked) {
740             var recId = selectableGridRecords[id];
741             var rec = queuedRecordsMap[recId];
742             if(!rec.import_time()) 
743                 records.push(recId);
744         }
745     }
746
747     fieldmapper.standardRequest(
748         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
749         {   async: true,
750             params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
751             onresponse: function(r) {
752                 var resp = r.recv().content();
753                 if(e = openils.Event.parse(resp))
754                     return alert(e);
755                 if(resp.complete) {
756                     return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
757                 } else {
758                     vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
759                 }
760             }, 
761         }
762     );
763 }
764
765 function vlImportAllRecords() {
766     vlImportRecordQueue(currentType, currentQueueId, false,
767         function(){displayGlobalDiv('vl-queue-div');});
768 }
769
770 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
771     displayGlobalDiv('vl-generic-progress-with-total');
772     var method = 'open-ils.vandelay.bib_queue.import';
773     if(noMatchOnly)
774         method = method.replace('import', 'nomatch.import');
775     if(type == 'auth')
776         method = method.replace('bib', 'auth');
777
778     fieldmapper.standardRequest(
779         ['open-ils.vandelay', method],
780         {   async: true,
781             params: [authtoken, queueId],
782             onresponse: function(r) {
783                 var resp = r.recv().content();
784                 if(e = openils.Event.parse(resp))
785                     return alert(e);
786                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
787             },
788             oncomplete: function() {onload();}
789         }
790     );
791 }
792
793
794 /**
795   * Create queue, upload MARC, process spool, load the newly created queue 
796   */
797 function batchUpload() {
798     var queueName = dijit.byId('vl-queue-name').getValue();
799     currentType = dijit.byId('vl-record-type').getValue();
800
801     var handleProcessSpool = function() {
802         console.log('records uploaded and spooled');
803         if(vlUploadQueueAutoImport.checked) {
804             vlImportRecordQueue(currentType, currentQueueId, true,  
805                 function() {
806                     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
807                 }
808             );
809         } else {
810             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
811         }
812     }
813
814     var handleUploadMARC = function(key) {
815         console.log('marc uploaded');
816         dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
817         processSpool(key, currentQueueId, currentType, handleProcessSpool);
818     };
819
820     var handleCreateQueue = function(queue) {
821         console.log('queue created ' + queue.name());
822         currentQueueId = queue.id();
823         uploadMARC(handleUploadMARC);
824     };
825     
826     if(vlUploadQueueSelector.getValue() && !queueName) {
827         currentQueueId = vlUploadQueueSelector.getValue();
828         console.log('adding records to existing queue ' + currentQueueId);
829         uploadMARC(handleUploadMARC);
830     } else {
831         createQueue(queueName, currentType, handleCreateQueue);
832     }
833 }
834
835
836 function vlFleshQueueSelect(selector, type) {
837     var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
838     selector.store = new dojo.data.ItemFileReadStore({data:data});
839     selector.setValue(null);
840     selector.setDisplayedValue('');
841     if(data[0])
842         selector.setValue(data[0].id());
843 }
844
845 function vlShowUploadForm() {
846     displayGlobalDiv('vl-marc-upload-div');
847     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
848     vlUploadSourceSelector.store = new dojo.data.ItemFileReadStore({data:cbs.toStoreData(vlBibSources)});
849 }
850
851 function vlShowQueueSelect() {
852     displayGlobalDiv('vl-queue-select-div');
853     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
854 }
855
856 function vlFetchQueueFromForm() {
857     currentType = vlQueueSelectType.getValue();
858     currentQueueId = vlQueueSelectQueueList.getValue();
859     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
860 }
861
862 function vlOpenMarcEditWindow(rec, postReloadHTMLHandler) {
863     /*
864         To run in Firefox directly, must set signed.applets.codebase_principal_support
865         to true in about:config
866     */
867     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
868     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
869
870     function onsave(r) {
871         // after the record is saved, reload the HTML display
872         var stat = r.recv().content();
873         if(e = openils.Event.parse(stat))
874             return alert(e);
875         alert(dojo.byId('vl-marc-edit-complete-label').innerHTML);
876         win.close();
877         vlLoadMARCHtml(rec.id(), false, postReloadHTMLHandler);
878     }
879
880     win.xulG = {
881         record : {marc : rec.marc()},
882         save : {
883             label: dojo.byId('vl-marc-edit-save-label').innerHTML,
884             func: function(xmlString) {
885                 var method = 'open-ils.permacrud.update.' + rec.classname;
886                 rec.marc(xmlString);
887                 fieldmapper.standardRequest(
888                     ['open-ils.permacrud', method],
889                     {   async: true,
890                         params: [authtoken, rec],
891                         oncomplete: onsave
892                     }
893                 );
894             },
895         }
896     };
897 }
898
899 function vlLoadMarcEditor(type, recId, postReloadHTMLHandler) {
900     var method = 'open-ils.permacrud.search.vqbr';
901     if(currentType != 'bib')
902         method = method.replace(/vqbr/,'vqar');
903
904     fieldmapper.standardRequest(
905         ['open-ils.permacrud', method],
906         {   async: true, 
907             params: [authtoken, {id : recId}],
908             oncomplete: function(r) {
909                 var rec = r.recv().content();
910                 if(e = openils.Event.parse(rec))
911                     return alert(e);
912                 vlOpenMarcEditWindow(rec, postReloadHTMLHandler);
913             }
914         }
915     );
916 }
917
918
919
920 //------------------------------------------------------------
921 // attribute editors
922
923 // attribute-editor global variables
924
925 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
926 var ATTR_EDIT_ID = null;                // id of current 'edit' attribute
927 var ATTR_EDIT_GROUP = 'bib';            // bib-attrs or auth-attrs
928
929 function vlAttrEditorInit() {
930     // set up tooltips on the edit form
931     connectTooltip('attr-editor-tags'); 
932     connectTooltip('attr-editor-subfields'); 
933 }
934
935 function vlShowAttrEditor() {
936     displayGlobalDiv('vl-attr-editor-div');
937     loadAttrEditorGrid();
938     idHide('vl-generic-progress');
939 }
940
941 function setAttrEditorGroup(groupName) {
942     // put us into 'bib'-attr or 'auth'-attr mode.
943     if (ATTR_EDIT_GROUP != groupName) {
944         ATTR_EDIT_GROUP = groupName;
945         loadAttrEditorGrid();
946     }
947 }
948
949 function onAttrEditorOpen() {
950     // the "bars" have the create/update/cancel/etc. buttons.
951     var create_bar = document.getElementById('attr-editor-create-bar');
952     var update_bar = document.getElementById('attr-editor-update-bar');
953     if (ATTR_EDITOR_IN_UPDATE_MODE) {
954         update_bar.style.display='table-row';
955         create_bar.style.display='none';
956         // hide the dropdown-button
957         idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
958     } else {
959         dijit.byId('attr-editor-dialog').reset();
960         create_bar.style.display='table-row';
961         update_bar.style.display='none';
962     }
963 }
964
965 function onAttrEditorClose() {
966     // reset the form to a "create" form. (We may have borrowed it for editing.)
967     ATTR_EDITOR_IN_UPDATE_MODE = false;
968     // show the dropdown-button
969     idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
970 }
971
972 function loadAttrEditorGrid() {
973     var _data = (ATTR_EDIT_GROUP == 'auth') ? 
974         vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
975                  
976     var store = new dojo.data.ItemFileReadStore({data:_data});
977     var model = new dojox.grid.data.DojoData(
978         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
979     attrEditorGrid.setModel(model);
980     attrEditorGrid.setStructure(vlAttrGridLayout);
981     attrEditorGrid.onRowClick = onAttrEditorClick;
982     attrEditorGrid.update();
983 }
984
985 function attrGridGetTag(n) {
986     // grid helper: return the tags from the row's xpath column.
987     var xp = this.grid.model.getRow(n);
988     return xp && xpathParser.parse(xp.xpath).tags;
989 }
990
991 function attrGridGetSubfield(n) {
992     // grid helper: return the subfields from the row's xpath column.
993     var xp = this.grid.model.getRow(n);
994     return xp && xpathParser.parse(xp.xpath).subfields;
995 }
996
997 function onAttrEditorClick(evt) {
998     var row = attrEditorGrid.model.getRow(evt.rowIndex);
999     ATTR_EDIT_ID = row.id;
1000     ATTR_EDITOR_IN_UPDATE_MODE = true;
1001
1002     // populate the popup editor.
1003     dojo.byId('attr-editor-code').value = row.code;
1004     dojo.byId('attr-editor-description').value = row.description;
1005     var parsed_xpath = xpathParser.parse(row.xpath);
1006     dojo.byId('attr-editor-tags').value = parsed_xpath.tags;
1007     dojo.byId('attr-editor-subfields').value = parsed_xpath.subfields;
1008     dojo.byId('attr-editor-identifier').value = (row.ident ? 'True':'False');
1009     dojo.byId('attr-editor-xpath').value = row.xpath;
1010     dojo.byId('attr-editor-remove').value = row.remove;
1011
1012     // set up UI for editing
1013     dojo.byId('vl-create-attr-editor-button').click();
1014 }
1015
1016 function vlSaveAttrDefinition(data) {
1017     idHide('vl-attr-editor-div');
1018     idShow('vl-generic-progress');
1019
1020     data.id = ATTR_EDIT_ID;
1021
1022     // this ought to honour custom xpaths, but overwrite xpaths
1023     // derived from tags/subfields.
1024     if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
1025         var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
1026         data.xpath = _xpath;
1027     }
1028
1029     // build up our permacrud params. Key variables here are
1030     // "create or update" and "bib or auth".
1031
1032     var isAuth   = (ATTR_EDIT_GROUP == 'auth');
1033     var isCreate = (ATTR_EDIT_ID == null);
1034     var rad      = isAuth ? new vqarad() : new vqbrad() ;
1035     var method   = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.') 
1036         + (isAuth ? 'vqarad' : 'vqbrad');
1037     var _data    = rad.fromStoreItem(data);
1038
1039     _data.ischanged(1);
1040
1041     fieldmapper.standardRequest(
1042         ['open-ils.permacrud', method],
1043         {   async: true,
1044             params: [authtoken, _data ],
1045             onresponse: function(r) { },
1046             oncomplete: function(r) {
1047                 attrEditorFetchAttrDefs(vlShowAttrEditor);
1048                 ATTR_EDIT_ID = null;
1049             },
1050             onerror: function(r) {
1051                 alert('vlSaveAttrDefinition comms error: ' + r);
1052             }
1053         }
1054     );
1055 }
1056
1057 function attrEditorFetchAttrDefs(callback) {
1058     var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
1059     return fn(callback);
1060 }
1061
1062 function vlAttrDelete() {
1063     idHide('vl-attr-editor-div');
1064     idShow('vl-generic-progress');
1065
1066     var isAuth = (ATTR_EDIT_GROUP == 'auth');
1067     var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
1068     var rad    = isAuth ? new vqarad() : new vqbrad() ;
1069     fieldmapper.standardRequest(
1070         ['open-ils.permacrud', method],
1071         {   async: true,
1072             params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
1073             oncomplete: function() {
1074                 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
1075                 attrEditorFetchAttrDefs(vlShowAttrEditor);
1076                 ATTR_EDIT_ID = null;
1077             },
1078             onerror: function(r) {
1079                 alert('vlAttrDelete comms error: ' + r);
1080             }
1081         }
1082     );
1083 }
1084
1085 // ------------------------------------------------------------
1086 // utilities for attribute editors
1087
1088 // dom utilities (maybe dojo does these, and these should be replaced)
1089
1090 function idStyle(obId, k, v)    { document.getElementById(obId).style[k] = v;   }
1091 function idShow(obId)           { idStyle(obId, 'display', 'block');            }
1092 function idHide(obId)           { idStyle(obId, 'display' , 'none');            }
1093
1094 function connectTooltip(fieldId) {
1095     // Given an element id, look up a tooltip element in the doc (same
1096     // id with a '-tip' suffix) and associate the two. Maybe dojo has
1097     // a better way to do this?
1098     var fld = dojo.byId(fieldId);
1099     var tip = dojo.byId(fieldId + '-tip');
1100     dojo.connect(fld, 'onfocus', function(evt) {
1101                      dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1102     dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1103 }
1104
1105 // xpath utilities
1106
1107 var xpathParser = new openils.MarcXPathParser();
1108
1109 function tagAndSubFieldsToXpath(tags, subfields) {
1110     // given tags, and subfields, build up an XPath.
1111     try {
1112         var parts = {
1113             'tags':tags.match(/[\d]+/g), 
1114             'subfields':subfields.match(/[a-zA-z]/g) };
1115         return xpathParser.compile(parts);
1116     } catch (err) {
1117         return {'parts':null, 'tags':null, 'error':err};
1118     }
1119 }
1120
1121 function looksLikeDerivedXpath(path) {
1122     // Does this path look like it was derived from tags and subfields?
1123     var parsed = xpathParser.parse(path);
1124     if (parsed.tags == null) 
1125         return false;
1126     var compiled = xpathParser.compile(parsed);
1127     return (path == compiled);
1128 }
1129
1130 // amazing xpath-util unit-tests
1131 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]'))     alert('vandelay xpath-utility error');
1132 if ( looksLikeDerivedXpath('ba-boo-ba-boo!'))                   alert('vandelay xpath-utility error');