1 /* ---------------------------------------------------------------------------
2 # Copyright (C) 2008 Georgia Public Library Service
3 # Bill Erickson <erickson@esilibrary.com>
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.
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');
45 'vl-generic-progress',
46 'vl-generic-progress-with-total',
51 'vl-queue-select-div',
52 'vl-marc-upload-status-div',
58 var VANDELAY_URL = '/vandelay-upload';
60 var authAttrDefs = [];
61 var queuedRecords = [];
62 var queuedRecordsMap = {};
63 var bibAttrsFetched = false;
64 var authAttrsFetched = false;
65 var attrDefMap = {}; // maps attr def code names to attr def ids
67 var currentQueueId = null;
69 var currentMatchedRecords; // set of loaded matched bib records
70 var currentOverlayRecordsMap; // map of import record to overlay record
71 var currentOverlayRecordsMapGid; // map of import record to overlay record grid id
72 var currentImportRecId; // when analyzing matches, this is the current import record
73 var userBibQueues = []; // only non-complete queues
74 var userAuthQueues = []; // only non-complete queues
76 var allUserAuthQueues;
77 var selectableGridRecords;
78 var cgi = new openils.CGI();
79 var vlQueueGridColumePicker = {};
80 var vlBibSources = [];
86 authtoken = dojo.cookie('ses') || cgi.param('ses');
87 var initNeeded = 5; // how many async responses do we need before we're init'd
88 var initCount = 0; // how many async reponses we've received
90 openils.Util.registerEnterHandler(
91 vlQueueDisplayPage.domNode, function(){retrieveQueuedRecords();});
92 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
94 function checkInitDone() {
96 if(initCount == initNeeded)
100 // Fetch the bib and authority attribute definitions
101 vlFetchBibAttrDefs(function () { checkInitDone(); });
102 vlFetchAuthAttrDefs(function () { checkInitDone(); });
104 vlRetrieveQueueList('bib', null,
106 allUserBibQueues = list;
107 for(var i = 0; i < allUserBibQueues.length; i++) {
108 if(allUserBibQueues[i].complete() == 'f')
109 userBibQueues.push(allUserBibQueues[i]);
115 vlRetrieveQueueList('auth', null,
117 allUserAuthQueues = list;
118 for(var i = 0; i < allUserAuthQueues.length; i++) {
119 if(allUserAuthQueues[i].complete() == 'f')
120 userAuthQueues.push(allUserAuthQueues[i]);
126 fieldmapper.standardRequest(
127 ['open-ils.permacrud', 'open-ils.permacrud.search.cbs.atomic'],
129 params: [authtoken, {id:{"!=":null}}, {order_by:{cbs:'id'}}],
130 oncomplete : function(r) {
131 vlBibSources = openils.Util.readResponse(r, false, true);
141 dojo.addOnLoad(vlInit);
144 // fetch the bib and authority attribute definitions
146 function vlFetchBibAttrDefs(postcomplete) {
148 fieldmapper.standardRequest(
149 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
151 params: [authtoken, {id:{'!=':null}}],
152 onresponse: function(r) {
153 var def = r.recv().content();
154 if(e = openils.Event.parse(def[0]))
156 bibAttrDefs.push(def);
158 oncomplete: function() {
159 bibAttrDefs = bibAttrDefs.sort(
161 if(a.id() > b.id()) return 1;
162 if(a.id() < b.id()) return -1;
172 function vlFetchAuthAttrDefs(postcomplete) {
174 fieldmapper.standardRequest(
175 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
177 params: [authtoken, {id:{'!=':null}}],
178 onresponse: function(r) {
179 var def = r.recv().content();
180 if(e = openils.Event.parse(def[0]))
182 authAttrDefs.push(def);
184 oncomplete: function() {
185 authAttrDefs = authAttrDefs.sort(
187 if(a.id() > b.id()) return 1;
188 if(a.id() < b.id()) return -1;
198 function vlRetrieveQueueList(type, filter, onload) {
199 type = (type == 'bib') ? type : 'authority';
200 fieldmapper.standardRequest(
201 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
203 params: [authtoken, null, filter],
204 oncomplete: function(r) {
205 var list = r.recv().content();
206 if(e = openils.Event.parse(list[0]))
215 function displayGlobalDiv(id) {
216 for(var i = 0; i < globalDivs.length; i++) {
218 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
220 alert('please define div ' + globalDivs[i]);
223 dojo.style(dojo.byId(id),'display','block');
225 openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
226 openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
227 openils.Util.removeCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
228 openils.Util.removeCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
231 case 'vl-marc-export-div':
232 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
234 case 'vl-marc-upload-div':
235 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
237 case 'vl-queue-select-div':
238 openils.Util.addCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
240 case 'vl-attr-editor-div':
241 openils.Util.addCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
246 function runStartupCommands() {
247 currentQueueId = cgi.param('qid');
248 currentType = cgi.param('qtype');
249 dojo.style('vl-nav-bar', 'visibility', 'visible');
251 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
256 * asynchronously upload a file of MARC records
258 function uploadMARC(onload){
259 dojo.byId('vl-upload-status-count').innerHTML = '0';
260 dojo.byId('vl-ses-input').value = authtoken;
261 displayGlobalDiv('vl-marc-upload-status-div');
262 dojo.io.iframe.send({
266 form: dojo.byId('vl-marc-upload-form'),
267 handle: function(data,ioArgs){
268 var content = data.documentElement.textContent;
275 * Creates a new vandelay queue
277 function createQueue(queueName, type, onload) {
278 var name = (type=='bib') ? 'bib' : 'authority';
279 var method = 'open-ils.vandelay.'+ name +'_queue.create'
280 fieldmapper.standardRequest(
281 ['open-ils.vandelay', method],
283 params: [authtoken, queueName, null, name],
284 oncomplete : function(r) {
285 var queue = r.recv().content();
286 if(e = openils.Event.parse(queue))
295 * Tells vandelay to pull a batch of records from the cache and explode them
296 * out into the vandelay tables
298 function processSpool(key, queueId, type, onload) {
299 fieldmapper.standardRequest(
300 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
302 params: [authtoken, key, queueId],
303 onresponse : function(r) {
304 var resp = r.recv().content();
305 if(e = openils.Event.parse(resp))
307 dojo.byId('vl-upload-status-count').innerHTML = resp;
309 oncomplete : function(r) {onload();}
314 function retrieveQueuedRecords(type, queueId, onload) {
315 displayGlobalDiv('vl-generic-progress');
317 queuedRecordsMap = {};
318 currentOverlayRecordsMap = {};
319 currentOverlayRecordsMapGid = {};
320 selectableGridRecords = {};
321 resetVlQueueGridLayout();
323 if(!type) type = currentType;
324 if(!queueId) queueId = currentQueueId;
325 if(!onload) onload = handleRetrieveRecords;
327 var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
328 if(vlQueueGridShowMatches.checked)
329 method = method.replace('records', 'records.matches');
331 //var limit = parseInt(vlQueueDisplayLimit.getValue());
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.getValue()-1);
336 var params = [authtoken, queueId, {clear_marc: 1, offset: offset, limit: limit}];
337 if(vlQueueGridShowNonImport.checked)
338 params[2].non_imported = 1;
340 fieldmapper.standardRequest(
341 ['open-ils.vandelay', method],
345 onresponse: function(r) {
346 console.log("ONREPONSE");
347 var rec = r.recv().content();
348 if(e = openils.Event.parse(rec))
350 console.log("got record " + rec.id());
351 queuedRecords.push(rec);
352 queuedRecordsMap[rec.id()] = rec;
355 oncomplete: function(r){
356 var recs = r.recv().content();
357 if(e = openils.Event.parse(recs[0]))
359 for(var i = 0; i < recs.length; i++) {
361 queuedRecords.push(rec);
362 queuedRecordsMap[rec.id()] = rec;
370 function vlLoadMatchUI(recId) {
371 displayGlobalDiv('vl-generic-progress');
372 var matches = queuedRecordsMap[recId].matches();
374 currentImportRecId = recId;
375 for(var i = 0; i < matches.length; i++)
376 records.push(matches[i].eg_record());
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}];
385 fieldmapper.standardRequest(
389 oncomplete: function(r) {
390 var recs = r.recv().content();
391 if(e = openils.Event.parse(recs))
395 displayGlobalDiv('vl-match-div');
396 resetVlMatchGridLayout();
397 currentMatchedRecords = recs;
398 if(!vlMatchGrid.structure)
399 vlMatchGrid.setStructure(vlMatchGridLayout);
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';
406 var matchSeenMap = {};
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;
423 // now populate the grid
424 vlPopulateMatchGrid(vlMatchGrid, dataStore);
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);
438 function showMe(id) {
439 dojo.style(dojo.byId(id), 'display', 'block');
441 function hideMe(id) {
442 dojo.style(dojo.byId(id), 'display', 'none');
446 function vlLoadMARCHtml(recId, inCat, oncomplete) {
447 dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
448 displayGlobalDiv('vl-generic-progress');
450 var params = [recId, 1];
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'];
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'];
468 fieldmapper.standardRequest(
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;
483 function getRecMatchesFromAttrCode(rec, attrCode) {
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())
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())
503 function getRecAttrDefFromAttr(attr, type) {
504 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
505 for(var i = 0; i < defs.length; i++) {
507 if(def.id() == attr.field())
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++) {
517 if(attr.field() == defId)
523 function vlGetViewMatches(rowIdx) {
524 var data = this.grid.model.getRow(rowIdx);
526 var rec = queuedRecordsMap[data.id];
527 if(rec.matches().length > 0)
528 return this.value.replace('RECID', data.id);
532 function getAttrValue(rowIdx) {
533 var data = this.grid.model.getRow(rowIdx);
535 var attrCode = this.field.split('.')[1];
536 var rec = queuedRecordsMap[data.id];
537 var attr = getRecAttrFromCode(rec, attrCode);
538 return (attr) ? attr.attr_value() : '';
541 function vlGetDateTimeField(rowIdx) {
542 data = this.grid.model.getRow(rowIdx);
544 if(!data[this.field]) return '';
545 var date = dojo.date.stamp.fromISOString(data[this.field]);
546 return dojo.date.locale.format(date, {selector:'date'});
549 function vlGetCreator(rowIdx) {
550 data = this.grid.model.getRow(rowIdx);
552 var id = data.creator;
554 return userCache[id].usrname();
555 var user = fieldmapper.standardRequest(
556 ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
557 if(e = openils.Event.parse(user))
559 userCache[id] = user;
560 return user.usrname();
563 function vlGetViewMARC(rowIdx) {
564 data = this.grid.model.getRow(rowIdx);
566 return this.value.replace('RECID', data.id);
569 function vlGetOverlayTargetSelector(rowIdx) {
570 data = this.grid.model.getRow(rowIdx);
572 var value = this.value.replace(/GRIDID/g, data._id);
573 value = value.replace(/RECID/g, currentImportRecId);
574 value = value.replace(/ID/g, data.id);
575 if(data._id == currentOverlayRecordsMapGid[currentImportRecId])
576 return value.replace('/>', 'checked="checked"/>');
582 * see if the user has enabled overlays for the current match set and,
583 * if so, map the current import record to the overlay target.
585 function vlHandleOverlayTargetSelected(recId, gridId) {
586 var noneSelected = true;
587 var checkboxes = dojo.query('[name=vl-overlay-target-'+currentImportRecId+']');
588 for(var i = 0; i < checkboxes.length; i++) {
589 var checkbox = checkboxes[i];
590 var matchRecId = checkbox.getAttribute('match');
591 var gid = checkbox.getAttribute('gridid');
592 if(checkbox.checked) {
593 if(matchRecId == recId && gid == gridId) {
594 noneSelected = false;
595 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
596 currentOverlayRecordsMapGid[currentImportRecId] = gid;
597 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
598 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
600 checkbox.checked = false;
606 delete currentOverlayRecordsMap[currentImportRecId];
607 delete currentOverlayRecordsMapGid[currentImportRecId];
608 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
609 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = '';
613 var valLastQueueType = null;
614 function buildRecordGrid(type) {
615 displayGlobalDiv('vl-queue-div');
617 if(valLastQueueType != type) {
618 valLastQueueType = type;
619 resetVlQueueGridLayout();
620 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
621 attrDefMap[type] = {};
622 for(var i = 0; i < defs.length; i++) {
624 attrDefMap[type][def.code()] = def.id();
626 name:def.description(),
627 field:'attr.' + def.code(),
629 selectableColumn:true
631 vlQueueGridLayout[0].cells[0].push(col);
637 storeData = vqbr.toStoreData(queuedRecords);
639 storeData = vqar.toStoreData(queuedRecords);
641 var store = new dojo.data.ItemFileReadStore({data:storeData});
642 var model = new dojox.grid.data.DojoData(
643 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
644 vlQueueGrid.setModel(model);
647 if(vlQueueGridColumePicker[type]) {
648 vlQueueGrid.update();
650 vlQueueGridColumePicker[type] =
651 new openils.GridColumnPicker(vlQueueGridColumePickerDialog,
652 vlQueueGrid, vlQueueGridLayout, authtoken, 'vandelay.queue.'+type);
653 vlQueueGridColumePicker[type].load();
657 function vlQueueGridPrevPage() {
658 var page = parseInt(vlQueueDisplayPage.getValue());
660 vlQueueDisplayPage.setValue(page - 1);
661 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
664 function vlQueueGridNextPage() {
665 vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
666 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
669 function vlDeleteQueue(type, queueId, onload) {
670 fieldmapper.standardRequest(
671 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
673 params: [authtoken, queueId],
674 oncomplete: function(r) {
675 var resp = r.recv().content();
676 if(e = openils.Event.parse(resp))
685 function vlQueueGridDrawSelectBox(rowIdx) {
686 var data = this.grid.model.getRow(rowIdx);
688 var domId = 'vl-record-list-selected-' +data.id;
689 selectableGridRecords[domId] = data.id;
690 return "<div><input type='checkbox' id='"+domId+"'/></div>";
693 function vlSelectAllQueueGridRecords() {
694 for(var id in selectableGridRecords)
695 dojo.byId(id).checked = true;
697 function vlSelectNoQueueGridRecords() {
698 for(var id in selectableGridRecords)
699 dojo.byId(id).checked = false;
701 function vlToggleQueueGridSelect() {
702 if(dojo.byId('vl-queue-grid-row-selector').checked)
703 vlSelectAllQueueGridRecords();
705 vlSelectNoQueueGridRecords();
708 var handleRetrieveRecords = function() {
709 buildRecordGrid(currentType);
710 vlFetchQueueSummary(currentQueueId, currentType,
712 dojo.byId('vl-queue-summary-name').innerHTML = summary.queue.name();
713 dojo.byId('vl-queue-summary-total-count').innerHTML = summary.total +'';
714 dojo.byId('vl-queue-summary-import-count').innerHTML = summary.imported + '';
719 function vlFetchQueueSummary(qId, type, onload) {
720 fieldmapper.standardRequest(
721 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.summary.retrieve'],
723 params: [authtoken, qId],
724 oncomplete : function(r) {
725 var summary = r.recv().content();
726 if(e = openils.Event.parse(summary))
728 return onload(summary);
735 function vlImportSelectedRecords() {
736 displayGlobalDiv('vl-generic-progress-with-total');
739 for(var id in selectableGridRecords) {
740 if(dojo.byId(id).checked) {
741 var recId = selectableGridRecords[id];
742 var rec = queuedRecordsMap[recId];
743 if(!rec.import_time())
748 fieldmapper.standardRequest(
749 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
751 params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
752 onresponse: function(r) {
753 var resp = r.recv().content();
754 if(e = openils.Event.parse(resp))
757 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
759 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
766 function vlImportAllRecords() {
767 vlImportRecordQueue(currentType, currentQueueId, false,
768 function(){displayGlobalDiv('vl-queue-div');});
771 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
772 displayGlobalDiv('vl-generic-progress-with-total');
773 var method = 'open-ils.vandelay.bib_queue.import';
775 method = method.replace('import', 'nomatch.import');
777 method = method.replace('bib', 'auth');
779 fieldmapper.standardRequest(
780 ['open-ils.vandelay', method],
782 params: [authtoken, queueId],
783 onresponse: function(r) {
784 var resp = r.recv().content();
785 if(e = openils.Event.parse(resp))
787 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
789 oncomplete: function() {onload();}
796 * Create queue, upload MARC, process spool, load the newly created queue
798 function batchUpload() {
799 var queueName = dijit.byId('vl-queue-name').getValue();
800 currentType = dijit.byId('vl-record-type').getValue();
802 var handleProcessSpool = function() {
803 console.log('records uploaded and spooled');
804 if(vlUploadQueueAutoImport.checked) {
805 vlImportRecordQueue(currentType, currentQueueId, true,
807 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
811 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
815 var handleUploadMARC = function(key) {
816 console.log('marc uploaded');
817 dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
818 processSpool(key, currentQueueId, currentType, handleProcessSpool);
821 var handleCreateQueue = function(queue) {
822 console.log('queue created ' + queue.name());
823 currentQueueId = queue.id();
824 uploadMARC(handleUploadMARC);
827 if(vlUploadQueueSelector.getValue() && !queueName) {
828 currentQueueId = vlUploadQueueSelector.getValue();
829 console.log('adding records to existing queue ' + currentQueueId);
830 uploadMARC(handleUploadMARC);
832 createQueue(queueName, currentType, handleCreateQueue);
837 function vlFleshQueueSelect(selector, type) {
838 var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
839 selector.store = new dojo.data.ItemFileReadStore({data:data});
840 selector.setValue(null);
841 selector.setDisplayedValue('');
843 selector.setValue(data[0].id());
846 function vlShowUploadForm() {
847 displayGlobalDiv('vl-marc-upload-div');
848 vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
849 vlUploadSourceSelector.store =
850 new dojo.data.ItemFileReadStore({data:cbs.toStoreData(vlBibSources, 'source')});
851 vlUploadSourceSelector.setValue(vlBibSources[0].id());
854 function vlShowQueueSelect() {
855 displayGlobalDiv('vl-queue-select-div');
856 vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
859 function vlFetchQueueFromForm() {
860 currentType = vlQueueSelectType.getValue();
861 currentQueueId = vlQueueSelectQueueList.getValue();
862 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
865 function vlOpenMarcEditWindow(rec, postReloadHTMLHandler) {
867 To run in Firefox directly, must set signed.applets.codebase_principal_support
868 to true in about:config
870 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
871 win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
874 // after the record is saved, reload the HTML display
875 var stat = r.recv().content();
876 if(e = openils.Event.parse(stat))
878 alert(dojo.byId('vl-marc-edit-complete-label').innerHTML);
880 vlLoadMARCHtml(rec.id(), false, postReloadHTMLHandler);
884 record : {marc : rec.marc()},
886 label: dojo.byId('vl-marc-edit-save-label').innerHTML,
887 func: function(xmlString) {
888 var method = 'open-ils.permacrud.update.' + rec.classname;
890 fieldmapper.standardRequest(
891 ['open-ils.permacrud', method],
893 params: [authtoken, rec],
902 function vlLoadMarcEditor(type, recId, postReloadHTMLHandler) {
903 var method = 'open-ils.permacrud.search.vqbr';
904 if(currentType != 'bib')
905 method = method.replace(/vqbr/,'vqar');
907 fieldmapper.standardRequest(
908 ['open-ils.permacrud', method],
910 params: [authtoken, {id : recId}],
911 oncomplete: function(r) {
912 var rec = r.recv().content();
913 if(e = openils.Event.parse(rec))
915 vlOpenMarcEditWindow(rec, postReloadHTMLHandler);
923 //------------------------------------------------------------
926 // attribute-editor global variables
928 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
929 var ATTR_EDIT_ID = null; // id of current 'edit' attribute
930 var ATTR_EDIT_GROUP = 'bib'; // bib-attrs or auth-attrs
932 function vlAttrEditorInit() {
933 // set up tooltips on the edit form
934 connectTooltip('attr-editor-tags');
935 connectTooltip('attr-editor-subfields');
938 function vlShowAttrEditor() {
939 displayGlobalDiv('vl-attr-editor-div');
940 loadAttrEditorGrid();
941 idHide('vl-generic-progress');
944 function setAttrEditorGroup(groupName) {
945 // put us into 'bib'-attr or 'auth'-attr mode.
946 if (ATTR_EDIT_GROUP != groupName) {
947 ATTR_EDIT_GROUP = groupName;
948 loadAttrEditorGrid();
952 function onAttrEditorOpen() {
953 // the "bars" have the create/update/cancel/etc. buttons.
954 var create_bar = document.getElementById('attr-editor-create-bar');
955 var update_bar = document.getElementById('attr-editor-update-bar');
956 if (ATTR_EDITOR_IN_UPDATE_MODE) {
957 update_bar.style.display='table-row';
958 create_bar.style.display='none';
959 // hide the dropdown-button
960 idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
962 dijit.byId('attr-editor-dialog').reset();
963 create_bar.style.display='table-row';
964 update_bar.style.display='none';
968 function onAttrEditorClose() {
969 // reset the form to a "create" form. (We may have borrowed it for editing.)
970 ATTR_EDITOR_IN_UPDATE_MODE = false;
971 // show the dropdown-button
972 idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
975 function loadAttrEditorGrid() {
976 var _data = (ATTR_EDIT_GROUP == 'auth') ?
977 vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
979 var store = new dojo.data.ItemFileReadStore({data:_data});
980 attrEditorGrid.setStore(store);
981 dojo.connect(attrEditorGrid, 'onRowDblClick', onAttrEditorClick);
982 attrEditorGrid.update();
985 function attrGridGetTag(n, item) {
986 // grid helper: return the tags from the row's xpath column.
987 return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).tags;
990 function attrGridGetSubfield(n, item) {
991 // grid helper: return the subfields from the row's xpath column.
992 return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).subfields;
995 function onAttrEditorClick() {
996 var row = this.getItem(this.focus.rowIndex);
997 ATTR_EDIT_ID = this.store.getValue(row, 'id');
998 ATTR_EDITOR_IN_UPDATE_MODE = true;
1000 // populate the popup editor.
1001 dijit.byId('attr-editor-code').attr('value', this.store.getValue(row, 'code'));
1002 dijit.byId('attr-editor-description').attr('value', this.store.getValue(row, 'description'));
1003 var parsed_xpath = xpathParser.parse(this.store.getValue(row, 'xpath'));
1004 dijit.byId('attr-editor-tags').attr('value', parsed_xpath.tags);
1005 dijit.byId('attr-editor-subfields').attr('value', parsed_xpath.subfields);
1006 dijit.byId('attr-editor-identifier').attr('value', this.store.getValue(row, 'ident'));
1007 dijit.byId('attr-editor-xpath').attr('value', this.store.getValue(row, 'xpath'));
1008 dijit.byId('attr-editor-remove').attr('value', this.store.getValue(row, 'remove'));
1010 // set up UI for editing
1011 dojo.byId('vl-create-attr-editor-button').click();
1014 function vlSaveAttrDefinition(data) {
1015 idHide('vl-attr-editor-div');
1016 idShow('vl-generic-progress');
1018 data.id = ATTR_EDIT_ID;
1020 // this ought to honour custom xpaths, but overwrite xpaths
1021 // derived from tags/subfields.
1022 if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
1023 var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
1024 data.xpath = _xpath;
1027 // build up our permacrud params. Key variables here are
1028 // "create or update" and "bib or auth".
1030 var isAuth = (ATTR_EDIT_GROUP == 'auth');
1031 var isCreate = (ATTR_EDIT_ID == null);
1032 var rad = isAuth ? new vqarad() : new vqbrad() ;
1033 var method = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.')
1034 + (isAuth ? 'vqarad' : 'vqbrad');
1035 var _data = rad.fromStoreItem(data);
1039 fieldmapper.standardRequest(
1040 ['open-ils.permacrud', method],
1042 params: [authtoken, _data ],
1043 onresponse: function(r) { },
1044 oncomplete: function(r) {
1045 attrEditorFetchAttrDefs(vlShowAttrEditor);
1046 ATTR_EDIT_ID = null;
1048 onerror: function(r) {
1049 alert('vlSaveAttrDefinition comms error: ' + r);
1055 function attrEditorFetchAttrDefs(callback) {
1056 var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
1057 return fn(callback);
1060 function vlAttrDelete() {
1061 idHide('vl-attr-editor-div');
1062 idShow('vl-generic-progress');
1064 var isAuth = (ATTR_EDIT_GROUP == 'auth');
1065 var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
1066 var rad = isAuth ? new vqarad() : new vqbrad() ;
1067 fieldmapper.standardRequest(
1068 ['open-ils.permacrud', method],
1070 params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
1071 oncomplete: function() {
1072 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
1073 attrEditorFetchAttrDefs(vlShowAttrEditor);
1074 ATTR_EDIT_ID = null;
1076 onerror: function(r) {
1077 alert('vlAttrDelete comms error: ' + r);
1083 // ------------------------------------------------------------
1084 // utilities for attribute editors
1086 // dom utilities (maybe dojo does these, and these should be replaced)
1088 function idStyle(obId, k, v) { document.getElementById(obId).style[k] = v; }
1089 function idShow(obId) { idStyle(obId, 'display', 'block'); }
1090 function idHide(obId) { idStyle(obId, 'display' , 'none'); }
1092 function connectTooltip(fieldId) {
1093 // Given an element id, look up a tooltip element in the doc (same
1094 // id with a '-tip' suffix) and associate the two. Maybe dojo has
1095 // a better way to do this?
1096 var fld = dojo.byId(fieldId);
1097 var tip = dojo.byId(fieldId + '-tip');
1098 dojo.connect(fld, 'onfocus', function(evt) {
1099 dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1100 dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1105 var xpathParser = new openils.MarcXPathParser();
1107 function tagAndSubFieldsToXpath(tags, subfields) {
1108 // given tags, and subfields, build up an XPath.
1111 'tags':tags.match(/[\d]+/g),
1112 'subfields':subfields.match(/[a-zA-z]/g) };
1113 return xpathParser.compile(parts);
1115 return {'parts':null, 'tags':null, 'error':err};
1119 function looksLikeDerivedXpath(path) {
1120 // Does this path look like it was derived from tags and subfields?
1121 var parsed = xpathParser.parse(path);
1122 if (parsed.tags == null)
1124 var compiled = xpathParser.compile(parsed);
1125 return (path == compiled);
1128 // amazing xpath-util unit-tests
1129 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]')) alert('vandelay xpath-utility error');
1130 if ( looksLikeDerivedXpath('ba-boo-ba-boo!')) alert('vandelay xpath-utility error');