]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/vandelay/vandelay.js
Fix a glitch where you could only edit one field in 'Edit Attributes' in
[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 ];
62
63 var authtoken;
64 var VANDELAY_URL = '/vandelay-upload';
65 var bibAttrDefs = [];
66 var authAttrDefs = [];
67 var queuedRecords = [];
68 var queuedRecordsMap = {};
69 var bibAttrsFetched = false;
70 var authAttrsFetched = false;
71 var attrDefMap = {}; // maps attr def code names to attr def ids
72 var currentType;
73 var currentQueueId = null;
74 var userCache = {};
75 var currentMatchedRecords; // set of loaded matched bib records
76 var currentOverlayRecordsMap; // map of import record to overlay record
77 var currentOverlayRecordsMapGid; // map of import record to overlay record grid id
78 var currentImportRecId; // when analyzing matches, this is the current import record
79 var userBibQueues = []; // only non-complete queues
80 var userAuthQueues = []; // only non-complete queues
81 var allUserBibQueues;
82 var allUserAuthQueues;
83 var selectableGridRecords;
84 var cgi = new openils.CGI();
85 var vlQueueGridColumePicker = {};
86 var vlBibSources = [];
87 var importItemDefs = [];
88
89 /**
90   * Grab initial data
91   */
92 function vlInit() {
93     authtoken = openils.User.authtoken;
94     var initNeeded = 6; // how many async responses do we need before we're init'd 
95     var initCount = 0; // how many async reponses we've received
96
97     openils.Util.registerEnterHandler(
98         vlQueueDisplayPage.domNode, function(){retrieveQueuedRecords();});
99     openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
100
101     function checkInitDone() {
102         initCount++;
103         if(initCount == initNeeded)
104             runStartupCommands();
105     }
106
107     var profiles = new openils.PermaCrud().retrieveAll('vmp');
108     vlUploadMergeProfile.store = new dojo.data.ItemFileReadStore({data:fieldmapper.vmp.toStoreData(profiles)});
109     vlUploadMergeProfile.labelAttr = 'name';
110     vlUploadMergeProfile.searchAttr = 'name';
111     vlUploadMergeProfile.startup();
112
113     vlUploadMergeProfile2.store = new dojo.data.ItemFileReadStore({data:fieldmapper.vmp.toStoreData(profiles)});
114     vlUploadMergeProfile2.labelAttr = 'name';
115     vlUploadMergeProfile2.searchAttr = 'name';
116     vlUploadMergeProfile2.startup();
117
118
119     // Fetch the bib and authority attribute definitions 
120     vlFetchBibAttrDefs(function () { checkInitDone(); });
121     vlFetchAuthAttrDefs(function () { checkInitDone(); });
122
123     vlRetrieveQueueList('bib', null, 
124         function(list) {
125             allUserBibQueues = list;
126             for(var i = 0; i < allUserBibQueues.length; i++) {
127                 if(allUserBibQueues[i].complete() == 'f')
128                     userBibQueues.push(allUserBibQueues[i]);
129             }
130             checkInitDone();
131         }
132     );
133
134     vlRetrieveQueueList('auth', null, 
135         function(list) {
136             allUserAuthQueues = list;
137             for(var i = 0; i < allUserAuthQueues.length; i++) {
138                 if(allUserAuthQueues[i].complete() == 'f')
139                     userAuthQueues.push(allUserAuthQueues[i]);
140             }
141             checkInitDone();
142         }
143     );
144
145     fieldmapper.standardRequest(
146         ['open-ils.permacrud', 'open-ils.permacrud.search.cbs.atomic'],
147         {   async: true,
148             params: [authtoken, {id:{"!=":null}}, {order_by:{cbs:'id'}}],
149             oncomplete : function(r) {
150                 vlBibSources = openils.Util.readResponse(r, false, true);
151                 checkInitDone();
152             }
153         }
154     );
155
156     var owner = fieldmapper.aou.orgNodeTrail(fieldmapper.aou.findOrgUnit(new openils.User().user.ws_ou()));
157     new openils.PermaCrud().search('viiad', 
158         {owner: owner.map(function(org) { return org.id(); })},
159         {   async: true,
160             oncomplete: function(r) {
161                 importItemDefs = openils.Util.readResponse(r);
162                 checkInitDone();
163             }
164         }
165     );
166
167     vlAttrEditorInit();
168 }
169
170
171 openils.Util.addOnLoad(vlInit);
172
173
174 // fetch the bib and authority attribute definitions
175
176 function vlFetchBibAttrDefs(postcomplete) {
177     bibAttrDefs = [];
178     fieldmapper.standardRequest(
179         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
180         {   async: true,
181             params: [authtoken, {id:{'!=':null}}],
182             onresponse: function(r) {
183                 var def = r.recv().content(); 
184                 if(e = openils.Event.parse(def[0])) 
185                     return alert(e);
186                 bibAttrDefs.push(def);
187             },
188             oncomplete: function() {
189                 bibAttrDefs = bibAttrDefs.sort(
190                     function(a, b) {
191                         if(a.id() > b.id()) return 1;
192                         if(a.id() < b.id()) return -1;
193                         return 0;
194                     }
195                 );
196                 postcomplete();
197             }
198         }
199     );
200 }
201
202 function vlFetchAuthAttrDefs(postcomplete) {
203     authAttrDefs = [];
204     fieldmapper.standardRequest(
205         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
206         {   async: true,
207             params: [authtoken, {id:{'!=':null}}],
208             onresponse: function(r) {
209                 var def = r.recv().content(); 
210                 if(e = openils.Event.parse(def[0])) 
211                     return alert(e);
212                 authAttrDefs.push(def);
213             },
214             oncomplete: function() {
215                 authAttrDefs = authAttrDefs.sort(
216                     function(a, b) {
217                         if(a.id() > b.id()) return 1;
218                         if(a.id() < b.id()) return -1;
219                         return 0;
220                     }
221                 );
222                 postcomplete();
223             }
224         }
225     );
226 }
227
228 function vlRetrieveQueueList(type, filter, onload) {
229     type = (type == 'bib') ? type : 'authority';
230     fieldmapper.standardRequest(
231         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
232         {   async: true,
233             params: [authtoken, null, filter],
234             oncomplete: function(r) {
235                 var list = r.recv().content();
236                 if(e = openils.Event.parse(list[0]))
237                     return alert(e);
238                 onload(list);
239             }
240         }
241     );
242
243 }
244
245 function displayGlobalDiv(id) {
246     for(var i = 0; i < globalDivs.length; i++) {
247         try {
248             dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
249         } catch(e) {
250             alert('please define div ' + globalDivs[i]);
251         }
252     }
253     dojo.style(dojo.byId(id),'display','block');
254
255     openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
256     openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
257     openils.Util.removeCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
258     openils.Util.removeCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
259     openils.Util.removeCSSClass(dojo.byId('vl-menu-profile-editor'), 'toolbar_selected');
260
261     switch(id) {
262         case 'vl-marc-export-div':
263             openils.Util.addCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
264             break;
265         case 'vl-marc-upload-div':
266             openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
267             break;
268         case 'vl-queue-select-div':
269             openils.Util.addCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
270             break;
271         case 'vl-attr-editor-div':
272             openils.Util.addCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
273             break;
274         case 'vl-profile-editor-div':
275             openils.Util.addCSSClass(dojo.byId('vl-menu-profile-editor'), 'toolbar_selected');
276             break;
277         case 'vl-item-attr-editor-div':
278             openils.Util.addCSSClass(dojo.byId('vl-menu-import-item-attr-editor'), 'toolbar_selected');
279             break;
280     }
281 }
282
283 function runStartupCommands() {
284     currentQueueId = cgi.param('qid');
285     currentType = cgi.param('qtype');
286     dojo.style('vl-nav-bar', 'visibility', 'visible');
287     if(currentQueueId)
288         return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
289     vlShowUploadForm();
290 }
291
292 /**
293   * asynchronously upload a file of MARC records
294   */
295 function uploadMARC(onload){
296     dojo.byId('vl-upload-status-count').innerHTML = '0';
297     dojo.byId('vl-ses-input').value = authtoken;
298     displayGlobalDiv('vl-marc-upload-status-div');
299     dojo.io.iframe.send({
300         url: VANDELAY_URL,
301         method: "post",
302         handleAs: "html",
303         form: dojo.byId('vl-marc-upload-form'),
304         handle: function(data,ioArgs){
305             var content = data.documentElement.textContent;
306             onload(content);
307         }
308     });
309 }       
310
311 /**
312   * Creates a new vandelay queue
313   */
314 function createQueue(queueName, type, onload, importDefId) {
315     var name = (type=='bib') ? 'bib' : 'authority';
316     var method = 'open-ils.vandelay.'+ name +'_queue.create'
317     fieldmapper.standardRequest(
318         ['open-ils.vandelay', method],
319         {   async: true,
320             params: [authtoken, queueName, null, name, importDefId],
321             oncomplete : function(r) {
322                 var queue = r.recv().content();
323                 if(e = openils.Event.parse(queue)) 
324                     return alert(e);
325                 onload(queue);
326             }
327         }
328     );
329 }
330
331 /**
332   * Tells vandelay to pull a batch of records from the cache and explode them
333   * out into the vandelay tables
334   */
335 function processSpool(key, queueId, type, onload) {
336     fieldmapper.standardRequest(
337         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
338         {   async: true,
339             params: [authtoken, key, queueId],
340             onresponse : function(r) {
341                 var resp = r.recv().content();
342                 if(e = openils.Event.parse(resp)) 
343                     return alert(e);
344                 dojo.byId('vl-upload-status-count').innerHTML = resp;
345             },
346             oncomplete : function(r) {onload();}
347         }
348     );
349 }
350
351 function retrieveQueuedRecords(type, queueId, onload) {
352     displayGlobalDiv('vl-generic-progress');
353     queuedRecords = [];
354     queuedRecordsMap = {};
355     currentOverlayRecordsMap = {};
356     currentOverlayRecordsMapGid = {};
357     selectableGridRecords = {};
358     //resetVlQueueGridLayout();
359
360     if(!type) type = currentType;
361     if(!queueId) queueId = currentQueueId;
362     if(!onload) onload = handleRetrieveRecords;
363
364     var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
365     if(vlQueueGridShowMatches.checked)
366         method = method.replace('records', 'records.matches');
367
368     var sel = dojo.byId('vl-queue-display-limit-selector');
369     var limit = parseInt(sel.options[sel.selectedIndex].value);
370     var offset = limit * parseInt(vlQueueDisplayPage.attr('value')-1);
371
372     var params =  [authtoken, queueId, {clear_marc: 1, offset: offset, limit: limit}];
373     if(vlQueueGridShowNonImport.checked)
374         params[2].non_imported = 1;
375
376     fieldmapper.standardRequest(
377         ['open-ils.vandelay', method],
378         {   async: true,
379             params: params,
380             /*
381             onresponse: function(r) {
382                 console.log("ONREPONSE");
383                 var rec = r.recv().content();
384                 if(e = openils.Event.parse(rec))
385                     return alert(e);
386                 console.log("got record " + rec.id());
387                 queuedRecords.push(rec);
388                 queuedRecordsMap[rec.id()] = rec;
389             },
390             */
391             oncomplete: function(r){
392                 var recs = r.recv().content();
393                 if(e = openils.Event.parse(recs[0]))
394                     return alert(e);
395                 for(var i = 0; i < recs.length; i++) {
396                     var rec = recs[i];
397                     queuedRecords.push(rec);
398                     queuedRecordsMap[rec.id()] = rec;
399                 }
400                 onload();
401             }
402         }
403     );
404 }
405
406 function vlLoadMatchUI(recId) {
407     displayGlobalDiv('vl-generic-progress');
408     var matches = queuedRecordsMap[recId].matches();
409     var records = [];
410     currentImportRecId = recId;
411     for(var i = 0; i < matches.length; i++)
412         records.push(matches[i].eg_record());
413
414     var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
415     var params = [records];
416     if(currentType == 'auth') {
417         retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
418         parmas = [authtoken, records, {clear_marc:1}];
419     }
420
421     fieldmapper.standardRequest(
422         retrieve,
423         {   async: true,
424             params:params,
425             oncomplete: function(r) {
426                 var recs = r.recv().content();
427                 if(e = openils.Event.parse(recs))
428                     return alert(e);
429
430                 /* ui mangling */
431                 displayGlobalDiv('vl-match-div');
432                 resetVlMatchGridLayout();
433                 currentMatchedRecords = recs;
434                 vlMatchGrid.setStructure(vlMatchGridLayout);
435
436                 // build the data store of records with match information
437                 var dataStore = bre.toStoreData(recs, null, 
438                     {virtualFields:['dest_matchpoint', 'src_matchpoint', '_id']});
439                 dataStore.identifier = '_id';
440
441                 var matchSeenMap = {};
442
443                 for(var i = 0; i < dataStore.items.length; i++) {
444                     var item = dataStore.items[i];
445                     item._id = i; // just need something unique
446                     for(var j = 0; j < matches.length; j++) {
447                         var match = matches[j];
448                         if(match.eg_record() == item.id && !matchSeenMap[match.id()]) {
449                             item.dest_matchpoint = match.field_type();
450                             var attr = getRecAttrFromMatch(queuedRecordsMap[recId], match);
451                             item.src_matchpoint = getRecAttrDefFromAttr(attr, currentType).code();
452                             matchSeenMap[match.id()] = 1;
453                             break;
454                         }
455                     }
456                 }
457
458                 // now populate the grid
459                 vlPopulateMatchGrid(vlMatchGrid, dataStore);
460             }
461         }
462     );
463 }
464
465 function vlPopulateMatchGrid(grid, data) {
466     var store = new dojo.data.ItemFileReadStore({data:data});
467     grid.setStore(store);
468     grid.update();
469 }
470
471 function showMe(id) {
472     dojo.style(dojo.byId(id), 'display', 'block');
473 }
474 function hideMe(id) {
475     dojo.style(dojo.byId(id), 'display', 'none');
476 }
477
478
479 function vlLoadMARCHtml(recId, inCat, oncomplete) {
480     dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
481     displayGlobalDiv('vl-generic-progress');
482     var api;
483     var params = [recId, 1];
484
485     if(inCat) {
486         hideMe('vl-marc-html-edit-button'); // don't show marc editor button
487         dijit.byId('vl-marc-html-edit-button').onClick = function(){}
488         api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
489         if(currentType == 'auth')
490             api = ['open-ils.search', 'open-ils.search.authority.to_html'];
491     } else {
492         showMe('vl-marc-html-edit-button'); // plug in the marc editor button
493         dijit.byId('vl-marc-html-edit-button').onClick = 
494             function() {vlLoadMarcEditor(currentType, recId, oncomplete);};
495         params = [authtoken, recId];
496         api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
497         if(currentType == 'auth')
498             api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
499     }
500
501     fieldmapper.standardRequest(
502         api, 
503         {   async: true,
504             params: params,
505             oncomplete: function(r) {
506             displayGlobalDiv('vl-marc-html-div');
507                 var html = r.recv().content();
508                 dojo.byId('vl-marc-record-html').innerHTML = html;
509             }
510         }
511     );
512 }
513
514
515 /*
516 function getRecMatchesFromAttrCode(rec, attrCode) {
517     var matches = [];
518     var attr = getRecAttrFromCode(rec, attrCode);
519     for(var j = 0; j < rec.matches().length; j++) {
520         var match = rec.matches()[j];
521         if(match.matched_attr() == attr.id()) 
522             matches.push(match);
523     }
524     return matches;
525 }
526 */
527
528 function getRecAttrFromMatch(rec, match) {
529     for(var i = 0; i < rec.attributes().length; i++) {
530         var attr = rec.attributes()[i];
531         if(attr.id() == match.matched_attr())
532             return attr;
533     }
534 }
535
536 function getRecAttrDefFromAttr(attr, type) {
537     var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
538     for(var i = 0; i < defs.length; i++) {
539         var def = defs[i];
540         if(def.id() == attr.field())
541             return def;
542     }
543 }
544
545 function getRecAttrFromCode(rec, attrCode) {
546     var defId = attrDefMap[currentType][attrCode];
547     var attrs = rec.attributes();
548     for(var i = 0; i < attrs.length; i++) {
549         var attr = attrs[i];
550         if(attr.field() == defId) 
551             return attr;
552     }
553     return null;
554 }
555
556 function vlGetViewMatches(rowIdx, item) {
557     if(item) {
558         var id = this.grid.store.getValue(item, 'id');
559         var rec = queuedRecordsMap[id];
560         if(rec.matches().length > 0)
561             return id;
562     }
563     return -1
564 }
565
566 function vlFormatViewMatches(id) {
567     if(id == -1) return '';
568     return '<a href="javascript:void(0);" onclick="vlLoadMatchUI(' + id + ');">' + this.name + '</a>';
569 }
570
571 function vlFormatViewMatchMARC(id) {
572     return '<a href="javascript:void(0);" onclick="vlLoadMARCHtml(' + id + ', true, '+
573         'function(){displayGlobalDiv(\'vl-match-div\');});">' + this.name + '</a>';
574 }
575
576 function getAttrValue(rowIdx, item) {
577     if(!item) return '';
578     var attrCode = this.field.split('.')[1];
579     var rec = queuedRecordsMap[this.grid.store.getValue(item, 'id')];
580     var attr = getRecAttrFromCode(rec, attrCode);
581     return (attr) ? attr.attr_value() : '';
582 }
583
584 function vlGetDateTimeField(rowIdx, item) {
585     if(!item) return '';
586     var value = this.grid.store.getValue(item, this.field);
587     if(!value) return '';
588     var date = dojo.date.stamp.fromISOString(value);
589     return dojo.date.locale.format(date, {selector:'date'});
590 }
591
592 function vlGetCreator(rowIdx, item) {
593     if(!item) return '';
594     var id = this.grid.store.getValue(item, 'creator');
595     if(userCache[id])
596         return userCache[id].usrname();
597     var user = fieldmapper.standardRequest(
598         ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
599     if(e = openils.Event.parse(user))
600         return alert(e);
601     userCache[id] = user;
602     return user.usrname();
603 }
604
605 function vlGetViewMARC(rowIdx, item) {
606     return item && this.grid.store.getValue(item, 'id');
607 }
608
609 function vlFormatViewMARC(id) {
610     return '<a href="javascript:void(0);" onclick="vlLoadMARCHtml(' + id + ', false, '+
611         'function(){displayGlobalDiv(\'vl-queue-div\');});">' + this.name + '</a>';
612 }
613
614 function vlGetOverlayTargetSelector(rowIdx, item) {
615     if(!item) return;
616     return this.grid.store.getValue(item, '_id') + ':' + this.grid.store.getValue(item, 'id');
617 }
618
619 function vlFormatOverlayTargetSelector(val) {
620     if(!val) return '';
621     var parts = val.split(':');
622     var _id = parts[0];
623     var id = parts[1];
624     var value = '<input type="checkbox" name="vl-overlay-target-RECID" '+
625         'onclick="vlHandleOverlayTargetSelected(ID, GRIDID);" gridid="GRIDID" match="ID"/>';
626     value = value.replace(/GRIDID/g, _id);
627     value = value.replace(/RECID/g, currentImportRecId);
628     value = value.replace(/ID/g, id);
629     if(_id == currentOverlayRecordsMapGid[currentImportRecId])
630         return value.replace('/>', 'checked="checked"/>');
631     return value;
632 }
633
634
635 /**
636   * see if the user has enabled overlays for the current match set and, 
637   * if so, map the current import record to the overlay target.
638   */
639 function vlHandleOverlayTargetSelected(recId, gridId) {
640     var noneSelected = true;
641     var checkboxes = dojo.query('[name=vl-overlay-target-'+currentImportRecId+']');
642     for(var i = 0; i < checkboxes.length; i++) {
643         var checkbox = checkboxes[i];
644         var matchRecId = checkbox.getAttribute('match');
645         var gid = checkbox.getAttribute('gridid');
646         if(checkbox.checked) {
647             if(matchRecId == recId && gid == gridId) {
648                 noneSelected = false;
649                 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
650                 currentOverlayRecordsMapGid[currentImportRecId] = gid;
651                 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
652                 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
653             } else {
654                 checkbox.checked = false;
655             }
656         }
657     }
658
659     if(noneSelected) {
660         delete currentOverlayRecordsMap[currentImportRecId];
661         delete currentOverlayRecordsMapGid[currentImportRecId];
662         dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
663         dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = '';
664     }
665 }
666
667 var valLastQueueType = null;
668 var vlQueueGridLayout = null;
669 function buildRecordGrid(type) {
670     displayGlobalDiv('vl-queue-div');
671
672     if(type == 'bib') {
673         openils.Util.show('vl-bib-queue-grid-wrapper');
674         openils.Util.hide('vl-auth-queue-grid-wrapper');
675         vlQueueGrid = vlBibQueueGrid;
676     } else {
677         openils.Util.show('vl-auth-queue-grid-wrapper');
678         openils.Util.hide('vl-bib-queue-grid-wrapper');
679         vlQueueGrid = vlAuthQueueGrid;
680     }
681
682
683     if(valLastQueueType != type) {
684         valLastQueueType = type;
685         vlQueueGridLayout = vlQueueGrid.attr('structure');
686         var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
687         attrDefMap[type] = {};
688         for(var i = 0; i < defs.length; i++) {
689             var def = defs[i]
690             attrDefMap[type][def.code()] = def.id();
691             var col = {
692                 name:def.description(), 
693                 field:'attr.' + def.code(),
694                 get: getAttrValue,
695                 selectableColumn:true
696             };
697             vlQueueGridLayout[0].cells[0].push(col);
698         }
699     }
700
701     dojo.forEach(vlQueueGridLayout[0].cells[0], 
702         function(cell) { 
703             if(cell.field.match(/^\+/)) 
704                 cell.nonSelectable=true;
705         }
706     );
707
708     var storeData;
709     if(type == 'bib')
710         storeData = vqbr.toStoreData(queuedRecords);
711     else
712         storeData = vqar.toStoreData(queuedRecords);
713
714     var store = new dojo.data.ItemFileReadStore({data:storeData});
715     vlQueueGrid.setStore(store);
716
717     if(vlQueueGridColumePicker[type]) {
718         vlQueueGrid.update();
719     } else {
720
721         vlQueueGridColumePicker[type] =
722             new openils.widget.GridColumnPicker(
723                 authtoken, 'vandelay.queue.'+type, vlQueueGrid, vlQueueGridLayout);
724         vlQueueGridColumePicker[type].load();
725     }
726 }
727
728 function vlQueueGridPrevPage() {
729     var page = parseInt(vlQueueDisplayPage.getValue());
730     if(page < 2) return;
731     vlQueueDisplayPage.setValue(page - 1);
732     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
733 }
734
735 function vlQueueGridNextPage() {
736     vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
737     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
738 }
739
740 function vlDeleteQueue(type, queueId, onload) {
741     fieldmapper.standardRequest(
742         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
743         {   async: true,
744             params: [authtoken, queueId],
745             oncomplete: function(r) {
746                 var resp = r.recv().content();
747                 if(e = openils.Event.parse(resp))
748                     return alert(e);
749                 onload();
750             }
751         }
752     );
753 }
754
755
756 function vlQueueGridDrawSelectBox(rowIdx, item) {
757     return item &&  this.grid.store.getValue(item, 'id');
758 }
759
760 function vlQueueGridFormatSelectBox(id) {
761     var domId = 'vl-record-list-selected-' + id;
762     if (id) { selectableGridRecords[domId] = id; }
763     return "<div><input type='checkbox' id='"+domId+"'/></div>";
764 }
765
766 function vlSelectAllQueueGridRecords() {
767     for(var id in selectableGridRecords) 
768         dojo.byId(id).checked = true;
769 }
770 function vlSelectNoQueueGridRecords() {
771     for(var id in selectableGridRecords) 
772         dojo.byId(id).checked = false;
773 }
774 function vlToggleQueueGridSelect() {
775     if(dojo.byId('vl-queue-grid-row-selector').checked)
776         vlSelectAllQueueGridRecords();
777     else
778         vlSelectNoQueueGridRecords();
779 }
780
781 var handleRetrieveRecords = function() {
782     buildRecordGrid(currentType);
783     vlFetchQueueSummary(currentQueueId, currentType, 
784         function(summary) {
785             dojo.byId('vl-queue-summary-name').innerHTML = summary.queue.name();
786             dojo.byId('vl-queue-summary-total-count').innerHTML = summary.total +'';
787             dojo.byId('vl-queue-summary-import-count').innerHTML = summary.imported + '';
788         }
789     );
790 }
791
792 function vlFetchQueueSummary(qId, type, onload) {
793     fieldmapper.standardRequest(
794         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.summary.retrieve'],
795         {   async: true,
796             params: [authtoken, qId],
797             oncomplete : function(r) {
798                 var summary = r.recv().content();
799                 if(e = openils.Event.parse(summary))
800                     return alert(e);
801                 return onload(summary);
802             }
803         }
804     );
805 }
806
807 function vlHandleQueueItemsAction(action) {
808
809     dojo.connect(
810         queueItemsImportCancelButton, 
811         'onClick', 
812         function() {
813             queueItemsImportDialog.hide();
814         }
815     );
816
817     dojo.connect(
818         queueItemsImportGoButton,
819         'onClick', 
820         function() {
821             queueItemsImportDialog.hide();
822
823             // hack to set the widgets the import funcs will be looking at.  Reset them below.
824             vlUploadQueueAutoImport.attr('value',  vlUploadQueueAutoImport2.attr('value'));
825             vlUploadQueueAutoOverlayExact.attr('value',  vlUploadQueueAutoOverlayExact2.attr('value'));
826             vlUploadQueueAutoOverlay1Match.attr('value',  vlUploadQueueAutoOverlay1Match2.attr('value'));
827             vlUploadMergeProfile.attr('value',  vlUploadMergeProfile2.attr('value'));
828
829             if(action == 'import') {
830                 vlImportSelectedRecords();
831             } else if(action == 'import_all') {
832                 vlImportAllRecords();
833             }
834             
835             // reset the widgets to prevent accidental future actions
836             vlUploadQueueAutoImport.attr('value',  false);
837             vlUploadQueueAutoImport2.attr('value', false);
838             vlUploadQueueAutoOverlayExact.attr('value', false);
839             vlUploadQueueAutoOverlayExact2.attr('value', false);
840             vlUploadQueueAutoOverlay1Match.attr('value', false);
841             vlUploadQueueAutoOverlay1Match2.attr('value', false);
842             vlUploadMergeProfile.attr('value', '');
843             vlUploadMergeProfile2.attr('value', '');
844         }
845     );
846
847     queueItemsImportDialog.show();
848 }
849     
850
851 function vlImportSelectedRecords() {
852     displayGlobalDiv('vl-generic-progress-with-total');
853     var records = [];
854
855     for(var id in selectableGridRecords) {
856         if(dojo.byId(id).checked) {
857             var recId = selectableGridRecords[id];
858             var rec = queuedRecordsMap[recId];
859             if(!rec.import_time()) 
860                 records.push(recId);
861         }
862     }
863
864     var options = {overlay_map : currentOverlayRecordsMap};
865
866     if(vlUploadQueueAutoOverlayExact.checked) {
867         options.auto_overlay_exact = true;
868         vlUploadQueueAutoOverlayExact.checked = false;
869     }
870
871     if(vlUploadQueueAutoOverlay1Match.checked) {
872         options.auto_overlay_1match = true;
873         vlUploadQueueAutoOverlay1Match.checked = false;
874     }
875     
876     var profile = vlUploadMergeProfile.attr('value');
877     if(profile != null && profile != '') {
878         options.merge_profile = profile;
879     }
880
881     fieldmapper.standardRequest(
882         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
883         {   async: true,
884             params: [authtoken, records, options],
885             onresponse: function(r) {
886                 var resp = r.recv().content();
887                 if(e = openils.Event.parse(resp))
888                     return alert(e);
889                 if(resp.complete) {
890                     return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
891                 } else {
892                     vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
893                 }
894             }, 
895         }
896     );
897 }
898
899 function vlImportAllRecords() {
900     vlImportRecordQueue(currentType, currentQueueId, false,
901         function(){displayGlobalDiv('vl-queue-div');});
902 }
903
904 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
905     displayGlobalDiv('vl-generic-progress-with-total');
906     var method = 'open-ils.vandelay.bib_queue.import';
907     if(noMatchOnly)
908         method = method.replace('import', 'nomatch.import');
909     if(type == 'auth')
910         method = method.replace('bib', 'auth');
911
912     var options = {};
913     if(vlUploadQueueAutoOverlayExact.checked) {
914         options.auto_overlay_exact = true;
915         vlUploadQueueAutoOverlayExact.checked = false;
916     }
917
918     if(vlUploadQueueAutoOverlay1Match.checked) {
919         options.auto_overlay_1match = true;
920         vlUploadQueueAutoOverlay1Match.checked = false;
921     }
922     
923     var profile = vlUploadMergeProfile.attr('value');
924     if(profile != null && profile != '') {
925         options.merge_profile = profile;
926     }
927
928     fieldmapper.standardRequest(
929         ['open-ils.vandelay', method],
930         {   async: true,
931             params: [authtoken, queueId, options],
932             onresponse: function(r) {
933                 var resp = r.recv().content();
934                 if(e = openils.Event.parse(resp))
935                     return alert(e);
936                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
937             },
938             oncomplete: function() {onload();}
939         }
940     );
941 }
942
943
944 function vlImportHoldings(queueId, importProfile, onload) {
945     displayGlobalDiv('vl-generic-progress-with-total');
946     fieldmapper.standardRequest(
947         ['open-ils.vandelay', 'open-ils.vandelay.bib_record.queue.asset.import'],
948         {   async: true,
949             params: [authtoken, importProfile, queueId],
950             onresponse: function(r) {
951                 var resp = openils.Util.readResponse(r);
952                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
953             },
954             oncomplete: function() {onload();}
955         }
956     );
957 }
958
959 /**
960   * Create queue, upload MARC, process spool, load the newly created queue 
961   */
962 function batchUpload() {
963     var queueName = dijit.byId('vl-queue-name').getValue();
964     currentType = dijit.byId('vl-record-type').getValue();
965
966     var handleProcessSpool = function() {
967         if(vlUploadQueueAutoImport.checked || vlUploadQueueAutoOverlayExact.checked || vlUploadQueueAutoOverlay1Match.checked) {
968
969             vlImportRecordQueue(
970                 currentType, 
971                 currentQueueId, 
972                 vlUploadQueueAutoImport.checked,  
973                 function() {
974                     if(vlUploadQueueHoldingsImport.checked) {
975                         vlImportHoldings(
976                             currentQueueId, 
977                             vlUploadQueueHoldingsImportProfile.attr('value'), 
978                             function() { 
979                                 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
980                             }
981                         );
982                     } else {
983                         retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
984                     }
985                 }
986             );
987         } else {
988             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
989         }
990     }
991
992     var handleUploadMARC = function(key) {
993         dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
994         processSpool(key, currentQueueId, currentType, handleProcessSpool);
995     };
996
997     var handleCreateQueue = function(queue) {
998         currentQueueId = queue.id();
999         uploadMARC(handleUploadMARC);
1000     };
1001     
1002     if(vlUploadQueueSelector.getValue() && !queueName) {
1003         currentQueueId = vlUploadQueueSelector.getValue();
1004         uploadMARC(handleUploadMARC);
1005     } else {
1006         createQueue(queueName, currentType, handleCreateQueue, vlUploadQueueHoldingsImportProfile.attr('value'));
1007     }
1008 }
1009
1010
1011 function vlFleshQueueSelect(selector, type) {
1012     var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
1013     selector.store = new dojo.data.ItemFileReadStore({data:data});
1014     selector.setValue(null);
1015     selector.setDisplayedValue('');
1016     if(data[0])
1017         selector.setValue(data[0].id());
1018 }
1019
1020 function vlShowUploadForm() {
1021     displayGlobalDiv('vl-marc-upload-div');
1022     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
1023     vlUploadSourceSelector.store = 
1024         new dojo.data.ItemFileReadStore({data:cbs.toStoreData(vlBibSources, 'source')});
1025     vlUploadSourceSelector.setValue(vlBibSources[0].id());
1026     vlUploadQueueHoldingsImportProfile.store = 
1027         new dojo.data.ItemFileReadStore({data:viiad.toStoreData(importItemDefs)});
1028     vlUploadQueueHoldingsImportProfile.attr('disabled', true);
1029     dojo.connect(vlUploadQueueHoldingsImport, 'onChange',
1030         function(val) {
1031             if(val)
1032                 vlUploadQueueHoldingsImportProfile.attr('disabled', false);
1033             else
1034                 vlUploadQueueHoldingsImportProfile.attr('disabled', true);
1035         }
1036     );
1037 }
1038
1039 function vlShowQueueSelect() {
1040     displayGlobalDiv('vl-queue-select-div');
1041     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
1042 }
1043
1044 function vlFetchQueueFromForm() {
1045     currentType = vlQueueSelectType.getValue();
1046     currentQueueId = vlQueueSelectQueueList.getValue();
1047     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
1048 }
1049
1050 function vlOpenMarcEditWindow(rec, postReloadHTMLHandler) {
1051     /*
1052         To run in Firefox directly, must set signed.applets.codebase_principal_support
1053         to true in about:config
1054     */
1055     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
1056     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
1057
1058     var type;
1059     if (currentType == 'bib') {
1060         type = 'bre';
1061     } else {
1062         type = 'are';
1063     }
1064
1065     function onsave(r) {
1066         // after the record is saved, reload the HTML display
1067         var stat = r.recv().content();
1068         if(e = openils.Event.parse(stat))
1069             return alert(e);
1070         alert(dojo.byId('vl-marc-edit-complete-label').innerHTML);
1071         win.close();
1072         vlLoadMARCHtml(rec.id(), false, postReloadHTMLHandler);
1073     }
1074
1075     win.xulG = {
1076         record : {marc : rec.marc(), "rtype": type},
1077         save : {
1078             label: dojo.byId('vl-marc-edit-save-label').innerHTML,
1079             func: function(xmlString) {
1080                 var method = 'open-ils.permacrud.update.' + rec.classname;
1081                 rec.marc(xmlString);
1082                 fieldmapper.standardRequest(
1083                     ['open-ils.permacrud', method],
1084                     {   async: true,
1085                         params: [authtoken, rec],
1086                         oncomplete: onsave
1087                     }
1088                 );
1089             },
1090         }
1091     };
1092 }
1093
1094 function vlLoadMarcEditor(type, recId, postReloadHTMLHandler) {
1095     var method = 'open-ils.permacrud.search.vqbr';
1096     if(currentType != 'bib')
1097         method = method.replace(/vqbr/,'vqar');
1098
1099     fieldmapper.standardRequest(
1100         ['open-ils.permacrud', method],
1101         {   async: true, 
1102             params: [authtoken, {id : recId}],
1103             oncomplete: function(r) {
1104                 var rec = r.recv().content();
1105                 if(e = openils.Event.parse(rec))
1106                     return alert(e);
1107                 vlOpenMarcEditWindow(rec, postReloadHTMLHandler);
1108             }
1109         }
1110     );
1111 }
1112
1113
1114
1115 //------------------------------------------------------------
1116 // attribute editors
1117
1118 // attribute-editor global variables
1119
1120 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
1121 var ATTR_EDIT_ID = null;                // id of current 'edit' attribute
1122 var ATTR_EDIT_GROUP = 'bib';            // bib-attrs or auth-attrs
1123
1124 function vlAttrEditorInit() {
1125     // set up tooltips on the edit form
1126     connectTooltip('attr-editor-tags'); 
1127     connectTooltip('attr-editor-subfields'); 
1128 }
1129
1130 function vlShowAttrEditor() {
1131     displayGlobalDiv('vl-attr-editor-div');
1132     loadAttrEditorGrid();
1133     idHide('vl-generic-progress');
1134 }
1135
1136 function setAttrEditorGroup(groupName) {
1137     // put us into 'bib'-attr or 'auth'-attr mode.
1138     if (ATTR_EDIT_GROUP != groupName) {
1139         ATTR_EDIT_GROUP = groupName;
1140         loadAttrEditorGrid();
1141     }
1142 }
1143
1144 function onAttrEditorOpen() {
1145     // the "bars" have the create/update/cancel/etc. buttons.
1146     var create_bar = document.getElementById('attr-editor-create-bar');
1147     var update_bar = document.getElementById('attr-editor-update-bar');
1148     if (ATTR_EDITOR_IN_UPDATE_MODE) {
1149         update_bar.style.display='table-row';
1150         create_bar.style.display='none';
1151         // hide the dropdown-button
1152         idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
1153     } else {
1154         dijit.byId('attr-editor-dialog').reset();
1155         create_bar.style.display='table-row';
1156         update_bar.style.display='none';
1157     }
1158 }
1159
1160 function onAttrEditorClose() {
1161     // reset the form to a "create" form. (We may have borrowed it for editing.)
1162     ATTR_EDITOR_IN_UPDATE_MODE = false;
1163     // show the dropdown-button
1164     idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
1165 }
1166
1167 function loadAttrEditorGrid() {
1168     var _data = (ATTR_EDIT_GROUP == 'auth') ? 
1169         vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
1170
1171     var store = new dojo.data.ItemFileReadStore({data:_data});
1172     attrEditorGrid.setStore(store);
1173     attrEditorGrid.onRowDblClick = onAttrEditorClick;
1174     attrEditorGrid.update();
1175 }
1176
1177 function attrGridGetTag(n, item) {
1178     // grid helper: return the tags from the row's xpath column.
1179     return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).tags;
1180 }
1181
1182 function attrGridGetSubfield(n, item) {
1183     // grid helper: return the subfields from the row's xpath column.
1184     return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).subfields;
1185 }
1186
1187 function onAttrEditorClick() {
1188     var row = this.getItem(this.focus.rowIndex);
1189     ATTR_EDIT_ID = this.store.getValue(row, 'id');
1190     ATTR_EDITOR_IN_UPDATE_MODE = true;
1191
1192     // populate the popup editor.
1193     dijit.byId('attr-editor-code').attr('value', this.store.getValue(row, 'code'));
1194     dijit.byId('attr-editor-description').attr('value', this.store.getValue(row, 'description'));
1195     var parsed_xpath = xpathParser.parse(this.store.getValue(row, 'xpath'));
1196     dijit.byId('attr-editor-tags').attr('value', parsed_xpath.tags);
1197     dijit.byId('attr-editor-subfields').attr('value', parsed_xpath.subfields);
1198     dijit.byId('attr-editor-identifier').attr('value', this.store.getValue(row, 'ident'));
1199     dijit.byId('attr-editor-xpath').attr('value', this.store.getValue(row, 'xpath'));
1200     dijit.byId('attr-editor-remove').attr('value', this.store.getValue(row, 'remove'));
1201
1202     // set up UI for editing
1203     dojo.byId('vl-create-attr-editor-button').click();
1204 }
1205
1206 function vlSaveAttrDefinition(data) {
1207     idHide('vl-attr-editor-div');
1208     idShow('vl-generic-progress');
1209
1210     data.id = ATTR_EDIT_ID;
1211
1212     // this ought to honour custom xpaths, but overwrite xpaths
1213     // derived from tags/subfields.
1214     if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
1215         var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
1216         data.xpath = _xpath;
1217     }
1218
1219     // build up our permacrud params. Key variables here are
1220     // "create or update" and "bib or auth".
1221
1222     var isAuth   = (ATTR_EDIT_GROUP == 'auth');
1223     var isCreate = (ATTR_EDIT_ID == null);
1224     var rad      = isAuth ? new vqarad() : new vqbrad() ;
1225     var method   = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.') 
1226         + (isAuth ? 'vqarad' : 'vqbrad');
1227     var _data    = rad.fromStoreItem(data);
1228
1229     _data.ischanged(1);
1230
1231     fieldmapper.standardRequest(
1232         ['open-ils.permacrud', method],
1233         {   async: true,
1234             params: [authtoken, _data ],
1235             onresponse: function(r) { },
1236             oncomplete: function(r) {
1237                 attrEditorFetchAttrDefs(vlShowAttrEditor);
1238                 ATTR_EDIT_ID = null;
1239             },
1240             onerror: function(r) {
1241                 alert('vlSaveAttrDefinition comms error: ' + r);
1242             }
1243         }
1244     );
1245 }
1246
1247 function attrEditorFetchAttrDefs(callback) {
1248     var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
1249     return fn(callback);
1250 }
1251
1252 function vlAttrDelete() {
1253     idHide('vl-attr-editor-div');
1254     idShow('vl-generic-progress');
1255
1256     var isAuth = (ATTR_EDIT_GROUP == 'auth');
1257     var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
1258     var rad    = isAuth ? new vqarad() : new vqbrad() ;
1259     fieldmapper.standardRequest(
1260         ['open-ils.permacrud', method],
1261         {   async: true,
1262             params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
1263             oncomplete: function() {
1264                 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
1265                 attrEditorFetchAttrDefs(vlShowAttrEditor);
1266                 ATTR_EDIT_ID = null;
1267             },
1268             onerror: function(r) {
1269                 alert('vlAttrDelete comms error: ' + r);
1270             }
1271         }
1272     );
1273 }
1274
1275 // ------------------------------------------------------------
1276 // utilities for attribute editors
1277
1278 // dom utilities (maybe dojo does these, and these should be replaced)
1279
1280 function idStyle(obId, k, v)    { document.getElementById(obId).style[k] = v;   }
1281 function idShow(obId)           { idStyle(obId, 'display', 'block');            }
1282 function idHide(obId)           { idStyle(obId, 'display' , 'none');            }
1283
1284 function connectTooltip(fieldId) {
1285     // Given an element id, look up a tooltip element in the doc (same
1286     // id with a '-tip' suffix) and associate the two. Maybe dojo has
1287     // a better way to do this?
1288     var fld = dojo.byId(fieldId);
1289     var tip = dojo.byId(fieldId + '-tip');
1290     dojo.connect(fld, 'onfocus', function(evt) {
1291                      dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1292     dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1293 }
1294
1295 // xpath utilities
1296
1297 var xpathParser = new openils.MarcXPathParser();
1298
1299 function tagAndSubFieldsToXpath(tags, subfields) {
1300     // given tags, and subfields, build up an XPath.
1301     try {
1302         var parts = {
1303             'tags':tags.match(/[\d]+/g), 
1304             'subfields':subfields.match(/[a-zA-z]/g) };
1305         return xpathParser.compile(parts);
1306     } catch (err) {
1307         return {'parts':null, 'tags':null, 'error':err};
1308     }
1309 }
1310
1311 function looksLikeDerivedXpath(path) {
1312     // Does this path look like it was derived from tags and subfields?
1313     var parsed = xpathParser.parse(path);
1314     if (parsed.tags == null) 
1315         return false;
1316     var compiled = xpathParser.compile(parsed);
1317     return (path == compiled);
1318 }
1319
1320 // amazing xpath-util unit-tests
1321 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]'))     alert('vandelay xpath-utility error');
1322 if ( looksLikeDerivedXpath('ba-boo-ba-boo!'))                   alert('vandelay xpath-utility error');
1323
1324
1325
1326 var profileContextOrg
1327 function vlShowProfileEditor() {
1328     displayGlobalDiv('vl-profile-editor-div');
1329     buildProfileGrid();
1330
1331     var connect = function() {
1332         dojo.connect(profileContextOrgSelector, 'onChange',
1333             function() {
1334                 profileContextOrg = this.attr('value');
1335                 pGrid.resetStore();
1336                 buildProfileGrid();
1337             }
1338         );
1339     };
1340
1341     new openils.User().buildPermOrgSelector(
1342         '"ADMIN_MERGE_PROFILE', profileContextOrgSelector, null, connect);
1343 }
1344
1345 function buildProfileGrid() {
1346
1347     if(profileContextOrg == null)
1348         profileContextOrg = openils.User.user.ws_ou();
1349
1350     pGrid.loadAll( 
1351         {order_by : {vmp : 'name'}}, 
1352         {owner : fieldmapper.aou.fullPath(profileContextOrg, true)}
1353     );
1354 }
1355
1356 /* --- Import Item Attr Grid --------------- */
1357
1358 var itemAttrContextOrg;
1359 function vlShowImportItemAttrEditor() {
1360     displayGlobalDiv('vl-item-attr-editor-div');
1361     buildImportItemAttrGrid();
1362
1363     var connect = function() {
1364         dojo.connect(itemAttrContextOrgSelector, 'onChange',
1365             function() {
1366                 itemAttrContextOrg = this.attr('value');
1367                 itemAttrGrid.resetStore();
1368                 vlShowImportItemAttrEditor();
1369             }
1370         );
1371     };
1372
1373     new openils.User().buildPermOrgSelector(
1374         'ADMIN_IMPORT_ITEM_ATTR_DEF', 
1375             itemAttrContextOrgSelector, null, connect);
1376 }
1377
1378 function buildImportItemAttrGrid() {
1379
1380     if(itemAttrContextOrg == null)
1381         itemAttrContextOrg = openils.User.user.ws_ou();
1382
1383     itemAttrGrid.loadAll( 
1384         {order_by : {viiad : 'name'}}, 
1385         {owner : fieldmapper.aou.fullPath(itemAttrContextOrg, true)}
1386     );
1387 }
1388