plupload.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. (function($) {
  2. Drupal.plupload = Drupal.plupload || {};
  3. /**
  4. * Attaches the Plupload behavior to each FileField Sources Plupload form element.
  5. */
  6. Drupal.behaviors.filefield_sources_plupload = {
  7. attach: function (context, settings) {
  8. $(".filefield-source-plupload .plupload-element", context).once('ffs-plupload-init', function () {
  9. // Merge the default settings and the element settings to get a full
  10. // settings object to pass to the Plupload library for this element.
  11. var id = $(this).attr('id');
  12. var defaultSettings = settings.plupload['_default'] ? settings.plupload['_default'] : {};
  13. var elementSettings = (id && settings.plupload[id]) ? settings.plupload[id] : {};
  14. var pluploadSettings = $.extend({}, defaultSettings, elementSettings);
  15. // Initialize Plupload for this element.
  16. $(this).pluploadQueue(pluploadSettings);
  17. // Hide upload button. We will do this using uploader.start()
  18. $(this).find('.plupload_start').hide();
  19. // While we are at it, hide the redundant file validation help
  20. $(this).closest('.filefield-source-plupload').find('div.description').hide();
  21. // Intercept the submit to start uploading and ensure all files are done
  22. // uploading before triggering the ajax form update.
  23. var $submit = $(this).closest('.filefield-source-plupload').find('input.form-submit');
  24. $submit.bind('click', function(e) {
  25. e.preventDefault();
  26. var uploader_element = $(this).closest('.filefield-source-plupload').find('.plupload-element');
  27. var uploader = uploader_element.pluploadQueue();
  28. uploader.bind('StateChanged', function() {
  29. if (uploader.total.uploaded == uploader.files.length) {
  30. // Custom ajax trigger
  31. $submit.trigger('pud_update');
  32. }
  33. });
  34. if (uploader.files.length > 0) {
  35. $submit.val(Drupal.t('Uploading...'));
  36. uploader.start();
  37. }
  38. return false;
  39. });
  40. });
  41. }
  42. }
  43. })(jQuery);