2006-03-09 23:20:24 +00:00
|
|
|
// $Id$
|
|
|
|
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
/**
|
|
|
|
* Attaches the upload behaviour to the upload form.
|
|
|
|
*/
|
2006-08-31 23:31:25 +00:00
|
|
|
Drupal.uploadAutoAttach = function() {
|
|
|
|
$('input.upload').each(function () {
|
|
|
|
var uri = this.value;
|
|
|
|
// Extract the base name from the id (edit-attach-url -> attach).
|
|
|
|
var base = this.id.substring(5, this.id.length - 4);
|
|
|
|
var button = base + '-button';
|
|
|
|
var wrapper = base + '-wrapper';
|
|
|
|
var hide = base + '-hide';
|
|
|
|
var upload = new Drupal.jsUpload(uri, button, wrapper, hide);
|
|
|
|
});
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JS upload object.
|
|
|
|
*/
|
2006-08-31 23:31:25 +00:00
|
|
|
Drupal.jsUpload = function(uri, button, wrapper, hide) {
|
|
|
|
// Note: these elements are replaced after an upload, so we re-select them
|
|
|
|
// everytime they are needed.
|
|
|
|
this.button = '#'+ button;
|
|
|
|
this.wrapper = '#'+ wrapper;
|
|
|
|
this.hide = '#'+ hide;
|
|
|
|
Drupal.redirectFormButton(uri, $(this.button).get(0), this);
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the form redirection submission.
|
|
|
|
*/
|
2006-08-31 23:31:25 +00:00
|
|
|
Drupal.jsUpload.prototype.onsubmit = function () {
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
// Insert progressbar and stretch to take the same space.
|
2006-08-31 23:31:25 +00:00
|
|
|
this.progress = new Drupal.progressBar('uploadprogress');
|
2006-01-22 17:37:41 +00:00
|
|
|
this.progress.setProgress(-1, 'Uploading file');
|
2006-08-31 23:31:25 +00:00
|
|
|
|
|
|
|
var hide = this.hide;
|
|
|
|
var el = this.progress.element;
|
|
|
|
var offset = $(hide).get(0).offsetHeight;
|
|
|
|
$(el).css({
|
|
|
|
width: '28em',
|
|
|
|
height: offset +'px',
|
|
|
|
paddingTop: '10px',
|
|
|
|
display: 'none'
|
|
|
|
});
|
|
|
|
$(hide).css('position', 'absolute');
|
|
|
|
|
|
|
|
$(hide).after(el);
|
|
|
|
$(el).fadeIn('slow');
|
|
|
|
$(hide).fadeOut('slow');
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the form redirection completion.
|
|
|
|
*/
|
2006-08-31 23:31:25 +00:00
|
|
|
Drupal.jsUpload.prototype.oncomplete = function (data) {
|
|
|
|
// Remove old form
|
|
|
|
Drupal.freezeHeight(); // Avoid unnecessary scrolling
|
|
|
|
$(this.wrapper).html('');
|
|
|
|
|
|
|
|
// Place HTML into temporary div
|
|
|
|
var div = document.createElement('div');
|
|
|
|
$(div).html(data);
|
|
|
|
|
|
|
|
// If uploading the first attachment fade in everything
|
|
|
|
if ($('tr', div).size() == 2) {
|
|
|
|
// Replace form and re-attach behaviour
|
|
|
|
$(div).hide();
|
|
|
|
$(this.wrapper).append(div);
|
|
|
|
$(div).fadeIn('slow');
|
|
|
|
Drupal.uploadAutoAttach();
|
|
|
|
}
|
|
|
|
// Else fade in only the last table row
|
|
|
|
else {
|
|
|
|
// Hide form and last table row
|
|
|
|
$('table tr:last-of-type td', div).hide();
|
|
|
|
|
|
|
|
// Note: workaround because jQuery's #id selector does not work outside of 'document'
|
|
|
|
// Should be: $(this.hide, div).hide();
|
|
|
|
var hide = this.hide;
|
|
|
|
$('div', div).each(function() {
|
|
|
|
if (('#'+ this.id) == hide) {
|
|
|
|
this.style.display = 'none';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Replace form, fade in items and re-attach behaviour
|
|
|
|
$(this.wrapper).append(div);
|
|
|
|
$('table tr:last-of-type td', div).fadeIn('slow');
|
|
|
|
$(this.hide, div).fadeIn('slow');
|
|
|
|
Drupal.uploadAutoAttach();
|
|
|
|
}
|
|
|
|
Drupal.unfreezeHeight();
|
2005-10-07 06:11:12 +00:00
|
|
|
}
|
2006-02-05 19:04:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the form redirection error.
|
|
|
|
*/
|
2006-08-31 23:31:25 +00:00
|
|
|
Drupal.jsUpload.prototype.onerror = function (error) {
|
2006-02-05 19:04:58 +00:00
|
|
|
alert('An error occurred:\n\n'+ error);
|
|
|
|
// Remove progressbar
|
2006-08-31 23:31:25 +00:00
|
|
|
$(this.progress.element).remove();
|
2006-02-05 19:04:58 +00:00
|
|
|
this.progress = null;
|
|
|
|
// Undo hide
|
2006-08-31 23:31:25 +00:00
|
|
|
$(this.hide).css({
|
|
|
|
position: 'static',
|
|
|
|
left: '0px'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Global killswitch
|
|
|
|
if (Drupal.jsEnabled) {
|
|
|
|
$(document).ready(Drupal.uploadAutoAttach);
|
2006-02-05 19:04:58 +00:00
|
|
|
}
|