plupload.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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': (index1)});
  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. $form.submit();
  100. return true;
  101. }
  102. }
  103. };
  104. uploader.bind('StateChanged', stateChangedHandler);
  105. uploader.start();
  106. }
  107. });
  108. if (completedPluploaders == totalPluploaders) {
  109. //Plupload's html4 runtime has a bug where it changes the
  110. //attributes of the form to handle the file upload, but then
  111. //fails to change them back after the upload is finished.
  112. for (var attr in originalFormAttributes) {
  113. $form.attr(attr, originalFormAttributes[attr]);
  114. }
  115. return true;
  116. }
  117. else if (0 < errors.length){
  118. alert(errors);
  119. }
  120. return false;
  121. });
  122. }
  123. });
  124. }
  125. };
  126. /**
  127. * Helper function to compare version strings.
  128. *
  129. * Returns one of:
  130. * - A negative integer if a < b.
  131. * - A positive integer if a > b.
  132. * - 0 if a == b.
  133. */
  134. Drupal.plupload.compareVersions = function (a, b) {
  135. a = a.split('.');
  136. b = b.split('.');
  137. // Return the most significant difference, if there is one.
  138. for (var i=0; i < Math.min(a.length, b.length); i++) {
  139. var compare = parseInt(a[i]) - parseInt(b[i]);
  140. if (compare != 0) {
  141. return compare;
  142. }
  143. }
  144. // If the common parts of the two version strings are equal, the greater
  145. // version number is the one with the most sections.
  146. return a.length - b.length;
  147. }
  148. })(jQuery);