From d38429248ee2cbaa442e396f251b1781acbc0d66 Mon Sep 17 00:00:00 2001 From: Steven Wittens Date: Sun, 5 Feb 2006 19:04:58 +0000 Subject: - #47510: Show JavaScript alert when PHP errors occur --- misc/drupal.js | 96 ++++++++++++++++++++++++++++++++++++++++++++------------ misc/progress.js | 26 ++++++++------- misc/update.js | 2 +- misc/upload.js | 15 ++++++++- 4 files changed, 106 insertions(+), 33 deletions(-) (limited to 'misc') diff --git a/misc/drupal.js b/misc/drupal.js index b962c89f7..d633fa56e 100644 --- a/misc/drupal.js +++ b/misc/drupal.js @@ -113,24 +113,8 @@ function HTTPPost(uri, callbackFunction, callbackParameter, object) { * window.parent.iframeHandler() after submission. */ function redirectFormButton(uri, button, handler) { - // Insert the iframe - // Note: some browsers require the literal name/id attributes on the tag, - // some want them set through JS. We do both. - var div = document.createElement('div'); - div.innerHTML = ''; - var iframe = div.firstChild; - with (iframe) { - name = 'redirect-target'; - setAttribute('name', 'redirect-target'); - id = 'redirect-target'; - } - with (iframe.style) { - position = 'absolute'; - height = '1px'; - width = '1px'; - visibility = 'hidden'; - } - document.body.appendChild(iframe); + // Make sure we have an iframe to target + createIframe(); // Trap the button button.onmouseover = button.onfocus = function() { @@ -147,11 +131,34 @@ function redirectFormButton(uri, button, handler) { handler.onsubmit(); // Set iframe handler for later - window.iframeHandler = function (data) { + window.iframeHandler = function () { + var iframe = $('redirect-target'); // Restore form submission button.form.action = action; button.form.target = target; - handler.oncomplete(data); + + // Get response from iframe body + try { + response = (iframe.contentWindow || iframe.contentDocument || iframe).document.body.innerHTML; + if (window.opera) { + // Opera-hack: it returns innerHTML sanitized. + response = response.replace(/"/g, '"'); + } + } + catch (e) { + response = null; + } + + // Recreate the iframe: re-using an old iframe can sometimes cause browser bugs. + createIframe(); + + response = parseJson(response); + // Check response code + if (response.status == 0) { + handler.onerror(response.data); + return; + } + handler.oncomplete(response.data); } return true; @@ -300,6 +307,55 @@ function stopEvent(event) { } } +/** + * Parse a JSON response. + * + * The result is either the JSON object, or an object with 'status' 0 and 'data' an error message. + */ +function parseJson(data) { + if (data.substring(0,1) != '{') { + return { status: 0, data: data.length ? data : 'Unspecified error' }; + } + return eval('(' + data + ');'); +} + +/** + * Create an invisible iframe for form submissions. + */ +function createIframe() { + // Delete any previous iframe + deleteIframe(); + // Note: some browsers require the literal name/id attributes on the tag, + // some want them set through JS. We do both. + window.iframeHandler = function () {}; + var div = document.createElement('div'); + div.id = 'redirect-holder'; + div.innerHTML = ''; + var iframe = div.firstChild; + with (iframe) { + name = 'redirect-target'; + setAttribute('name', 'redirect-target'); + id = 'redirect-target'; + } + with (iframe.style) { + position = 'absolute'; + height = '1px'; + width = '1px'; + visibility = 'hidden'; + } + document.body.appendChild(div); +} + +/** + * Delete the invisible iframe for form submissions. + */ +function deleteIframe() { + var holder = $('redirect-holder'); + if (typeof holder != 'undefined') { + removeNode(holder); + } +} + /** * Wrapper around document.getElementById(). */ diff --git a/misc/progress.js b/misc/progress.js index 940c32713..84979d68f 100644 --- a/misc/progress.js +++ b/misc/progress.js @@ -3,7 +3,7 @@ * the DOM afterwards through progressBar.element. * * method is the function which will perform the HTTP request to get the - * progress bar status. Either HTTPGet or HTTPPost. + * progress bar state. Either HTTPGet or HTTPPost. * * e.g. pb = new progressBar('myProgressBar'); * some_element.appendChild(pb.element); @@ -18,14 +18,14 @@ function progressBar(id, callback, method) { this.element.id = id; this.element.className = 'progress'; this.element.innerHTML = '
'+ - '
 
'+ + '
 
'+ '
'; } /** * Set the percentage and status message for the progressbar. */ -progressBar.prototype.setProgress = function (percentage, status) { +progressBar.prototype.setProgress = function (percentage, message) { var divs = this.element.getElementsByTagName('div'); var div; for (var i = 0; div = divs[i]; ++i) { @@ -37,12 +37,12 @@ progressBar.prototype.setProgress = function (percentage, status) { divs[i].innerHTML = percentage + '%'; } } - if (hasClass(divs[i], 'status')) { - divs[i].innerHTML = status; + if (hasClass(divs[i], 'message')) { + divs[i].innerHTML = message; } } if (this.callback) { - this.callback(percentage, status, this); + this.callback(percentage, message, this); } } @@ -84,12 +84,16 @@ progressBar.prototype.receivePing = function (string, xmlhttp, pb) { if (xmlhttp.status != 200) { return alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ pb.uri); } - // Split into values - var matches = string.length > 0 ? string.split('|') : []; - // Update progress - if (matches.length >= 2) { - pb.setProgress(matches[0], matches[1]); + // Parse response + var progress = parseJson(string); + // Display errors + if (progress.status == 0) { + alert(progress.data); + return; } + + // Update display + pb.setProgress(progress.percentage, progress.message); // Schedule next timer pb.timer = setTimeout(function() { pb.sendPing(); }, pb.delay); } diff --git a/misc/update.js b/misc/update.js index 117713b69..2586fbf50 100644 --- a/misc/update.js +++ b/misc/update.js @@ -13,7 +13,7 @@ if (isJsEnabled()) { } var progress = new progressBar('updateprogress', updateCallback, HTTPPost); - progress.setProgress(-1, 'Starting updates...'); + progress.setProgress(-1, 'Starting updates'); $('progress').appendChild(progress.element); progress.startMonitoring('update.php?op=do_update', 0); } diff --git a/misc/upload.js b/misc/upload.js index 9945803f6..dcf246dfb 100644 --- a/misc/upload.js +++ b/misc/upload.js @@ -54,9 +54,22 @@ jsUpload.prototype.onsubmit = function () { */ jsUpload.prototype.oncomplete = function (data) { // Remove progressbar - removeNode(this.progress); + removeNode(this.progress.element); this.progress = null; // Replace form and re-attach behaviour $(this.wrapper).innerHTML = data; uploadAutoAttach(); } + +/** + * Handler for the form redirection error. + */ +jsUpload.prototype.onerror = function (error) { + alert('An error occurred:\n\n'+ error); + // Remove progressbar + removeNode(this.progress.element); + this.progress = null; + // Undo hide + $(this.hide).style.position = 'static'; + $(this.hide).style.left = '0px'; +} -- cgit v1.2.3