/** Stores the name of event handlers that we will process when chaning id values for an element. */ hyf.editabletable.eventNames = ['onclick', 'onfocus', 'onblur', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup', 'onmousemove', 'onkeypress', 'onkeyup', 'onkeydown', 'onchange']; /** * Changes the name/ids etc of every HTML component in the given element * so that references to oldId in the strings are replaced with newId. */ hyf.editabletable.changeIds = function(elem, oldId, newId) { if (elem.nodeType == 1) { //QUESTION: Should we just check all attributes? if (elem.getAttribute('id') != null) elem.setAttribute('id', hyf.editabletable.replaceString(elem.getAttribute('id'), oldId, newId)); if (elem.getAttribute('name') != null) { if ((dojo.isIE < 8) && ((elem.tagName.toLowerCase() == 'input') || (elem.tagName.toLowerCase() == 'a') || (elem.tagName.toLowerCase() == 'select') || (elem.tagName.toLowerCase() == 'textarea'))) { //IE versions before 8 do not support dynamicaly changing the name attribute properly, so instead need to recreate the element with the new name //We use the outerHTML IE property below, but that will not include any events that wern't in the original HTML string. //Also, if any events have been added to via dojo.connect the initial content will no longer be present. //Therefore, for now we just check for this scenario to try and restore the original eevent content //This still means that events added to this element could be lost. for (var i = 0; i < hyf.editabletable.eventNames.length; ++i) { var eventName = hyf.editabletable.eventNames[i]; if (elem[eventName] && (typeof(elem[eventName].target) != 'undefined') && (elem[eventName].target != null)) { var funcString = '' + dojo._ie_listener.handlers[elem[eventName].target]; funcString = dojo.trim(funcString.substring(funcString.indexOf('{') +1, funcString.lastIndexOf('}'))); elem.setAttribute(eventName, funcString); } } var newHTML = hyf.editabletable.replaceString(elem.outerHTML, oldId, newId); var tempDiv = document.createElement('div'); tempDiv.innerHTML = newHTML; newElem = tempDiv.firstChild; elem.parentNode.replaceChild(newElem, elem); elem = newElem; } else elem.setAttribute('name', hyf.editabletable.replaceString(elem.getAttribute('name'), oldId, newId)); } if (elem.getAttribute('href') != null) elem.setAttribute('href', hyf.editabletable.replaceString(elem.getAttribute('href'), oldId, newId)); //process any event handlers defined for this element, as the event script may include reference to the field or repeat name //which needs to be updated (eg our default events include the repeatId) for (var i = 0; i < hyf.editabletable.eventNames.length; ++i) { var eventName = hyf.editabletable.eventNames[i]; if (elem[eventName] || elem.getAttribute(eventName) != null) { //if dojo.connect has been used to add additional functions to the event //then in ie, the actual event script which needs to be updated is stored elsewhere if (dojo.isIE && elem[eventName].target) { var funcString = '' + dojo._ie_listener.handlers[elem[eventName].target]; //check the function needs converting if (funcString.indexOf(oldId) != -1) { var converted = hyf.editabletable.replaceString(funcString, oldId, newId); converted = converted.substring(converted.indexOf('{') +1, converted.lastIndexOf('}')); dojo._ie_listener.handlers[elem[eventName].target] = Function(converted); } } else if (typeof(elem.getAttribute(eventName)) == 'function') { var funcString = '' + elem.getAttribute(eventName); //check the function needs converting if (funcString.indexOf(oldId) != -1) { var converted = hyf.editabletable.replaceString(funcString, oldId, newId); converted = dojo.trim(converted.substring(converted.indexOf('{') +1, converted.lastIndexOf('}'))); elem[eventName] = Function(converted); } } else if (typeof(elem.getAttribute(eventName)) == 'string') { elem.setAttribute(eventName, hyf.editabletable.replaceString(elem.getAttribute(eventName), oldId, newId)); } } } if (elem.getAttribute('for') != null) elem.setAttribute('for', hyf.editabletable.replaceString(elem.getAttribute('for'), oldId, newId)); if (elem.getAttribute('htmlFor') != null) elem.setAttribute('htmlFor', hyf.editabletable.replaceString(elem.getAttribute('htmlFor'), oldId, newId)); if (elem.getAttribute('_element') != null) elem.setAttribute('_element', hyf.editabletable.replaceString(elem.getAttribute('_element'), oldId, newId)); //if this is a hidden field storing the html content, then we need to update this html string value //to contain the new id where needed if ((elem.tagName.toLowerCase() == 'input') && (elem.type == 'hidden') && (elem.id.lastIndexOf(hyf.editabletable.HTML_CONTENT_ID_END) + hyf.editabletable.HTML_CONTENT_ID_END.length == elem.id.length)) { elem.value = hyf.editabletable.replaceString(elem.value, oldId, newId); } //now process any children elements for (var i = 0 ;i < elem.childNodes.length; ++i) { hyf.editabletable.changeIds(elem.childNodes.item(i), oldId, newId); } } }