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