3 // Add an option to a select form field
4 function add_option(select, text, value, selected)
6 var option = document.createElement('option');
9 if (selected) option.selected = true;
11 select.add(option, null);
19 // Return the value of a parameter from the url
20 function returnValueParam(param)
22 var params = location.search.substr(1);
23 var arr = params.split("&");
24 var pattern = param + "=";
25 for (var i=0; i < arr.length; i++) {
26 if (arr[i].indexOf(pattern) == 0) {
27 return unescape(arr[i].substr(pattern.length));
34 // Return a value from a position on the result string
35 function returnValuePosFromResult(result, pos)
38 if ((index = pos.indexOf("-")) > 0) {
39 var ini = parseInt(pos.substring(0, index) ,10);
40 var end = parseInt(pos.substr(index + 1) ,10);
41 return result.substring(ini, end + 1);
43 return result.substr(pos, 1);
45 }//returnValuePosFromResult
48 // Build string from form fields
49 function returnResultFromValues(form)
51 var resultStr = form.result.value;
54 for (var i=0; i < form.elements.length; i++) {
55 var pattern = new RegExp("f[0-9]+(?:[0-9]+)?");
56 if (pattern.test(form.elements[i].name)) {
57 pos = form.elements[i].name.substr(1);
58 value = (pos.indexOf("-") > 0)?form.elements[i].value:form.elements[i].options[form.elements[i].selectedIndex].value;
59 resultStr = changePosResult(pos, value, resultStr);
63 }//returnResultFromValues
66 // Build/modify result string for a position and a value
67 function changePosResult(pos, value, resultStr)
71 if ((index = pos.indexOf("-")) > 0) {
72 var ini = parseInt(pos.substring(0, index) ,10);
73 var end = parseInt(pos.substr(index + 1) ,10);
74 var roffset = (1 + end - ini)- value.length;
75 if (roffset > 0) for (var i=0; i < roffset; i++) value += " ";
77 result = value + resultStr.substr(end + 1);
79 result = resultStr.substring(0, ini) + value;
80 if (end < resultStr.length)
81 result += resultStr.substr(end + 1);
84 var ini = parseInt(pos, 10);
86 result = value + resultStr.substr(1);
88 result = resultStr.substring(0, ini) + value;
89 if (ini < resultStr.length)
90 result += resultStr.substr(ini + 1);
93 result = result.replace(/#/g, " ");
98 // Display the result string on a row of a table indicating positions and coloring them if they are incorrect or they are selected
99 function renderResult(tr_result, result)
103 if (tr_result.cells.length != result.length) {
104 for (var i = tr_result.cells.length - 1; i >= 0; i--)
105 tr_result.deleteCell(i);
106 for (var i=0; i < result.length; i++) {
107 value = result.charAt(i);
108 td = tr_result.insertCell(tr_result.cells.length);
114 var args = renderResult.arguments;
115 var whiteAllTD = false;
116 if (args.length > 2) {
117 if (typeof(args[2]) == "boolean") {
118 whiteAllTD = args[2];
121 if ((index = args[2].indexOf("-")) > 0) {
122 ini = parseInt(args[2].substring(0, index) ,10);
123 end = parseInt(args[2].substr(index + 1) ,10);
124 } else ini = parseInt(args[2], 10);
127 for (var i=0; i < result.length; i++) {
128 value = result.charAt(i);
129 td = tr_result.cells[i];
130 if (td.style.backgroundColor != "yellow" || whiteAllTD) td.style.backgroundColor = "white";
131 td.innerHTML = (value == " ")?" ":value;
132 td.title = "Pos " + i + ". Value: \"" + value + "\"";
135 if (ini <= i && i <= end) td.style.backgroundColor = "#cccccc";
136 } else if (i == ini) td.style.backgroundColor = "#cccccc";
138 var pos = (i < 10)?'0' + i:i + '';
140 if ((obj = document.getElementById('f' + pos)) != null) {
142 for (var j=0; j < obj.options.length && !found; j++)
143 if (obj.options[j].value == value) found = true;
145 td.style.backgroundColor = "yellow";
146 td.title = "Pos " + i + ". Incorrect Value: \"" + value + "\"";
155 // Change displaying of result in the page
156 function changeH4Result(form, h4_result, tr_result, pos, value)
158 var resultStr = form.result.value;
159 var result = changePosResult(pos, value, resultStr);
160 renderResult(tr_result, result, pos);
161 h4_result.innerHTML = """ + result + """;
162 form.result.value = result;
167 // Class to read the xml and render the type of material
171 xmlControlField = function(tagfield, form_id, select, table, h4_result, tr_result, idMaterial, themelang)
173 this.tagfield = tagfield;
174 this.idMaterial = idMaterial;
175 this.form_id = form_id;
176 this.form = document.getElementById(form_id);
177 this.select = select;
179 this.h4_result = h4_result;
180 this.tr_result = tr_result;
181 this.themelang = themelang;
185 xmlControlField.prototype =
199 setIdMaterial: function(idMaterial)
201 this.idMaterial = idMaterial;
204 loadXmlValues: function()
206 this.xmlDoc = $.ajax({
208 url: this.themelang + "/modules/cataloguing/value_builder/marc21_field_" + this.tagfield + ".xml",
212 if (this.xmlDoc) this.renderTemplate();
213 $("*").ajaxError(function(evt, request, settings){
214 alert(_("AJAX error: receiving data from ") + settings.url);
219 renderTemplate: function()
221 this.root = this.xmlDoc.documentElement;
222 if (this.root.nodeName == "Tagfield" && this.root.nodeType == 1 && this.root.hasChildNodes()) {
223 var tag = this.root.attributes.getNamedItem("tag").nodeValue;
224 var nodeMaterial = this.root.getElementsByTagName('Material');
225 if (nodeMaterial != null && nodeMaterial.length > 0) {
226 if (this.idMaterial == "") this.idMaterial = nodeMaterial[0].attributes.getNamedItem("id").nodeValue;
227 this.renderSelectMaterial(nodeMaterial);
228 this.renderPositions(nodeMaterial, (this.form.result.value != "")?this.form.result.value:returnValueParam("result"));
234 renderSelectMaterial: function(nodeMaterial)
236 if (this.select != null && nodeMaterial != null && nodeMaterial.length > 0) {
237 if (this.select.options.length <= 1) {
240 var arrSort = new Array();
241 var arrEquiv = new Array();
242 for (var i=0; i < nodeMaterial.length; i++) {
243 id = nodeMaterial[i].attributes.getNamedItem("id").nodeValue;
244 name = nodeMaterial[i].attributes.getNamedItem("name").nodeValue;
250 for (var i=0; i < arrSort.length; i++) {
251 j = arrEquiv[arrSort[i]];
252 add_option(this.select, arrSort[i] + " - " + nodeMaterial[j].attributes.getNamedItem("name").nodeValue, arrSort[i], (this.idMaterial != "" && arrSort[i] == this.idMaterial)?true:false);
254 } else if (this.idMaterial != "") {
255 for (var i=0; i < this.select.options.length; i++) {
256 if (this.select.options[i].value == this.idMaterial) this.select.options[i].selected = true;
260 },//renderSelectMaterial
263 renderPositions: function(nodeMaterial, result)
267 var resultXPath = this.xmlDoc.evaluate("//a:Material[@id='" + this.idMaterial + "']", this.xmlDoc.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
268 materialNode = resultXPath.singleNodeValue;
270 for (var i=0; i < nodeMaterial.length; i++) {
271 if (this.idMaterial == nodeMaterial[i].attributes.getNamedItem("id").nodeValue) {
272 materialNode = nodeMaterial[i];
277 if (this.table != null) { // Render table
278 var tbody = this.table.tBodies[0];
280 if (tbody.rows.length > 0)
281 for (var i = tbody.rows.length - 1; i >= 1; i--)
283 // Parse Material node
284 if (materialNode != undefined && materialNode != null && materialNode.nodeType == 1 && materialNode.hasChildNodes()) {
285 var nodePos = materialNode.firstChild;
291 var strInnerHTML = "";
295 while (nodePos != null) {
296 if (nodePos.nodeType == 1 && nodePos.nodeName == "Position") {
297 tr = tbody.insertRow(tbody.rows.length);
298 td = tr.insertCell(tr.cells.length);
299 pos = nodePos.attributes.getNamedItem("pos").nodeValue;
300 title = ((nodePos.getAttributeNode("description") || nodePos.hasAttribute("description")) && nodePos.getAttribute("description") != "")?nodePos.attributes.getNamedItem("description").nodeValue:nodePos.attributes.getNamedItem("name").nodeValue;
302 url = ((nodePos.getAttributeNode("url") || nodePos.hasAttribute("url")) && nodePos.getAttribute("url") != "" && nodePos.getAttribute("urltext") != "")?" <a href='" + nodePos.attributes.getNamedItem("url").nodeValue + "' target='_blank'>" + nodePos.attributes.getNamedItem("urltext").nodeValue + "</a>":"";
303 } catch (e) { url = "";}
304 td.innerHTML = "<label for='" + pos + "' title='" + title + "'>" + pos + " - " + nodePos.attributes.getNamedItem("name").nodeValue + url + "</label>";
305 td = tr.insertCell(tr.cells.length);
306 value = returnValuePosFromResult(result, pos);
307 if ((index = pos.indexOf("-")) > 0) { // Position interval
308 var ini = parseInt(pos.substring(0, index) ,10);
309 var end = parseInt(pos.substr(index + 1) ,10);
310 value = value.replace(/ /g, "#");
311 strInnerHTML = "<input type='text' name='f" + pos + "' id='f" + pos + "' value='" + value + "' size='" + (1 + end - ini) + "' maxlength='" + (1 + end - ini) + "' onkeyup='this.value = this.value.replace(/ /g, \"#\"); changeH4Result(document.getElementById(\"" + this.form_id + "\"), document.getElementById(\"" + this.h4_result + "\"), document.getElementById(\"" + this.tr_result + "\"), \"" + pos + "\", this.value)' onfocus='changeH4Result(document.getElementById(\"" + this.form_id + "\"), document.getElementById(\"" + this.h4_result + "\"), document.getElementById(\"" + this.tr_result + "\"), \"" + pos + "\", this.value)' />";
313 strInnerHTML = "<select name='f" + pos + "' id='f" + pos + "' style='width:400px' onchange='changeH4Result(document.getElementById(\"" + this.form_id + "\"), document.getElementById(\"" + this.h4_result + "\"), document.getElementById(\"" + this.tr_result + "\"), \"" + pos + "\", this.options[this.selectedIndex].value)' onfocus='changeH4Result(document.getElementById(\"" + this.form_id + "\"), document.getElementById(\"" + this.h4_result + "\"), document.getElementById(\"" + this.tr_result + "\"), \"" + pos + "\", this.options[this.selectedIndex].value)'>";
314 value = value.replace("#", " ");
315 if (nodePos.hasChildNodes()) {
316 var nodeValue = nodePos.firstChild;
317 while (nodeValue != null) {
318 if (nodeValue.nodeType == 1 && nodeValue.nodeName == "Value" && nodeValue.hasChildNodes()) {
319 var valNode = nodeValue.firstChild.nodeValue;
320 valNode = valNode.replace("#", " ");
321 selected = (value == valNode)?"selected='selected'":"";
322 strInnerHTML += "<option value='" + valNode + "' " + selected + ">" + nodeValue.firstChild.nodeValue + " - " + nodeValue.attributes.getNamedItem("description").nodeValue + "</option>";
324 nodeValue = nodeValue.nextSibling;
327 strInnerHTML += "<option value=' ' " + ((value == " ")?"selected='selected'":"") + "># - " + title + "</option>";
328 strInnerHTML += "<option value='|' " + ((value == "|")?"selected='selected'":"") + ">| - " + title + "</option>";
330 strInnerHTML += "</select>";
332 td.innerHTML = strInnerHTML;
334 nodePos = nodePos.nextSibling;