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');
42 dojo.require('openils.widget.GridColumnPicker');
46 'vl-generic-progress',
47 'vl-generic-progress-with-total',
52 'vl-queue-select-div',
53 'vl-marc-upload-status-div',
59 var VANDELAY_URL = '/vandelay-upload';
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
68 var currentQueueId = null;
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
77 var allUserAuthQueues;
78 var selectableGridRecords;
79 var cgi = new openils.CGI();
80 var vlQueueGridColumePicker = {};
81 var vlBibSources = [];
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
91 openils.Util.registerEnterHandler(
92 vlQueueDisplayPage.domNode, function(){retrieveQueuedRecords();});
93 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
95 function checkInitDone() {
97 if(initCount == initNeeded)
101 // Fetch the bib and authority attribute definitions
102 vlFetchBibAttrDefs(function () { checkInitDone(); });
103 vlFetchAuthAttrDefs(function () { checkInitDone(); });
105 vlRetrieveQueueList('bib', null,
107 allUserBibQueues = list;
108 for(var i = 0; i < allUserBibQueues.length; i++) {
109 if(allUserBibQueues[i].complete() == 'f')
110 userBibQueues.push(allUserBibQueues[i]);
116 vlRetrieveQueueList('auth', null,
118 allUserAuthQueues = list;
119 for(var i = 0; i < allUserAuthQueues.length; i++) {
120 if(allUserAuthQueues[i].complete() == 'f')
121 userAuthQueues.push(allUserAuthQueues[i]);
127 fieldmapper.standardRequest(
128 ['open-ils.permacrud', 'open-ils.permacrud.search.cbs.atomic'],
130 params: [authtoken, {id:{"!=":null}}, {order_by:{cbs:'id'}}],
131 oncomplete : function(r) {
132 vlBibSources = openils.Util.readResponse(r, false, true);
142 dojo.addOnLoad(vlInit);
145 // fetch the bib and authority attribute definitions
147 function vlFetchBibAttrDefs(postcomplete) {
149 fieldmapper.standardRequest(
150 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
152 params: [authtoken, {id:{'!=':null}}],
153 onresponse: function(r) {
154 var def = r.recv().content();
155 if(e = openils.Event.parse(def[0]))
157 bibAttrDefs.push(def);
159 oncomplete: function() {
160 bibAttrDefs = bibAttrDefs.sort(
162 if(a.id() > b.id()) return 1;
163 if(a.id() < b.id()) return -1;
173 function vlFetchAuthAttrDefs(postcomplete) {
175 fieldmapper.standardRequest(
176 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
178 params: [authtoken, {id:{'!=':null}}],
179 onresponse: function(r) {
180 var def = r.recv().content();
181 if(e = openils.Event.parse(def[0]))
183 authAttrDefs.push(def);
185 oncomplete: function() {
186 authAttrDefs = authAttrDefs.sort(
188 if(a.id() > b.id()) return 1;
189 if(a.id() < b.id()) return -1;
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'],
204 params: [authtoken, null, filter],
205 oncomplete: function(r) {
206 var list = r.recv().content();
207 if(e = openils.Event.parse(list[0]))
216 function displayGlobalDiv(id) {
217 for(var i = 0; i < globalDivs.length; i++) {
219 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
221 alert('please define div ' + globalDivs[i]);
224 dojo.style(dojo.byId(id),'display','block');
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');
232 case 'vl-marc-export-div':
233 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-export'), 'toolbar_selected');
235 case 'vl-marc-upload-div':
236 openils.Util.addCSSClass(dojo.byId('vl-menu-marc-upload'), 'toolbar_selected');
238 case 'vl-queue-select-div':
239 openils.Util.addCSSClass(dojo.byId('vl-menu-queue-select'), 'toolbar_selected');
241 case 'vl-attr-editor-div':
242 openils.Util.addCSSClass(dojo.byId('vl-menu-attr-editor'), 'toolbar_selected');
247 function runStartupCommands() {
248 currentQueueId = cgi.param('qid');
249 currentType = cgi.param('qtype');
250 dojo.style('vl-nav-bar', 'visibility', 'visible');
252 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
257 * asynchronously upload a file of MARC records
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({
267 form: dojo.byId('vl-marc-upload-form'),
268 handle: function(data,ioArgs){
269 var content = data.documentElement.textContent;
276 * Creates a new vandelay queue
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],
284 params: [authtoken, queueName, null, name],
285 oncomplete : function(r) {
286 var queue = r.recv().content();
287 if(e = openils.Event.parse(queue))
296 * Tells vandelay to pull a batch of records from the cache and explode them
297 * out into the vandelay tables
299 function processSpool(key, queueId, type, onload) {
300 fieldmapper.standardRequest(
301 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
303 params: [authtoken, key, queueId],
304 onresponse : function(r) {
305 var resp = r.recv().content();
306 if(e = openils.Event.parse(resp))
308 dojo.byId('vl-upload-status-count').innerHTML = resp;
310 oncomplete : function(r) {onload();}
315 function retrieveQueuedRecords(type, queueId, onload) {
316 displayGlobalDiv('vl-generic-progress');
318 queuedRecordsMap = {};
319 currentOverlayRecordsMap = {};
320 currentOverlayRecordsMapGid = {};
321 selectableGridRecords = {};
322 //resetVlQueueGridLayout();
324 if(!type) type = currentType;
325 if(!queueId) queueId = currentQueueId;
326 if(!onload) onload = handleRetrieveRecords;
328 var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
329 if(vlQueueGridShowMatches.checked)
330 method = method.replace('records', 'records.matches');
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);
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, item) {
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);
532 function getAttrValue(rowIdx, item) {
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() : '';
540 function vlGetDateTimeField(rowIdx, item) {
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'});
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, item) {
564 return this.value.replace('RECID', this.grid.store.getValue(item, 'id'));
567 function vlGetOverlayTargetSelector(rowIdx) {
568 data = this.grid.model.getRow(rowIdx);
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"/>');
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.
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';
598 checkbox.checked = false;
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 = '';
611 var valLastQueueType = null;
612 function buildRecordGrid(type) {
613 displayGlobalDiv('vl-queue-div');
616 openils.Util.show('vl-bib-queue-grid-wrapper');
617 openils.Util.hide('vl-auth-queue-grid-wrapper');
618 vlQueueGrid = vlBibQueueGrid;
619 vlQueueGridMenu = vlBibQueueGridMenu;
621 openils.Util.show('vl-auth-queue-grid-wrapper');
622 openils.Util.hide('vl-bib-queue-grid-wrapper');
623 vlQueueGrid = vlAuthQueueGrid;
624 vlQueueGridMenu = vlAuthQueueGridMenu;
627 if(valLastQueueType != type) {
628 valLastQueueType = type;
629 resetVlQueueGridLayout();
630 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
631 attrDefMap[type] = {};
632 for(var i = 0; i < defs.length; i++) {
634 attrDefMap[type][def.code()] = def.id();
636 name:def.description(),
637 field:'attr.' + def.code(),
639 selectableColumn:true
641 vlQueueGridLayout[0].cells[0].push(col);
647 storeData = vqbr.toStoreData(queuedRecords);
649 storeData = vqar.toStoreData(queuedRecords);
651 var store = new dojo.data.ItemFileReadStore({data:storeData});
652 vlQueueGrid.setStore(store);
654 if(vlQueueGridColumePicker[type]) {
655 vlQueueGrid.update();
657 vlQueueGrid.attr('structure', vlQueueGridLayout);
658 vlQueueGridMenu.init({
660 authtoken : authtoken,
661 prefix : 'vandelay.queue.'+type
663 vlQueueGridMenu.load();
664 vlQueueGridColumePicker[type] = vlQueueGridMenu;
668 function vlQueueGridPrevPage() {
669 var page = parseInt(vlQueueDisplayPage.getValue());
671 vlQueueDisplayPage.setValue(page - 1);
672 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
675 function vlQueueGridNextPage() {
676 vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
677 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
680 function vlDeleteQueue(type, queueId, onload) {
681 fieldmapper.standardRequest(
682 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
684 params: [authtoken, queueId],
685 oncomplete: function(r) {
686 var resp = r.recv().content();
687 if(e = openils.Event.parse(resp))
696 function vlQueueGridDrawSelectBox(rowIdx, item) {
698 var id = this.grid.store.getValue(item, 'id');
699 var domId = 'vl-record-list-selected-' + id;
700 selectableGridRecords[domId] = id;
701 return "<div><input type='checkbox' id='"+domId+"'/></div>";
704 function vlSelectAllQueueGridRecords() {
705 for(var id in selectableGridRecords)
706 dojo.byId(id).checked = true;
708 function vlSelectNoQueueGridRecords() {
709 for(var id in selectableGridRecords)
710 dojo.byId(id).checked = false;
712 function vlToggleQueueGridSelect() {
713 if(dojo.byId('vl-queue-grid-row-selector').checked)
714 vlSelectAllQueueGridRecords();
716 vlSelectNoQueueGridRecords();
719 var handleRetrieveRecords = function() {
720 buildRecordGrid(currentType);
721 vlFetchQueueSummary(currentQueueId, currentType,
723 dojo.byId('vl-queue-summary-name').innerHTML = summary.queue.name();
724 dojo.byId('vl-queue-summary-total-count').innerHTML = summary.total +'';
725 dojo.byId('vl-queue-summary-import-count').innerHTML = summary.imported + '';
730 function vlFetchQueueSummary(qId, type, onload) {
731 fieldmapper.standardRequest(
732 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.summary.retrieve'],
734 params: [authtoken, qId],
735 oncomplete : function(r) {
736 var summary = r.recv().content();
737 if(e = openils.Event.parse(summary))
739 return onload(summary);
746 function vlImportSelectedRecords() {
747 displayGlobalDiv('vl-generic-progress-with-total');
750 for(var id in selectableGridRecords) {
751 if(dojo.byId(id).checked) {
752 var recId = selectableGridRecords[id];
753 var rec = queuedRecordsMap[recId];
754 if(!rec.import_time())
759 fieldmapper.standardRequest(
760 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
762 params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
763 onresponse: function(r) {
764 var resp = r.recv().content();
765 if(e = openils.Event.parse(resp))
768 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
770 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
777 function vlImportAllRecords() {
778 vlImportRecordQueue(currentType, currentQueueId, false,
779 function(){displayGlobalDiv('vl-queue-div');});
782 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
783 displayGlobalDiv('vl-generic-progress-with-total');
784 var method = 'open-ils.vandelay.bib_queue.import';
786 method = method.replace('import', 'nomatch.import');
788 method = method.replace('bib', 'auth');
790 fieldmapper.standardRequest(
791 ['open-ils.vandelay', method],
793 params: [authtoken, queueId],
794 onresponse: function(r) {
795 var resp = r.recv().content();
796 if(e = openils.Event.parse(resp))
798 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
800 oncomplete: function() {onload();}
807 * Create queue, upload MARC, process spool, load the newly created queue
809 function batchUpload() {
810 var queueName = dijit.byId('vl-queue-name').getValue();
811 currentType = dijit.byId('vl-record-type').getValue();
813 var handleProcessSpool = function() {
814 if(vlUploadQueueAutoImport.checked) {
815 vlImportRecordQueue(currentType, currentQueueId, true,
817 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
821 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
825 var handleUploadMARC = function(key) {
826 dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
827 processSpool(key, currentQueueId, currentType, handleProcessSpool);
830 var handleCreateQueue = function(queue) {
831 currentQueueId = queue.id();
832 uploadMARC(handleUploadMARC);
835 if(vlUploadQueueSelector.getValue() && !queueName) {
836 currentQueueId = vlUploadQueueSelector.getValue();
837 uploadMARC(handleUploadMARC);
839 createQueue(queueName, currentType, handleCreateQueue);
844 function vlFleshQueueSelect(selector, type) {
845 var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
846 selector.store = new dojo.data.ItemFileReadStore({data:data});
847 selector.setValue(null);
848 selector.setDisplayedValue('');
850 selector.setValue(data[0].id());
853 function vlShowUploadForm() {
854 displayGlobalDiv('vl-marc-upload-div');
855 vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
856 vlUploadSourceSelector.store =
857 new dojo.data.ItemFileReadStore({data:cbs.toStoreData(vlBibSources, 'source')});
858 vlUploadSourceSelector.setValue(vlBibSources[0].id());
861 function vlShowQueueSelect() {
862 displayGlobalDiv('vl-queue-select-div');
863 vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
866 function vlFetchQueueFromForm() {
867 currentType = vlQueueSelectType.getValue();
868 currentQueueId = vlQueueSelectQueueList.getValue();
869 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
872 function vlOpenMarcEditWindow(rec, postReloadHTMLHandler) {
874 To run in Firefox directly, must set signed.applets.codebase_principal_support
875 to true in about:config
877 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
878 win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
881 // after the record is saved, reload the HTML display
882 var stat = r.recv().content();
883 if(e = openils.Event.parse(stat))
885 alert(dojo.byId('vl-marc-edit-complete-label').innerHTML);
887 vlLoadMARCHtml(rec.id(), false, postReloadHTMLHandler);
891 record : {marc : rec.marc()},
893 label: dojo.byId('vl-marc-edit-save-label').innerHTML,
894 func: function(xmlString) {
895 var method = 'open-ils.permacrud.update.' + rec.classname;
897 fieldmapper.standardRequest(
898 ['open-ils.permacrud', method],
900 params: [authtoken, rec],
909 function vlLoadMarcEditor(type, recId, postReloadHTMLHandler) {
910 var method = 'open-ils.permacrud.search.vqbr';
911 if(currentType != 'bib')
912 method = method.replace(/vqbr/,'vqar');
914 fieldmapper.standardRequest(
915 ['open-ils.permacrud', method],
917 params: [authtoken, {id : recId}],
918 oncomplete: function(r) {
919 var rec = r.recv().content();
920 if(e = openils.Event.parse(rec))
922 vlOpenMarcEditWindow(rec, postReloadHTMLHandler);
930 //------------------------------------------------------------
933 // attribute-editor global variables
935 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
936 var ATTR_EDIT_ID = null; // id of current 'edit' attribute
937 var ATTR_EDIT_GROUP = 'bib'; // bib-attrs or auth-attrs
939 function vlAttrEditorInit() {
940 // set up tooltips on the edit form
941 connectTooltip('attr-editor-tags');
942 connectTooltip('attr-editor-subfields');
945 function vlShowAttrEditor() {
946 displayGlobalDiv('vl-attr-editor-div');
947 loadAttrEditorGrid();
948 idHide('vl-generic-progress');
951 function setAttrEditorGroup(groupName) {
952 // put us into 'bib'-attr or 'auth'-attr mode.
953 if (ATTR_EDIT_GROUP != groupName) {
954 ATTR_EDIT_GROUP = groupName;
955 loadAttrEditorGrid();
959 function onAttrEditorOpen() {
960 // the "bars" have the create/update/cancel/etc. buttons.
961 var create_bar = document.getElementById('attr-editor-create-bar');
962 var update_bar = document.getElementById('attr-editor-update-bar');
963 if (ATTR_EDITOR_IN_UPDATE_MODE) {
964 update_bar.style.display='table-row';
965 create_bar.style.display='none';
966 // hide the dropdown-button
967 idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
969 dijit.byId('attr-editor-dialog').reset();
970 create_bar.style.display='table-row';
971 update_bar.style.display='none';
975 function onAttrEditorClose() {
976 // reset the form to a "create" form. (We may have borrowed it for editing.)
977 ATTR_EDITOR_IN_UPDATE_MODE = false;
978 // show the dropdown-button
979 idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
982 function loadAttrEditorGrid() {
983 var _data = (ATTR_EDIT_GROUP == 'auth') ?
984 vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
986 var store = new dojo.data.ItemFileReadStore({data:_data});
987 attrEditorGrid.setStore(store);
988 dojo.connect(attrEditorGrid, 'onRowDblClick', onAttrEditorClick);
989 attrEditorGrid.update();
992 function attrGridGetTag(n, item) {
993 // grid helper: return the tags from the row's xpath column.
994 return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).tags;
997 function attrGridGetSubfield(n, item) {
998 // grid helper: return the subfields from the row's xpath column.
999 return item && xpathParser.parse(this.grid.store.getValue(item, 'xpath')).subfields;
1002 function onAttrEditorClick() {
1003 var row = this.getItem(this.focus.rowIndex);
1004 ATTR_EDIT_ID = this.store.getValue(row, 'id');
1005 ATTR_EDITOR_IN_UPDATE_MODE = true;
1007 // populate the popup editor.
1008 dijit.byId('attr-editor-code').attr('value', this.store.getValue(row, 'code'));
1009 dijit.byId('attr-editor-description').attr('value', this.store.getValue(row, 'description'));
1010 var parsed_xpath = xpathParser.parse(this.store.getValue(row, 'xpath'));
1011 dijit.byId('attr-editor-tags').attr('value', parsed_xpath.tags);
1012 dijit.byId('attr-editor-subfields').attr('value', parsed_xpath.subfields);
1013 dijit.byId('attr-editor-identifier').attr('value', this.store.getValue(row, 'ident'));
1014 dijit.byId('attr-editor-xpath').attr('value', this.store.getValue(row, 'xpath'));
1015 dijit.byId('attr-editor-remove').attr('value', this.store.getValue(row, 'remove'));
1017 // set up UI for editing
1018 dojo.byId('vl-create-attr-editor-button').click();
1021 function vlSaveAttrDefinition(data) {
1022 idHide('vl-attr-editor-div');
1023 idShow('vl-generic-progress');
1025 data.id = ATTR_EDIT_ID;
1027 // this ought to honour custom xpaths, but overwrite xpaths
1028 // derived from tags/subfields.
1029 if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
1030 var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
1031 data.xpath = _xpath;
1034 // build up our permacrud params. Key variables here are
1035 // "create or update" and "bib or auth".
1037 var isAuth = (ATTR_EDIT_GROUP == 'auth');
1038 var isCreate = (ATTR_EDIT_ID == null);
1039 var rad = isAuth ? new vqarad() : new vqbrad() ;
1040 var method = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.')
1041 + (isAuth ? 'vqarad' : 'vqbrad');
1042 var _data = rad.fromStoreItem(data);
1046 fieldmapper.standardRequest(
1047 ['open-ils.permacrud', method],
1049 params: [authtoken, _data ],
1050 onresponse: function(r) { },
1051 oncomplete: function(r) {
1052 attrEditorFetchAttrDefs(vlShowAttrEditor);
1053 ATTR_EDIT_ID = null;
1055 onerror: function(r) {
1056 alert('vlSaveAttrDefinition comms error: ' + r);
1062 function attrEditorFetchAttrDefs(callback) {
1063 var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
1064 return fn(callback);
1067 function vlAttrDelete() {
1068 idHide('vl-attr-editor-div');
1069 idShow('vl-generic-progress');
1071 var isAuth = (ATTR_EDIT_GROUP == 'auth');
1072 var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
1073 var rad = isAuth ? new vqarad() : new vqbrad() ;
1074 fieldmapper.standardRequest(
1075 ['open-ils.permacrud', method],
1077 params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
1078 oncomplete: function() {
1079 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
1080 attrEditorFetchAttrDefs(vlShowAttrEditor);
1081 ATTR_EDIT_ID = null;
1083 onerror: function(r) {
1084 alert('vlAttrDelete comms error: ' + r);
1090 // ------------------------------------------------------------
1091 // utilities for attribute editors
1093 // dom utilities (maybe dojo does these, and these should be replaced)
1095 function idStyle(obId, k, v) { document.getElementById(obId).style[k] = v; }
1096 function idShow(obId) { idStyle(obId, 'display', 'block'); }
1097 function idHide(obId) { idStyle(obId, 'display' , 'none'); }
1099 function connectTooltip(fieldId) {
1100 // Given an element id, look up a tooltip element in the doc (same
1101 // id with a '-tip' suffix) and associate the two. Maybe dojo has
1102 // a better way to do this?
1103 var fld = dojo.byId(fieldId);
1104 var tip = dojo.byId(fieldId + '-tip');
1105 dojo.connect(fld, 'onfocus', function(evt) {
1106 dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1107 dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1112 var xpathParser = new openils.MarcXPathParser();
1114 function tagAndSubFieldsToXpath(tags, subfields) {
1115 // given tags, and subfields, build up an XPath.
1118 'tags':tags.match(/[\d]+/g),
1119 'subfields':subfields.match(/[a-zA-z]/g) };
1120 return xpathParser.compile(parts);
1122 return {'parts':null, 'tags':null, 'error':err};
1126 function looksLikeDerivedXpath(path) {
1127 // Does this path look like it was derived from tags and subfields?
1128 var parsed = xpathParser.parse(path);
1129 if (parsed.tags == null)
1131 var compiled = xpathParser.compile(parsed);
1132 return (path == compiled);
1135 // amazing xpath-util unit-tests
1136 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]')) alert('vandelay xpath-utility error');
1137 if ( looksLikeDerivedXpath('ba-boo-ba-boo!')) alert('vandelay xpath-utility error');