function moveEventFunction(p_id,p_curr_event,p_new_event,conditionalFunc){
/* Get instance of the tag we are modifying. */
var formItem = document.getElementById(p_id);
var eventFunc = null;
/* Get text of target event handler code */
var existingEventCode = formItem.attributes[p_curr_event].value;
/* Only execute the move if the targeted event handler is populated */
if (existingEventCode.length > 0){
/* Convert text of existing event handler back into a working function */
var existingFunction = new Function(existingEventCode);
/* Create a function that will assign a new event handler to the desired item event.
* This needs to be dynamic since we can only make a direct assignment under "normal"
* circumstances. Ex. node.onclick= or node.onchange
*/
var addNewEvent = new Function("node","eventfunc","node."+p_new_event+"=eventfunc;");
/* We assign "nothing" to the existing event handler. This must be dynamic
* for the same reason as the function above.
*/
var removeExistingEvent = new Function("node","node."+p_curr_event+"='';");
/* If a conditinal function (boolean function that can halt the event ...)
* is provided, then wrap it around the existing code. Otherwise, execute
* the original handler under the new event.
*/
if (typeof(conditionalFunc) != "undefined"){
eventFunc = function(){ if (conditionalFunc()){ existingFunction()}};
}else{
eventFunc = existingFunction;
}
/* Add the new event handler */
addNewEvent(formItem,eventFunc);
/* Remove the old event handler */
removeExistingEvent(formItem);
}
}
The function, moveEventFunction, will move or swap the code assigned to one event handler and move it to a new event handler on the same node (or tag). The function also gives the developer the option of wrapping a new conditional (Boolean) function around the original event handler code.
Now that we have a solution, how do we apply it? You have a couple of options. First, place the call to moveEventFunction at the bottom of the page (between script tags). It will fire after all of the ADF/JSF HTML has been rendered.
<script>
moveEventFunction(<id of target tag>,<existing event name>,<new event name>,<optional conditional function>);
</script>
However, if you plan on making the target tag a partial target (dynamic refresh), you will lose the swap. The second option handles this situation. Place the call on the onFocus event of the target tag. Doing so guarantees it the swap.
onFocus="moveEventFunction(this.id,<existing event name>,<new event name>,<optional conditional function>);"
The conditional function (the last parameter) can perform multiple tasks prior to executing the main function (original function). In the case of the scenario above, the conditional function would check for the "Hot Key" that triggers our auto population. However, it must return true or false at the end of its execution. The conditional function needs to be defined in the manner:
var fnc_MyConditionalFunction = function MyConditionalFunction(){ ...}
This will allows allows you to pass the actual function as a parameter without it actually being executed (or evaluated) . Instead of passing in MyConditionalFunction, you pass in fnc_MyConditionalFunction.
(Another option that isn't JavaScript related might be to get an instance of the UIComponent prior to page rendering and make the swap ... haven't tried that yet.)