diff options
Diffstat (limited to 'misc/drupal.js')
-rw-r--r-- | misc/drupal.js | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/misc/drupal.js b/misc/drupal.js index 80414afb0..4007817c4 100644 --- a/misc/drupal.js +++ b/misc/drupal.js @@ -102,6 +102,42 @@ function HTTPPost(uri, object, callbackFunction, callbackParameter) { } /** + * Redirects a button's form submission to a hidden iframe and displays the result + * in a given wrapper. The iframe should contain a call to + * window.parent.iframeHandler() after submission. + */ +function redirectFormButton(uri, button, handler) { + // Insert the iframe + var div = document.createElement('div'); + div.innerHTML = '<iframe name="redirect-target" id="redirect-target" src="" style="width:0px;height:0px;border:0;"></iframe>'; + button.parentNode.appendChild(div); + + // Trap the button + button.onfocus = function() { + button.onclick = function() { + // Prepare vars for use in anonymous function. + var button = this; + var action = button.form.action; + var target = button.form.target; + // Redirect form submission + this.form.action = uri; + this.form.target = 'redirect-target'; + handler.onsubmit(); + // Set iframe handler for later + window.iframeHandler = function (data) { + // Restore form submission + button.form.action = action; + button.form.target = target; + handler.oncomplete(data); + } + } + } + button.onblur = function() { + button.onclick = null; + } +} + +/** * Adds a function to the window onload event */ function addLoadEvent(func) { @@ -118,6 +154,21 @@ function addLoadEvent(func) { } /** + * Adds a function to the window onload event + */ +function addSubmitEvent(form, func) { + var oldSubmit = form.onsubmit; + if (typeof oldSubmit != 'function') { + form.onsubmit = func; + } + else { + form.onsubmit = function() { + return oldSubmit() && func(); + } + } +} + +/** * Retrieves the absolute position of an element on the screen */ function absolutePosition(el) { @@ -196,7 +247,7 @@ function eregReplace(search, replace, subject) { */ function removeNode(node) { if (typeof node == 'string') { - node = document.getElementById(node); + node = $(node); } if (node && node.parentNode) { return node.parentNode.removeChild(node); @@ -205,3 +256,10 @@ function removeNode(node) { return false; } } + +/** + * Wrapper around document.getElementById(). + */ +function $(id) { + return document.getElementById(id); +} |