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.MarcXPathParser');
39 dojo.require('openils.GridColumnPicker');
43 'vl-generic-progress',
44 'vl-generic-progress-with-total',
49 'vl-queue-select-div',
50 'vl-marc-upload-status-div',
56 var VANDELAY_URL = '/vandelay-upload';
58 var authAttrDefs = [];
59 var queuedRecords = [];
60 var queuedRecordsMap = {};
61 var bibAttrsFetched = false;
62 var authAttrsFetched = false;
63 var attrDefMap = {}; // maps attr def code names to attr def ids
65 var currentQueueId = null;
67 var currentMatchedRecords; // set of loaded matched bib records
68 var currentOverlayRecordsMap; // map of import record to overlay record
69 var currentOverlayRecordsMapGid; // map of import record to overlay record grid id
70 var currentImportRecId; // when analyzing matches, this is the current import record
71 var userBibQueues = []; // only non-complete queues
72 var userAuthQueues = []; // only non-complete queues
74 var allUserAuthQueues;
75 var selectableGridRecords;
76 var cgi = new openils.CGI();
77 var vlQueueGridColumePicker = {};
83 authtoken = dojo.cookie('ses') || cgi.param('ses');
84 var initNeeded = 4; // how many async responses do we need before we're init'd
85 var initCount = 0; // how many async reponses we've received
87 function checkInitDone() {
89 if(initCount == initNeeded)
93 // Fetch the bib and authority attribute definitions
94 vlFetchBibAttrDefs(function () { checkInitDone(); });
95 vlFetchAuthAttrDefs(function () { checkInitDone(); });
97 vlRetrieveQueueList('bib', null,
99 allUserBibQueues = list;
100 for(var i = 0; i < allUserBibQueues.length; i++) {
101 if(allUserBibQueues[i].complete() == 'f')
102 userBibQueues.push(allUserBibQueues[i]);
108 vlRetrieveQueueList('auth', null,
110 allUserAuthQueues = list;
111 for(var i = 0; i < allUserAuthQueues.length; i++) {
112 if(allUserAuthQueues[i].complete() == 'f')
113 userAuthQueues.push(allUserAuthQueues[i]);
123 dojo.addOnLoad(vlInit);
126 // fetch the bib and authority attribute definitions
128 function vlFetchBibAttrDefs(postcomplete) {
130 fieldmapper.standardRequest(
131 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
133 params: [authtoken, {id:{'!=':null}}],
134 onresponse: function(r) {
135 var def = r.recv().content();
136 if(e = openils.Event.parse(def[0]))
138 bibAttrDefs.push(def);
140 oncomplete: function() {
141 bibAttrDefs = bibAttrDefs.sort(
143 if(a.id() > b.id()) return 1;
144 if(a.id() < b.id()) return -1;
154 function vlFetchAuthAttrDefs(postcomplete) {
156 fieldmapper.standardRequest(
157 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
159 params: [authtoken, {id:{'!=':null}}],
160 onresponse: function(r) {
161 var def = r.recv().content();
162 if(e = openils.Event.parse(def[0]))
164 authAttrDefs.push(def);
166 oncomplete: function() {
167 authAttrDefs = authAttrDefs.sort(
169 if(a.id() > b.id()) return 1;
170 if(a.id() < b.id()) return -1;
180 function vlRetrieveQueueList(type, filter, onload) {
181 type = (type == 'bib') ? type : 'authority';
182 fieldmapper.standardRequest(
183 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
185 params: [authtoken, null, filter],
186 oncomplete: function(r) {
187 var list = r.recv().content();
188 if(e = openils.Event.parse(list[0]))
197 function displayGlobalDiv(id) {
198 for(var i = 0; i < globalDivs.length; i++) {
200 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
202 alert('please define div ' + globalDivs[i]);
205 dojo.style(dojo.byId(id),'display','block');
208 function runStartupCommands() {
209 currentQueueId = cgi.param('qid');
210 currentType = cgi.param('qtype');
211 dojo.style('vl-nav-bar', 'visibility', 'visible');
213 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
218 * asynchronously upload a file of MARC records
220 function uploadMARC(onload){
221 dojo.byId('vl-upload-status-count').innerHTML = '0';
222 dojo.byId('vl-ses-input').value = authtoken;
223 displayGlobalDiv('vl-marc-upload-status-div');
224 dojo.io.iframe.send({
228 form: dojo.byId('vl-marc-upload-form'),
229 handle: function(data,ioArgs){
230 var content = data.documentElement.textContent;
237 * Creates a new vandelay queue
239 function createQueue(queueName, type, onload) {
240 var name = (type=='bib') ? 'bib' : 'authority';
241 var method = 'open-ils.vandelay.'+ name +'_queue.create'
242 fieldmapper.standardRequest(
243 ['open-ils.vandelay', method],
245 params: [authtoken, queueName, null, name],
246 oncomplete : function(r) {
247 var queue = r.recv().content();
248 if(e = openils.Event.parse(queue))
257 * Tells vandelay to pull a batch of records from the cache and explode them
258 * out into the vandelay tables
260 function processSpool(key, queueId, type, onload) {
261 fieldmapper.standardRequest(
262 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
264 params: [authtoken, key, queueId],
265 onresponse : function(r) {
266 var resp = r.recv().content();
267 if(e = openils.Event.parse(resp))
269 dojo.byId('vl-upload-status-count').innerHTML = resp;
271 oncomplete : function(r) {onload();}
276 function retrieveQueuedRecords(type, queueId, onload) {
277 displayGlobalDiv('vl-generic-progress');
279 queuedRecordsMap = {};
280 currentOverlayRecordsMap = {};
281 currentOverlayRecordsMapGid = {};
282 selectableGridRecords = {};
283 resetVlQueueGridLayout();
285 var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
286 if(vlQueueGridShowMatches.checked)
287 method = method.replace('records', 'records.matches');
289 var limit = parseInt(vlQueueDisplayLimit.getValue());
290 var offset = limit * parseInt(vlQueueDisplayPage.getValue()-1);
292 var params = [authtoken, queueId, {clear_marc: 1, offset: offset, limit: limit}];
293 if(vlQueueGridShowNonImport.checked)
294 params[2].non_imported = 1;
296 fieldmapper.standardRequest(
297 ['open-ils.vandelay', method],
301 onresponse: function(r) {
302 console.log("ONREPONSE");
303 var rec = r.recv().content();
304 if(e = openils.Event.parse(rec))
306 console.log("got record " + rec.id());
307 queuedRecords.push(rec);
308 queuedRecordsMap[rec.id()] = rec;
311 oncomplete: function(r){
312 var recs = r.recv().content();
313 if(e = openils.Event.parse(recs[0]))
315 for(var i = 0; i < recs.length; i++) {
317 queuedRecords.push(rec);
318 queuedRecordsMap[rec.id()] = rec;
326 function vlLoadMatchUI(recId) {
327 displayGlobalDiv('vl-generic-progress');
328 var matches = queuedRecordsMap[recId].matches();
330 currentImportRecId = recId;
331 for(var i = 0; i < matches.length; i++)
332 records.push(matches[i].eg_record());
334 var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
335 var params = [records];
336 if(currentType == 'auth') {
337 retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
338 parmas = [authtoken, records, {clear_marc:1}];
341 fieldmapper.standardRequest(
345 oncomplete: function(r) {
346 var recs = r.recv().content();
347 if(e = openils.Event.parse(recs))
351 displayGlobalDiv('vl-match-div');
352 resetVlMatchGridLayout();
353 currentMatchedRecords = recs;
354 if(!vlMatchGrid.structure)
355 vlMatchGrid.setStructure(vlMatchGridLayout);
357 // build the data store of records with match information
358 var dataStore = bre.toStoreData(recs, null,
359 {virtualFields:['dest_matchpoint', 'src_matchpoint', '_id']});
360 dataStore.identifier = '_id';
362 var matchSeenMap = {};
364 for(var i = 0; i < dataStore.items.length; i++) {
365 var item = dataStore.items[i];
366 item._id = i; // just need something unique
367 for(var j = 0; j < matches.length; j++) {
368 var match = matches[j];
369 if(match.eg_record() == item.id && !matchSeenMap[match.id()]) {
370 item.dest_matchpoint = match.field_type();
371 var attr = getRecAttrFromMatch(queuedRecordsMap[recId], match);
372 item.src_matchpoint = getRecAttrDefFromAttr(attr, currentType).code();
373 matchSeenMap[match.id()] = 1;
379 // now populate the grid
380 vlPopulateMatchGrid(vlMatchGrid, dataStore);
386 function vlPopulateMatchGrid(grid, data) {
387 var store = new dojo.data.ItemFileReadStore({data:data});
388 var model = new dojox.grid.data.DojoData(
389 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
390 grid.setModel(model);
394 function showMe(id) {
395 dojo.style(dojo.byId(id), 'display', 'block');
397 function hideMe(id) {
398 dojo.style(dojo.byId(id), 'display', 'none');
402 function vlLoadMARCHtml(recId, inCat, oncomplete) {
403 dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
404 displayGlobalDiv('vl-generic-progress');
406 var params = [recId, 1];
409 hideMe('vl-marc-html-edit-button'); // don't show marc editor button
410 dijit.byId('vl-marc-html-edit-button').onClick = function(){}
411 api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
412 if(currentType == 'auth')
413 api = ['open-ils.search', 'open-ils.search.authority.to_html'];
415 showMe('vl-marc-html-edit-button'); // plug in the marc editor button
416 dijit.byId('vl-marc-html-edit-button').onClick =
417 function() {vlLoadMarcEditor(currentType, recId, oncomplete);};
418 params = [authtoken, recId];
419 api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
420 if(currentType == 'auth')
421 api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
424 fieldmapper.standardRequest(
428 oncomplete: function(r) {
429 displayGlobalDiv('vl-marc-html-div');
430 var html = r.recv().content();
431 dojo.byId('vl-marc-record-html').innerHTML = html;
439 function getRecMatchesFromAttrCode(rec, attrCode) {
441 var attr = getRecAttrFromCode(rec, attrCode);
442 for(var j = 0; j < rec.matches().length; j++) {
443 var match = rec.matches()[j];
444 if(match.matched_attr() == attr.id())
451 function getRecAttrFromMatch(rec, match) {
452 for(var i = 0; i < rec.attributes().length; i++) {
453 var attr = rec.attributes()[i];
454 if(attr.id() == match.matched_attr())
459 function getRecAttrDefFromAttr(attr, type) {
460 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
461 for(var i = 0; i < defs.length; i++) {
463 if(def.id() == attr.field())
468 function getRecAttrFromCode(rec, attrCode) {
469 var defId = attrDefMap[currentType][attrCode];
470 var attrs = rec.attributes();
471 for(var i = 0; i < attrs.length; i++) {
473 if(attr.field() == defId)
479 function vlGetViewMatches(rowIdx) {
480 var data = this.grid.model.getRow(rowIdx);
482 var rec = queuedRecordsMap[data.id];
483 if(rec.matches().length > 0)
484 return this.value.replace('RECID', data.id);
488 function getAttrValue(rowIdx) {
489 var data = this.grid.model.getRow(rowIdx);
491 var attrCode = this.field.split('.')[1];
492 var rec = queuedRecordsMap[data.id];
493 var attr = getRecAttrFromCode(rec, attrCode);
494 return (attr) ? attr.attr_value() : '';
497 function vlGetDateTimeField(rowIdx) {
498 data = this.grid.model.getRow(rowIdx);
500 if(!data[this.field]) return '';
501 var date = dojo.date.stamp.fromISOString(data[this.field]);
502 return dojo.date.locale.format(date, {selector:'date'});
505 function vlGetCreator(rowIdx) {
506 data = this.grid.model.getRow(rowIdx);
508 var id = data.creator;
510 return userCache[id].usrname();
511 var user = fieldmapper.standardRequest(
512 ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
513 if(e = openils.Event.parse(user))
515 userCache[id] = user;
516 return user.usrname();
519 function vlGetViewMARC(rowIdx) {
520 data = this.grid.model.getRow(rowIdx);
522 return this.value.replace('RECID', data.id);
525 function vlGetOverlayTargetSelector(rowIdx) {
526 data = this.grid.model.getRow(rowIdx);
528 var value = this.value.replace(/GRIDID/g, data._id);
529 value = value.replace(/RECID/g, currentImportRecId);
530 value = value.replace(/ID/g, data.id);
531 if(data._id == currentOverlayRecordsMapGid[currentImportRecId])
532 return value.replace('/>', 'checked="checked"/>');
538 * see if the user has enabled overlays for the current match set and,
539 * if so, map the current import record to the overlay target.
541 function vlHandleOverlayTargetSelected(recId, gridId) {
542 var noneSelected = true;
543 var checkboxes = dojo.query('[name=vl-overlay-target-'+currentImportRecId+']');
544 for(var i = 0; i < checkboxes.length; i++) {
545 var checkbox = checkboxes[i];
546 var matchRecId = checkbox.getAttribute('match');
547 var gid = checkbox.getAttribute('gridid');
548 if(checkbox.checked) {
549 if(matchRecId == recId && gid == gridId) {
550 noneSelected = false;
551 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
552 currentOverlayRecordsMapGid[currentImportRecId] = gid;
553 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
554 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
556 checkbox.checked = false;
562 delete currentOverlayRecordsMap[currentImportRecId];
563 delete currentOverlayRecordsMapGid[currentImportRecId];
564 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
565 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = '';
569 var valLastQueueType = null;
570 function buildRecordGrid(type) {
571 displayGlobalDiv('vl-queue-div');
573 if(valLastQueueType != type) {
574 valLastQueueType = type;
575 resetVlQueueGridLayout();
576 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
577 attrDefMap[type] = {};
578 for(var i = 0; i < defs.length; i++) {
580 attrDefMap[type][def.code()] = def.id();
582 name:def.description(),
583 field:'attr.' + def.code(),
585 selectableColumn:true
587 vlQueueGridLayout[0].cells[0].push(col);
593 storeData = vqbr.toStoreData(queuedRecords);
595 storeData = vqar.toStoreData(queuedRecords);
597 var store = new dojo.data.ItemFileReadStore({data:storeData});
598 var model = new dojox.grid.data.DojoData(
599 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
600 vlQueueGrid.setModel(model);
603 if(vlQueueGridColumePicker[type]) {
604 vlQueueGrid.update();
606 vlQueueGridColumePicker[type] =
607 new openils.GridColumnPicker(vlQueueGridColumePickerDialog,
608 vlQueueGrid, vlQueueGridLayout, authtoken, 'vandelay.queue.'+type);
609 vlQueueGridColumePicker[type].load();
613 function vlQueueGridPrevPage() {
614 var page = parseInt(vlQueueDisplayPage.getValue());
616 vlQueueDisplayPage.setValue(page - 1);
617 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
620 function vlQueueGridNextPage() {
621 vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
622 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
625 function vlDeleteQueue(type, queueId, onload) {
626 fieldmapper.standardRequest(
627 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
629 params: [authtoken, queueId],
630 oncomplete: function(r) {
631 var resp = r.recv().content();
632 if(e = openils.Event.parse(resp))
641 function vlQueueGridDrawSelectBox(rowIdx) {
642 var data = this.grid.model.getRow(rowIdx);
644 var domId = 'vl-record-list-selected-' +data.id;
645 selectableGridRecords[domId] = data.id;
646 return "<div><input type='checkbox' id='"+domId+"'/></div>";
649 function vlSelectAllQueueGridRecords() {
650 for(var id in selectableGridRecords)
651 dojo.byId(id).checked = true;
653 function vlSelectNoQueueGridRecords() {
654 for(var id in selectableGridRecords)
655 dojo.byId(id).checked = false;
657 function vlToggleQueueGridSelect() {
658 if(dojo.byId('vl-queue-grid-row-selector').checked)
659 vlSelectAllQueueGridRecords();
661 vlSelectNoQueueGridRecords();
664 var handleRetrieveRecords = function() {
665 buildRecordGrid(currentType);
666 vlFetchQueueSummary(currentQueueId, currentType,
668 dojo.byId('vl-queue-summary-name').innerHTML = summary.queue.name();
669 dojo.byId('vl-queue-summary-total-count').innerHTML = summary.total +'';
670 dojo.byId('vl-queue-summary-import-count').innerHTML = summary.imported + '';
675 function vlFetchQueueSummary(qId, type, onload) {
676 fieldmapper.standardRequest(
677 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.summary.retrieve'],
679 params: [authtoken, qId],
680 oncomplete : function(r) {
681 var summary = r.recv().content();
682 if(e = openils.Event.parse(summary))
684 return onload(summary);
691 function vlImportSelectedRecords() {
692 displayGlobalDiv('vl-generic-progress-with-total');
695 for(var id in selectableGridRecords) {
696 if(dojo.byId(id).checked) {
697 var recId = selectableGridRecords[id];
698 var rec = queuedRecordsMap[recId];
699 if(!rec.import_time())
704 fieldmapper.standardRequest(
705 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
707 params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
708 onresponse: function(r) {
709 var resp = r.recv().content();
710 if(e = openils.Event.parse(resp))
713 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
715 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
722 function vlImportAllRecords() {
723 vlImportRecordQueue(currentType, currentQueueId, false,
724 function(){displayGlobalDiv('vl-queue-div');});
727 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
728 displayGlobalDiv('vl-generic-progress-with-total');
729 var method = 'open-ils.vandelay.bib_queue.import';
731 method = method.replace('import', 'nomatch.import');
733 method = method.replace('bib', 'auth');
735 fieldmapper.standardRequest(
736 ['open-ils.vandelay', method],
738 params: [authtoken, queueId],
739 onresponse: function(r) {
740 var resp = r.recv().content();
741 if(e = openils.Event.parse(resp))
743 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
745 oncomplete: function() {onload();}
752 * Create queue, upload MARC, process spool, load the newly created queue
754 function batchUpload() {
755 var queueName = dijit.byId('vl-queue-name').getValue();
756 currentType = dijit.byId('vl-record-type').getValue();
758 var handleProcessSpool = function() {
759 console.log('records uploaded and spooled');
760 if(vlUploadQueueAutoImport.checked) {
761 vlImportRecordQueue(currentType, currentQueueId, true,
763 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
767 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
771 var handleUploadMARC = function(key) {
772 console.log('marc uploaded');
773 dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
774 processSpool(key, currentQueueId, currentType, handleProcessSpool);
777 var handleCreateQueue = function(queue) {
778 console.log('queue created ' + queue.name());
779 currentQueueId = queue.id();
780 uploadMARC(handleUploadMARC);
783 if(vlUploadQueueSelector.getValue() && !queueName) {
784 currentQueueId = vlUploadQueueSelector.getValue();
785 console.log('adding records to existing queue ' + currentQueueId);
786 uploadMARC(handleUploadMARC);
788 createQueue(queueName, currentType, handleCreateQueue);
793 function vlFleshQueueSelect(selector, type) {
794 var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
795 selector.store = new dojo.data.ItemFileReadStore({data:data});
796 selector.setValue(null);
797 selector.setDisplayedValue('');
799 selector.setValue(data[0].id());
802 function vlShowUploadForm() {
803 displayGlobalDiv('vl-marc-upload-div');
804 vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
807 function vlShowQueueSelect() {
808 displayGlobalDiv('vl-queue-select-div');
809 vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
812 function vlFetchQueueFromForm() {
813 currentType = vlQueueSelectType.getValue();
814 currentQueueId = vlQueueSelectQueueList.getValue();
815 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
818 function vlOpenMarcEditWindow(rec, postReloadHTMLHandler) {
820 To run in Firefox directly, must set signed.applets.codebase_principal_support
821 to true in about:config
823 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
824 win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
827 // after the record is saved, reload the HTML display
828 var stat = r.recv().content();
829 if(e = openils.Event.parse(stat))
831 alert(dojo.byId('vl-marc-edit-complete-label').innerHTML);
833 vlLoadMARCHtml(rec.id(), false, postReloadHTMLHandler);
837 record : {marc : rec.marc()},
839 label: dojo.byId('vl-marc-edit-save-label').innerHTML,
840 func: function(xmlString) {
841 var method = 'open-ils.permacrud.update.' + rec.classname;
843 fieldmapper.standardRequest(
844 ['open-ils.permacrud', method],
846 params: [authtoken, rec],
855 function vlLoadMarcEditor(type, recId, postReloadHTMLHandler) {
856 var method = 'open-ils.permacrud.search.vqbr';
857 if(currentType != 'bib')
858 method = method.replace(/vqbr/,'vqar');
860 fieldmapper.standardRequest(
861 ['open-ils.permacrud', method],
863 params: [authtoken, {id : recId}],
864 oncomplete: function(r) {
865 var rec = r.recv().content();
866 if(e = openils.Event.parse(rec))
868 vlOpenMarcEditWindow(rec, postReloadHTMLHandler);
876 //------------------------------------------------------------
879 // attribute-editor global variables
881 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
882 var ATTR_EDIT_ID = null; // id of current 'edit' attribute
883 var ATTR_EDIT_GROUP = 'bib'; // bib-attrs or auth-attrs
885 function vlAttrEditorInit() {
886 // set up tooltips on the edit form
887 connectTooltip('attr-editor-tags');
888 connectTooltip('attr-editor-subfields');
891 function vlShowAttrEditor() {
892 displayGlobalDiv('vl-attr-editor-div');
893 loadAttrEditorGrid();
894 idHide('vl-generic-progress');
897 function setAttrEditorGroup(groupName) {
898 // put us into 'bib'-attr or 'auth'-attr mode.
899 if (ATTR_EDIT_GROUP != groupName) {
900 ATTR_EDIT_GROUP = groupName;
901 loadAttrEditorGrid();
905 function onAttrEditorOpen() {
906 // the "bars" have the create/update/cancel/etc. buttons.
907 var create_bar = document.getElementById('attr-editor-create-bar');
908 var update_bar = document.getElementById('attr-editor-update-bar');
909 if (ATTR_EDITOR_IN_UPDATE_MODE) {
910 update_bar.style.display='table-row';
911 create_bar.style.display='none';
912 // hide the dropdown-button
913 idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
915 dijit.byId('attr-editor-dialog').reset();
916 create_bar.style.display='table-row';
917 update_bar.style.display='none';
921 function onAttrEditorClose() {
922 // reset the form to a "create" form. (We may have borrowed it for editing.)
923 ATTR_EDITOR_IN_UPDATE_MODE = false;
924 // show the dropdown-button
925 idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
928 function loadAttrEditorGrid() {
929 var _data = (ATTR_EDIT_GROUP == 'auth') ?
930 vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
932 var store = new dojo.data.ItemFileReadStore({data:_data});
933 var model = new dojox.grid.data.DojoData(
934 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
935 attrEditorGrid.setModel(model);
936 attrEditorGrid.setStructure(vlAttrGridLayout);
937 attrEditorGrid.onRowClick = onAttrEditorClick;
938 attrEditorGrid.update();
941 function attrGridGetTag(n) {
942 // grid helper: return the tags from the row's xpath column.
943 var xp = this.grid.model.getRow(n);
944 return xp && xpathParser.parse(xp.xpath).tags;
947 function attrGridGetSubfield(n) {
948 // grid helper: return the subfields from the row's xpath column.
949 var xp = this.grid.model.getRow(n);
950 return xp && xpathParser.parse(xp.xpath).subfields;
953 function onAttrEditorClick(evt) {
954 var row = attrEditorGrid.model.getRow(evt.rowIndex);
955 ATTR_EDIT_ID = row.id;
956 ATTR_EDITOR_IN_UPDATE_MODE = true;
958 // populate the popup editor.
959 dojo.byId('attr-editor-code').value = row.code;
960 dojo.byId('attr-editor-description').value = row.description;
961 var parsed_xpath = xpathParser.parse(row.xpath);
962 dojo.byId('attr-editor-tags').value = parsed_xpath.tags;
963 dojo.byId('attr-editor-subfields').value = parsed_xpath.subfields;
964 dojo.byId('attr-editor-identifier').value = (row.ident ? 'True':'False');
965 dojo.byId('attr-editor-xpath').value = row.xpath;
966 dojo.byId('attr-editor-remove').value = row.remove;
968 // set up UI for editing
969 dojo.byId('vl-create-attr-editor-button').click();
972 function vlSaveAttrDefinition(data) {
973 idHide('vl-attr-editor-div');
974 idShow('vl-generic-progress');
976 data.id = ATTR_EDIT_ID;
978 // this ought to honour custom xpaths, but overwrite xpaths
979 // derived from tags/subfields.
980 if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
981 var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
985 // build up our permacrud params. Key variables here are
986 // "create or update" and "bib or auth".
988 var isAuth = (ATTR_EDIT_GROUP == 'auth');
989 var isCreate = (ATTR_EDIT_ID == null);
990 var rad = isAuth ? new vqarad() : new vqbrad() ;
991 var method = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.')
992 + (isAuth ? 'vqarad' : 'vqbrad');
993 var _data = rad.fromStoreItem(data);
997 fieldmapper.standardRequest(
998 ['open-ils.permacrud', method],
1000 params: [authtoken, _data ],
1001 onresponse: function(r) { },
1002 oncomplete: function(r) {
1003 attrEditorFetchAttrDefs(vlShowAttrEditor);
1004 ATTR_EDIT_ID = null;
1006 onerror: function(r) {
1007 alert('vlSaveAttrDefinition comms error: ' + r);
1013 function attrEditorFetchAttrDefs(callback) {
1014 var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
1015 return fn(callback);
1018 function vlAttrDelete() {
1019 idHide('vl-attr-editor-div');
1020 idShow('vl-generic-progress');
1022 var isAuth = (ATTR_EDIT_GROUP == 'auth');
1023 var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
1024 var rad = isAuth ? new vqarad() : new vqbrad() ;
1025 fieldmapper.standardRequest(
1026 ['open-ils.permacrud', method],
1028 params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
1029 oncomplete: function() {
1030 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
1031 attrEditorFetchAttrDefs(vlShowAttrEditor);
1032 ATTR_EDIT_ID = null;
1034 onerror: function(r) {
1035 alert('vlAttrDelete comms error: ' + r);
1041 // ------------------------------------------------------------
1042 // utilities for attribute editors
1044 // dom utilities (maybe dojo does these, and these should be replaced)
1046 function idStyle(obId, k, v) { document.getElementById(obId).style[k] = v; }
1047 function idShow(obId) { idStyle(obId, 'display', 'block'); }
1048 function idHide(obId) { idStyle(obId, 'display' , 'none'); }
1050 function connectTooltip(fieldId) {
1051 // Given an element id, look up a tooltip element in the doc (same
1052 // id with a '-tip' suffix) and associate the two. Maybe dojo has
1053 // a better way to do this?
1054 var fld = dojo.byId(fieldId);
1055 var tip = dojo.byId(fieldId + '-tip');
1056 dojo.connect(fld, 'onfocus', function(evt) {
1057 dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1058 dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1063 var xpathParser = new openils.MarcXPathParser();
1065 function tagAndSubFieldsToXpath(tags, subfields) {
1066 // given tags, and subfields, build up an XPath.
1069 'tags':tags.match(/[\d]+/g),
1070 'subfields':subfields.match(/[a-zA-z]/g) };
1071 return xpathParser.compile(parts);
1073 return {'parts':null, 'tags':null, 'error':err};
1077 function looksLikeDerivedXpath(path) {
1078 // Does this path look like it was derived from tags and subfields?
1079 var parsed = xpathParser.parse(path);
1080 if (parsed.tags == null)
1082 var compiled = xpathParser.compile(parsed);
1083 return (path == compiled);
1086 // amazing xpath-util unit-tests
1087 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]')) alert('vandelay xpath-utility error');
1088 if ( looksLikeDerivedXpath('ba-boo-ba-boo!')) alert('vandelay xpath-utility error');