]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/vandelay/vandelay.js
Vandelay import improvements / bug fixes
[working/Evergreen.git] / Open-ILS / web / js / ui / default / vandelay / vandelay.js
1 /* ---------------------------------------------------------------------------
2 # Copyright (C) 2008  Georgia Public Library Service
3 # Bill Erickson <erickson@esilibrary.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # --------------------------------------------------------------------------- */
15 dojo.require("dojo.parser");
16 dojo.require("dojo.io.iframe"); 
17 dojo.require("dijit.ProgressBar"); 
18 dojo.require("dijit.form.FilteringSelect"); 
19 dojo.require("dijit.layout.ContentPane");
20 dojo.require("dijit.layout.TabContainer");
21 dojo.require("dijit.layout.LayoutContainer");
22 dojo.require('dijit.form.Button');
23 dojo.require('dijit.form.CheckBox');
24 dojo.require('dijit.Toolbar');
25 dojo.require('dijit.Tooltip');
26 dojo.require('dijit.Menu');
27 dojo.require("dijit.Dialog");
28 dojo.require("dojo.cookie");
29 dojo.require('dojox.grid.DataGrid');
30 dojo.require("dojo.data.ItemFileReadStore");
31 dojo.require('dojo.date.locale');
32 dojo.require('dojo.date.stamp');
33 dojo.require("fieldmapper.Fieldmapper");
34 dojo.require("fieldmapper.dojoData");
35 dojo.require("fieldmapper.OrgUtils");
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.widget.GridColumnPicker');
42 dojo.require('openils.PermaCrud');
43 dojo.require('openils.widget.OrgUnitFilteringSelect');
44 dojo.require('openils.widget.AutoGrid');
45 dojo.require('openils.widget.AutoFieldWidget');
46
47
48 var globalDivs = [
49     'vl-generic-progress',
50     'vl-generic-progress-with-total',
51     'vl-marc-upload-div',
52     'vl-queue-div',
53     'vl-match-div',
54     'vl-marc-html-div',
55     'vl-queue-select-div',
56     'vl-marc-upload-status-div',
57     'vl-attr-editor-div',
58     'vl-marc-export-div',
59     'vl-profile-editor-div',
60     'vl-item-attr-editor-div',
61     'vl-import-error-div'
62 ];
63
64 var authtoken;
65 var VANDELAY_URL = '/vandelay-upload';
66 var bibAttrDefs = [];
67 var authAttrDefs = [];
68 var queuedRecords = [];
69 var queuedRecordsMap = {};
70 var bibAttrsFetched = false;
71 var authAttrsFetched = false;
72 var attrDefMap = {}; // maps attr def code names to attr def ids
73 var currentType;
74 var currentQueueId = null;
75 var userCache = {};
76 var currentMatchedRecords; // set of loaded matched bib records
77 var currentOverlayRecordsMap; // map of import record to overlay record
78 var currentOverlayRecordsMapGid; // map of import record to overlay record grid id
79 var currentImportRecId; // when analyzing matches, this is the current import record
80 var userBibQueues = []; // only non-complete queues
81 var userAuthQueues = []; // only non-complete queues
82 var allUserBibQueues;
83 var allUserAuthQueues;
84 var selectableGridRecords;
85 var cgi = new openils.CGI();
86 var vlQueueGridColumePicker = {};
87 var vlBibSources = [];
88 var importItemDefs = [];
89 var matchSets = {};
90 var mergeProfiles = [];
91
92 /**
93   * Grab initial data
94   */
95 function vlInit() {
96     authtoken = openils.User.authtoken;
97     var initNeeded = 7; // how many async responses do we need before we're init'd 
98     var initCount = 0; // how many async reponses we've received
99
100     openils.Util.registerEnterHandler(
101         vlQueueDisplayPage.domNode, function(){retrieveQueuedRecords();});
102     openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
103
104     function checkInitDone() {
105         initCount++;
106         if(initCount == initNeeded)
107             runStartupCommands();
108     }
109
110     mergeProfiles = new openils.PermaCrud().retrieveAll('vmp');
111     vlUploadMergeProfile.store = new dojo.data.ItemFileReadStore({data:fieldmapper.vmp.toStoreData(mergeProfiles)});
112     vlUploadMergeProfile.labelAttr = 'name';
113     vlUploadMergeProfile.searchAttr = 'name';
114     vlUploadMergeProfile.startup();
115
116     vlUploadMergeProfile2.store = new dojo.data.ItemFileReadStore({data:fieldmapper.vmp.toStoreData(mergeProfiles)});
117     vlUploadMergeProfile2.labelAttr = 'name';
118     vlUploadMergeProfile2.searchAttr = 'name';
119     vlUploadMergeProfile2.startup();
120
121
122     // Fetch the bib and authority attribute definitions 
123     vlFetchBibAttrDefs(function () { checkInitDone(); });
124     vlFetchAuthAttrDefs(function () { checkInitDone(); });
125
126     vlRetrieveQueueList('bib', null, 
127         function(list) {
128             allUserBibQueues = list;
129             for(var i = 0; i < allUserBibQueues.length; i++) {
130                 if(allUserBibQueues[i].complete() == 'f')
131                     userBibQueues.push(allUserBibQueues[i]);
132             }
133             checkInitDone();
134         }
135     );
136
137     vlRetrieveQueueList('auth', null, 
138         function(list) {
139             allUserAuthQueues = list;
140             for(var i = 0; i < allUserAuthQueues.length; i++) {
141                 if(allUserAuthQueues[i].complete() == 'f')
142                     userAuthQueues.push(allUserAuthQueues[i]);
143             }
144             checkInitDone();
145         }
146     );
147
148     fieldmapper.standardRequest(
149         ['open-ils.permacrud', 'open-ils.permacrud.search.cbs.atomic'],
150         {   async: true,
151             params: [authtoken, {id:{"!=":null}}, {order_by:{cbs:'id'}}],
152             oncomplete : function(r) {
153                 vlBibSources = openils.Util.readResponse(r, false, true);
154                 checkInitDone();
155             }
156         }
157     );
158
159     var owner = fieldmapper.aou.orgNodeTrail(fieldmapper.aou.findOrgUnit(new openils.User().user.ws_ou()));
160     new openils.PermaCrud().search('viiad', 
161         {owner: owner.map(function(org) { return org.id(); })},
162         {   async: true,
163             oncomplete: function(r) {
164                 importItemDefs = openils.Util.readResponse(r);
165                 checkInitDone();
166             }
167         }
168     );
169
170     new openils.PermaCrud().search('vms',
171         {owner: owner.map(function(org) { return org.id(); })},
172         {   async: true,
173             oncomplete: function(r) {
174                 var sets = openils.Util.readResponse(r);
175                 dojo.forEach(sets, 
176                     function(set) {
177                         if(!matchSets[set.mtype()])
178                             matchSets[set.mtype()] = [];
179                         matchSets[set.mtype()].push(set);
180                     }
181                 );
182                 checkInitDone();
183             }
184         }
185     );
186
187     vlAttrEditorInit();
188     vlExportInit();
189 }
190
191
192 openils.Util.addOnLoad(vlInit);
193
194
195 // fetch the bib and authority attribute definitions
196
197 function vlFetchBibAttrDefs(postcomplete) {
198     bibAttrDefs = [];
199     fieldmapper.standardRequest(
200         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
201         {   async: true,
202             params: [authtoken, {id:{'!=':null}}],
203             onresponse: function(r) {
204                 var def = r.recv().content(); 
205                 if(e = openils.Event.parse(def[0])) 
206                     return alert(e);
207                 bibAttrDefs.push(def);
208             },
209             oncomplete: function() {
210                 bibAttrDefs = bibAttrDefs.sort(
211                     function(a, b) {
212                         if(a.id() > b.id()) return 1;
213                         if(a.id() < b.id()) return -1;
214                         return 0;
215                     }
216                 );
217                 postcomplete();
218             }
219         }
220     );
221 }
222
223 function vlFetchAuthAttrDefs(postcomplete) {
224     authAttrDefs = [];
225     fieldmapper.standardRequest(
226         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
227         {   async: true,
228             params: [authtoken, {id:{'!=':null}}],
229             onresponse: function(r) {
230                 var def = r.recv().content(); 
231                 if(e = openils.Event.parse(def[0])) 
232                     return alert(e);
233                 authAttrDefs.push(def);
234             },
235             oncomplete: function() {
236                 authAttrDefs = authAttrDefs.sort(
237                     function(a, b) {
238                         if(a.id() > b.id()) return 1;
239                         if(a.id() < b.id()) return -1;
240                         return 0;
241                     }
242                 );
243                 postcomplete();
244             }
245         }
246     );
247 }
248
249 function vlRetrieveQueueList(type, filter, onload) {
250     type = (type == 'bib') ? type : 'authority';
251     fieldmapper.standardRequest(
252         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
253         {   async: true,
254             params: [authtoken, null, filter],
255             oncomplete: function(r) {
256                 var list = r.recv().content();
257                 if(e = openils.Event.parse(list[0]))
258                     return alert(e);
259                 onload(list);
260             }
261         }
262     );
263
264 }
265
266 function displayGlobalDiv(id) {
267     for(var i = 0; i < globalDivs.length; i++) {
268         try {
269             dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
270         } catch(e) {
271             alert('please define div ' + globalDivs[i]);
272         }
273     }
274     dojo.style(dojo.byId(id),'display','block');
275
276     openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
277     openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
278     openils.Util.removeCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
279     openils.Util.removeCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
280     openils.Util.removeCSSClass(dojo.byId('vl-menu-profile-editor'), 'toolbar_selected');
281     openils.Util.removeCSSClass(dojo.byId('vl-menu-match-set-editor'), 'toolbar_selected');
282
283     if(dojo.byId('vl-match-set-iframe'))
284         dojo.byId('vl-match-set-editor-div').removeChild(dojo.byId('vl-match-set-iframe'));
285
286     switch(id) {
287         case 'vl-marc-export-div':
288             openils.Util.addCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
289             break;
290         case 'vl-marc-upload-div':
291             openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
292             break;
293         case 'vl-queue-select-div':
294             openils.Util.addCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
295             break;
296         case 'vl-attr-editor-div':
297             openils.Util.addCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
298             break;
299         case 'vl-profile-editor-div':
300             openils.Util.addCSSClass(dojo.byId('vl-menu-profile-editor'), 'toolbar_selected');
301             break;
302         case 'vl-item-attr-editor-div':
303             openils.Util.addCSSClass(dojo.byId('vl-menu-import-item-attr-editor'), 'toolbar_selected');
304             break;
305         case 'vl-match-set-editor-div':
306             openils.Util.addCSSClass(dojo.byId('vl-menu-match-set-editor'), 'toolbar_selected');
307             break;
308     }
309 }
310
311 function runStartupCommands() {
312     currentQueueId = cgi.param('qid');
313     currentType = cgi.param('qtype');
314     dojo.style('vl-nav-bar', 'visibility', 'visible');
315     if(currentQueueId)
316         return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
317     vlShowUploadForm();
318 }
319
320 /**
321   * asynchronously upload a file of MARC records
322   */
323 function uploadMARC(onload){
324     dojo.byId('vl-upload-status-count').innerHTML = '0';
325     dojo.byId('vl-ses-input').value = authtoken;
326     displayGlobalDiv('vl-marc-upload-status-div');
327     dojo.io.iframe.send({
328         url: VANDELAY_URL,
329         method: "post",
330         handleAs: "html",
331         form: dojo.byId('vl-marc-upload-form'),
332         handle: function(data,ioArgs){
333             var content = data.documentElement.textContent;
334             onload(content);
335         }
336     });
337 }       
338
339 /**
340   * Creates a new vandelay queue
341   */
342 function createQueue(queueName, type, onload, importDefId, matchSet) {
343     var name = (type=='bib') ? 'bib' : 'authority';
344     var method = 'open-ils.vandelay.'+ name +'_queue.create'
345     fieldmapper.standardRequest(
346         ['open-ils.vandelay', method],
347         {   async: true,
348             params: [authtoken, queueName, null, name, matchSet, importDefId],
349             oncomplete : function(r) {
350                 var queue = r.recv().content();
351                 if(e = openils.Event.parse(queue)) 
352                     return alert(e);
353                 onload(queue);
354             }
355         }
356     );
357 }
358
359 /**
360   * Tells vandelay to pull a batch of records from the cache and explode them
361   * out into the vandelay tables
362   */
363 function processSpool(key, queueId, type, onload) {
364     fieldmapper.standardRequest(
365         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
366         {   async: true,
367             params: [authtoken, key, queueId],
368             onresponse : function(r) {
369                 var resp = r.recv().content();
370                 if(e = openils.Event.parse(resp)) 
371                     return alert(e);
372                 dojo.byId('vl-upload-status-count').innerHTML = resp;
373             },
374             oncomplete : function(r) {onload();}
375         }
376     );
377 }
378
379 function vlExportInit() {
380
381     // queue export
382     var qsel = dojo.byId('vl-queue-export-options');
383     qsel.onchange = function(newVal) {
384         var value = qsel.options[qsel.selectedIndex].value;
385         qsel.selectedIndex = 0;
386         if(!value) return;
387         if(!confirm('Export as "' + value + '"?')) return; // TODO: i18n
388         retrieveQueuedRecords(
389             currentType, 
390             currentQueueId, 
391             function(r) { 
392                 exportHandler(value, r);
393                 displayGlobalDiv('vl-queue-div');
394             },
395             value
396         );
397     }
398
399     // item export
400     var isel = dojo.byId('vl-item-export-options');
401     isel.onchange = function(newVal) {
402         var value = isel.options[isel.selectedIndex].value;
403         isel.selectedIndex = 0;
404         if(!value) return;
405         if(!confirm('Export as "' + value + '"?')) return; // TODO: i18n
406
407         var method = 'open-ils.vandelay.import_item.queue.export.' + value + '.atomic';
408
409         fieldmapper.standardRequest(
410             ['open-ils.vandelay', method],
411             {
412                 params : [
413                     authtoken, 
414                     currentQueueId, 
415                     {with_import_error: (vlImportItemsShowErrors.checked) ? 1 : null}
416                 ],
417                 async : true,
418                 oncomplete : function(r) {exportHandler(value, r)}
419             }
420         );
421     }
422 }
423
424 function exportHandler(type, response) {
425     try {
426         var content = openils.Util.readResponse(response);
427         if (type=='email') {
428             if (content==1) { alert('Email sent.'); return; }
429             throw(content);
430         }
431         /* handle .atomic versus non-atomic method calls */
432         content = content.constructor == Array
433             ? content[0].template_output().data()
434             : content.template_output().data();
435         switch(type) {
436             case 'print':
437                 openils.Util.printHtmlString(content);
438             break;
439             case 'csv':
440                 //content = content.replace(/\\t/g,'\t'); // if we really wanted to do .tsv instead
441                 openils.XUL.contentToFileSaveDialog(content);
442             break;
443             default:
444                 alert('response = ' + response + '\tcontent:\n' + content);
445         }
446     } catch(E) {
447         alert('Error exporting data: ' + E);
448     }
449 }
450
451 function retrieveQueuedRecords(type, queueId, onload, doExport) {
452     displayGlobalDiv('vl-generic-progress');
453     queuedRecords = [];
454     queuedRecordsMap = {};
455     currentOverlayRecordsMap = {};
456     currentOverlayRecordsMapGid = {};
457     selectableGridRecords = {};
458
459     if(!type) type = currentType;
460     if(!queueId) queueId = currentQueueId;
461     if(!onload) onload = handleRetrieveRecords;
462
463     var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve';
464
465     if(doExport) method += '.export.' + doExport;
466     if(vlQueueGridShowMatches.checked)
467         method = method.replace('records', 'records.matches');
468
469     method += '.atomic';
470
471     var sel = dojo.byId('vl-queue-display-limit-selector');
472     var limit = parseInt(sel.options[sel.selectedIndex].value);
473     var offset = limit * parseInt(vlQueueDisplayPage.attr('value')-1);
474
475     var params =  [authtoken, queueId, {clear_marc: 1, offset: offset, limit: limit, flesh_import_items:1}];
476     if(vlQueueGridShowNonImport.checked)
477         params[2].non_imported = 1;
478
479     if(vlQueueGridShowImportErrors.checked)
480         params[2].with_import_error = 1;
481
482     fieldmapper.standardRequest(
483         ['open-ils.vandelay', method],
484         {   async: true,
485             params: params,
486             oncomplete: function(r){
487                 if(doExport) return onload(r);
488                 var recs = r.recv().content();
489                 if(e = openils.Event.parse(recs[0]))
490                     return alert(e);
491                 for(var i = 0; i < recs.length; i++) {
492                     var rec = recs[i];
493                     queuedRecords.push(rec);
494                     queuedRecordsMap[rec.id()] = rec;
495                 }
496                 onload();
497             }
498         }
499     );
500 }
501
502 function vlLoadMatchUI(recId) {
503     displayGlobalDiv('vl-generic-progress');
504     var queuedRec = queuedRecordsMap[recId];
505     var matches = queuedRec.matches();
506     var records = [];
507     currentImportRecId = recId;
508     for(var i = 0; i < matches.length; i++)
509         records.push(matches[i].eg_record());
510
511     var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
512     var params = [records];
513     if(currentType == 'auth') {
514         retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
515         params = [authtoken, records, {clear_marc:1}];
516     }
517
518     fieldmapper.standardRequest(
519         retrieve,
520         {   async: true,
521             params:params,
522             oncomplete: function(r) {
523                 var recs = r.recv().content();
524                 if(e = openils.Event.parse(recs))
525                     return alert(e);
526
527                 /* ui mangling */
528                 displayGlobalDiv('vl-match-div');
529                 resetVlMatchGridLayout();
530                 currentMatchedRecords = recs;
531                 vlMatchGrid.setStructure(vlMatchGridLayout);
532
533                 // build the data store of records with match information
534                 var dataStore = bre.toStoreData(recs, null, 
535                     {virtualFields:['_id', 'match_score', 'match_quality', 'rec_quality']});
536                 dataStore.identifier = '_id';
537
538                 var matchSeenMap = {};
539
540                 for(var i = 0; i < dataStore.items.length; i++) {
541                     var item = dataStore.items[i];
542                     item._id = i; // just need something unique
543                     for(var j = 0; j < matches.length; j++) {
544                         var match = matches[j];
545                         if(match.eg_record() == item.id && !matchSeenMap[match.id()]) {
546                             if(match.match_score)
547                                 item.match_score = match.match_score();
548                             item.match_quality = match.quality();
549                             item.rec_quality = queuedRec.quality();
550                             matchSeenMap[match.id()] = 1;
551                             break;
552                         }
553                     }
554                 }
555
556                 // now populate the grid
557                 vlPopulateMatchGrid(vlMatchGrid, dataStore);
558             }
559         }
560     );
561 }
562
563 function vlPopulateMatchGrid(grid, data) {
564     var store = new dojo.data.ItemFileReadStore({data:data});
565     grid.setStore(store);
566     grid.update();
567 }
568
569 function showMe(id) {
570     dojo.style(dojo.byId(id), 'display', 'block');
571 }
572 function hideMe(id) {
573     dojo.style(dojo.byId(id), 'display', 'none');
574 }
575
576
577 function vlLoadMARCHtml(recId, inCat, oncomplete) {
578     dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
579     displayGlobalDiv('vl-generic-progress');
580     var api;
581     var params = [recId, 1];
582
583     if(inCat) {
584         hideMe('vl-marc-html-edit-button'); // don't show marc editor button
585         dijit.byId('vl-marc-html-edit-button').onClick = function(){}
586         api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
587         if(currentType == 'auth')
588             api = ['open-ils.search', 'open-ils.search.authority.to_html'];
589     } else {
590         showMe('vl-marc-html-edit-button'); // plug in the marc editor button
591         dijit.byId('vl-marc-html-edit-button').onClick = 
592             function() {vlLoadMarcEditor(currentType, recId, oncomplete);};
593         params = [authtoken, recId];
594         api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
595         if(currentType == 'auth')
596             api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
597     }
598
599     fieldmapper.standardRequest(
600         api, 
601         {   async: true,
602             params: params,
603             oncomplete: function(r) {
604             displayGlobalDiv('vl-marc-html-div');
605                 var html = r.recv().content();
606                 dojo.byId('vl-marc-record-html').innerHTML = html;
607             }
608         }
609     );
610 }
611
612
613 /*
614 function getRecMatchesFromAttrCode(rec, attrCode) {
615     var matches = [];
616     var attr = getRecAttrFromCode(rec, attrCode);
617     for(var j = 0; j < rec.matches().length; j++) {
618         var match = rec.matches()[j];
619         if(match.matched_attr() == attr.id()) 
620             matches.push(match);
621     }
622     return matches;
623 }
624 */
625
626 /*
627 function getRecAttrFromMatch(rec, match) {
628     for(var i = 0; i < rec.attributes().length; i++) {
629         var attr = rec.attributes()[i];
630         if(attr.id() == match.matched_attr())
631             return attr;
632     }
633 }
634 */
635
636 function getRecAttrDefFromAttr(attr, type) {
637     var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
638     for(var i = 0; i < defs.length; i++) {
639         var def = defs[i];
640         if(def.id() == attr.field())
641             return def;
642     }
643 }
644
645 function getRecAttrFromCode(rec, attrCode) {
646     var defId = attrDefMap[currentType][attrCode];
647     var attrs = rec.attributes();
648     for(var i = 0; i < attrs.length; i++) {
649         var attr = attrs[i];
650         if(attr.field() == defId) 
651             return attr;
652     }
653     return null;
654 }
655
656 function vlGetViewMatches(rowIdx, item) {
657     if(item) {
658         var id = this.grid.store.getValue(item, 'id');
659         var rec = queuedRecordsMap[id];
660         if(rec.matches().length > 0)
661             return id;
662     }
663     return -1
664 }
665
666 function vlFormatViewMatches(id) {
667     if(id == -1) return '';
668     return '<a href="javascript:void(0);" onclick="vlLoadMatchUI(' + id + ');">' + this.name + '</a>';
669 }
670
671 function vlGetViewErrors(rowIdx, item) {
672     if(item) {
673         var id = this.grid.store.getValue(item, 'id');
674         var rec = queuedRecordsMap[id];
675         // id:rec_error:item_import_error_count
676         return id + ':' + 
677             (rec.import_error() ? 1 : '') + ':' + 
678             (typeof rec.import_items == 'function'
679                 ? rec.import_items().filter(function(i) {return i.import_error()}).length
680                 :''
681             );
682     }
683     return -1
684 }
685
686 function vlFormatViewErrors(chunk) {
687     if(chunk == -1) return '';
688     var id = chunk.split(':')[0];
689     var rec = chunk.split(':')[1];
690     var count = chunk.split(':')[2];
691     var links = '';
692     if(rec) 
693         links += '<a href="javascript:void(0);" onclick="vlLoadErrorUI(' + id + ');">Record</a><br/>'; // TODO I18N
694     if(Number(count))
695         links += '<a href="javascript:void(0);" onclick="vlLoadErrorUI(' + id + ');">Items ('+count+')</a>'; // TODO I18N
696     return links;
697 }
698
699 //var vlItemErrorColumnPicker;
700 function vlLoadErrorUI(id) {
701
702     displayGlobalDiv('vl-import-error-div');
703     openils.Util.hide('vl-import-error-grid-all');
704     openils.Util.show('vl-import-error-record');
705
706     var rec = queuedRecordsMap[id];
707
708     dojo.byId('vl-error-id').innerHTML = rec.id();
709     dojo.forEach( // TODO sane authority rec. fields
710         ['title', 'author', 'isbn', 'issn', 'upc'],
711         function(field) {
712             var attr =  getRecAttrFromCode(rec, field);
713             var eid = 'vl-error-' + field;
714             if(attr) {
715                 openils.Util.show(dojo.byId(eid).parentNode, 'table-row');
716                 dojo.byId(eid).innerHTML = attr.attr_value();
717             } else {
718                 openils.Util.hide(dojo.byId(eid).parentNode);
719             }
720         }
721     );
722     var iediv = dojo.byId('vl-error-import-error');
723     var eddiv = dojo.byId('vl-error-error-detail');
724     if(rec.import_error()) {
725         openils.Util.show(iediv.parentNode, 'table-row');
726         openils.Util.show(eddiv.parentNode, 'table-row');
727         iediv.innerHTML = rec.import_error();
728         eddiv.innerHTML = rec.error_detail();
729     } else {
730         openils.Util.hide(iediv.parentNode);
731         openils.Util.hide(eddiv.parentNode);
732     }
733
734     var errorItems = rec.import_items().filter(function(i) {return i.import_error()});
735     if(errorItems.length) {
736         openils.Util.show('vl-import-error-grid-some');
737         storeData = vqbr.toStoreData(errorItems);
738         var store = new dojo.data.ItemFileReadStore({data:storeData});
739         vlImportErrorGrid.setStore(store);
740         vlImportErrorGrid.update();
741     } else {
742         openils.Util.hide('vl-import-error-grid-some');
743     }
744 }
745
746 function vlLoadErrorUIAll() {
747
748     displayGlobalDiv('vl-import-error-div');
749     openils.Util.hide('vl-import-error-grid-some');
750     openils.Util.hide('vl-import-error-record');
751     openils.Util.show('vl-import-error-grid-all');
752     vlAllImportErrorGrid.resetStore();
753
754     vlImportErrorGrid.displayOffset = 0;
755
756     vlAllImportErrorGrid.dataLoader = function() {
757
758         vlAllImportErrorGrid.showLoadProgressIndicator();
759
760         fieldmapper.standardRequest(
761             ['open-ils.vandelay', 'open-ils.vandelay.import_item.queue.retrieve'],
762             {
763                 async : true,
764                 params : [
765                     authtoken, currentQueueId, {   
766                         with_import_error: (vlImportItemsShowErrors.checked) ? 1 : null,
767                         offset : vlAllImportErrorGrid.displayOffset,
768                         limit : vlAllImportErrorGrid.displayLimit
769                     }
770                 ],
771                 onresponse : function(r) {
772                     var item = openils.Util.readResponse(r);
773                     if(!item) return;
774                     vlAllImportErrorGrid.store.newItem(vii.toStoreItem(item));
775                 },
776                 oncomplete : function() {
777                     vlAllImportErrorGrid.hideLoadProgressIndicator();
778                 }
779             }
780         );
781     };
782
783     vlAllImportErrorGrid.dataLoader();
784 }
785
786 function vlGetOrg(rowIdx, item) {
787     if(!item) return '';
788     var value = this.grid.store.getValue(item, this.field);
789     if(value) return fieldmapper.aou.findOrgUnit(value).shortname();
790     return '';
791 }
792
793 function vlFormatViewMatchMARC(id) {
794     return '<a href="javascript:void(0);" onclick="vlLoadMARCHtml(' + id + ', true, '+
795         'function(){displayGlobalDiv(\'vl-match-div\');});">' + this.name + '</a>';
796 }
797
798 function getAttrValue(rowIdx, item) {
799     if(!item) return '';
800     var attrCode = this.field.split('.')[1];
801     var rec = queuedRecordsMap[this.grid.store.getValue(item, 'id')];
802     var attr = getRecAttrFromCode(rec, attrCode);
803     return (attr) ? attr.attr_value() : '';
804 }
805
806 function vlGetDateTimeField(rowIdx, item) {
807     if(!item) return '';
808     var value = this.grid.store.getValue(item, this.field);
809     if(!value) return '';
810     var date = dojo.date.stamp.fromISOString(value);
811     return dojo.date.locale.format(date, {selector:'date'});
812 }
813
814 function vlGetCreator(rowIdx, item) {
815     if(!item) return '';
816     var id = this.grid.store.getValue(item, 'creator');
817     if(userCache[id])
818         return userCache[id].usrname();
819     var user = fieldmapper.standardRequest(
820         ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
821     if(e = openils.Event.parse(user))
822         return alert(e);
823     userCache[id] = user;
824     return user.usrname();
825 }
826
827 function vlGetViewMARC(rowIdx, item) {
828     return item && this.grid.store.getValue(item, 'id');
829 }
830
831 function vlFormatViewMARC(id) {
832     return '<a href="javascript:void(0);" onclick="vlLoadMARCHtml(' + id + ', false, '+
833         'function(){displayGlobalDiv(\'vl-queue-div\');});">' + this.name + '</a>';
834 }
835
836 function vlGetOverlayTargetSelector(rowIdx, item) {
837     if(!item) return;
838     return this.grid.store.getValue(item, '_id') + ':' + this.grid.store.getValue(item, 'id');
839 }
840
841 function vlFormatOverlayTargetSelector(val) {
842     if(!val) return '';
843     var parts = val.split(':');
844     var _id = parts[0];
845     var id = parts[1];
846     var value = '<input type="checkbox" name="vl-overlay-target-RECID" '+
847         'onclick="vlHandleOverlayTargetSelected(ID, GRIDID);" gridid="GRIDID" match="ID"/>';
848     value = value.replace(/GRIDID/g, _id);
849     value = value.replace(/RECID/g, currentImportRecId);
850     value = value.replace(/ID/g, id);
851     if(_id == currentOverlayRecordsMapGid[currentImportRecId])
852         return value.replace('/>', 'checked="checked"/>');
853     return value;
854 }
855
856
857 /**
858   * see if the user has enabled overlays for the current match set and, 
859   * if so, map the current import record to the overlay target.
860   */
861 function vlHandleOverlayTargetSelected(recId, gridId) {
862     var noneSelected = true;
863     var checkboxes = dojo.query('[name=vl-overlay-target-'+currentImportRecId+']');
864     for(var i = 0; i < checkboxes.length; i++) {
865         var checkbox = checkboxes[i];
866         var matchRecId = checkbox.getAttribute('match');
867         var gid = checkbox.getAttribute('gridid');
868         if(checkbox.checked) {
869             if(matchRecId == recId && gid == gridId) {
870                 noneSelected = false;
871                 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
872                 currentOverlayRecordsMapGid[currentImportRecId] = gid;
873                 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
874                 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
875             } else {
876                 checkbox.checked = false;
877             }
878         }
879     }
880
881     if(noneSelected) {
882         delete currentOverlayRecordsMap[currentImportRecId];
883         delete currentOverlayRecordsMapGid[currentImportRecId];
884         dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
885         dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = '';
886     }
887 }
888
889 var valLastQueueType = null;
890 var vlQueueGridLayout = null;
891 function buildRecordGrid(type) {
892     displayGlobalDiv('vl-queue-div');
893
894     if(type == 'bib') {
895         openils.Util.show('vl-bib-queue-grid-wrapper');
896         openils.Util.hide('vl-auth-queue-grid-wrapper');
897         vlQueueGrid = vlBibQueueGrid;
898     } else {
899         openils.Util.show('vl-auth-queue-grid-wrapper');
900         openils.Util.hide('vl-bib-queue-grid-wrapper');
901         vlQueueGrid = vlAuthQueueGrid;
902     }
903
904
905     if(valLastQueueType != type) {
906         valLastQueueType = type;
907         vlQueueGridLayout = vlQueueGrid.attr('structure');
908         var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
909         attrDefMap[type] = {};
910         for(var i = 0; i < defs.length; i++) {
911             var def = defs[i]
912             attrDefMap[type][def.code()] = def.id();
913             var col = {
914                 name:def.description(), 
915                 field:'attr.' + def.code(),
916                 get: getAttrValue,
917                 selectableColumn:true
918             };
919             vlQueueGridLayout[0].cells[0].push(col);
920         }
921     }
922
923     dojo.forEach(vlQueueGridLayout[0].cells[0], 
924         function(cell) { 
925             if(cell.field.match(/^\+/)) 
926                 cell.nonSelectable=true;
927         }
928     );
929
930     var storeData;
931     if(type == 'bib')
932         storeData = vqbr.toStoreData(queuedRecords);
933     else
934         storeData = vqar.toStoreData(queuedRecords);
935
936     var store = new dojo.data.ItemFileReadStore({data:storeData});
937     vlQueueGrid.setStore(store);
938
939     if(vlQueueGridColumePicker[type]) {
940         vlQueueGrid.update();
941     } else {
942
943         vlQueueGridColumePicker[type] =
944             new openils.widget.GridColumnPicker(
945                 authtoken, 'vandelay.queue.'+type, vlQueueGrid, vlQueueGridLayout);
946         vlQueueGridColumePicker[type].load();
947     }
948 }
949
950 function vlQueueGridPrevPage() {
951     var page = parseInt(vlQueueDisplayPage.getValue());
952     if(page < 2) return;
953     vlQueueDisplayPage.setValue(page - 1);
954     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
955 }
956
957 function vlQueueGridNextPage() {
958     vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
959     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
960 }
961
962 function vlDeleteQueue(type, queueId, onload) {
963     fieldmapper.standardRequest(
964         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
965         {   async: true,
966             params: [authtoken, queueId],
967             oncomplete: function(r) {
968                 var resp = r.recv().content();
969                 if(e = openils.Event.parse(resp))
970                     return alert(e);
971                 onload();
972             }
973         }
974     );
975 }
976
977
978 function vlQueueGridDrawSelectBox(rowIdx, item) {
979     return item &&  this.grid.store.getValue(item, 'id');
980 }
981
982 function vlQueueGridFormatSelectBox(id) {
983     var domId = 'vl-record-list-selected-' + id;
984     if (id) { selectableGridRecords[domId] = id; }
985     return "<div><input type='checkbox' id='"+domId+"'/></div>";
986 }
987
988 function vlSelectAllQueueGridRecords() {
989     for(var id in selectableGridRecords) 
990         dojo.byId(id).checked = true;
991 }
992 function vlSelectNoQueueGridRecords() {
993     for(var id in selectableGridRecords) 
994         dojo.byId(id).checked = false;
995 }
996 function vlToggleQueueGridSelect() {
997     if(dojo.byId('vl-queue-grid-row-selector').checked)
998         vlSelectAllQueueGridRecords();
999     else
1000         vlSelectNoQueueGridRecords();
1001 }
1002
1003 var handleRetrieveRecords = function() {
1004     buildRecordGrid(currentType);
1005     vlFetchQueueSummary(currentQueueId, currentType, 
1006         function(summary) {
1007             dojo.byId('vl-queue-summary-name').innerHTML = summary.queue.name();
1008             dojo.byId('vl-queue-summary-total-count').innerHTML = summary.total +'';
1009             dojo.byId('vl-queue-summary-import-count').innerHTML = summary.imported + '';
1010             dojo.byId('vl-queue-summary-import-item-count').innerHTML = summary.total_items + '';
1011             dojo.byId('vl-queue-summary-rec-error-count').innerHTML = summary.rec_import_errors + '';
1012             dojo.byId('vl-queue-summary-item-error-count').innerHTML = summary.item_import_errors + '';
1013         }
1014     );
1015 }
1016
1017 function vlFetchQueueSummary(qId, type, onload) {
1018     fieldmapper.standardRequest(
1019         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.summary.retrieve'],
1020         {   async: true,
1021             params: [authtoken, qId],
1022             oncomplete : function(r) {
1023                 var summary = r.recv().content();
1024                 if(e = openils.Event.parse(summary))
1025                     return alert(e);
1026                 return onload(summary);
1027             }
1028         }
1029     );
1030 }
1031
1032 function vlHandleQueueItemsAction(action) {
1033
1034     dojo.connect(
1035         queueItemsImportCancelButton, 
1036         'onClick', 
1037         function() {
1038             queueItemsImportDialog.hide();
1039         }
1040     );
1041
1042     dojo.connect(
1043         queueItemsImportGoButton,
1044         'onClick', 
1045         function() {
1046             queueItemsImportDialog.hide();
1047
1048             // hack to set the widgets the import funcs will be looking at.  Reset them below.
1049             vlUploadQueueImportNoMatch.attr('value',  vlUploadQueueImportNoMatch2.attr('value'));
1050             vlUploadQueueAutoOverlayExact.attr('value',  vlUploadQueueAutoOverlayExact2.attr('value'));
1051             vlUploadQueueAutoOverlay1Match.attr('value',  vlUploadQueueAutoOverlay1Match2.attr('value'));
1052             vlUploadMergeProfile.attr('value',  vlUploadMergeProfile2.attr('value'));
1053             vlUploadQueueAutoOverlayBestMatch.attr('value',  vlUploadQueueAutoOverlayBestMatch2.attr('value'));
1054             vlUploadQueueAutoOverlayBestMatchRatio.attr('value',  vlUploadQueueAutoOverlayBestMatchRatio2.attr('value'));
1055
1056             if(action == 'import') {
1057                 vlImportSelectedRecords();
1058             } else if(action == 'import_all') {
1059                 vlImportAllRecords();
1060             }
1061             
1062             // reset the widgets to prevent accidental future actions
1063             vlUploadQueueImportNoMatch.attr('value',  false);
1064             vlUploadQueueImportNoMatch2.attr('value', false);
1065             vlUploadQueueAutoOverlayExact.attr('value', false);
1066             vlUploadQueueAutoOverlayExact2.attr('value', false);
1067             vlUploadQueueAutoOverlay1Match.attr('value', false);
1068             vlUploadQueueAutoOverlay1Match2.attr('value', false);
1069             vlUploadMergeProfile.attr('value', '');
1070             vlUploadMergeProfile2.attr('value', '');
1071             vlUploadQueueAutoOverlayBestMatch.attr('value', false);
1072             vlUploadQueueAutoOverlayBestMatch2.attr('value', false);
1073             vlUploadQueueAutoOverlayBestMatchRatio.attr('value', '0.0');
1074             vlUploadQueueAutoOverlayBestMatchRatio2.attr('value', '0.0');
1075         }
1076     );
1077
1078     queueItemsImportDialog.show();
1079 }
1080     
1081
1082 /* import user-selected records */
1083 function vlImportSelectedRecords() {
1084     var records = [];
1085
1086     for(var id in selectableGridRecords) {
1087         if(dojo.byId(id).checked) {
1088             var recId = selectableGridRecords[id];
1089             var rec = queuedRecordsMap[recId];
1090             if(!rec.import_time()) 
1091                 records.push(recId);
1092         }
1093     }
1094
1095     vlImportRecordQueue(
1096         currentType, 
1097         currentQueueId, 
1098         records,
1099         function(){
1100             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
1101         }
1102     );
1103 }
1104
1105 /* import all (non-imported) queue records */
1106 function vlImportAllRecords() {
1107     vlImportRecordQueue(
1108         currentType, 
1109         currentQueueId, 
1110         null,
1111         function(){
1112             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
1113         }
1114     );
1115 }
1116
1117 /* if recList has values, import only those records */
1118 function vlImportRecordQueue(type, queueId, recList, onload) {
1119     displayGlobalDiv('vl-generic-progress-with-total');
1120
1121     /* set up options */
1122     var mergeOpt = false;
1123     var options = {overlay_map : currentOverlayRecordsMap};
1124
1125     if(vlUploadQueueImportNoMatch.checked) {
1126         options.import_no_match = true;
1127         vlUploadQueueImportNoMatch.checked = false;
1128     }
1129
1130     if(vlUploadQueueAutoOverlayExact.checked) {
1131         options.auto_overlay_exact = true;
1132         vlUploadQueueAutoOverlayExact.checked = false;
1133         mergeOpt = true;
1134     }
1135
1136     if(vlUploadQueueAutoOverlayBestMatch.checked) {
1137         options.auto_overlay_best_match = true;
1138         vlUploadQueueAutoOverlayBestMatch.checked = false;
1139         options.match_quality_ratio = vlUploadQueueAutoOverlayBestMatchRatio.attr('value');
1140         mergeOpt = true;
1141     }
1142
1143     if(vlUploadQueueAutoOverlay1Match.checked) {
1144         options.auto_overlay_1match = true;
1145         vlUploadQueueAutoOverlay1Match.checked = false;
1146         options.match_quality_ratio = vlUploadQueueAutoOverlayBestMatchRatio.attr('value');
1147         mergeOpt = true;
1148     }
1149
1150     var profile = vlUploadMergeProfile.attr('value');
1151     if(profile != null && profile != '') {
1152         options.merge_profile = profile;
1153     }
1154
1155     /* determine which method we're calling */
1156
1157     var method = 'open-ils.vandelay.bib_queue.import';
1158     if(type == 'auth')
1159         method = method.replace('bib', 'auth');
1160
1161     if(!mergeOpt) {
1162         // in the interest of speed, if no merge options are 
1163         // chosen, tell the back-end code to only process records
1164         // that have no matches
1165         method = method.replace(/\.import/, '.nomatch.import');
1166     }
1167
1168     var params = [authtoken, queueId, options];
1169     if(recList) {
1170         method = 'open-ils.vandelay.'+currentType+'_record.list.import';
1171         params[1] = recList;
1172     }
1173
1174     fieldmapper.standardRequest(
1175         ['open-ils.vandelay', method],
1176         {   async: true,
1177             params: params,
1178             onresponse: function(r) {
1179                 var resp = r.recv().content();
1180                 if(e = openils.Event.parse(resp))
1181                     return alert(e);
1182                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
1183             },
1184             oncomplete: function() {onload();}
1185         }
1186     );
1187 }
1188
1189
1190 /**
1191   * Create queue, upload MARC, process spool, load the newly created queue 
1192   */
1193 function batchUpload() {
1194     var queueName = dijit.byId('vl-queue-name').getValue();
1195     currentType = dijit.byId('vl-record-type').getValue();
1196
1197     var handleProcessSpool = function() {
1198         if( 
1199             vlUploadQueueImportNoMatch.checked || 
1200             vlUploadQueueAutoOverlayExact.checked || 
1201             vlUploadQueueAutoOverlay1Match.checked ||
1202             vlUploadQueueAutoOverlayBestMatch.checked ) {
1203
1204                 vlImportRecordQueue(
1205                     currentType, 
1206                     currentQueueId, 
1207                     null,
1208                     function() {
1209                         retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
1210                     }
1211                 );
1212         } else {
1213             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
1214         }
1215     }
1216
1217     var handleUploadMARC = function(key) {
1218         dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
1219         processSpool(key, currentQueueId, currentType, handleProcessSpool);
1220     };
1221
1222     var handleCreateQueue = function(queue) {
1223         currentQueueId = queue.id();
1224         uploadMARC(handleUploadMARC);
1225     };
1226     
1227     if(vlUploadQueueSelector.getValue() && !queueName) {
1228         currentQueueId = vlUploadQueueSelector.getValue();
1229         uploadMARC(handleUploadMARC);
1230     } else {
1231         createQueue(queueName, currentType, handleCreateQueue, 
1232             vlUploadQueueHoldingsImportProfile.attr('value'),
1233             vlUploadQueueMatchSet.attr('value')
1234         );
1235     }
1236 }
1237
1238
1239 function vlFleshQueueSelect(selector, type) {
1240     var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
1241     selector.store = new dojo.data.ItemFileReadStore({data:data});
1242     selector.setValue(null);
1243     selector.setDisplayedValue('');
1244     if(data[0])
1245         selector.setValue(data[0].id());
1246
1247     var qInput = dijit.byId('vl-queue-name');
1248
1249     var selChange = function(val) {
1250         console.log('selector onchange');
1251         // user selected a queue from the selector;  clear the input and 
1252         // set the item import profile already defined for the queue
1253         var queue = allUserBibQueues.filter(function(q) { return (q.id() == val) })[0];
1254         if(val) {
1255             vlUploadQueueHoldingsImportProfile.attr('value', queue.item_attr_def() || '');
1256             vlUploadQueueHoldingsImportProfile.attr('disabled', true);
1257             vlUploadQueueMatchSet.attr('value', queue.match_set() || '');
1258             vlUploadQueueMatchSet.attr('disabled', true);
1259         } else {
1260             vlUploadQueueHoldingsImportProfile.attr('value', '');
1261             vlUploadQueueHoldingsImportProfile.attr('disabled', false);
1262             vlUploadQueueMatchSet.attr('value', '');
1263             vlUploadQueueMatchSet.attr('disabled', false);
1264         }
1265         dojo.disconnect(qInput._onchange);
1266         qInput.attr('value', '');
1267         qInput._onchange = dojo.connect(qInput, 'onChange', inputChange);
1268     }
1269     
1270     var inputChange = function(val) {
1271         console.log('qinput onchange');
1272         // user entered a new queue name. clear the selector 
1273         vlUploadQueueHoldingsImportProfile.attr('value', '');
1274         vlUploadQueueHoldingsImportProfile.attr('disabled', false);
1275         vlUploadQueueMatchSet.attr('value', '');
1276         vlUploadQueueMatchSet.attr('disabled', false);
1277         dojo.disconnect(selector._onchange);
1278         selector.attr('value', '');
1279         selector._onchange = dojo.connect(selector, 'onChange', selChange);
1280     }
1281
1282     selector._onchange = dojo.connect(selector, 'onChange', selChange);
1283     qInput._onchange = dojo.connect(qInput, 'onChange', inputChange);
1284 }
1285
1286 function vlUpdateMatchSetSelector(type) {
1287     type = (type.match(/bib/)) ? 'biblio' : 'authority';
1288     vlUploadQueueMatchSet.store = 
1289         new dojo.data.ItemFileReadStore({data:vms.toStoreData(matchSets[type])});
1290 }
1291
1292 function vlShowUploadForm() {
1293     displayGlobalDiv('vl-marc-upload-div');
1294     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
1295     vlUploadSourceSelector.store = 
1296         new dojo.data.ItemFileReadStore({data:cbs.toStoreData(vlBibSources, 'source')});
1297     vlUploadSourceSelector.setValue(vlBibSources[0].id());
1298     vlUploadQueueHoldingsImportProfile.store = 
1299         new dojo.data.ItemFileReadStore({data:viiad.toStoreData(importItemDefs)});
1300     vlUpdateMatchSetSelector(vlUploadRecordType.getValue());
1301
1302     // use ratio from the merge profile if it's set
1303     dojo.connect(
1304         vlUploadMergeProfile, 
1305         'onChange',
1306         function(val) {
1307             if(!val) return;
1308             var profile = mergeProfiles.filter(function(p) { return (p.id() == val); })[0];
1309             if(profile.lwm_ratio() != null)
1310                vlUploadQueueAutoOverlayBestMatchRatio.attr('value', profile.lwm_ratio()+''); 
1311         }
1312     );
1313     dojo.connect(
1314         vlUploadMergeProfile2, 
1315         'onChange',
1316         function(val) {
1317             if(!val) return;
1318             var profile = mergeProfiles.filter(function(p) { return (p.id() == val); })[0];
1319             if(profile.lwm_ratio() != null)
1320                vlUploadQueueAutoOverlayBestMatchRatio2.attr('value', profile.lwm_ratio()+''); 
1321         }
1322     );
1323
1324 }
1325
1326 function vlShowQueueSelect() {
1327     displayGlobalDiv('vl-queue-select-div');
1328     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
1329 }
1330
1331 function vlShowMatchSetEditor() {
1332     displayGlobalDiv('vl-match-set-editor-div');
1333     dojo.byId('vl-match-set-editor-div').appendChild(
1334         dojo.create('iframe', {
1335             id : 'vl-match-set-iframe',
1336             src : oilsBasePath + '/eg/conify/global/vandelay/match_set',
1337             style : 'width:100%; height:500px; border:none; margin:0px;'
1338         })
1339     );
1340 }
1341
1342 function vlFetchQueueFromForm() {
1343     currentType = vlQueueSelectType.getValue();
1344     currentQueueId = vlQueueSelectQueueList.getValue();
1345     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
1346 }
1347
1348 function vlOpenMarcEditWindow(rec, postReloadHTMLHandler) {
1349     /*
1350         To run in Firefox directly, must set signed.applets.codebase_principal_support
1351         to true in about:config
1352     */
1353     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
1354     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
1355
1356     var type;
1357     if (currentType == 'bib') {
1358         type = 'bre';
1359     } else {
1360         type = 'are';
1361     }
1362
1363     function onsave(r) {
1364         // after the record is saved, reload the HTML display
1365         var stat = r.recv().content();
1366         if(e = openils.Event.parse(stat))
1367             return alert(e);
1368         alert(dojo.byId('vl-marc-edit-complete-label').innerHTML);
1369         win.close();
1370         vlLoadMARCHtml(rec.id(), false, postReloadHTMLHandler);
1371     }
1372
1373     win.xulG = {
1374         record : {marc : rec.marc(), "rtype": type},
1375         save : {
1376             label: dojo.byId('vl-marc-edit-save-label').innerHTML,
1377             func: function(xmlString) {
1378                 var method = 'open-ils.permacrud.update.' + rec.classname;
1379                 rec.marc(xmlString);
1380                 fieldmapper.standardRequest(
1381                     ['open-ils.permacrud', method],
1382                     {   async: true,
1383                         params: [authtoken, rec],
1384                         oncomplete: onsave
1385                     }
1386                 );
1387             },
1388         },
1389         'lock_tab' : typeof xulG != 'undefined' ? (typeof xulG['lock_tab'] != 'undefined' ? xulG.lock_tab : undefined) : undefined,
1390         'unlock_tab' : typeof xulG != 'undefined' ? (typeof xulG['unlock_tab'] != 'undefined' ? xulG.unlock_tab : undefined) : undefined
1391     };
1392 }
1393
1394 function vlLoadMarcEditor(type, recId, postReloadHTMLHandler) {
1395     var method = 'open-ils.permacrud.search.vqbr';
1396     if(currentType != 'bib')
1397         method = method.replace(/vqbr/,'vqar');
1398
1399     fieldmapper.standardRequest(
1400         ['open-ils.permacrud', method],
1401         {   async: true, 
1402             params: [authtoken, {id : recId}],
1403             oncomplete: function(r) {
1404                 var rec = r.recv().content();
1405                 if(e = openils.Event.parse(rec))
1406                     return alert(e);
1407                 vlOpenMarcEditWindow(rec, postReloadHTMLHandler);
1408             }
1409         }
1410     );
1411 }
1412
1413
1414
1415 //------------------------------------------------------------
1416 // attribute editors
1417
1418 // attribute-editor global variables
1419
1420 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
1421 var ATTR_EDIT_ID = null;                // id of current 'edit' attribute
1422 var ATTR_EDIT_GROUP = 'bib';            // bib-attrs or auth-attrs
1423
1424 function vlAttrEditorInit() {
1425     // set up tooltips on the edit form
1426     connectTooltip('attr-editor-tags'); 
1427     connectTooltip('attr-editor-subfields'); 
1428 }
1429
1430 function vlShowAttrEditor() {
1431     displayGlobalDiv('vl-attr-editor-div');
1432     loadAttrEditorGrid();
1433     idHide('vl-generic-progress');
1434 }
1435
1436 function setAttrEditorGroup(groupName) {
1437     // put us into 'bib'-attr or 'auth'-attr mode.
1438     if (ATTR_EDIT_GROUP != groupName) {
1439         ATTR_EDIT_GROUP = groupName;
1440         loadAttrEditorGrid();
1441     }
1442 }
1443
1444 function onAttrEditorOpen() {
1445     // the "bars" have the create/update/cancel/etc. buttons.
1446     var create_bar = document.getElementById('attr-editor-create-bar');
1447     var update_bar = document.getElementById('attr-editor-update-bar');
1448     if (ATTR_EDITOR_IN_UPDATE_MODE) {
1449         update_bar.style.display='table-row';
1450         create_bar.style.display='none';
1451         // hide the dropdown-button
1452         idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
1453     } else {
1454         dijit.byId('attr-editor-dialog').reset();
1455         create_bar.style.display='table-row';
1456         update_bar.style.display='none';
1457     }
1458 }
1459
1460 function onAttrEditorClose() {
1461     // reset the form to a "create" form. (We may have borrowed it for editing.)
1462     ATTR_EDITOR_IN_UPDATE_MODE = false;
1463     // show the dropdown-button
1464     idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
1465 }
1466
1467 function loadAttrEditorGrid() {
1468     var _data = (ATTR_EDIT_GROUP == 'auth') ? 
1469         vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
1470
1471     var store = new dojo.data.ItemFileReadStore({data:_data});
1472     attrEditorGrid.setStore(store);
1473     attrEditorGrid.onRowDblClick = onAttrEditorClick;
1474     attrEditorGrid.update();
1475 }
1476
1477 function attrGridGetTag(n, item) {
1478     // grid helper: return the tags from the row's xpath column.
1479     return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).tags;
1480 }
1481
1482 function attrGridGetSubfield(n, item) {
1483     // grid helper: return the subfields from the row's xpath column.
1484     return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).subfields;
1485 }
1486
1487 function onAttrEditorClick() {
1488     var row = this.getItem(this.focus.rowIndex);
1489     ATTR_EDIT_ID = this.store.getValue(row, 'id');
1490     ATTR_EDITOR_IN_UPDATE_MODE = true;
1491
1492     // populate the popup editor.
1493     dijit.byId('attr-editor-code').attr('value', this.store.getValue(row, 'code'));
1494     dijit.byId('attr-editor-description').attr('value', this.store.getValue(row, 'description'));
1495     var parsed_xpath = xpathParser.parse(this.store.getValue(row, 'xpath'));
1496     dijit.byId('attr-editor-tags').attr('value', parsed_xpath.tags);
1497     dijit.byId('attr-editor-subfields').attr('value', parsed_xpath.subfields);
1498     dijit.byId('attr-editor-xpath').attr('value', this.store.getValue(row, 'xpath'));
1499     dijit.byId('attr-editor-remove').attr('value', this.store.getValue(row, 'remove'));
1500
1501     // set up UI for editing
1502     dojo.byId('vl-create-attr-editor-button').click();
1503 }
1504
1505 function vlSaveAttrDefinition(data) {
1506     idHide('vl-attr-editor-div');
1507     idShow('vl-generic-progress');
1508
1509     data.id = ATTR_EDIT_ID;
1510
1511     // this ought to honour custom xpaths, but overwrite xpaths
1512     // derived from tags/subfields.
1513     if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
1514         var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
1515         data.xpath = _xpath;
1516     }
1517
1518     // build up our permacrud params. Key variables here are
1519     // "create or update" and "bib or auth".
1520
1521     var isAuth   = (ATTR_EDIT_GROUP == 'auth');
1522     var isCreate = (ATTR_EDIT_ID == null);
1523     var rad      = isAuth ? new vqarad() : new vqbrad() ;
1524     var method   = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.') 
1525         + (isAuth ? 'vqarad' : 'vqbrad');
1526     var _data    = rad.fromStoreItem(data);
1527
1528     _data.ischanged(1);
1529
1530     fieldmapper.standardRequest(
1531         ['open-ils.permacrud', method],
1532         {   async: true,
1533             params: [authtoken, _data ],
1534             onresponse: function(r) { },
1535             oncomplete: function(r) {
1536                 attrEditorFetchAttrDefs(vlShowAttrEditor);
1537                 ATTR_EDIT_ID = null;
1538             },
1539             onerror: function(r) {
1540                 alert('vlSaveAttrDefinition comms error: ' + r);
1541             }
1542         }
1543     );
1544 }
1545
1546 function attrEditorFetchAttrDefs(callback) {
1547     var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
1548     return fn(callback);
1549 }
1550
1551 function vlAttrDelete() {
1552     idHide('vl-attr-editor-div');
1553     idShow('vl-generic-progress');
1554
1555     var isAuth = (ATTR_EDIT_GROUP == 'auth');
1556     var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
1557     var rad    = isAuth ? new vqarad() : new vqbrad() ;
1558     fieldmapper.standardRequest(
1559         ['open-ils.permacrud', method],
1560         {   async: true,
1561             params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
1562             oncomplete: function() {
1563                 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
1564                 attrEditorFetchAttrDefs(vlShowAttrEditor);
1565                 ATTR_EDIT_ID = null;
1566             },
1567             onerror: function(r) {
1568                 alert('vlAttrDelete comms error: ' + r);
1569             }
1570         }
1571     );
1572 }
1573
1574 // ------------------------------------------------------------
1575 // utilities for attribute editors
1576
1577 // dom utilities (maybe dojo does these, and these should be replaced)
1578
1579 function idStyle(obId, k, v)    { document.getElementById(obId).style[k] = v;   }
1580 function idShow(obId)           { idStyle(obId, 'display', 'block');            }
1581 function idHide(obId)           { idStyle(obId, 'display' , 'none');            }
1582
1583 function connectTooltip(fieldId) {
1584     // Given an element id, look up a tooltip element in the doc (same
1585     // id with a '-tip' suffix) and associate the two. Maybe dojo has
1586     // a better way to do this?
1587     var fld = dojo.byId(fieldId);
1588     var tip = dojo.byId(fieldId + '-tip');
1589     dojo.connect(fld, 'onfocus', function(evt) {
1590                      dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1591     dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1592 }
1593
1594 // xpath utilities
1595
1596 var xpathParser = new openils.MarcXPathParser();
1597
1598 function tagAndSubFieldsToXpath(tags, subfields) {
1599     // given tags, and subfields, build up an XPath.
1600     try {
1601         var parts = {
1602             'tags':tags.match(/[\d]+/g), 
1603             'subfields':subfields.match(/[a-zA-z]/g) };
1604         return xpathParser.compile(parts);
1605     } catch (err) {
1606         return {'parts':null, 'tags':null, 'error':err};
1607     }
1608 }
1609
1610 function looksLikeDerivedXpath(path) {
1611     // Does this path look like it was derived from tags and subfields?
1612     var parsed = xpathParser.parse(path);
1613     if (parsed.tags == null) 
1614         return false;
1615     var compiled = xpathParser.compile(parsed);
1616     return (path == compiled);
1617 }
1618
1619 // amazing xpath-util unit-tests
1620 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]'))     alert('vandelay xpath-utility error');
1621 if ( looksLikeDerivedXpath('ba-boo-ba-boo!'))                   alert('vandelay xpath-utility error');
1622
1623
1624
1625 var profileContextOrg
1626 function vlShowProfileEditor() {
1627     displayGlobalDiv('vl-profile-editor-div');
1628     buildProfileGrid();
1629
1630     var connect = function() {
1631         dojo.connect(profileContextOrgSelector, 'onChange',
1632             function() {
1633                 profileContextOrg = this.attr('value');
1634                 pGrid.resetStore();
1635                 buildProfileGrid();
1636             }
1637         );
1638     };
1639
1640     new openils.User().buildPermOrgSelector(
1641         'ADMIN_MERGE_PROFILE', profileContextOrgSelector, null, connect);
1642 }
1643
1644 function buildProfileGrid() {
1645
1646     if(profileContextOrg == null)
1647         profileContextOrg = openils.User.user.ws_ou();
1648
1649     pGrid.loadAll( 
1650         {order_by : {vmp : 'name'}}, 
1651         {owner : fieldmapper.aou.fullPath(profileContextOrg, true)}
1652     );
1653 }
1654
1655 /* --- Import Item Attr Grid --------------- */
1656
1657 var itemAttrContextOrg;
1658 function vlShowImportItemAttrEditor() {
1659     displayGlobalDiv('vl-item-attr-editor-div');
1660     buildImportItemAttrGrid();
1661
1662     var connect = function() {
1663         dojo.connect(itemAttrContextOrgSelector, 'onChange',
1664             function() {
1665                 itemAttrContextOrg = this.attr('value');
1666                 itemAttrGrid.resetStore();
1667                 vlShowImportItemAttrEditor();
1668             }
1669         );
1670     };
1671
1672     new openils.User().buildPermOrgSelector(
1673         'ADMIN_IMPORT_ITEM_ATTR_DEF', 
1674             itemAttrContextOrgSelector, null, connect);
1675 }
1676
1677 function buildImportItemAttrGrid() {
1678
1679     if(itemAttrContextOrg == null)
1680         itemAttrContextOrg = openils.User.user.ws_ou();
1681
1682     itemAttrGrid.loadAll( 
1683         {order_by : {viiad : 'name'}}, 
1684         {owner : fieldmapper.aou.fullPath(itemAttrContextOrg, true)}
1685     );
1686 }
1687