3 * Copyright (C) 2004-2008 Georgia Public Library Service
4 * Copyright (C) 2008-2010 Equinox Software, Inc.
5 * Mike Rylander <miker@esilibrary.com>
7 * Copyright (C) 2010 Dan Scott <dan@coffeecode.net>
8 * Copyright (C) 2010 Internationaal Instituut voor Sociale Geschiedenis <info@iisg.nl>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 // Pretty printing kills whitespace too, so disable it.
22 XML.prettyPrinting = false;
23 var xmlDeclaration = /^<\?xml version[^>]+?>/;
25 var serializer = new XMLSerializer();
26 var marcns = new Namespace("http://www.loc.gov/MARC21/slim");
27 var gw = new Namespace("http://opensrf.org/-/namespaces/gateway/v1");
28 var xulns = new Namespace("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
29 default xml namespace = marcns;
31 var tooltip_hash = {};
43 var show_auth_menu = false;
45 var _fixed_field_values = {}, _fixed_field_context_menus = {};
46 var _fixed_field_anonymous_func_counter = 0;
48 function $(id) { return document.getElementById(id); }
50 var acs; // AuthorityControlSet
52 function get_new_008() {
55 var y = now.getUTCFullYear().toString().substr(2,2);
56 var m = now.getUTCMonth() + 1;
57 if (m < 10) m = '0' + m;
58 var d = now.getUTCDate();
59 if (d < 10) d = '0' + d;
61 if (xml_record.controlfield.(@tag == '008')) {
62 var field = xml_record.controlfield.(@tag == '008')[0];
63 orig008 = field.text();
66 /* lang code from 041a */
67 var lang = orig008.substr(35, 3);
68 if (xml_record.datafield.(@tag == '041')) {
69 var field = xml_record.datafield.(@tag == '041')[0];
70 if (field && field.subfield.(@code == 'a')) {
71 lang = field.subfield.(@code == 'a')[0];
75 /* country code from 044a */
76 var country = orig008.substr(15, 3);
77 if (xml_record.datafield.(@tag == '044')) {
78 var field = xml_record.datafield.(@tag == '044')[0];
79 if (field && field.subfield.(@code == 'a')) {
80 country = field.subfield.(@code == 'a')[0];
83 while (country.length < 3) country = country + ' ';
84 if (country.length > 3) country = country.substr(0,3);
87 var date1 = now.getUTCFullYear().toString();
88 if (xml_record.datafield.(@tag == '260')) {
89 var field = xml_record.datafield.(@tag == '260')[0];
90 if (field && field.subfield.(@code == 'c')) {
91 var tmpd = field.subfield.(@code == 'c')[0].replace(/[^0-9]/g, '');
92 if (tmpd.match(/^\d\d\d\d/)) {
93 date1 = tmpd.substr(0, 4);
98 var date2 = orig008.substr(11, 4);
99 var datetype = orig008.substr(6, 1);
100 var modded = orig008.substr(38, 1);
101 var catsrc = orig008.substr(39, 1);
103 return '' + y + m + d + datetype + date1 + date2 + country + ' ' + lang + modded + catsrc;
107 function mangle_005() {
108 var now = new Date();
109 var y = now.getUTCFullYear();
111 var m = now.getUTCMonth() + 1;
112 if (m < 10) m = '0' + m;
114 var d = now.getUTCDate();
115 if (d < 10) d = '0' + d;
117 var H = now.getUTCHours();
118 if (H < 10) H = '0' + H;
120 var M = now.getUTCMinutes();
121 if (M < 10) M = '0' + M;
123 var S = now.getUTCSeconds();
124 if (S < 10) S = '0' + S;
127 var stamp = '' + y + m + d + H + M + S + '.0';
128 createControlField('005',stamp);
132 function createControlField (tag,data) {
133 // first, remove the old field, if any;
134 for (var i in xml_record.controlfield.(@tag == tag)) delete xml_record.controlfield.(@tag == tag)[i];
136 var cf = <controlfield tag="" xmlns="http://www.loc.gov/MARC21/slim">{ data }</controlfield>;
139 // then, find the right position and insert it
141 var cfields = xml_record.controlfield;
142 var base = Number(tag.substring(2));
143 for (var i in cfields) {
144 var t = Number(cfields[i].@tag.toString().substring(2));
146 xml_record.insertChildBefore( cfields[i], cf );
152 if (!done) xml_record.insertChildBefore( xml_record.datafield[0], cf );
157 function xml_escape_unicode ( str ) {
159 /([\u0080-\ufffe])/g,
160 function (r,s) { return "&#x" + s.charCodeAt(0).toString(16) + ";"; }
164 function wrap_long_fields (node) {
165 var text_size = dojo.attr(node, 'size');
166 var hard_width = 100;
167 if (text_size > hard_width) {
168 dojo.attr(node, 'multiline', 'true');
169 dojo.attr(node, 'cols', hard_width);
170 var text_rows = (text_size / hard_width) + 1;
171 dojo.attr(node, 'rows', text_rows);
175 function set_flat_editor (useFlatText) {
177 var xe = $('xul-editor');
178 var te = $('text-editor');
181 if (xe.hidden) { return; }
185 if (te.hidden) { return; }
191 // get the marcxml from the text box
192 var xml_string = new MARC.Record({
193 marcbreaker : $('text-editor-box').value,
197 // reset the xml record and rerender it
198 xml_record = new XML( xml_string );
199 if (xml_record..record[0]) xml_record = xml_record..record[0];
202 var xml_string = xml_record.toXMLString();
204 // push the xml record into the textbox
205 var rec = new MARC.Record ({ delimiter : '$', marcxml : xml_string });
206 $('text-editor-box').value = rec.toBreaker();
213 if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
214 JSAN.errorLevel = "die"; // none, warn, or die
215 JSAN.addRepository('/xul/server/');
217 dojo.require('openils.AuthorityControlSet');
218 acs = new openils.AuthorityControlSet ();
220 // Fake xulG for standalone...
225 window.xulG.record = {};
226 window.xulG.save = {};
227 window.xulG.marc_control_number_identifier = 'CONS';
229 window.xulG.save.label = $('catStrings').getString('staff.cat.marcedit.save.label');
230 window.xulG.save.func = function (r) { alert(r); }
233 var _rid = cgi.param('record');
235 window.xulG.record.id = _rid;
236 window.xulG.record.url = '/opac/extras/supercat/retrieve/marcxml/record/' + _rid;
240 // End faking part...
242 /* Check for an explicitly passed record type
243 * This is not the same as the fixed-field record type; we can't trust
244 * the fixed fields when making modifications to the attributes for a
245 * given record (in particular, config.bib_source only applies for bib
246 * records, but an auth or MFHD record with the same ID and bad fixed
247 * fields could trample the config.bib_source value for the
248 * corresponding bib record if we're not careful.
250 * are = authority record
251 * sre = serial record (MFHD)
252 * bre = bibliographic record
254 if (!window.xulG.record.rtype) {
256 window.xulG.record.rtype = cgi.param('rtype') || false;
259 document.getElementById('save-button').setAttribute('label', window.xulG.save.label);
260 /* Ugh. Sorry about the spaghetti. */
261 document.getElementById('save-button').setAttribute('oncommand',
262 'var to_save = function() { ' + /* begin to_save() */
263 'if ($("xul-editor").hidden) set_flat_editor(false); ' +
265 'var xml_string = xml_escape_unicode( xml_record.toXMLString() ); ' +
266 'save_attempt( xml_string ); ' +
268 '}; ' + /* end to_save() */
270 'if (typeof _owPCW == "object") { ' +
271 ' for (var k in _owPCW) { ' +
272 ' if (_owPCW[k].active) { ' +
273 ' try { _owPCW[k].apply(to_save); to_save.ran = true; } ' +
274 ' catch (E) { alert("_ow_PCW[" + k + "]: " + E); } ' +
279 'if (!to_save.ran) to_save();'
282 if (window.xulG.record.url) {
283 var req = new XMLHttpRequest();
284 req.open('POST',window.xulG.record.url,false);
286 window.xulG.record.marc = req.responseText.replace(xmlDeclaration, '');
289 xml_record = new XML( window.xulG.record.marc );
290 if (xml_record..record[0]) xml_record = xml_record..record[0];
292 // Get the tooltip xml all async like
293 req = new XMLHttpRequest();
295 // Set a default locale in case preferences fail us
296 var locale = "en-US";
298 // Try to get the locale from our preferences
300 const Cc = Components.classes;
301 const Ci = Components.interfaces;
302 locale = Cc["@mozilla.org/preferences-service;1"].
303 getService(Ci.nsIPrefBranch).
304 getCharPref("general.useragent.locale");
308 // TODO: We should send a HEAD request to check for the existence of the desired file
309 // then fall back to the default locale if preferred locale is not necessary;
310 // however, for now we have a simplistic check:
312 // we currently have translations for only three locales; in the absence of a
313 // valid locale, default to the almighty en-US
314 if (locale != 'en-US' && locale != 'fr-CA' && locale != 'fi-FI') {
318 // grab the right tooltip based on MARC type
319 var tooltip_doc = 'marcedit-tooltips.xml';
320 switch (window.xulG.record.rtype) {
322 tooltip_doc = 'marcedit-tooltips.xml';
325 tooltip_doc = 'marcedit-tooltips-authority.xml';
326 locale = 'en-US'; // FIXME - note TODO above; at moment only en-US has this
329 tooltip_doc = 'marcedit-tooltips-mfhd.xml';
330 locale = 'en-US'; // FIXME - note TODO above; at moment only en-US has this
333 tooltip_doc = 'marcedit-tooltips.xml';
336 // Get the locale-specific tooltips
337 req.open('GET','/xul/server/locale/' + locale + '/' + tooltip_doc,true);
339 context_menus = createComplexXULElement('popupset');
340 document.documentElement.appendChild( context_menus );
342 tag_menu = createMenuPopup({position : 'after_start', id : 'tags_popup'});
343 context_menus.appendChild( tag_menu );
345 tag_menu.appendChild(
347 { label : $('catStrings').getString('staff.cat.marcedit.add_row.label'),
349 'var e = document.createEvent("KeyEvents");' +
350 'e.initKeyEvent("keypress",1,1,null,1,0,0,0,13,0);' +
351 'current_focus.inputField.dispatchEvent(e);'
356 tag_menu.appendChild(
358 { label : $('catStrings').getString('staff.cat.marcedit.insert_row.label'),
360 'var e = document.createEvent("KeyEvents");' +
361 'e.initKeyEvent("keypress",1,1,null,1,0,1,0,13,0);' +
362 'current_focus.inputField.dispatchEvent(e);'
367 tag_menu.appendChild(
369 { label : $('catStrings').getString('staff.cat.marcedit.remove_row.label'),
371 'var e = document.createEvent("KeyEvents");' +
372 'e.initKeyEvent("keypress",1,1,null,1,0,0,0,46,0);' +
373 'current_focus.inputField.dispatchEvent(e);'
378 tag_menu.appendChild( createComplexXULElement( 'separator' ) );
380 tag_menu.appendChild(
382 { label : $('catStrings').getString('staff.cat.marcedit.replace_006.label'),
384 'var e = document.createEvent("KeyEvents");' +
385 'e.initKeyEvent("keypress",1,1,null,1,0,0,0,117,0);' +
386 'current_focus.inputField.dispatchEvent(e);'
391 tag_menu.appendChild(
393 { label : $('catStrings').getString('staff.cat.marcedit.replace_007.label'),
395 'var e = document.createEvent("KeyEvents");' +
396 'e.initKeyEvent("keypress",1,1,null,1,0,0,0,118,0);' +
397 'current_focus.inputField.dispatchEvent(e);'
402 tag_menu.appendChild(
404 { label : $('catStrings').getString('staff.cat.marcedit.replace_008.label'),
406 'var e = document.createEvent("KeyEvents");' +
407 'e.initKeyEvent("keypress",1,1,null,1,0,0,0,119,0);' +
408 'current_focus.inputField.dispatchEvent(e);'
413 tag_menu.appendChild( createComplexXULElement( 'separator' ) );
415 p = createComplexXULElement('popupset');
416 document.documentElement.appendChild( p );
418 req.onreadystatechange = function () {
419 if (req.readyState == 4) {
420 bib_data = new XML( req.responseText.replace(xmlDeclaration, '') );
429 if (! xulG.fast_add_item) {
430 document.getElementById('fastItemAdd_checkbox').hidden = true;
432 document.getElementById('fastItemAdd_textboxes').hidden = document.getElementById('fastItemAdd_checkbox').hidden || !document.getElementById('fastItemAdd_checkbox').checked;
434 // Only show bib sources for bib records that already exist in the database
435 if (xulG.record.rtype == 'bre' && xulG.record.id) {
436 dojo.require('openils.PermaCrud');
437 var authtoken = ses();
438 // Retrieve the current record attributes
439 var bib = new openils.PermaCrud({"authtoken": authtoken}).retrieve('bre', xulG.record.id);
441 // Remember the current bib source of the record
442 xulG.record.bre = bib;
444 buildBibSourceList(authtoken, xulG.record.id);
447 preparePhysCharWizardContext();
448 dojo.require('MARC.FixedFields');
449 dojo.require("openils.widget.PhysCharWizard");
452 alert('FIXME, MARC Editor, my_init: ' + E);
457 function createComplexHTMLElement (e, attrs, objects, text) {
458 var l = document.createElementNS('http://www.w3.org/1999/xhtml',e);
461 for (var i in attrs) l.setAttribute(i,attrs[i]);
465 for ( var i in objects ) l.appendChild( objects[i] );
469 l.appendChild( document.createTextNode(text) )
475 function createComplexXULElement (e, attrs, objects) {
476 var l = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',e);
479 for (var i in attrs) {
480 if (typeof attrs[i] == 'function') {
481 l.addEventListener( i, attrs[i], true );
483 l.setAttribute(i,attrs[i]);
489 for ( var i in objects ) l.appendChild( objects[i] );
495 function createDescription (attrs) {
496 return createComplexXULElement('description', attrs, Array.prototype.slice.apply(arguments, [1]) );
499 function createTooltip (attrs) {
500 return createComplexXULElement('tooltip', attrs, Array.prototype.slice.apply(arguments, [1]) );
503 function createLabel (attrs) {
504 return createComplexXULElement('label', attrs, Array.prototype.slice.apply(arguments, [1]) );
507 function createVbox (attrs) {
508 return createComplexXULElement('vbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
511 function createHbox (attrs) {
512 return createComplexXULElement('hbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
515 function createRow (attrs) {
516 return createComplexXULElement('row', attrs, Array.prototype.slice.apply(arguments, [1]) );
519 function createTextbox (attrs) {
520 return createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
523 function createMenu (attrs) {
524 return createComplexXULElement('menu', attrs, Array.prototype.slice.apply(arguments, [1]) );
527 function createMenuPopup (attrs) {
528 return createComplexXULElement('menupopup', attrs, Array.prototype.slice.apply(arguments, [1]) );
531 function createPopup (attrs) {
532 return createComplexXULElement('popup', attrs, Array.prototype.slice.apply(arguments, [1]) );
535 function createMenuitem (attrs) {
536 return createComplexXULElement('menuitem', attrs, Array.prototype.slice.apply(arguments, [1]) );
539 function createCheckbox (attrs) {
540 return createComplexXULElement('checkbox', attrs, Array.prototype.slice.apply(arguments, [1]) );
543 // Find the next textbox that we can use for a focus point
544 // For control fields, use the first editable text box
545 // For data fields, focus on the first subfield text box
546 function setFocusToNextTag (row, direction) {
547 var keep_looking = true;
548 while (keep_looking && (direction == 'up' ? row = row.previousSibling : row = row.nextSibling)) {
549 // Is it a datafield?
550 dojo.query('hbox', row).query('hbox').query('textbox').forEach(function(node, index, arr) {
552 keep_looking = false;
555 // No, it's a control field; use the first textbox
557 dojo.query('textbox', row).forEach(function(node, index, arr) {
559 keep_looking = false;
567 function set_lock_on_keypress(ev) {
569 //dump('keypress: isChar = ' + ev.isChar + ' char = ' + ev.char + ' charCode = ' + ev.charCode + ' key = ' + ev.key + ' keyCode = ' + ev.keyCode + '\n');
574 || ev.keyCode == ev.DOM_VK_F1
575 || ev.keyCode == ev.DOM_VK_F2
576 || ev.keyCode == ev.DOM_VK_F3
577 || ev.keyCode == ev.DOM_VK_F4
578 || ev.keyCode == ev.DOM_VK_F5
579 || ev.keyCode == ev.DOM_VK_F6
580 || ev.keyCode == ev.DOM_VK_F7
581 || ev.keyCode == ev.DOM_VK_F8
582 || ev.keyCode == ev.DOM_VK_F9
583 || ev.keyCode == ev.DOM_VK_F10
584 || ev.keyCode == ev.DOM_VK_F11
585 || ev.keyCode == ev.DOM_VK_F12
586 || ev.keyCode == ev.DOM_VK_F13
587 || ev.keyCode == ev.DOM_VK_F14
588 || ev.keyCode == ev.DOM_VK_F15
589 || ev.keyCode == ev.DOM_VK_F16
590 || ev.keyCode == ev.DOM_VK_F17
591 || ev.keyCode == ev.DOM_VK_F18
592 || ev.keyCode == ev.DOM_VK_F19
593 || ev.keyCode == ev.DOM_VK_F20
594 || ev.keyCode == ev.DOM_VK_F21
595 || ev.keyCode == ev.DOM_VK_F22
596 || ev.keyCode == ev.DOM_VK_F23
597 || ev.keyCode == ev.DOM_VK_F24
606 function createMARCTextbox (element,attrs) {
608 var box = createComplexXULElement('textbox', attrs, Array.prototype.slice.apply(arguments, [2]) );
609 box.addEventListener(
611 set_lock_on_keypress,
614 box.onkeypress = function (event) {
617 while(node = node.parent()) {
621 var row = event.target;
622 while (row.tagName != 'row') row = row.parentNode;
624 if (element.nodeKind() == 'attribute') element[0]=box.value;
625 else element.setChildren( box.value );
627 if (element.localName() != 'controlfield') {
628 if ((event.charCode == 100 || event.charCode == 105) && event.ctrlKey) { // ctrl+d or ctrl+i
630 var index_sf, target, move_data;
631 if (element.localName() == 'subfield') {
633 target = event.target.parentNode;
635 var start = event.target.selectionStart;
636 var end = event.target.selectionEnd - event.target.selectionStart ?
637 event.target.selectionEnd :
638 event.target.value.length;
640 move_data = event.target.value.substring(start,end);
641 event.target.value = event.target.value.substring(0,start) + event.target.value.substring(end);
642 event.target.setAttribute('size', event.target.value.length + 2);
644 element.setChildren( event.target.value );
646 } else if (element.localName() == 'code') {
647 index_sf = element.parent();
648 target = event.target.parentNode;
649 } else if (element.localName() == 'tag' || element.localName() == 'ind1' || element.localName() == 'ind2') {
650 index_sf = element.parent().children()[element.parent().children().length() - 1];
651 target = event.target.parentNode.lastChild.lastChild;
654 var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{ move_data }</subfield>;
656 index_sf.parent().insertChildAfter( index_sf, sf );
658 var new_sf = marcSubfield(sf);
660 if (target === target.parentNode.lastChild) {
661 target.parentNode.appendChild( new_sf );
663 target.parentNode.insertBefore( new_sf, target.nextSibling );
666 new_sf.firstChild.nextSibling.focus();
668 event.preventDefault();
671 } else if (event.keyCode == 13 || event.keyCode == 77) {
672 if (event.ctrlKey) { // ctrl+enter
675 if (element.localName() == 'subfield') index = element.parent();
676 if (element.localName() == 'code') index = element.parent().parent();
677 if (element.localName() == 'tag') index = element.parent();
678 if (element.localName() == 'ind1') index = element.parent();
679 if (element.localName() == 'ind2') index = element.parent();
681 var df = <datafield tag="" ind1="" ind2="" xmlns="http://www.loc.gov/MARC21/slim"><subfield code="" /></datafield>;
683 if (event.shiftKey) { // ctrl+shift+enter
684 index.parent().insertChildBefore( index, df );
686 index.parent().insertChildAfter( index, df );
689 var new_df = marcDatafield(df);
691 if (row.parentNode.lastChild === row) {
692 row.parentNode.appendChild( new_df );
694 if (event.shiftKey) { // ctrl+shift+enter
695 row.parentNode.insertBefore( new_df, row );
697 row.parentNode.insertBefore( new_df, row.nextSibling );
701 new_df.firstChild.focus();
703 event.preventDefault();
706 } else if (event.shiftKey) {
707 if (row.previousSibling.className.match('marcDatafieldRow'))
708 row.previousSibling.firstChild.focus();
710 row.nextSibling.firstChild.focus();
713 } else if (event.keyCode == 38 || event.keyCode == 40) { // up-arrow or down-arrow
714 if (event.ctrlKey) { // CTRL key: copy the field
716 if (element.localName() == 'subfield') index = element.parent();
717 if (element.localName() == 'code') index = element.parent().parent();
718 if (element.localName() == 'tag') index = element.parent();
719 if (element.localName() == 'ind1') index = element.parent();
720 if (element.localName() == 'ind2') index = element.parent();
722 var copyField = index.copy();
724 if (event.keyCode == 38) { // ctrl+up-arrow
725 index.parent().insertChildBefore( index, copyField );
727 index.parent().insertChildAfter( index, copyField );
730 var new_df = marcDatafield(copyField);
732 if (row.parentNode.lastChild === row) {
733 row.parentNode.appendChild( new_df );
735 if (event.keyCode == 38) { // ctrl+up-arrow
736 row.parentNode.insertBefore( new_df, row );
737 } else { // ctrl+down-arrow
738 row.parentNode.insertBefore( new_df, row.nextSibling );
742 new_df.firstChild.focus();
744 event.preventDefault();
748 if (event.keyCode == 38) {
749 return setFocusToNextTag(row, 'up');
751 if (event.keyCode == 40) {
752 return setFocusToNextTag(row, 'down');
757 } else if (event.keyCode == 46 && event.ctrlKey) { // ctrl+del
760 if (element.localName() == 'subfield') index = element.parent();
761 if (element.localName() == 'code') index = element.parent().parent();
762 if (element.localName() == 'tag') index = element.parent();
763 if (element.localName() == 'ind1') index = element.parent();
764 if (element.localName() == 'ind2') index = element.parent();
766 for (var i in index.parent().children()) {
767 if (index === index.parent().children()[i]) {
768 delete index.parent().children()[i];
773 row.previousSibling.firstChild.focus();
774 row.parentNode.removeChild(row);
776 event.preventDefault();
779 } else if (event.keyCode == 46 && event.shiftKey) { // shift+del
782 if (element.localName() == 'subfield') index = element;
783 if (element.localName() == 'code') index = element.parent();
786 for (var i in index.parent().children()) {
787 if (index === index.parent().children()[i]) {
788 delete index.parent().children()[i];
793 if (event.target.parentNode === event.target.parentNode.parentNode.lastChild) {
794 event.target.parentNode.previousSibling.lastChild.focus();
796 event.target.parentNode.nextSibling.firstChild.nextSibling.focus();
799 event.target.parentNode.parentNode.removeChild(event.target.parentNode);
801 event.preventDefault();
804 } else if (event.keyCode == 117 && event.ctrlKey) { // ctrl + F6
806 createControlField('006',' ');
808 } else if (event.keyCode == 118 && event.ctrlKey) { // ctrl + F7
810 createControlField('007',' ');
812 } else if (event.keyCode == 119 && event.ctrlKey) { // ctrl + F8
814 createControlField('008', get_new_008());
820 } else { // event on a control field
821 if (event.keyCode == 38) {
822 return setFocusToNextTag(row, 'up');
823 } else if (event.keyCode == 40) {
824 return setFocusToNextTag(row, 'down');
829 box.addEventListener(
832 if (element.nodeKind() == 'attribute') element[0]=box.value;
833 else element.setChildren( box.value );
839 box.addEventListener(
842 if (element.nodeKind() == 'attribute') element[0]=box.value;
843 else element.setChildren( box.value );
849 box.addEventListener(
852 if (element.nodeKind() == 'attribute') element[0]=box.value;
853 else element.setChildren( box.value );
859 // 'input' event catches the box value after the keypress
860 box.addEventListener(
863 if (element.nodeKind() == 'attribute') element[0]=box.value;
864 else element.setChildren( box.value );
870 box.addEventListener(
873 if (element.localName() == 'controlfield')
874 eval('fillFixedFields();');
882 function toggleFFE () {
883 var grid = document.getElementById('leaderGrid');
892 function changeFFEditor (type) {
893 var grid = document.getElementById('leaderGrid');
894 grid.setAttribute('type',type);
895 document.getElementById('recordTypeLabel').setAttribute('value',type);
897 // Hide FFEditor rows that we don't need for our current type
898 // If all of the labels for a given row do not include our
899 // desired type in their set attribute, we can hide that row
900 dojo.query('rows', grid).query('row').forEach(function(node, index, arr) {
901 if (dojo.query('label[set~=' + type + ']', node).length == 0) {
909 updateFFEditorContexts(grid, type);
912 alert( /* XXX i18n - the marc editor either isn't tied in to
913 an overall i18n infrastructure, or it's different
914 enough from other Evergreen parts that I don't
915 understand the right way to do i18n here */
916 "failed to load fixed field values for rec_type '" +
923 function getFFValuesForType(type, callback, errback) {
924 if (_fixed_field_values[type]) {
927 dojo.require("openils.Util");
929 fieldmapper.standardRequest(
931 "open-ils.cat.biblio.fixed_field_values.by_rec_type"], {
934 "oncomplete": function(r) {
935 if (r = openils.Util.readResponse(r)) {
936 _fixed_field_values[type] = r;
948 function updateFFEditorContexts(grid, type) {
950 /* XXX Dojo to navigate a XUL DOM. Bad form? We do it elsewhere in this
951 * file, but I think I've heard it scoffed at. */
953 var rows_node = dojo.query("rows", grid)[0];
954 dojo.query("row", rows_node).forEach(
956 dojo.query("textbox", row).forEach(
958 var name = tb.getAttribute("name");
959 if (_fixed_field_values[type][name]) {
961 "context", getFFContextMenu(type, name)
964 tb.setAttribute("context", "clipboard");
972 function getFFContextMenu(type, name) {
973 if (!_fixed_field_context_menus[type]) {
974 _fixed_field_context_menus[type] = {};
977 var context_menu_id = "_fixed_field_context_menus_" + type + "_" + name;
979 if (!_fixed_field_context_menus[type][name]) {
980 var p = document.getElementsByTagName("popupset")[0];
981 var m = document.createElement("menupopup");
982 m.setAttribute("id", context_menu_id);
983 _fixed_field_values[type][name].forEach(
985 var mi = document.createElement("menuitem");
986 var funcname = "_ff_anon_" +
987 String(_fixed_field_anonymous_func_counter++);
989 /* These anon functions linger and take up memory, but due
990 * to caching there's a limit on how much, and that limit is
991 * a function of the size of the dataset defined in the
992 * ccvm/cmfpm tables. */
994 var el = document.getElementById(name + "_tb");
995 window[funcname] = function() {
997 updateFixedFields(el);
1000 /* In XUL land we can't set an element's
1001 * attribute like oncommand to a code reference. It has to be
1002 * an actual string to be eval'd. */
1003 mi.setAttribute("oncommand", funcname + "()");
1004 mi.setAttribute("label", v[0] + ": " + v[1]); /* XXX i18n ? */
1008 _fixed_field_context_menus[type][name] = m;
1012 return context_menu_id;
1015 /* This just sets up a special context menu for a 007 data field to use, so
1016 * that users can right-click for a menu and get a choice to launch the
1017 * Physical Characteristics Wizard.
1019 function preparePhysCharWizardContext() {
1020 var menu = document.getElementById("physCharWizardContext");
1021 menu.appendChild(document.createElement("menuseparator"));
1023 var clipb_children = document.getElementById("clipboard").childNodes;
1024 for (var i = 0; i < clipb_children.length; i++) /* collection not array */ {
1025 var child = clipb_children[i];
1026 if (child.nodeName == 'menuitem')
1027 menu.appendChild(child.cloneNode(true));
1031 function launchPhysCharWizard(popup_node) {
1033 new openils.widget.PhysCharWizard({
1035 "onapply": function(v) {
1036 createControlField("007", v);
1041 alert("Exception raised by openils.widget.PhysCharWizard:\n" + E);
1045 function fillFixedFields () {
1047 var grid = document.getElementById('leaderGrid');
1048 var marc_rec = new MARC.Record ({ delimiter : '$', marcxml : xml_record.toXMLString() });
1051 var pre_list = grid.getElementsByTagName('label');
1052 for (var i in pre_list) {
1053 if ( pre_list[i].getAttribute && pre_list[i].getAttribute('set').indexOf(grid.getAttribute('type')) > -1 ) {
1054 list.push( pre_list[i] );
1058 for (var i in list) {
1059 var name = list[i].getAttribute('name');
1060 var value = marc_rec.extractFixedField(name, true);
1062 if (value === null) continue;
1064 list[i].nextSibling.value = value;
1069 alert('FIXME, MARC Editor, fillFixedFields: ' + E);
1073 function updateFixedFields (element) {
1074 var grid = document.getElementById('leaderGrid');
1075 var recGrid = document.getElementById('recGrid');
1076 var new_value = element.value;
1077 // Don't take focus away/adjust the record on partial changes
1078 var length = element.getAttribute('maxlength');
1079 if(new_value.length < length) return true;
1081 var marc_rec = new MARC.Record ({ delimiter : '$', marcxml : xml_record.toXMLString() });
1082 marc_rec.setFixedField(element.getAttribute('name'), new_value);
1084 var xml_string = marc_rec.toXmlString();
1085 xml_record = new XML( xml_string );
1086 if (xml_record..record[0]) xml_record = xml_record..record[0];
1088 // Put the cursor back to the current fixed field
1094 function marcLeader (leader) {
1095 var row = createRow(
1096 { class : 'marcLeaderRow',
1101 tooltiptext : $('catStrings').getString('staff.cat.marcedit.marcTag.LDR.label') } ),
1104 class : 'marcInd1' } ),
1107 class : 'marcInd2' } ),
1109 { value : leader.text(),
1110 class : 'marcLeader' } )
1116 function marcControlfield (field) {
1117 tagname = field.@tag.toString().substr(2);
1119 if (tagname == '1' || tagname == '3' || tagname == '6' || tagname == '7' || tagname == '8') {
1121 { class : 'marcControlfieldRow',
1122 tag : '_' + tagname },
1124 { value : field.@tag,
1126 context : 'tags_popup',
1127 onmouseover : 'getTooltip(this, "tag");',
1128 tooltipid : 'tag' + field.@tag } ),
1130 { value : field.@ind1,
1132 onmouseover : 'getTooltip(this, "ind1");',
1133 tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
1135 { value : field.@ind2,
1137 onmouseover : 'getTooltip(this, "ind2");',
1138 tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
1141 { value : field.text(),
1142 class : 'plain marcEditableControlfield',
1143 name : 'CONTROL' + tagname,
1144 context : tagname == 7 ? 'physCharWizardContext': 'clipboard',
1150 { class : 'marcControlfieldRow',
1151 tag : '_' + tagname },
1153 { value : field.@tag,
1155 onmouseover : 'getTooltip(this, "tag");',
1156 tooltipid : 'tag' + field.@tag } ),
1158 { value : field.@ind1,
1160 onmouseover : 'getTooltip(this, "ind1");',
1161 tooltipid : 'tag' + field.@tag + 'ind1val' + field.@ind1 } ),
1163 { value : field.@ind2,
1165 onmouseover : 'getTooltip(this, "ind2");',
1166 tooltipid : 'tag' + field.@tag + 'ind2val' + field.@ind2 } ),
1168 { value : field.text(),
1169 class : 'marcControlfield' } )
1176 function stackSubfields(checkbox) {
1177 var list = document.getElementsByAttribute('name','sf_box');
1180 if (!checkbox.checked) o = 'horizontal';
1182 for (var i = 0; i < list.length; i++) {
1183 if (list[i]) list[i].setAttribute('orient',o);
1187 function fastItemAdd_toggle(checkbox) {
1188 var x = document.getElementById('fastItemAdd_textboxes');
1189 if (checkbox.checked) {
1191 document.getElementById('fastItemAdd_callnumber').focus();
1192 document.getElementById('fastItemAdd_callnumber').select();
1198 function fastItemAdd_attempt(doc_id) {
1200 if (typeof window.xulG.fast_add_item != 'function') { return; }
1201 if (!document.getElementById('fastItemAdd_checkbox').checked) { return; }
1202 if (!document.getElementById('fastItemAdd_callnumber').value) { return; }
1203 if (!document.getElementById('fastItemAdd_barcode').value) { return; }
1204 window.xulG.fast_add_item( doc_id, document.getElementById('fastItemAdd_callnumber').value, document.getElementById('fastItemAdd_barcode').value );
1205 document.getElementById('fastItemAdd_barcode').value = '';
1208 alert('fastItemAdd_attempt: ' + E);
1212 function save_attempt(xml_string) {
1214 var result = window.xulG.save.func( xml_string );
1215 // I'd prefer to pass on_complete on through to fast_item_add,
1216 // but with the way these window scopes get destroyed with
1217 // tab replacement, maybe not a good idea
1218 var replace_on_complete = false;
1222 replace_on_complete = fastItemAdd_attempt(result.id);
1224 if (!replace_on_complete && typeof result.on_complete == 'function') {
1225 result.on_complete();
1229 alert('save_attempt: ' + E);
1233 function marcDatafield (field) {
1234 var row = createRow(
1235 { class : 'marcDatafieldRow' },
1238 { value : field.@tag,
1239 class : 'plain marcTag',
1241 context : 'tags_popup',
1242 oninput : 'if (this.value.length == 3) { this.nextSibling.focus(); }',
1245 onmouseover : 'current_focus = this; getTooltip(this, "tag");' } ),
1248 { value : field.@ind1,
1249 class : 'plain marcInd1',
1251 oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
1254 onmouseover : 'current_focus = this; getContextMenu(this, "ind1"); getTooltip(this, "ind1");',
1255 oncontextmenu : 'getContextMenu(this, "ind1");' } ),
1258 { value : field.@ind2,
1259 class : 'plain marcInd2',
1261 oninput : 'if (this.value.length == 1) { this.nextSibling.firstChild.firstChild.focus(); }',
1264 onmouseover : 'current_focus = this; getContextMenu(this, "ind2"); getTooltip(this, "ind2");',
1265 oncontextmenu : 'getContextMenu(this, "ind2");' } ),
1266 createHbox({ name : 'sf_box' })
1269 if (!current_focus && field.@tag == '') current_focus = row.childNodes[0];
1270 if (!current_focus && field.@ind1 == '') current_focus = row.childNodes[1];
1271 if (!current_focus && field.@ind2 == '') current_focus = row.childNodes[2];
1273 var sf_box = row.lastChild;
1274 if (document.getElementById('stackSubfields').checked)
1275 sf_box.setAttribute('orient','vertical');
1277 sf_box.addEventListener(
1280 if (sf_box === e.target) {
1281 sf_box.lastChild.lastChild.focus();
1282 } else if (e.target.parentNode === sf_box) {
1283 e.target.lastChild.focus();
1290 for (var i in field.subfield) {
1291 var sf = field.subfield[i];
1296 dojo.query('.marcSubfield', sf_box).forEach(wrap_long_fields);
1298 if (sf.@code == '' && (!current_focus || current_focus.className.match(/Ind/)))
1299 current_focus = sf_box.lastChild.childNodes[1];
1305 function marcSubfield (sf) {
1307 { class : 'marcSubfieldBox' },
1310 class : 'plain marcSubfieldDelimiter',
1311 onmouseover : 'getTooltip(this.nextSibling, "subfield");',
1312 oncontextmenu : 'getContextMenu(this.nextSibling, "subfield");',
1313 //onclick : 'this.nextSibling.focus();',
1314 onfocus : 'this.nextSibling.focus();',
1319 class : 'plain marcSubfieldCode',
1321 name : 'marcSubfieldCode',
1322 onmouseover : 'current_focus = this; getContextMenu(this, "subfield"); getTooltip(this, "subfield");',
1323 oncontextmenu : 'getContextMenu(this, "subfield");',
1324 oninput : 'if (this.value.length == 1) { this.nextSibling.focus(); }',
1329 { value : sf.text(),
1330 name : sf.parent().@tag + ':' + sf.@code,
1331 class : 'plain marcSubfield',
1333 onmouseover : 'getTooltip(this, "subfield");',
1334 contextmenu : function (event) { getAuthorityContextMenu(event.target, sf) },
1335 size : new String(sf.text()).length + 2,
1336 oninput : "this.setAttribute('size', this.value.length + 2);"
1341 function loadRecord() {
1343 var grid_rows = document.getElementById('recGrid').lastChild;
1345 while (grid_rows.firstChild) grid_rows.removeChild(grid_rows.firstChild);
1347 grid_rows.appendChild( marcLeader( xml_record.leader ) );
1349 for (var i in xml_record.controlfield) {
1350 grid_rows.appendChild( marcControlfield( xml_record.controlfield[i] ) );
1353 for (var i in xml_record.datafield) {
1354 grid_rows.appendChild( marcDatafield( xml_record.datafield[i] ) );
1357 grid_rows.getElementsByAttribute('class','marcDatafieldRow')[0].firstChild.focus();
1359 var marc_rec = new MARC.Record ({ delimiter : '$', marcxml : xml_record.toXMLString() });
1360 changeFFEditor(marc_rec.recordType());
1363 alert('FIXME, MARC Editor, loadRecord: ' + E);
1368 function genToolTips () {
1369 for (var i in bib_data.field) {
1370 var f = bib_data.field[i];
1372 tag_menu.appendChild(
1376 'current_focus.value = "' + f.@tag + '";' +
1377 'var e = document.createEvent("MutationEvents");' +
1378 'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1379 'current_focus.inputField.dispatchEvent(e);',
1380 disabled : f.@tag < '010' ? "true" : "false",
1381 tooltiptext : f.description }
1385 var i1_popup = createMenuPopup({position : 'after_start', id : 't' + f.@tag + 'i1' });
1386 context_menus.appendChild( i1_popup );
1388 var i2_popup = createMenuPopup({position : 'after_start', id : 't' + f.@tag + 'i2' });
1389 context_menus.appendChild( i2_popup );
1391 var sf_popup = createMenuPopup({position : 'after_start', id : 't' + f.@tag + 'sf' });
1392 context_menus.appendChild( sf_popup );
1394 tooltip_hash['tag' + f.@tag] = f.description;
1395 for (var j in f.indicator) {
1396 var ind = f.indicator[j];
1397 tooltip_hash['tag' + f.@tag + 'ind' + ind.@position + 'val' + ind.@value] = ind.description;
1399 if (ind.@position == 1) {
1400 i1_popup.appendChild(
1402 { label : ind.@value,
1404 'current_focus.value = "' + ind.@value + '";' +
1405 'var e = document.createEvent("MutationEvents");' +
1406 'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1407 'current_focus.inputField.dispatchEvent(e);',
1408 tooltiptext : ind.description }
1413 if (ind.@position == 2) {
1414 i2_popup.appendChild(
1416 { label : ind.@value,
1418 'current_focus.value = "' + ind.@value + '";' +
1419 'var e = document.createEvent("MutationEvents");' +
1420 'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1421 'current_focus.inputField.dispatchEvent(e);',
1422 tooltiptext : ind.description }
1428 for (var j in f.subfield) {
1429 var sf = f.subfield[j];
1430 tooltip_hash['tag' + f.@tag + 'sf' + sf.@code] = sf.description;
1432 sf_popup.appendChild(
1436 'current_focus.value = "' + sf.@code + '";' +
1437 'var e = document.createEvent("MutationEvents");' +
1438 'e.initMutationEvent("change",1,1,null,0,0,0,0);' +
1439 'current_focus.inputField.dispatchEvent(e);',
1440 tooltiptext : sf.description
1448 function getTooltip (target, type) {
1451 if (type == 'subfield')
1452 tt = 'tag' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf' + target.parentNode.childNodes[1].value;
1455 tt = 'tag' + target.parentNode.firstChild.value + 'ind1val' + target.value;
1458 tt = 'tag' + target.parentNode.firstChild.value + 'ind2val' + target.value;
1461 tt = 'tag' + target.parentNode.firstChild.value;
1463 if (!document.getElementById( tt )) {
1468 orient : 'vertical',
1469 onpopupshown : 'this.width = this.firstChild.boxObject.width + 10; this.height = this.firstChild.boxObject.height + 10;',
1470 class : 'tooltip' },
1471 createDescription({}, document.createTextNode( tooltip_hash[tt] ) )
1476 target.tooltip = tt;
1480 function getContextMenu (target, type) {
1483 if (type == 'subfield')
1484 tt = 't' + target.parentNode.parentNode.parentNode.firstChild.value + 'sf';
1487 tt = 't' + target.parentNode.firstChild.value + 'i1';
1490 tt = 't' + target.parentNode.firstChild.value + 'i2';
1492 target.setAttribute('context', tt);
1498 'a' : { 100 : 'a' },
1499 'd' : { 100 : 'd' },
1500 'e' : { 100 : 'e' },
1504 'a' : { 110 : 'a' },
1508 'a' : { 111 : 'a' },
1512 'a' : { 130 : 'a' },
1516 'a' : { 130 : 'a' },
1520 'a' : { 100 : 'a' },
1524 'a' : { 110 : 'a' },
1528 'a' : { 111 : 'a' },
1532 'a' : { 130 : 'a' },
1533 'n' : { 130 : 'n' },
1537 'a' : { 100 : 'a' },
1538 'd' : { 100 : 'd' },
1539 'q' : { 100 : 'q' },
1543 'a' : { 110 : 'a' },
1547 'a' : { 111 : 'a' },
1548 'c' : { 111 : 'c' },
1552 'a' : { 130 : 'a' },
1556 'a' : { 100 : 'a' },
1560 'a' : { 110 : 'a' },
1564 'a' : { 111 : 'a' },
1568 'a' : { 130 : 'a' },
1572 'a' : { 100 : 'a' },
1573 'd' : { 100 : 'd' },
1574 'q' : { 100 : 'q' },
1575 't' : { 100 : 't' },
1602 'a' : { 110 : 'a' },
1603 'd' : { 110 : 'd' },
1604 't' : { 110 : 't' },
1631 'a' : { 111 : 'a' },
1632 'd' : { 111 : 'd' },
1633 't' : { 111 : 't' },
1660 'a' : { 130 : 'a' },
1664 'a' : { 148 : 'a' },
1665 'v' : { 148 : 'v' },
1666 'x' : { 148 : 'x' },
1667 'y' : { 148 : 'y' },
1671 'a' : { 150 : 'a' },
1672 'b' : { 150 : 'b' },
1699 'a' : { 151 : 'a' },
1726 'a' : { 155 : 'a' },
1754 function getAuthorityContextMenu (target, sf) {
1755 var menu_id = sf.parent().@tag + ':' + sf.@code + '-authority-context-' + sf;
1758 var old = dojo.byId( menu_id );
1760 page = auth_pages[menu_id];
1761 old.parentNode.removeChild(old);
1763 auth_pages[menu_id] = 0;
1766 var sf_popup = createMenuPopup({ id : menu_id, flex : 1 });
1768 sf_popup.addEventListener("popuphiding", function(event) {
1769 if (show_auth_menu) {
1770 show_auth_menu = false;
1771 getAuthorityContextMenu(target, sf);
1772 dojo.byId(menu_id).openPopup();
1776 context_menus.appendChild( sf_popup );
1779 dojo.forEach( acs.controlSetList(), function (acs_id) {
1780 if (acs.controlSet(acs_id).control_map[sf.parent().@tag]) found_acs.push(acs_id);
1783 if (!found_acs.length) {
1784 sf_popup.appendChild(createLabel( { value : $('catStrings').getString('staff.cat.marcedit.not_authority_field.label') } ) );
1785 target.setAttribute('context', 'clipboard');
1789 if (sf.toString().replace(/\s*/, '')) {
1790 return browseAuthority(sf_popup, menu_id, target, sf, 20, page);
1796 /* Apply the complete 1xx */
1797 function applyFullAuthority ( target, ui_sf, e4x_sf ) {
1798 var new_vals = dojo.query('*[tag^="1"]', target);
1799 return applyAuthority( target, ui_sf, e4x_sf, new_vals );
1802 function applySelectedAuthority ( target, ui_sf, e4x_sf ) {
1803 var new_vals = target.getElementsByAttribute('checked','true');
1804 return applyAuthority( target, ui_sf, e4x_sf, new_vals );
1807 function applyAuthority ( target, ui_sf, e4x_sf, new_vals ) {
1808 var field = e4x_sf.parent();
1810 for (var i = 0; i < new_vals.length; i++) {
1812 var sf_list = field.subfield;
1813 for (var j in sf_list) {
1815 if (sf_list[j].@code == new_vals[i].getAttribute('subfield')) {
1816 sf_list[j] = new_vals[i].getAttribute('value');
1817 new_vals[i].setAttribute('subfield','');
1823 for (var i = 0; i < new_vals.length; i++) {
1825 /* indicators for the authority datafield are carried over in the main entry linking subfield */
1826 if (new_vals[i].getAttribute('subfield') == '0') {
1827 field.@ind1 = new_vals[i].getAttribute('ind1');
1828 field.@ind2 = new_vals[i].getAttribute('ind2');
1831 if (!new_vals[i].getAttribute('subfield')) continue;
1833 var val = new_vals[i].getAttribute('value');
1835 var sf = <subfield code="" xmlns="http://www.loc.gov/MARC21/slim">{val}</subfield>;
1836 sf.@code = new_vals[i].getAttribute('subfield');
1838 field.insertChildAfter(field.subfield[field.subfield.length() - 1], sf);
1841 var row = marcDatafield( field );
1844 while (node.nodeName != 'row') {
1845 node = node.parentNode;
1848 node.parentNode.replaceChild( row, node );
1852 function validateAuthority (button) {
1853 var grid = document.getElementById('recGrid');
1854 var label = button.getAttribute('label');
1857 var rows = grid.lastChild.childNodes;
1858 for (var i = 0; i < rows.length; i++) {
1860 var tag = row.firstChild;
1863 dojo.forEach(acs.controlSetList(), function (acs_id) {
1865 var control_map = acs.controlSet(acs_id).control_map;
1867 if (!control_map[tag.value]) return;
1868 button.setAttribute('label', label + ' - ' + tag.value);
1870 var ind1 = tag.nextSibling;
1871 var ind2 = ind1.nextSibling;
1872 var subfields = ind2.nextSibling.childNodes;
1875 for (var j = 0; j < subfields.length; j++) {
1876 var sf = subfields[j];
1877 sf_list.push( [ sf.childNodes[1].value, sf.childNodes[2].value ] );
1880 var matches = acs.findMatchingAuthorities(
1883 'subfields' : sf_list
1887 // matches = [ { "$csetId" : [ ... ] } ]
1890 if (matches[0]) { // probably set
1891 for (var cset in matches[0]) {
1892 var arr = matches[0][cset];
1894 // protect against errant empty string values
1895 if (arr.length == 1 && arr[0] == '')
1904 // XXX If adt, etc should be validated separately from vxz, etc then move this up into the above for loop
1905 for (var j = 0; j < subfields.length; j++) {
1906 var sf = subfields[j];
1908 dojo.removeClass(sf.childNodes[2], 'marcValidated');
1909 dojo.addClass(sf.childNodes[2], 'marcUnvalidated');
1911 dojo.removeClass(sf.childNodes[2], 'marcUnvalidated');
1912 dojo.addClass(sf.childNodes[2], 'marcValidated');
1916 if (found) done = true;
1920 button.setAttribute('label', label);
1927 function validateBibField (tags, searches) {
1928 var url = "/gateway?input_format=json&format=xml&service=open-ils.search&method=open-ils.search.authority.validate.tag";
1929 url += '¶m="tags"¶m=' + js2JSON(tags);
1930 url += '¶m="searches"¶m=' + js2JSON(searches);
1933 var req = new XMLHttpRequest();
1934 req.open('GET',url,false);
1942 function searchAuthority (term, tag, sf, limit) {
1943 var url = "/gateway?input_format=json&format=xml&service=open-ils.search&method=open-ils.search.authority.fts";
1944 url += '¶m="term"¶m="' + term + '"';
1945 url += '¶m="limit"¶m=' + limit;
1946 url += '¶m="tag"¶m=' + tag;
1947 url += '¶m="subfield"¶m="' + sf + '"';
1950 var req = new XMLHttpRequest();
1951 req.open('GET',url,false);
1958 /* TODO new authority browse support for context sets, and use that here */
1959 function browseAuthority (sf_popup, menu_id, target, sf, limit, page) {
1960 dojo.require('dojox.xml.parser');
1962 // map tag + subfield to the appropriate authority browse axis:
1963 // currently authority.author, authority.subject, authority.title, authority.topic
1964 // based on mappings in OpenILS::Application::SuperCat, though Authority Control
1965 // Sets will change that
1967 var axis_list = acs.bibFieldBrowseAxes( sf.parent().@tag.toString() );
1969 // No matching tag means no authorities to search - shortcut
1970 if (axis_list.length == 0) {
1971 target.setAttribute('context', 'clipboard');
1975 var type = 'authority.' + axis_list[0]; // Just take the first for now
1976 // TODO support multiple axes ... loop?
1986 var sf_list = sf.parent().subfield;
1987 for ( var i in sf_list) {
1988 sf_string += sf_list[i].toString() + ' ';
1989 if (sf_list[i] === sf) break;
1992 var url = '/opac/extras/browse/marcxml/'
1994 + '/1' // OU - currently unscoped
2000 // would be good to carve this out into a separate function
2001 dojo.xhrGet({"url":url, "sync": true, "preventCache": true, "handleAs":"xml", "load": function(records) {
2002 var create_menu = createMenu({ label: $('catStrings').getString('staff.cat.marcedit.create_authority.label')});
2004 var cm_popup = create_menu.appendChild(
2008 cm_popup.appendChild(
2009 createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.create_authority_now.label'),
2010 command : function() {
2011 // Call middle-layer function to create and save the new authority
2012 var source_f = summarizeField(sf);
2013 var new_auth = fieldmapper.standardRequest(
2014 ["open-ils.cat", "open-ils.cat.authority.record.create_from_bib"],
2015 [source_f, xulG.marc_control_number_identifier, ses()]
2017 if (new_auth && new_auth.id()) {
2018 addNewAuthorityID(new_auth, sf, target);
2024 cm_popup.appendChild(
2025 createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.create_authority_edit.label'),
2026 command : function() {
2027 // Generate the new authority by calling the new middle-layer
2028 // function (a non-saving variant), then display in another
2030 var source_f = summarizeField(sf);
2031 var authtoken = ses();
2032 dojo.require('openils.PermaCrud');
2033 var pcrud = new openils.PermaCrud({"authtoken": authtoken});
2034 var rec = fieldmapper.standardRequest(
2035 ["open-ils.cat", "open-ils.cat.authority.record.create_from_bib.readonly"],
2036 { "params": [source_f, xulG.marc_control_number_identifier] }
2038 loadMarcEditor(pcrud, rec, target, sf);
2043 sf_popup.appendChild(create_menu);
2044 sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2046 // append "Previous page" results browser
2047 sf_popup.appendChild(
2048 createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.previous_page.label'),
2049 command : function(event) {
2050 auth_pages[menu_id] -= 1;
2051 show_auth_menu = true;
2055 sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2057 dojo.query('record', records).forEach(function(record) {
2061 var auth_id = dojox.xml.parser.textContent(dojo.query('datafield[tag="901"]', record).query('subfield[code="c"]')[0]);
2063 if (dojo.query('controlfield[tag="003"]', record).length > 0) {
2064 auth_org = dojox.xml.parser.textContent(dojo.query('controlfield[tag="003"]', record)[0]);
2067 // Grab the fields with tags beginning with 1 (main entries) and iterate through the subfields
2068 dojo.query('datafield[tag^="1"]', record).forEach(function(field) {
2069 dojo.query('subfield', field).forEach(function(subfield) {
2073 main_text += dojox.xml.parser.textContent(subfield);
2077 // Grab the fields with tags beginning with 4 (see from entries) and iterate through the subfields
2078 dojo.query('datafield[tag^="4"]', record).forEach(function(field) {
2080 dojo.query('subfield', field).forEach(function(subfield) {
2084 see_text += dojox.xml.parser.textContent(subfield);
2086 see_from.push($('catStrings').getFormattedString('staff.cat.marcedit.authority_see_from', [see_text]));
2089 // Grab the fields with tags beginning with 5 (see also entries) and iterate through the subfields
2090 dojo.query('datafield[tag^="5"]', record).forEach(function(field) {
2092 dojo.query('subfield', field).forEach(function(subfield) {
2096 see_text += dojox.xml.parser.textContent(subfield);
2098 see_also.push($('catStrings').getFormattedString('staff.cat.marcedit.authority_see_also', [see_text]));
2101 buildAuthorityPopup(main_text, record, auth_org, auth_id, sf_popup, target, sf);
2103 dojo.forEach(see_from, function(entry_text) {
2104 buildAuthorityPopup(entry_text, record, auth_org, auth_id, sf_popup, target, sf, "font-style: italic; margin-left: 2em;");
2107 // To-do: instead of launching the standard selector menu, invoke
2108 // a new authority search using the 5XX entry text
2109 dojo.forEach(see_also, function(entry_text) {
2110 buildAuthorityPopup(entry_text, record, auth_org, auth_id, sf_popup, target, sf, "font-style: italic; margin-left: 2em;");
2115 if (sf_popup.childNodes.length == 0) {
2116 sf_popup.appendChild(createLabel( { value : $('catStrings').getString('staff.cat.marcedit.no_authority_match.label') } ) );
2118 // append "Next page" results browser
2119 sf_popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2120 sf_popup.appendChild(
2121 createMenuitem({ label : $('catStrings').getString('staff.cat.marcedit.next_page.label'),
2122 command : function(event) {
2123 auth_pages[menu_id] += 1;
2124 show_auth_menu = true;
2130 target.setAttribute('context', menu_id);
2136 function buildAuthorityPopup (entry_text, record, auth_org, auth_id, sf_popup, target, sf, style) {
2137 var grid = dojo.query('[name="authority-marc-template"]')[0].cloneNode(true);
2138 grid.setAttribute('name','-none-');
2139 grid.setAttribute('style','overflow:scroll');
2141 var submenu = createMenu( { "label": entry_text } );
2143 var popup = createMenuPopup({ "flex": "1" });
2145 submenu.setAttribute('style', style);
2146 popup.setAttribute('style', 'font-style: normal; margin-left: 0em;');
2148 submenu.appendChild(popup);
2150 dojo.query('datafield[tag^="1"]', record).forEach(function(field) {
2151 buildAuthorityPopupSelector(field, grid, auth_org, auth_id);
2153 dojo.query('datafield[tag^="4"]', record).forEach(function(field) {
2154 buildAuthorityPopupSelector(field, grid, auth_org, auth_id);
2156 dojo.query('datafield[tag^="5"]', record).forEach(function(field) {
2157 buildAuthorityPopupSelector(field, grid, auth_org, auth_id);
2160 grid.hidden = false;
2161 popup.appendChild( grid );
2165 { label : $('catStrings').getString('staff.cat.marcedit.apply_selected.label'),
2166 command : function (event) {
2167 applySelectedAuthority(event.target.previousSibling, target, sf);
2174 popup.appendChild( createComplexXULElement( 'menuseparator' ) );
2178 { label : $('catStrings').getString('staff.cat.marcedit.apply_full.label'),
2179 command : function (event) {
2180 applyFullAuthority(event.target.previousSibling.previousSibling.previousSibling, target, sf);
2187 sf_popup.appendChild( submenu );
2190 function buildAuthorityPopupSelector (field, grid, auth_org, auth_id) {
2191 var row = createRow(
2193 createLabel( { "value" : dojo.attr(field, 'tag') } ),
2194 createLabel( { "value" : dojo.attr(field, 'ind1') } ),
2195 createLabel( { "value" : dojo.attr(field, 'ind2') } )
2198 var sf_box = createHbox();
2199 dojo.query('subfield', field).forEach(function(subfield) {
2202 { "label" : '\u2021' + dojo.attr(subfield, 'code') + ' ' + dojox.xml.parser.textContent(subfield),
2203 "subfield" : dojo.attr(subfield, 'code'),
2204 "tag" : dojo.attr(field, 'tag'),
2205 "value" : dojox.xml.parser.textContent(subfield)
2209 row.appendChild(sf_box);
2212 // Append the authority linking subfield only for main entries
2213 if (dojo.attr(field, 'tag').charAt(0) == '1') {
2216 { "label" : '\u2021' + '0' + ' (' + auth_org + ')' + auth_id,
2218 "tag" : dojo.attr(field, 'tag'),
2219 "ind1" : dojo.attr(field, 'ind1'),
2220 "ind2" : dojo.attr(field, 'ind2'),
2221 "value" : '(' + auth_org + ')' + auth_id
2226 row.appendChild(sf_box);
2228 grid.lastChild.appendChild(row);
2231 function summarizeField(sf) {
2239 source_f.tag = sf.parent().@tag.toString();
2240 source_f.ind1 = sf.parent().@ind1.toString();
2241 source_f.ind2 = sf.parent().@ind2.toString();
2244 dojo.forEach( acs.controlSetList(), function (acs_id) {
2245 if (acs.controlSet(acs_id).control_map[sf.parent().@tag]) found_acs.push(acs_id);
2249 if (!found_acs.length) {
2252 cmap = acs.controlSet(found_acs[0]).control_map;
2255 for (var i = 0; i < sf.parent().subfield.length(); i++) {
2256 var sf_iter = sf.parent().subfield[i];
2258 /* Filter out subfields that are not controlled for this tag */
2259 if (!cmap[source_f.tag][sf_iter.@code.toString()]) {
2263 source_f.subfields.push([sf_iter.@code.toString(), sf_iter.toString()]);
2269 function buildBibSourceList (authtoken, recId) {
2270 /* TODO: Work out how to set the bib source of the bre that does not yet
2271 * exist - this is specifically in the case of Z39.50 imports. Right now
2272 * we just avoid populating and showing the config.bib_source list
2278 var bib = xulG.record.bre;
2280 dojo.require('openils.PermaCrud');
2282 // cbsList = the XUL menulist that contains the available bib sources
2283 var cbsList = dojo.byId('bib-source-list');
2285 // bibSources = an array containing all of the bib source objects
2286 var bibSources = new openils.PermaCrud({"authtoken": authtoken}).retrieveAll('cbs');
2288 // A tad ugly, but gives us the index of the bib source ID in cbsList
2290 var cbsListArr = [];
2291 dojo.forEach(bibSources, function (item) {
2292 cbsList.appendItem(item.source(), item.id());
2293 cbsListArr[item.id()] = x;
2297 // Show the current value of the bib source for this record
2298 cbsList.selectedIndex = cbsListArr[bib.source()];
2300 // Display the bib source selection widget
2301 dojo.byId('bib-source-list-caption').hidden = false;
2302 dojo.byId('bib-source-list').hidden = false;
2303 dojo.byId('bib-source-list-button').disabled = true;
2304 dojo.byId('bib-source-list-button').hidden = false;
2307 // Fired when the "Update Source" button is clicked
2308 // Updates the value of the bib source for the current record
2309 function updateBibSource() {
2310 var authtoken = ses();
2311 var cbs = dojo.byId('bib-source-list').selectedItem.value;
2312 var recId = xulG.record.id;
2313 var pcrud = new openils.PermaCrud({"authtoken": authtoken});
2314 var bib = pcrud.retrieve('bre', recId);
2315 if (bib.source() != cbs) {
2317 bib.ischanged = true;
2322 function onBibSourceSelect() {
2323 var cbs = dojo.byId('bib-source-list').selectedItem.value;
2324 var bib = xulG.record.bre;
2325 if (bib.source() != cbs) {
2326 dojo.byId('bib-source-list-button').disabled = false;
2328 dojo.byId('bib-source-list-button').disabled = true;
2332 function addNewAuthorityID(authority, sf, target) {
2333 var id_sf = <subfield code="0" xmlns="http://www.loc.gov/MARC21/slim">({xulG.marc_control_number_identifier}){authority.id()}</subfield>;
2334 sf.parent().appendChild(id_sf);
2335 var new_sf = marcSubfield(id_sf);
2338 while (dojo.attr(node, 'name') != 'sf_box') {
2339 node = node.parentNode;
2341 node.appendChild( new_sf );
2343 alert($('catStrings').getString('staff.cat.marcedit.create_authority_success.label'));
2346 function loadMarcEditor(pcrud, marcxml, target, sf) {
2348 To run in Firefox directly, must set signed.applets.codebase_principal_support
2349 to true in about:config
2351 win = window.open('/xul/server/cat/marcedit.xul', '_blank', 'chrome'); // XXX version?
2353 // Match marc2are.pl last_xact_id format, roughly
2355 var xact_id = 'IMPORT-' + Date.parse(now);
2358 "record": {"marc": marcxml, "rtype": "are"},
2360 "label": $('catStrings').getString('staff.cat.marcedit.save.label'),
2361 "func": function(xmlString) {
2362 var rec = new are();
2363 rec.marc(xmlString);
2364 rec.last_xact_id(xact_id);
2367 "oncomplete": function (r, objs) {
2368 var new_rec = objs[0];
2373 addNewAuthorityID(new_rec, sf, target);