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.LayoutContainer");
21 dojo.require("dijit.layout.ContentPane");
22 dojo.require("dijit.layout.TabContainer");
23 dojo.require("dijit.layout.LayoutContainer");
24 dojo.require('dijit.form.Button');
25 dojo.require('dijit.Toolbar');
26 dojo.require('dijit.Tooltip');
27 dojo.require('dijit.Menu');
28 dojo.require("dijit.Dialog");
29 dojo.require("dojo.cookie");
30 dojo.require("dojox.grid.Grid");
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.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',
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 currentImportRecId; // when analyzing matches, this is the current import record
70 var userBibQueues = []; // only non-complete queues
71 var userAuthQueues = []; // only non-complete queues
73 var allUserAuthQueues;
74 var selectableGridRecords;
75 var cgi = new openils.CGI();
76 var vlQueueGridColumePicker;
82 authtoken = dojo.cookie('ses') || cgi.param('ses');
83 var initNeeded = 4; // how many async responses do we need before we're init'd
84 var initCount = 0; // how many async reponses we've received
86 function checkInitDone() {
88 if(initCount == initNeeded)
92 // Fetch the bib and authority attribute definitions
93 vlFetchBibAttrDefs(function () { checkInitDone(); });
94 vlFetchAuthAttrDefs(function () { checkInitDone(); });
96 vlRetrieveQueueList('bib', null,
98 allUserBibQueues = list;
99 for(var i = 0; i < allUserBibQueues.length; i++) {
100 if(allUserBibQueues[i].complete() == 'f')
101 userBibQueues.push(allUserBibQueues[i]);
107 vlRetrieveQueueList('auth', null,
109 allUserAuthQueues = list;
110 for(var i = 0; i < allUserAuthQueues.length; i++) {
111 if(allUserAuthQueues[i].complete() == 'f')
112 userAuthQueues.push(allUserAuthQueues[i]);
122 dojo.addOnLoad(vlInit);
125 // fetch the bib and authority attribute definitions
127 function vlFetchBibAttrDefs(postcomplete) {
129 fieldmapper.standardRequest(
130 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
132 params: [authtoken, {id:{'!=':null}}],
133 onresponse: function(r) {
134 var def = r.recv().content();
135 if(e = openils.Event.parse(def[0]))
137 bibAttrDefs.push(def);
139 oncomplete: function() {
140 bibAttrDefs = bibAttrDefs.sort(
142 if(a.id() > b.id()) return 1;
143 if(a.id() < b.id()) return -1;
153 function vlFetchAuthAttrDefs(postcomplete) {
155 fieldmapper.standardRequest(
156 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
158 params: [authtoken, {id:{'!=':null}}],
159 onresponse: function(r) {
160 var def = r.recv().content();
161 if(e = openils.Event.parse(def[0]))
163 authAttrDefs.push(def);
165 oncomplete: function() {
166 authAttrDefs = authAttrDefs.sort(
168 if(a.id() > b.id()) return 1;
169 if(a.id() < b.id()) return -1;
179 function vlRetrieveQueueList(type, filter, onload) {
180 type = (type == 'bib') ? type : 'authority';
181 fieldmapper.standardRequest(
182 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
184 params: [authtoken, null, filter],
185 oncomplete: function(r) {
186 var list = r.recv().content();
187 if(e = openils.Event.parse(list[0]))
196 function displayGlobalDiv(id) {
197 for(var i = 0; i < globalDivs.length; i++) {
199 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
201 alert('please define div ' + globalDivs[i]);
204 dojo.style(dojo.byId(id),'display','block');
207 function runStartupCommands() {
208 currentQueueId = cgi.param('qid');
209 currentType = cgi.param('qtype');
211 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
212 dojo.style('vl-nav-bar', 'visibility', 'visible');
217 * asynchronously upload a file of MARC records
219 function uploadMARC(onload){
220 dojo.byId('vl-upload-status-count').innerHTML = '0';
221 dojo.byId('vl-ses-input').value = authtoken;
222 displayGlobalDiv('vl-marc-upload-status-div');
223 dojo.io.iframe.send({
227 form: dojo.byId('vl-marc-upload-form'),
228 handle: function(data,ioArgs){
229 var content = data.documentElement.textContent;
236 * Creates a new vandelay queue
238 function createQueue(queueName, type, onload) {
239 fieldmapper.standardRequest(
240 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
242 params: [authtoken, queueName, null, type],
243 oncomplete : function(r) {
244 var queue = r.recv().content();
245 if(e = openils.Event.parse(queue))
254 * Tells vandelay to pull a batch of records from the cache and explode them
255 * out into the vandelay tables
257 function processSpool(key, queueId, type, onload) {
258 fieldmapper.standardRequest(
259 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
261 params: [authtoken, key, queueId],
262 onresponse : function(r) {
263 var resp = r.recv().content();
264 if(e = openils.Event.parse(resp))
266 dojo.byId('vl-upload-status-count').innerHTML = resp;
268 oncomplete : function(r) {onload();}
273 function retrieveQueuedRecords(type, queueId, onload) {
274 displayGlobalDiv('vl-generic-progress');
276 queuedRecordsMap = {};
277 currentOverlayRecordsMap = {};
278 selectableGridRecords = {};
279 resetVlQueueGridLayout();
281 var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
282 if(vlQueueGridShowMatches.checked)
283 method = method.replace('records', 'records.matches');
285 var limit = parseInt(vlQueueDisplayLimit.getValue());
286 var offset = limit * parseInt(vlQueueDisplayPage.getValue()-1);
288 fieldmapper.standardRequest(
289 ['open-ils.vandelay', method],
291 params: [authtoken, queueId,
297 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
298 onresponse: function(r) {
299 var rec = r.recv().content();
300 if(e = openils.Event.parse(rec))
302 queuedRecords.push(rec);
303 queuedRecordsMap[rec.id()] = rec;
306 oncomplete: function(r){
307 var recs = r.recv().content();
308 if(e = openils.Event.parse(recs[0]))
310 for(var i = 0; i < recs.length; i++) {
312 queuedRecords.push(rec);
313 queuedRecordsMap[rec.id()] = rec;
321 //function vlLoadMatchUI(recId, attrCode) {
322 function vlLoadMatchUI(recId) {
323 displayGlobalDiv('vl-generic-progress');
324 //var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
325 var matches = queuedRecordsMap[recId].matches();
327 currentImportRecId = recId;
328 for(var i = 0; i < matches.length; i++)
329 records.push(matches[i].eg_record());
331 var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
332 var params = [records];
333 if(currentType == 'auth') {
334 retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
335 parmas = [authtoken, records, {clear_marc:1}];
338 fieldmapper.standardRequest(
342 oncomplete: function(r) {
343 var recs = r.recv().content();
344 if(e = openils.Event.parse(recs))
348 displayGlobalDiv('vl-match-div');
349 resetVlMatchGridLayout();
350 currentMatchedRecords = recs;
351 vlMatchGrid.setStructure(vlMatchGridLayout);
353 // build the data store of records with match information
354 var dataStore = bre.toStoreData(recs, null,
355 {virtualFields:['dest_matchpoint', 'src_matchpoint', '_id']});
356 dataStore.identifier = '_id';
359 for(var i = 0; i < dataStore.items.length; i++) {
360 var item = dataStore.items[i];
361 item._id = i; // just need something unique
362 for(var j = 0; j < matches.length; j++) {
363 var match = matches[j];
364 if(match.eg_record() == item.id) {
365 item.dest_matchpoint = match.field_type();
366 var attr = getRecAttrFromMatch(queuedRecordsMap[recId], match);
367 //item.src_matchpoint = getRecAttrDefFromAttr(attr, currentType).description();
368 item.src_matchpoint = getRecAttrDefFromAttr(attr, currentType).code();
373 // now populate the grid
374 vlPopulateMatchGrid(vlMatchGrid, dataStore);
380 function vlPopulateMatchGrid(grid, data) {
381 var store = new dojo.data.ItemFileReadStore({data:data});
382 var model = new dojox.grid.data.DojoData(
383 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
384 grid.setModel(model);
389 function vlLoadMARCHtml(recId, inCat, oncomplete) {
390 dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
391 displayGlobalDiv('vl-generic-progress');
393 var params = [recId, 1];
395 api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
396 if(currentType == 'auth')
397 api = ['open-ils.search', 'open-ils.search.authority.to_html'];
399 params = [authtoken, recId];
400 api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
401 if(currentType == 'auth')
402 api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
404 fieldmapper.standardRequest(
408 oncomplete: function(r) {
409 displayGlobalDiv('vl-marc-html-div');
410 var html = r.recv().content();
411 dojo.byId('vl-marc-record-html').innerHTML = html;
419 function getRecMatchesFromAttrCode(rec, attrCode) {
421 var attr = getRecAttrFromCode(rec, attrCode);
422 for(var j = 0; j < rec.matches().length; j++) {
423 var match = rec.matches()[j];
424 if(match.matched_attr() == attr.id())
431 function getRecAttrFromMatch(rec, match) {
432 for(var i = 0; i < rec.attributes().length; i++) {
433 var attr = rec.attributes()[i];
434 if(attr.id() == match.matched_attr())
439 function getRecAttrDefFromAttr(attr, type) {
440 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
441 for(var i = 0; i < defs.length; i++) {
443 if(def.id() == attr.field())
448 function getRecAttrFromCode(rec, attrCode) {
449 var defId = attrDefMap[attrCode];
450 var attrs = rec.attributes();
451 for(var i = 0; i < attrs.length; i++) {
453 if(attr.field() == defId)
459 function vlGetViewMatches(rowIdx) {
460 var data = this.grid.model.getRow(rowIdx);
462 var rec = queuedRecordsMap[data.id];
463 if(rec.matches().length > 0)
464 return this.value.replace('RECID', data.id);
468 function getAttrValue(rowIdx) {
469 var data = this.grid.model.getRow(rowIdx);
471 var attrCode = this.field.split('.')[1];
472 var rec = queuedRecordsMap[data.id];
473 var attr = getRecAttrFromCode(rec, attrCode);
474 return (attr) ? attr.attr_value() : '';
477 function vlGetDateTimeField(rowIdx) {
478 data = this.grid.model.getRow(rowIdx);
480 if(!data[this.field]) return '';
481 var date = dojo.date.stamp.fromISOString(data[this.field]);
482 return dojo.date.locale.format(date, {selector:'date'});
485 function vlGetCreator(rowIdx) {
486 data = this.grid.model.getRow(rowIdx);
488 var id = data.creator;
490 return userCache[id].usrname();
491 var user = fieldmapper.standardRequest(
492 ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
493 if(e = openils.Event.parse(user))
495 userCache[id] = user;
496 return user.usrname();
499 function vlGetViewMARC(rowIdx) {
500 data = this.grid.model.getRow(rowIdx);
502 return this.value.replace('RECID', data.id);
505 function vlGetOverlayTargetSelector(rowIdx) {
506 data = this.grid.model.getRow(rowIdx);
508 var value = this.value.replace('ID', data.id);
509 var overlay = currentOverlayRecordsMap[currentImportRecId];
510 if(overlay && overlay == data.id)
511 value = value.replace('/>', 'checked="checked"/>');
517 * see if the user has enabled overlays for the current match set and,
518 * if so, map the current import record to the overlay target.
520 function vlHandleOverlayTargetSelected() {
521 if(vlOverlayTargetEnable.checked) {
522 for(var i = 0; i < currentMatchedRecords.length; i++) {
523 var matchRecId = currentMatchedRecords[i].id();
524 if(dojo.byId('vl-overlay-target-'+matchRecId).checked) {
525 console.log("found overlay target " + matchRecId);
526 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
527 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
528 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
533 delete currentOverlayRecordsMap[currentImportRecId];
534 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
538 var vlQueueGridBuilt = false;
539 function buildRecordGrid(type) {
540 displayGlobalDiv('vl-queue-div');
542 currentOverlayRecordsMap = {};
544 if(!vlQueueGridBuilt) {
545 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
546 for(var i = 0; i < defs.length; i++) {
548 attrDefMap[def.code()] = def.id();
550 name:def.description(),
551 field:'attr.' + def.code(),
553 selectableColumn:true
555 vlQueueGridLayout[0].cells[0].push(col);
557 vlQueueGridBuilt = true;
562 storeData = vqbr.toStoreData(queuedRecords);
564 storeData = vqar.toStoreData(queuedRecords);
566 var store = new dojo.data.ItemFileReadStore({data:storeData});
567 var model = new dojox.grid.data.DojoData(
568 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
570 vlQueueGrid.setModel(model);
571 if(vlQueueGridColumePicker)
572 vlQueueGrid.setStructure(vlQueueGridColumePicker.structure);
574 vlQueueGrid.setStructure(vlQueueGridLayout);
575 vlQueueGrid.update();
577 if(!vlQueueGridColumePicker) {
578 vlQueueGridColumePicker =
579 new openils.GridColumnPicker(vlQueueGridColumePickerDialog, vlQueueGrid);
583 function vlQueueGridPrevPage() {
584 var page = parseInt(vlQueueDisplayPage.getValue());
586 vlQueueDisplayPage.setValue(page - 1);
587 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
590 function vlQueueGridNextPage() {
591 vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
592 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
595 function vlDeleteQueue(type, queueId, onload) {
596 fieldmapper.standardRequest(
597 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
599 params: [authtoken, queueId],
600 oncomplete: function(r) {
601 var resp = r.recv().content();
602 if(e = openils.Event.parse(resp))
611 function vlQueueGridDrawSelectBox(rowIdx) {
612 var data = this.grid.model.getRow(rowIdx);
614 var domId = 'vl-record-list-selected-' +data.id;
615 selectableGridRecords[domId] = data.id;
616 return "<div><input type='checkbox' id='"+domId+"'/></div>";
619 function vlSelectAllQueueGridRecords() {
620 for(var id in selectableGridRecords)
621 dojo.byId(id).checked = true;
623 function vlSelectNoQueueGridRecords() {
624 for(var id in selectableGridRecords)
625 dojo.byId(id).checked = false;
627 function vlToggleQueueGridSelect() {
628 if(dojo.byId('vl-queue-grid-row-selector').checked)
629 vlSelectAllQueueGridRecords();
631 vlSelectNoQueueGridRecords();
634 var handleRetrieveRecords = function() {
635 buildRecordGrid(currentType);
638 function vlImportSelectedRecords() {
639 displayGlobalDiv('vl-generic-progress-with-total');
642 for(var id in selectableGridRecords) {
643 if(dojo.byId(id).checked) {
644 var recId = selectableGridRecords[id];
645 var rec = queuedRecordsMap[recId];
646 if(!rec.import_time())
651 fieldmapper.standardRequest(
652 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
654 params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
655 onresponse: function(r) {
656 var resp = r.recv().content();
657 if(e = openils.Event.parse(resp))
659 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
661 oncomplete: function() {
662 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
668 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
669 displayGlobalDiv('vl-generic-progress-with-total');
670 var method = 'open-ils.vandelay.bib_queue.import';
672 method = method.replace('import', 'nomatch.import');
674 method = method.replace('bib', 'auth');
676 fieldmapper.standardRequest(
677 ['open-ils.vandelay', method],
679 params: [authtoken, queueId],
680 onresponse: function(r) {
681 var resp = r.recv().content();
682 if(e = openils.Event.parse(resp))
684 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
686 oncomplete: function() {onload();}
693 * Create queue, upload MARC, process spool, load the newly created queue
695 function batchUpload() {
696 var queueName = dijit.byId('vl-queue-name').getValue();
697 currentType = dijit.byId('vl-record-type').getValue();
699 var handleProcessSpool = function() {
700 console.log('records uploaded and spooled');
701 if(vlUploadQueueAutoImport.checked) {
702 vlImportRecordQueue(currentType, currentQueueId, true,
704 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
708 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
712 var handleUploadMARC = function(key) {
713 console.log('marc uploaded');
714 dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
715 processSpool(key, currentQueueId, currentType, handleProcessSpool);
718 var handleCreateQueue = function(queue) {
719 console.log('queue created ' + queue.name());
720 currentQueueId = queue.id();
721 uploadMARC(handleUploadMARC);
724 if(vlUploadQueueSelector.getValue() && !queueName) {
725 currentQueueId = vlUploadQueueSelector.getValue();
726 console.log('adding records to existing queue ' + currentQueueId);
727 uploadMARC(handleUploadMARC);
729 createQueue(queueName, currentType, handleCreateQueue);
734 function vlFleshQueueSelect(selector, type) {
735 var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
736 selector.store = new dojo.data.ItemFileReadStore({data:data});
737 selector.setValue(null);
738 selector.setDisplayedValue('');
740 selector.setValue(data[0].id());
743 function vlShowUploadForm() {
744 displayGlobalDiv('vl-marc-upload-div');
745 vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
748 function vlShowQueueSelect() {
749 displayGlobalDiv('vl-queue-select-div');
750 vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
753 function vlFetchQueueFromForm() {
754 currentType = vlQueueSelectType.getValue();
755 currentQueueId = vlQueueSelectQueueList.getValue();
756 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
761 //------------------------------------------------------------
764 // attribute-editor global variables
766 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
767 var ATTR_EDIT_ID = null; // id of current 'edit' attribute
768 var ATTR_EDIT_GROUP = 'bib'; // bib-attrs or auth-attrs
770 function vlAttrEditorInit() {
771 // set up tooltips on the edit form
772 connectTooltip('attr-editor-tags');
773 connectTooltip('attr-editor-subfields');
776 function vlShowAttrEditor() {
777 displayGlobalDiv('vl-attr-editor-div');
778 loadAttrEditorGrid();
779 idHide('vl-generic-progress');
782 function setAttrEditorGroup(groupName) {
783 // put us into 'bib'-attr or 'auth'-attr mode.
784 if (ATTR_EDIT_GROUP != groupName) {
785 ATTR_EDIT_GROUP = groupName;
786 loadAttrEditorGrid();
790 function onAttrEditorOpen() {
791 // the "bars" have the create/update/cancel/etc. buttons.
792 var create_bar = document.getElementById('attr-editor-create-bar');
793 var update_bar = document.getElementById('attr-editor-update-bar');
794 if (ATTR_EDITOR_IN_UPDATE_MODE) {
795 update_bar.style.display='table-row';
796 create_bar.style.display='none';
797 // hide the dropdown-button
798 idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
800 dijit.byId('attr-editor-dialog').reset();
801 create_bar.style.display='table-row';
802 update_bar.style.display='none';
806 function onAttrEditorClose() {
807 // reset the form to a "create" form. (We may have borrowed it for editing.)
808 ATTR_EDITOR_IN_UPDATE_MODE = false;
809 // show the dropdown-button
810 idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
813 function loadAttrEditorGrid() {
814 var _data = (ATTR_EDIT_GROUP == 'auth') ?
815 vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
817 var store = new dojo.data.ItemFileReadStore({data:_data});
818 var model = new dojox.grid.data.DojoData(
819 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
820 attrEditorGrid.setModel(model);
821 attrEditorGrid.setStructure(vlAttrGridLayout);
822 attrEditorGrid.onRowClick = onAttrEditorClick;
823 attrEditorGrid.update();
826 function attrGridGetTag(n) {
827 // grid helper: return the tags from the row's xpath column.
828 var xp = this.grid.model.getRow(n);
829 return xp && xpathParser.parse(xp.xpath).tags;
832 function attrGridGetSubfield(n) {
833 // grid helper: return the subfields from the row's xpath column.
834 var xp = this.grid.model.getRow(n);
835 return xp && xpathParser.parse(xp.xpath).subfields;
838 function onAttrEditorClick(evt) {
839 var row = attrEditorGrid.model.getRow(evt.rowIndex);
840 ATTR_EDIT_ID = row.id;
841 ATTR_EDITOR_IN_UPDATE_MODE = true;
843 // populate the popup editor.
844 dojo.byId('attr-editor-code').value = row.code;
845 dojo.byId('attr-editor-description').value = row.description;
846 var parsed_xpath = xpathParser.parse(row.xpath);
847 dojo.byId('attr-editor-tags').value = parsed_xpath.tags;
848 dojo.byId('attr-editor-subfields').value = parsed_xpath.subfields;
849 dojo.byId('attr-editor-identifier').value = (row.ident ? 'True':'False');
850 dojo.byId('attr-editor-xpath').value = row.xpath;
851 dojo.byId('attr-editor-remove').value = row.remove;
853 // set up UI for editing
854 dojo.byId('vl-create-attr-editor-button').click();
857 function vlSaveAttrDefinition(data) {
858 idHide('vl-attr-editor-div');
859 idShow('vl-generic-progress');
861 data.id = ATTR_EDIT_ID;
863 // this ought to honour custom xpaths, but overwrite xpaths
864 // derived from tags/subfields.
865 if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
866 var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
870 // build up our permacrud params. Key variables here are
871 // "create or update" and "bib or auth".
873 var isAuth = (ATTR_EDIT_GROUP == 'auth');
874 var isCreate = (ATTR_EDIT_ID == null);
875 var rad = isAuth ? new vqarad() : new vqbrad() ;
876 var method = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.')
877 + (isAuth ? 'vqarad' : 'vqbrad');
878 var _data = rad.fromStoreItem(data);
882 fieldmapper.standardRequest(
883 ['open-ils.permacrud', method],
885 params: [authtoken, _data ],
886 onresponse: function(r) { },
887 oncomplete: function(r) {
888 attrEditorFetchAttrDefs(vlShowAttrEditor);
891 onerror: function(r) {
892 alert('vlSaveAttrDefinition comms error: ' + r);
898 function attrEditorFetchAttrDefs(callback) {
899 var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
903 function vlAttrDelete() {
904 idHide('vl-attr-editor-div');
905 idShow('vl-generic-progress');
907 var isAuth = (ATTR_EDIT_GROUP == 'auth');
908 var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
909 var rad = isAuth ? new vqarad() : new vqbrad() ;
910 fieldmapper.standardRequest(
911 ['open-ils.permacrud', method],
913 params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
914 oncomplete: function() {
915 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
916 attrEditorFetchAttrDefs(vlShowAttrEditor);
919 onerror: function(r) {
920 alert('vlAttrDelete comms error: ' + r);
926 // ------------------------------------------------------------
927 // utilities for attribute editors
929 // dom utilities (maybe dojo does these, and these should be replaced)
931 function idStyle(obId, k, v) { document.getElementById(obId).style[k] = v; }
932 function idShow(obId) { idStyle(obId, 'display', 'block'); }
933 function idHide(obId) { idStyle(obId, 'display' , 'none'); }
935 function connectTooltip(fieldId) {
936 // Given an element id, look up a tooltip element in the doc (same
937 // id with a '-tip' suffix) and associate the two. Maybe dojo has
938 // a better way to do this?
939 var fld = dojo.byId(fieldId);
940 var tip = dojo.byId(fieldId + '-tip');
941 dojo.connect(fld, 'onfocus', function(evt) {
942 dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
943 dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
948 var xpathParser = new openils.MarcXPathParser();
950 function tagAndSubFieldsToXpath(tags, subfields) {
951 // given tags, and subfields, build up an XPath.
954 'tags':tags.match(/[\d]+/g),
955 'subfields':subfields.match(/[a-zA-z]/g) };
956 return xpathParser.compile(parts);
958 return {'parts':null, 'tags':null, 'error':err};
962 function looksLikeDerivedXpath(path) {
963 // Does this path look like it was derived from tags and subfields?
964 var parsed = xpathParser.parse(path);
965 if (parsed.tags == null)
967 var compiled = xpathParser.compile(parsed);
968 return (path == compiled);
971 // amazing xpath-util unit-tests
972 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]')) alert('vandelay xpath-utility error');
973 if ( looksLikeDerivedXpath('ba-boo-ba-boo!')) alert('vandelay xpath-utility error');