plupload.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. (function($) {
  2. Drupal.plupload = Drupal.plupload || {};
  3. /**
  4. * Attaches the Plupload behavior to each Plupload form element.
  5. */
  6. Drupal.behaviors.plupload = {
  7. attach: function (context, settings) {
  8. $(".plupload-element", context).once('plupload-init', function () {
  9. var $this = $(this);
  10. // Merge the default settings and the element settings to get a full
  11. // settings object to pass to the Plupload library for this element.
  12. var id = $this.attr('id');
  13. var defaultSettings = settings.plupload['_default'] ? settings.plupload['_default'] : {};
  14. var elementSettings = (id && settings.plupload[id]) ? settings.plupload[id] : {};
  15. var pluploadSettings = $.extend({}, defaultSettings, elementSettings);
  16. // Do additional requirements testing to prevent a less than ideal runtime
  17. // from being used. For example, the Plupload library treats Firefox 3.5
  18. // as supporting HTML 5, but this is incorrect, because Firefox 3.5
  19. // doesn't support the 'multiple' attribute for file input controls. So,
  20. // if settings.plupload._requirements.html5.mozilla = '1.9.2', then we
  21. // remove 'html5' from pluploadSettings.runtimes if $.browser.mozilla is
  22. // true and if $.browser.version is less than '1.9.2'.
  23. if (settings.plupload['_requirements'] && pluploadSettings.runtimes) {
  24. var runtimes = pluploadSettings.runtimes.split(',');
  25. var filteredRuntimes = [];
  26. for (var i = 0; i < runtimes.length; i++) {
  27. var includeRuntime = true;
  28. if (settings.plupload['_requirements'][runtimes[i]]) {
  29. var requirements = settings.plupload['_requirements'][runtimes[i]];
  30. for (var browser in requirements) {
  31. if ($.browser[browser] && Drupal.plupload.compareVersions($.browser.version, requirements[browser]) < 0) {
  32. includeRuntime = false;
  33. }
  34. }
  35. }
  36. if (includeRuntime) {
  37. filteredRuntimes.push(runtimes[i]);
  38. }
  39. }
  40. pluploadSettings.runtimes = filteredRuntimes.join(',');
  41. }
  42. // Initialize Plupload for this element.
  43. $this.pluploadQueue(pluploadSettings);
  44. });
  45. }
  46. };
  47. /**
  48. * Attaches the Plupload behavior to each Plupload form element.
  49. */
  50. Drupal.behaviors.pluploadform = {
  51. attach: function(context, settings) {
  52. $('form', context).once('plupload-form', function() {
  53. if (0 < $(this).find('.plupload-element').length) {
  54. var $form = $(this);
  55. var originalFormAttributes = {
  56. 'method': $form.attr('method'),
  57. 'enctype': $form.attr('enctype'),
  58. 'action': $form.attr('action'),
  59. 'target': $form.attr('target')
  60. };
  61. $(this).submit(function(e) {
  62. var completedPluploaders = 0;
  63. var totalPluploaders = $(this).find('.plupload-element').length;
  64. var errors = '';
  65. $(this).find('.plupload-element').each( function(index){
  66. var uploader = $(this).pluploadQueue();
  67. var id = $(this).attr('id');
  68. var defaultSettings = settings.plupload['_default'] ? settings.plupload['_default'] : {};
  69. var elementSettings = (id && settings.plupload[id]) ? settings.plupload[id] : {};
  70. var pluploadSettings = $.extend({}, defaultSettings, elementSettings);
  71. //Only allow the submit to proceed if there are files and they've all
  72. //completed uploading.
  73. //TODO: Implement a setting for whether the field is required, rather
  74. //than assuming that all are.
  75. if (uploader.state == plupload.STARTED) {
  76. errors += Drupal.t("Please wait while your files are being uploaded.");
  77. }
  78. else if (uploader.files.length == 0 && !pluploadSettings.required) {
  79. completedPluploaders++;
  80. }
  81. else if (uploader.files.length == 0) {
  82. errors += Drupal.t("@index: You must upload at least one file.\n",{'@index': (index + 1)});
  83. }
  84. else if (uploader.files.length > 0 && uploader.total.uploaded == uploader.files.length) {
  85. completedPluploaders++;
  86. }
  87. else {
  88. var stateChangedHandler = function() {
  89. if (uploader.total.uploaded == uploader.files.length) {
  90. uploader.unbind('StateChanged', stateChangedHandler);
  91. completedPluploaders++;
  92. if (completedPluploaders == totalPluploaders ) {
  93. //Plupload's html4 runtime has a bug where it changes the
  94. //attributes of the form to handle the file upload, but then
  95. //fails to change them back after the upload is finished.
  96. for (var attr in originalFormAttributes) {
  97. $form.attr(attr, originalFormAttributes[attr]);
  98. }
  99. // Click a specific element if one is specified.
  100. if (settings.plupload[id].submit_element) {
  101. $(settings.plupload[id].submit_element).click();
  102. }
  103. else {
  104. $form.submit();
  105. }
  106. return true;
  107. }
  108. }
  109. };
  110. uploader.bind('StateChanged', stateChangedHandler);
  111. uploader.start();
  112. }
  113. });
  114. if (completedPluploaders == totalPluploaders) {
  115. //Plupload's html4 runtime has a bug where it changes the
  116. //attributes of the form to handle the file upload, but then
  117. //fails to change them back after the upload is finished.
  118. for (var attr in originalFormAttributes) {
  119. $form.attr(attr, originalFormAttributes[attr]);
  120. }
  121. return true;
  122. }
  123. else if (0 < errors.length){
  124. alert(errors);
  125. }
  126. return false;
  127. });
  128. }
  129. });
  130. }
  131. };
  132. /**
  133. * Helper function to compare version strings.
  134. *
  135. * Returns one of:
  136. * - A negative integer if a < b.
  137. * - A positive integer if a > b.
  138. * - 0 if a == b.
  139. */
  140. Drupal.plupload.compareVersions = function (a, b) {
  141. a = a.split('.');
  142. b = b.split('.');
  143. // Return the most significant difference, if there is one.
  144. for (var i=0; i < Math.min(a.length, b.length); i++) {
  145. var compare = parseInt(a[i]) - parseInt(b[i]);
  146. if (compare != 0) {
  147. return compare;
  148. }
  149. }
  150. // If the common parts of the two version strings are equal, the greater
  151. // version number is the one with the most sections.
  152. return a.length - b.length;
  153. }
  154. })(jQuery);