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