From e03ce2f99670b75a7f3e0709dd8705a2a3f4625e Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 31 Aug 2005 18:37:30 +0000 Subject: - Patch #28483 by Steven: JavaScript enabled uploading. Comment from Steven: It does this by redirecting the submission of the form to a hidden '; + 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 */ @@ -117,6 +153,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 */ @@ -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); +} diff --git a/misc/progress.gif b/misc/progress.gif new file mode 100644 index 000000000..3564e7b06 Binary files /dev/null and b/misc/progress.gif differ diff --git a/misc/progress.js b/misc/progress.js new file mode 100644 index 000000000..b4e4c9039 --- /dev/null +++ b/misc/progress.js @@ -0,0 +1,80 @@ +/** + * A progressbar object. Initialized with the given id. Must be inserted into + * the DOM afterwards through progressBar.element. + * + * e.g. pb = new progressBar('myProgressBar'); + * some_element.appendChild(pb.element); + */ +function progressBar(id) { + var pb = this; + this.id = id; + + this.element = document.createElement('div'); + 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) { + var divs = this.element.getElementsByTagName('div'); + for (i in divs) { + if (percentage >= 0) { + if (hasClass(divs[i], 'filled')) { + divs[i].style.width = percentage + '%'; + } + if (hasClass(divs[i], 'percentage')) { + divs[i].innerHTML = percentage + '%'; + } + } + if (hasClass(divs[i], 'status')) { + divs[i].innerHTML = status; + } + } +} + +/** + * Start monitoring progress via Ajax. + */ +progressBar.prototype.startMonitoring = function (uri, delay) { + this.delay = delay; + this.uri = uri; + this.sendPing(); +} + +/** + * Stop monitoring progress via Ajax. + */ +progressBar.prototype.stopMonitoring = function () { + clearTimeout(this.timer); +} + +/** + * Request progress data from server. + */ +progressBar.prototype.sendPing = function () { + if (this.timer) { + clearTimeout(this.timer); + } + HTTPGet(this.uri, this.receivePing, this); +} + +/** + * HTTP callback function. Passes data back to the progressbar and sets a new + * timer for the next ping. + */ +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('|') : []; + if (matches.length >= 2) { + pb.setProgress(matches[0], matches[1]); + } + pb.timer = setTimeout(function() { pb.sendPing(); }, pb.delay); +} diff --git a/misc/upload.js b/misc/upload.js new file mode 100644 index 000000000..48f403448 --- /dev/null +++ b/misc/upload.js @@ -0,0 +1,59 @@ +// Global killswitch +if (isJsEnabled()) { + addLoadEvent(uploadAutoAttach); +} + +/** + * Attaches the upload behaviour to the upload form. + */ +function uploadAutoAttach() { + var acdb = []; + var inputs = document.getElementsByTagName('input'); + for (i = 0; input = inputs[i]; i++) { + if (input && hasClass(input, 'upload')) { + var uri = input.value; + var button = input.id.substr(5); + var wrapper = button + '-wrapper'; + var hide = button + '-hide'; + var upload = new jsUpload(uri, button, wrapper, hide); + } + } +} + +/** + * JS upload object. + */ +function jsUpload(uri, button, wrapper, hide) { + var upload = this; + this.button = button; + this.wrapper = wrapper; + this.hide = hide; + redirectFormButton(uri, $(button), this); +} + +/** + * Handler for the form redirection submission. + */ +jsUpload.prototype.onsubmit = function () { + var hide = $(this.hide); + // Insert progressbar and stretch to take the same space. + this.progress = new progressBar('uploadprogress'); + this.progress.setProgress(-1, 'Uploading file...'); + this.progress.element.style.width = '28em'; + this.progress.element.style.height = hide.offsetHeight +'px'; + hide.parentNode.insertBefore(this.progress.element, hide); + // Hide file form + hide.style.display = 'none'; +} + +/** + * Handler for the form redirection completion. + */ +jsUpload.prototype.oncomplete = function (data) { + // Remove progressbar + removeNode(this.progress); + this.progress = null; + // Replace form and re-attach behaviour + $(this.wrapper).innerHTML = data; + uploadAutoAttach(); +} \ No newline at end of file diff --git a/modules/upload.module b/modules/upload.module index 3cf56d16c..335fd8dfc 100644 --- a/modules/upload.module +++ b/modules/upload.module @@ -60,6 +60,12 @@ function upload_menu($may_cache) { 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM ); + $items[] = array( + 'path' => 'upload/js', + 'callback' => 'upload_js', + 'access' => user_access('upload files'), + 'type' => MENU_CALLBACK + ); } else { // Add handlers for previewing new uploads. @@ -378,8 +384,18 @@ function upload_delete($node) { } function upload_form($node) { + drupal_add_js('misc/progress.js'); + drupal_add_js('misc/upload.js'); + + $output = '
'. _upload_form($node) .'
'; + + return '
'. form_group_collapsible(t('File attachments'), $output, empty($node->files), t('Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.')) .'
'; +} + +function _upload_form($node) { $header = array(t('Delete'), t('List'), t('Url'), t('Size')); $rows = array(); + $output = ''; if (is_array($node->files)) { foreach ($node->files as $key => $file) { @@ -393,15 +409,19 @@ function upload_form($node) { } if (count($node->files)) { - $output = theme('table', $header, $rows); + $output .= theme('table', $header, $rows); } if (user_access('upload files')) { + $output .= '
'; $output .= form_file(t('Attach new file'), "upload", 40); $output .= form_button(t('Attach'), 'fileop'); + // The class triggers the js upload behaviour. + $output .= form_hidden('fileop', url('upload/js', NULL, NULL, TRUE), 'edit', array('class' => 'upload')); + $output .= '
'; } - return '
'. form_group_collapsible(t('File attachments'), $output, empty($node->files), t('Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.')) .'
'; + return $output; } function upload_load($node) { @@ -438,4 +458,16 @@ function _upload_image($file) { return $file; } - +/** + * Menu-callback for JavaScript-based uploads. + */ +function upload_js() { + // We only do the upload.module part of the node validation process. + $node = array2object($_POST['edit']); + upload_nodeapi(&$node, 'validate', NULL); + $output = theme('status_messages') . _upload_form($node); + + // We send the updated file attachments form. + print drupal_call_js('window.parent.iframeHandler', $output); + exit; +} diff --git a/modules/upload/upload.module b/modules/upload/upload.module index 3cf56d16c..335fd8dfc 100644 --- a/modules/upload/upload.module +++ b/modules/upload/upload.module @@ -60,6 +60,12 @@ function upload_menu($may_cache) { 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM ); + $items[] = array( + 'path' => 'upload/js', + 'callback' => 'upload_js', + 'access' => user_access('upload files'), + 'type' => MENU_CALLBACK + ); } else { // Add handlers for previewing new uploads. @@ -378,8 +384,18 @@ function upload_delete($node) { } function upload_form($node) { + drupal_add_js('misc/progress.js'); + drupal_add_js('misc/upload.js'); + + $output = '
'. _upload_form($node) .'
'; + + return '
'. form_group_collapsible(t('File attachments'), $output, empty($node->files), t('Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.')) .'
'; +} + +function _upload_form($node) { $header = array(t('Delete'), t('List'), t('Url'), t('Size')); $rows = array(); + $output = ''; if (is_array($node->files)) { foreach ($node->files as $key => $file) { @@ -393,15 +409,19 @@ function upload_form($node) { } if (count($node->files)) { - $output = theme('table', $header, $rows); + $output .= theme('table', $header, $rows); } if (user_access('upload files')) { + $output .= '
'; $output .= form_file(t('Attach new file'), "upload", 40); $output .= form_button(t('Attach'), 'fileop'); + // The class triggers the js upload behaviour. + $output .= form_hidden('fileop', url('upload/js', NULL, NULL, TRUE), 'edit', array('class' => 'upload')); + $output .= '
'; } - return '
'. form_group_collapsible(t('File attachments'), $output, empty($node->files), t('Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.')) .'
'; + return $output; } function upload_load($node) { @@ -438,4 +458,16 @@ function _upload_image($file) { return $file; } - +/** + * Menu-callback for JavaScript-based uploads. + */ +function upload_js() { + // We only do the upload.module part of the node validation process. + $node = array2object($_POST['edit']); + upload_nodeapi(&$node, 'validate', NULL); + $output = theme('status_messages') . _upload_form($node); + + // We send the updated file attachments form. + print drupal_call_js('window.parent.iframeHandler', $output); + exit; +} -- cgit v1.2.3