plupload.js 6.3 KB

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