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