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("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');
44 'vl-generic-progress',
45 'vl-generic-progress-with-total',
50 'vl-queue-select-div',
51 'vl-marc-upload-status-div',
57 var VANDELAY_URL = '/vandelay-upload';
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
66 var currentQueueId = null;
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
75 var allUserAuthQueues;
76 var selectableGridRecords;
77 var cgi = new openils.CGI();
78 var vlQueueGridColumePicker = {};
79 var vlBibSources = [];
85 authtoken = dojo.cookie('ses') || cgi.param('ses');
86 var initNeeded = 5; // how many async responses do we need before we're init'd
87 var initCount = 0; // how many async reponses we've received
89 openils.Util.registerEnterHandler(
90 vlQueueDisplayPage.domNode, function(){retrieveQueuedRecords();});
91 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
93 function checkInitDone() {
95 if(initCount == initNeeded)
99 // Fetch the bib and authority attribute definitions
100 vlFetchBibAttrDefs(function () { checkInitDone(); });
101 vlFetchAuthAttrDefs(function () { checkInitDone(); });
103 vlRetrieveQueueList('bib', null,
105 allUserBibQueues = list;
106 for(var i = 0; i < allUserBibQueues.length; i++) {
107 if(allUserBibQueues[i].complete() == 'f')
108 userBibQueues.push(allUserBibQueues[i]);
114 vlRetrieveQueueList('auth', null,
116 allUserAuthQueues = list;
117 for(var i = 0; i < allUserAuthQueues.length; i++) {
118 if(allUserAuthQueues[i].complete() == 'f')
119 userAuthQueues.push(allUserAuthQueues[i]);
125 fieldmapper.standardRequest(
126 ['open-ils.permacrud', 'open-ils.permacrud.search.cbs.atomic'],
128 params: [authtoken, {id:{"!=":null}}, {order_by:{cbs:'id'}}],
129 oncomplete : function(r) {
130 vlBibSources = openils.Util.readResponse(r, false, true);
140 dojo.addOnLoad(vlInit);
143 // fetch the bib and authority attribute definitions
145 function vlFetchBibAttrDefs(postcomplete) {
147 fieldmapper.standardRequest(
148 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
150 params: [authtoken, {id:{'!=':null}}],
151 onresponse: function(r) {
152 var def = r.recv().content();
153 if(e = openils.Event.parse(def[0]))
155 bibAttrDefs.push(def);
157 oncomplete: function() {
158 bibAttrDefs = bibAttrDefs.sort(
160 if(a.id() > b.id()) return 1;
161 if(a.id() < b.id()) return -1;
171 function vlFetchAuthAttrDefs(postcomplete) {
173 fieldmapper.standardRequest(
174 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
176 params: [authtoken, {id:{'!=':null}}],
177 onresponse: function(r) {
178 var def = r.recv().content();
179 if(e = openils.Event.parse(def[0]))
181 authAttrDefs.push(def);
183 oncomplete: function() {
184 authAttrDefs = authAttrDefs.sort(
186 if(a.id() > b.id()) return 1;
187 if(a.id() < b.id()) return -1;
197 function vlRetrieveQueueList(type, filter, onload) {
198 type = (type == 'bib') ? type : 'authority';
199 fieldmapper.standardRequest(
200 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
202 params: [authtoken, null, filter],
203 oncomplete: function(r) {
204 var list = r.recv().content();
205 if(e = openils.Event.parse(list[0]))
214 function displayGlobalDiv(id) {
215 for(var i = 0; i < globalDivs.length; i++) {
217 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
219 alert('please define div ' + globalDivs[i]);
222 dojo.style(dojo.byId(id),'display','block');
224 openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
225 openils.Util.removeCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
226 openils.Util.removeCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
227 openils.Util.removeCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
230 case 'vl-marc-export-div':
231 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
233 case 'vl-marc-upload-div':
234 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
236 case 'vl-queue-select-div':
237 openils.Util.addCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
239 case 'vl-attr-editor-div':
240 openils.Util.addCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
245 function runStartupCommands() {
246 currentQueueId = cgi.param('qid');
247 currentType = cgi.param('qtype');
248 dojo.style('vl-nav-bar', 'visibility', 'visible');
250 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
255 * asynchronously upload a file of MARC records
257 function uploadMARC(onload){
258 dojo.byId('vl-upload-status-count').innerHTML = '0';
259 dojo.byId('vl-ses-input').value = authtoken;
260 displayGlobalDiv('vl-marc-upload-status-div');
261 dojo.io.iframe.send({
265 form: dojo.byId('vl-marc-upload-form'),
266 handle: function(data,ioArgs){
267 var content = data.documentElement.textContent;
274 * Creates a new vandelay queue
276 function createQueue(queueName, type, onload) {
277 var name = (type=='bib') ? 'bib' : 'authority';
278 var method = 'open-ils.vandelay.'+ name +'_queue.create'
279 fieldmapper.standardRequest(
280 ['open-ils.vandelay', method],
282 params: [authtoken, queueName, null, name],
283 oncomplete : function(r) {
284 var queue = r.recv().content();
285 if(e = openils.Event.parse(queue))
294 * Tells vandelay to pull a batch of records from the cache and explode them
295 * out into the vandelay tables
297 function processSpool(key, queueId, type, onload) {
298 fieldmapper.standardRequest(
299 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
301 params: [authtoken, key, queueId],
302 onresponse : function(r) {
303 var resp = r.recv().content();
304 if(e = openils.Event.parse(resp))
306 dojo.byId('vl-upload-status-count').innerHTML = resp;
308 oncomplete : function(r) {onload();}
313 function retrieveQueuedRecords(type, queueId, onload) {
314 displayGlobalDiv('vl-generic-progress');
316 queuedRecordsMap = {};
317 currentOverlayRecordsMap = {};
318 currentOverlayRecordsMapGid = {};
319 selectableGridRecords = {};
320 resetVlQueueGridLayout();
322 if(!type) type = currentType;
323 if(!queueId) queueId = currentQueueId;
324 if(!onload) onload = handleRetrieveRecords;
326 var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
327 if(vlQueueGridShowMatches.checked)
328 method = method.replace('records', 'records.matches');
330 //var limit = parseInt(vlQueueDisplayLimit.getValue());
331 var sel = dojo.byId('vl-queue-display-limit-selector');
332 var limit = parseInt(sel.options[sel.selectedIndex].value);
333 var offset = limit * parseInt(vlQueueDisplayPage.getValue()-1);
335 var params = [authtoken, queueId, {clear_marc: 1, offset: offset, limit: limit}];
336 if(vlQueueGridShowNonImport.checked)
337 params[2].non_imported = 1;
339 fieldmapper.standardRequest(
340 ['open-ils.vandelay', method],
344 onresponse: function(r) {
345 console.log("ONREPONSE");
346 var rec = r.recv().content();
347 if(e = openils.Event.parse(rec))
349 console.log("got record " + rec.id());
350 queuedRecords.push(rec);
351 queuedRecordsMap[rec.id()] = rec;
354 oncomplete: function(r){
355 var recs = r.recv().content();
356 if(e = openils.Event.parse(recs[0]))
358 for(var i = 0; i < recs.length; i++) {
360 queuedRecords.push(rec);
361 queuedRecordsMap[rec.id()] = rec;
369 function vlLoadMatchUI(recId) {
370 displayGlobalDiv('vl-generic-progress');
371 var matches = queuedRecordsMap[recId].matches();
373 currentImportRecId = recId;
374 for(var i = 0; i < matches.length; i++)
375 records.push(matches[i].eg_record());
377 var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
378 var params = [records];
379 if(currentType == 'auth') {
380 retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
381 parmas = [authtoken, records, {clear_marc:1}];
384 fieldmapper.standardRequest(
388 oncomplete: function(r) {
389 var recs = r.recv().content();
390 if(e = openils.Event.parse(recs))
394 displayGlobalDiv('vl-match-div');
395 resetVlMatchGridLayout();
396 currentMatchedRecords = recs;
397 if(!vlMatchGrid.structure)
398 vlMatchGrid.setStructure(vlMatchGridLayout);
400 // build the data store of records with match information
401 var dataStore = bre.toStoreData(recs, null,
402 {virtualFields:['dest_matchpoint', 'src_matchpoint', '_id']});
403 dataStore.identifier = '_id';
405 var matchSeenMap = {};
407 for(var i = 0; i < dataStore.items.length; i++) {
408 var item = dataStore.items[i];
409 item._id = i; // just need something unique
410 for(var j = 0; j < matches.length; j++) {
411 var match = matches[j];
412 if(match.eg_record() == item.id && !matchSeenMap[match.id()]) {
413 item.dest_matchpoint = match.field_type();
414 var attr = getRecAttrFromMatch(queuedRecordsMap[recId], match);
415 item.src_matchpoint = getRecAttrDefFromAttr(attr, currentType).code();
416 matchSeenMap[match.id()] = 1;
422 // now populate the grid
423 vlPopulateMatchGrid(vlMatchGrid, dataStore);
429 function vlPopulateMatchGrid(grid, data) {
430 var store = new dojo.data.ItemFileReadStore({data:data});
431 var model = new dojox.grid.data.DojoData(
432 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
433 grid.setModel(model);
437 function showMe(id) {
438 dojo.style(dojo.byId(id), 'display', 'block');
440 function hideMe(id) {
441 dojo.style(dojo.byId(id), 'display', 'none');
445 function vlLoadMARCHtml(recId, inCat, oncomplete) {
446 dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
447 displayGlobalDiv('vl-generic-progress');
449 var params = [recId, 1];
452 hideMe('vl-marc-html-edit-button'); // don't show marc editor button
453 dijit.byId('vl-marc-html-edit-button').onClick = function(){}
454 api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
455 if(currentType == 'auth')
456 api = ['open-ils.search', 'open-ils.search.authority.to_html'];
458 showMe('vl-marc-html-edit-button'); // plug in the marc editor button
459 dijit.byId('vl-marc-html-edit-button').onClick =
460 function() {vlLoadMarcEditor(currentType, recId, oncomplete);};
461 params = [authtoken, recId];
462 api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
463 if(currentType == 'auth')
464 api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
467 fieldmapper.standardRequest(
471 oncomplete: function(r) {
472 displayGlobalDiv('vl-marc-html-div');
473 var html = r.recv().content();
474 dojo.byId('vl-marc-record-html').innerHTML = html;
482 function getRecMatchesFromAttrCode(rec, attrCode) {
484 var attr = getRecAttrFromCode(rec, attrCode);
485 for(var j = 0; j < rec.matches().length; j++) {
486 var match = rec.matches()[j];
487 if(match.matched_attr() == attr.id())
494 function getRecAttrFromMatch(rec, match) {
495 for(var i = 0; i < rec.attributes().length; i++) {
496 var attr = rec.attributes()[i];
497 if(attr.id() == match.matched_attr())
502 function getRecAttrDefFromAttr(attr, type) {
503 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
504 for(var i = 0; i < defs.length; i++) {
506 if(def.id() == attr.field())
511 function getRecAttrFromCode(rec, attrCode) {
512 var defId = attrDefMap[currentType][attrCode];
513 var attrs = rec.attributes();
514 for(var i = 0; i < attrs.length; i++) {
516 if(attr.field() == defId)
522 function vlGetViewMatches(rowIdx) {
523 var data = this.grid.model.getRow(rowIdx);
525 var rec = queuedRecordsMap[data.id];
526 if(rec.matches().length > 0)
527 return this.value.replace('RECID', data.id);
531 function getAttrValue(rowIdx) {
532 var data = this.grid.model.getRow(rowIdx);
534 var attrCode = this.field.split('.')[1];
535 var rec = queuedRecordsMap[data.id];
536 var attr = getRecAttrFromCode(rec, attrCode);
537 return (attr) ? attr.attr_value() : '';
540 function vlGetDateTimeField(rowIdx) {
541 data = this.grid.model.getRow(rowIdx);
543 if(!data[this.field]) return '';
544 var date = dojo.date.stamp.fromISOString(data[this.field]);
545 return dojo.date.locale.format(date, {selector:'date'});
548 function vlGetCreator(rowIdx) {
549 data = this.grid.model.getRow(rowIdx);
551 var id = data.creator;
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))
558 userCache[id] = user;
559 return user.usrname();
562 function vlGetViewMARC(rowIdx) {
563 data = this.grid.model.getRow(rowIdx);
565 return this.value.replace('RECID', data.id);
568 function vlGetOverlayTargetSelector(rowIdx) {
569 data = this.grid.model.getRow(rowIdx);
571 var value = this.value.replace(/GRIDID/g, data._id);
572 value = value.replace(/RECID/g, currentImportRecId);
573 value = value.replace(/ID/g, data.id);
574 if(data._id == currentOverlayRecordsMapGid[currentImportRecId])
575 return value.replace('/>', 'checked="checked"/>');
581 * see if the user has enabled overlays for the current match set and,
582 * if so, map the current import record to the overlay target.
584 function vlHandleOverlayTargetSelected(recId, gridId) {
585 var noneSelected = true;
586 var checkboxes = dojo.query('[name=vl-overlay-target-'+currentImportRecId+']');
587 for(var i = 0; i < checkboxes.length; i++) {
588 var checkbox = checkboxes[i];
589 var matchRecId = checkbox.getAttribute('match');
590 var gid = checkbox.getAttribute('gridid');
591 if(checkbox.checked) {
592 if(matchRecId == recId && gid == gridId) {
593 noneSelected = false;
594 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
595 currentOverlayRecordsMapGid[currentImportRecId] = gid;
596 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
597 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
599 checkbox.checked = false;
605 delete currentOverlayRecordsMap[currentImportRecId];
606 delete currentOverlayRecordsMapGid[currentImportRecId];
607 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
608 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = '';
612 var valLastQueueType = null;
613 function buildRecordGrid(type) {
614 displayGlobalDiv('vl-queue-div');
616 if(valLastQueueType != type) {
617 valLastQueueType = type;
618 resetVlQueueGridLayout();
619 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
620 attrDefMap[type] = {};
621 for(var i = 0; i < defs.length; i++) {
623 attrDefMap[type][def.code()] = def.id();
625 name:def.description(),
626 field:'attr.' + def.code(),
628 selectableColumn:true
630 vlQueueGridLayout[0].cells[0].push(col);
636 storeData = vqbr.toStoreData(queuedRecords);
638 storeData = vqar.toStoreData(queuedRecords);
640 var store = new dojo.data.ItemFileReadStore({data:storeData});
641 var model = new dojox.grid.data.DojoData(
642 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
643 vlQueueGrid.setModel(model);
646 if(vlQueueGridColumePicker[type]) {
647 vlQueueGrid.update();
649 vlQueueGridColumePicker[type] =
650 new openils.GridColumnPicker(vlQueueGridColumePickerDialog,
651 vlQueueGrid, vlQueueGridLayout, authtoken, 'vandelay.queue.'+type);
652 vlQueueGridColumePicker[type].load();
656 function vlQueueGridPrevPage() {
657 var page = parseInt(vlQueueDisplayPage.getValue());
659 vlQueueDisplayPage.setValue(page - 1);
660 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
663 function vlQueueGridNextPage() {
664 vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
665 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
668 function vlDeleteQueue(type, queueId, onload) {
669 fieldmapper.standardRequest(
670 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
672 params: [authtoken, queueId],
673 oncomplete: function(r) {
674 var resp = r.recv().content();
675 if(e = openils.Event.parse(resp))
684 function vlQueueGridDrawSelectBox(rowIdx) {
685 var data = this.grid.model.getRow(rowIdx);
687 var domId = 'vl-record-list-selected-' +data.id;
688 selectableGridRecords[domId] = data.id;
689 return "<div><input type='checkbox' id='"+domId+"'/></div>";
692 function vlSelectAllQueueGridRecords() {
693 for(var id in selectableGridRecords)
694 dojo.byId(id).checked = true;
696 function vlSelectNoQueueGridRecords() {
697 for(var id in selectableGridRecords)
698 dojo.byId(id).checked = false;
700 function vlToggleQueueGridSelect() {
701 if(dojo.byId('vl-queue-grid-row-selector').checked)
702 vlSelectAllQueueGridRecords();
704 vlSelectNoQueueGridRecords();
707 var handleRetrieveRecords = function() {
708 buildRecordGrid(currentType);
709 vlFetchQueueSummary(currentQueueId, currentType,
711 dojo.byId('vl-queue-summary-name').innerHTML = summary.queue.name();
712 dojo.byId('vl-queue-summary-total-count').innerHTML = summary.total +'';
713 dojo.byId('vl-queue-summary-import-count').innerHTML = summary.imported + '';
718 function vlFetchQueueSummary(qId, type, onload) {
719 fieldmapper.standardRequest(
720 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.summary.retrieve'],
722 params: [authtoken, qId],
723 oncomplete : function(r) {
724 var summary = r.recv().content();
725 if(e = openils.Event.parse(summary))
727 return onload(summary);
734 function vlImportSelectedRecords() {
735 displayGlobalDiv('vl-generic-progress-with-total');
738 for(var id in selectableGridRecords) {
739 if(dojo.byId(id).checked) {
740 var recId = selectableGridRecords[id];
741 var rec = queuedRecordsMap[recId];
742 if(!rec.import_time())
747 fieldmapper.standardRequest(
748 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
750 params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
751 onresponse: function(r) {
752 var resp = r.recv().content();
753 if(e = openils.Event.parse(resp))
756 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
758 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
765 function vlImportAllRecords() {
766 vlImportRecordQueue(currentType, currentQueueId, false,
767 function(){displayGlobalDiv('vl-queue-div');});
770 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
771 displayGlobalDiv('vl-generic-progress-with-total');
772 var method = 'open-ils.vandelay.bib_queue.import';
774 method = method.replace('import', 'nomatch.import');
776 method = method.replace('bib', 'auth');
778 fieldmapper.standardRequest(
779 ['open-ils.vandelay', method],
781 params: [authtoken, queueId],
782 onresponse: function(r) {
783 var resp = r.recv().content();
784 if(e = openils.Event.parse(resp))
786 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
788 oncomplete: function() {onload();}
795 * Create queue, upload MARC, process spool, load the newly created queue
797 function batchUpload() {
798 var queueName = dijit.byId('vl-queue-name').getValue();
799 currentType = dijit.byId('vl-record-type').getValue();
801 var handleProcessSpool = function() {
802 console.log('records uploaded and spooled');
803 if(vlUploadQueueAutoImport.checked) {
804 vlImportRecordQueue(currentType, currentQueueId, true,
806 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
810 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
814 var handleUploadMARC = function(key) {
815 console.log('marc uploaded');
816 dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
817 processSpool(key, currentQueueId, currentType, handleProcessSpool);
820 var handleCreateQueue = function(queue) {
821 console.log('queue created ' + queue.name());
822 currentQueueId = queue.id();
823 uploadMARC(handleUploadMARC);
826 if(vlUploadQueueSelector.getValue() && !queueName) {
827 currentQueueId = vlUploadQueueSelector.getValue();
828 console.log('adding records to existing queue ' + currentQueueId);
829 uploadMARC(handleUploadMARC);
831 createQueue(queueName, currentType, handleCreateQueue);
836 function vlFleshQueueSelect(selector, type) {
837 var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
838 selector.store = new dojo.data.ItemFileReadStore({data:data});
839 selector.setValue(null);
840 selector.setDisplayedValue('');
842 selector.setValue(data[0].id());
845 function vlShowUploadForm() {
846 displayGlobalDiv('vl-marc-upload-div');
847 vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
848 vlUploadSourceSelector.store =
849 new dojo.data.ItemFileReadStore({data:cbs.toStoreData(vlBibSources, 'source')});
850 vlUploadSourceSelector.setValue(vlBibSources[0].id());
853 function vlShowQueueSelect() {
854 displayGlobalDiv('vl-queue-select-div');
855 vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
858 function vlFetchQueueFromForm() {
859 currentType = vlQueueSelectType.getValue();
860 currentQueueId = vlQueueSelectQueueList.getValue();
861 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
864 function vlOpenMarcEditWindow(rec, postReloadHTMLHandler) {
866 To run in Firefox directly, must set signed.applets.codebase_principal_support
867 to true in about:config
869 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
870 win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
873 // after the record is saved, reload the HTML display
874 var stat = r.recv().content();
875 if(e = openils.Event.parse(stat))
877 alert(dojo.byId('vl-marc-edit-complete-label').innerHTML);
879 vlLoadMARCHtml(rec.id(), false, postReloadHTMLHandler);
883 record : {marc : rec.marc()},
885 label: dojo.byId('vl-marc-edit-save-label').innerHTML,
886 func: function(xmlString) {
887 var method = 'open-ils.permacrud.update.' + rec.classname;
889 fieldmapper.standardRequest(
890 ['open-ils.permacrud', method],
892 params: [authtoken, rec],
901 function vlLoadMarcEditor(type, recId, postReloadHTMLHandler) {
902 var method = 'open-ils.permacrud.search.vqbr';
903 if(currentType != 'bib')
904 method = method.replace(/vqbr/,'vqar');
906 fieldmapper.standardRequest(
907 ['open-ils.permacrud', method],
909 params: [authtoken, {id : recId}],
910 oncomplete: function(r) {
911 var rec = r.recv().content();
912 if(e = openils.Event.parse(rec))
914 vlOpenMarcEditWindow(rec, postReloadHTMLHandler);
922 //------------------------------------------------------------
925 // attribute-editor global variables
927 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
928 var ATTR_EDIT_ID = null; // id of current 'edit' attribute
929 var ATTR_EDIT_GROUP = 'bib'; // bib-attrs or auth-attrs
931 function vlAttrEditorInit() {
932 // set up tooltips on the edit form
933 connectTooltip('attr-editor-tags');
934 connectTooltip('attr-editor-subfields');
937 function vlShowAttrEditor() {
938 displayGlobalDiv('vl-attr-editor-div');
939 loadAttrEditorGrid();
940 idHide('vl-generic-progress');
943 function setAttrEditorGroup(groupName) {
944 // put us into 'bib'-attr or 'auth'-attr mode.
945 if (ATTR_EDIT_GROUP != groupName) {
946 ATTR_EDIT_GROUP = groupName;
947 loadAttrEditorGrid();
951 function onAttrEditorOpen() {
952 // the "bars" have the create/update/cancel/etc. buttons.
953 var create_bar = document.getElementById('attr-editor-create-bar');
954 var update_bar = document.getElementById('attr-editor-update-bar');
955 if (ATTR_EDITOR_IN_UPDATE_MODE) {
956 update_bar.style.display='table-row';
957 create_bar.style.display='none';
958 // hide the dropdown-button
959 idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
961 dijit.byId('attr-editor-dialog').reset();
962 create_bar.style.display='table-row';
963 update_bar.style.display='none';
967 function onAttrEditorClose() {
968 // reset the form to a "create" form. (We may have borrowed it for editing.)
969 ATTR_EDITOR_IN_UPDATE_MODE = false;
970 // show the dropdown-button
971 idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
974 function loadAttrEditorGrid() {
975 var _data = (ATTR_EDIT_GROUP == 'auth') ?
976 vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
978 var store = new dojo.data.ItemFileReadStore({data:_data});
979 var model = new dojox.grid.data.DojoData(
980 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
981 attrEditorGrid.setModel(model);
982 attrEditorGrid.setStructure(vlAttrGridLayout);
983 attrEditorGrid.onRowClick = onAttrEditorClick;
984 attrEditorGrid.update();
987 function attrGridGetTag(n) {
988 // grid helper: return the tags from the row's xpath column.
989 var xp = this.grid.model.getRow(n);
990 return xp && xpathParser.parse(xp.xpath).tags;
993 function attrGridGetSubfield(n) {
994 // grid helper: return the subfields from the row's xpath column.
995 var xp = this.grid.model.getRow(n);
996 return xp && xpathParser.parse(xp.xpath).subfields;
999 function onAttrEditorClick(evt) {
1000 var row = attrEditorGrid.model.getRow(evt.rowIndex);
1001 ATTR_EDIT_ID = row.id;
1002 ATTR_EDITOR_IN_UPDATE_MODE = true;
1004 // populate the popup editor.
1005 dojo.byId('attr-editor-code').value = row.code;
1006 dojo.byId('attr-editor-description').value = row.description;
1007 var parsed_xpath = xpathParser.parse(row.xpath);
1008 dojo.byId('attr-editor-tags').value = parsed_xpath.tags;
1009 dojo.byId('attr-editor-subfields').value = parsed_xpath.subfields;
1010 dojo.byId('attr-editor-identifier').value = (row.ident ? 'True':'False');
1011 dojo.byId('attr-editor-xpath').value = row.xpath;
1012 dojo.byId('attr-editor-remove').value = row.remove;
1014 // set up UI for editing
1015 dojo.byId('vl-create-attr-editor-button').click();
1018 function vlSaveAttrDefinition(data) {
1019 idHide('vl-attr-editor-div');
1020 idShow('vl-generic-progress');
1022 data.id = ATTR_EDIT_ID;
1024 // this ought to honour custom xpaths, but overwrite xpaths
1025 // derived from tags/subfields.
1026 if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
1027 var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
1028 data.xpath = _xpath;
1031 // build up our permacrud params. Key variables here are
1032 // "create or update" and "bib or auth".
1034 var isAuth = (ATTR_EDIT_GROUP == 'auth');
1035 var isCreate = (ATTR_EDIT_ID == null);
1036 var rad = isAuth ? new vqarad() : new vqbrad() ;
1037 var method = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.')
1038 + (isAuth ? 'vqarad' : 'vqbrad');
1039 var _data = rad.fromStoreItem(data);
1043 fieldmapper.standardRequest(
1044 ['open-ils.permacrud', method],
1046 params: [authtoken, _data ],
1047 onresponse: function(r) { },
1048 oncomplete: function(r) {
1049 attrEditorFetchAttrDefs(vlShowAttrEditor);
1050 ATTR_EDIT_ID = null;
1052 onerror: function(r) {
1053 alert('vlSaveAttrDefinition comms error: ' + r);
1059 function attrEditorFetchAttrDefs(callback) {
1060 var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
1061 return fn(callback);
1064 function vlAttrDelete() {
1065 idHide('vl-attr-editor-div');
1066 idShow('vl-generic-progress');
1068 var isAuth = (ATTR_EDIT_GROUP == 'auth');
1069 var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
1070 var rad = isAuth ? new vqarad() : new vqbrad() ;
1071 fieldmapper.standardRequest(
1072 ['open-ils.permacrud', method],
1074 params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
1075 oncomplete: function() {
1076 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
1077 attrEditorFetchAttrDefs(vlShowAttrEditor);
1078 ATTR_EDIT_ID = null;
1080 onerror: function(r) {
1081 alert('vlAttrDelete comms error: ' + r);
1087 // ------------------------------------------------------------
1088 // utilities for attribute editors
1090 // dom utilities (maybe dojo does these, and these should be replaced)
1092 function idStyle(obId, k, v) { document.getElementById(obId).style[k] = v; }
1093 function idShow(obId) { idStyle(obId, 'display', 'block'); }
1094 function idHide(obId) { idStyle(obId, 'display' , 'none'); }
1096 function connectTooltip(fieldId) {
1097 // Given an element id, look up a tooltip element in the doc (same
1098 // id with a '-tip' suffix) and associate the two. Maybe dojo has
1099 // a better way to do this?
1100 var fld = dojo.byId(fieldId);
1101 var tip = dojo.byId(fieldId + '-tip');
1102 dojo.connect(fld, 'onfocus', function(evt) {
1103 dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1104 dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1109 var xpathParser = new openils.MarcXPathParser();
1111 function tagAndSubFieldsToXpath(tags, subfields) {
1112 // given tags, and subfields, build up an XPath.
1115 'tags':tags.match(/[\d]+/g),
1116 'subfields':subfields.match(/[a-zA-z]/g) };
1117 return xpathParser.compile(parts);
1119 return {'parts':null, 'tags':null, 'error':err};
1123 function looksLikeDerivedXpath(path) {
1124 // Does this path look like it was derived from tags and subfields?
1125 var parsed = xpathParser.parse(path);
1126 if (parsed.tags == null)
1128 var compiled = xpathParser.compile(parsed);
1129 return (path == compiled);
1132 // amazing xpath-util unit-tests
1133 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]')) alert('vandelay xpath-utility error');
1134 if ( looksLikeDerivedXpath('ba-boo-ba-boo!')) alert('vandelay xpath-utility error');