plup.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. (function ($) {
  2. // Bind removing of file to selector elements
  3. function plup_remove_item(selector) {
  4. $(selector).bind('click', function(event) {
  5. var parent = $(this).parent();
  6. var parentsParent = parent.parent();
  7. parent.remove();
  8. parentsParent.trigger('formUpdated');
  9. });
  10. }
  11. // Bind resize effect on title and alt fields on focus
  12. function plup_resize_input(selector) {
  13. var w = $(selector).outerWidth();
  14. if (w < 300) {
  15. $(selector).bind('focus', function(event) {
  16. $(this).css('z-index', 10).animate({'width': '300px'}, 300);
  17. });
  18. $(selector).bind('blur', function(event) {
  19. $(this).removeAttr('style');
  20. });
  21. }
  22. }
  23. Drupal.behaviors.plup = {
  24. attach: function(context, settings) {
  25. // Apply Plupload to each form element instance
  26. $.each(settings.plup, function(plupName, plupInstance) {
  27. var cid = plupInstance.container;
  28. var id = '#' + cid;
  29. // Apply setting only once
  30. $(id).once('plup', function() {
  31. /**
  32. * Initial tasks
  33. */
  34. $(id +' .plup-list').sortable(); // Activate sortable
  35. $(id +'-plup-upload').hide(); // Hide upload button
  36. var uploader = new plupload.Uploader(plupInstance); // Create Plupload instance
  37. plup_remove_item(id +' .plup-remove-item'); // Bind remove functionality on existing images
  38. plup_resize_input(id +' .plup-list > li > input.form-text'); // Bind resizing effect on existing inputs
  39. /**
  40. * Events
  41. */
  42. // Initialization
  43. uploader.bind('Init', function(up, params) {
  44. // Nothing to do, just reminder of the presence for this event
  45. });
  46. // Starting upload
  47. uploader.bind('Start', function(up, files) {
  48. // Nothing to do, just reminder of the presence for this event
  49. });
  50. // Upload process state
  51. uploader.bind('StateChanged', function(up) {
  52. if (up.state == 2) {
  53. $(id +' .plup-progress').progressbar(); // Activate progressbar
  54. }
  55. if (up.state == 1) {
  56. $(id +' .plup-progress').progressbar('destroy'); // Destroy progressbar
  57. $(id +'-plup-upload').hide(); // Hide upload button
  58. $(id +' .plup-drag-info').show(); // Show info
  59. }
  60. })
  61. // Files were added into queue(only CURRENTLY added files)
  62. uploader.bind('FilesAdded', function(up, files) {
  63. $(id +' .plup-drag-info').hide(); // Hide info
  64. $(id +'-plup-upload').show(); // Show upload button
  65. // Put files visually into queue
  66. $.each(files, function(i, file) {
  67. $(id +'-plup-filelist table').append('<tr id="' + file.id + '">' +
  68. '<td class="plup-filelist-file">' + file.name + '</td>' +
  69. '<td class="plup-filelist-size">' + plupload.formatSize(file.size) + '</td>' +
  70. '<td class="plup-filelist-message"></td>' +
  71. '<td class="plup-filelist-remove"><a class="plup-remove-file"></a></td>' +
  72. '</tr>');
  73. // Bind "remove" functionality to files added into queue
  74. $(id +' #' + file.id + ' .plup-filelist-remove > a').bind('click', function(event) {
  75. $('#' + file.id).remove();
  76. up.removeFile(file);
  77. });
  78. });
  79. up.refresh(); // Refresh for flash or silverlight
  80. });
  81. // File was removed from queue
  82. // Doc states this ARE files but actually it IS a file
  83. uploader.bind('FilesRemoved', function(up, file) {
  84. // If there are not files in queue
  85. if (up.files.length == 0) {
  86. $(id +'-plup-upload').hide(); // Hide upload button
  87. $(id +' .plup-drag-info').show(); // Show info
  88. }
  89. });
  90. // File is being uploaded
  91. uploader.bind('UploadProgress', function(up, file) {
  92. // Refresh progressbar
  93. $(id +' .plup-progress').progressbar({value: uploader.total.percent});
  94. });
  95. // Error event
  96. uploader.bind('Error', function(up, err) {
  97. $(id +' #' + err.file.id + ' > .plup-filelist-message').append('<b>Error: ' + err.message + '</b>');
  98. up.refresh(); // Refresh for flash or silverlight
  99. });
  100. // Event after a file has been uploaded from queue
  101. uploader.bind('FileUploaded', function(up, file, response) {
  102. // Response is an object with response parameter so 2x response
  103. var fileSaved = $.parseJSON(response.response);
  104. var delta = $(id +' .plup-list li').length;
  105. var name = plupName +'[' + delta + ']';
  106. // Plupload has weird error handling behavior so we have to check for errors here
  107. if (fileSaved.error_message) {
  108. $(id +' #' + file.id + ' > .plup-filelist-message').append('<b>Error: ' + fileSaved.error_message + '</b>');
  109. up.refresh(); // Refresh for flash or silverlight
  110. return;
  111. }
  112. $(id +'-plup-filelist #' + file.id).remove(); // Remove uploaded file from queue
  113. var title_field = plupInstance.title_field ? '<input title="'+ Drupal.t('Title') +'" type="text" class="form-text plup-title" name="'+ name + '[title]" value="" />' : '';
  114. var alt_field = plupInstance.alt_field ? '<input title="'+ Drupal.t('Alternative text') +'" type="text" class="form-text plup-alt" name="'+ name + '[alt]" value="" />' : '';
  115. // Add image thumbnail into list of uploaded items
  116. $(id +' .plup-list').append(
  117. '<li class="ui-state-default">' +
  118. '<div class="plup-thumb-wrapper"><img src="'+ plupInstance.image_style_path + file.target_name + '" title="'+ Drupal.checkPlain(file.target_name) +'" /></div>' +
  119. '<a class="plup-remove-item"></a>' +
  120. title_field +
  121. alt_field +
  122. '<input type="hidden" name="' + name + '[fid]" value="' + fileSaved.fid + '" />' +
  123. '<input type="hidden" name="' + name + '[weight]" value="' + delta + '" />' +
  124. '<input type="hidden" name="' + name + '[rename]" value="' + file.name +'" />' +
  125. '</li>');
  126. // Bind remove functionality to uploaded file
  127. var new_element = $('input[name="'+ name +'[fid]"]');
  128. var remove_element = $(new_element).siblings('.plup-remove-item');
  129. plup_remove_item(remove_element);
  130. // Bind resize effect to inputs of uploaded file
  131. var text_element = $(new_element).siblings('input.form-text');
  132. plup_resize_input(text_element);
  133. // Tell Drupal that form has been updated
  134. new_element.trigger('formUpdated');
  135. });
  136. // All fields from queue has been uploaded
  137. uploader.bind('UploadComplete', function(up, files) {
  138. $(id +' .plup-list').sortable('refresh'); // Refresh sortable
  139. $(id +' .plup-drag-info').show(); // Show info
  140. });
  141. /**
  142. * Additional tasks
  143. */
  144. // Upload button functionality
  145. $('#'+ plupInstance.upload).click(function(e) {
  146. // Forbid uploading more than the allowed number of files, if set
  147. if (plupInstance.max_files > 0) {
  148. var uploaded = $(id +' .plup-list li').length;
  149. var selected = $(id +'-plup-filelist td.plup-filelist-file').length;
  150. var allowed = plupInstance.max_files - uploaded;
  151. if ((selected + uploaded) > plupInstance.max_files) {
  152. alert(Drupal.formatPlural(allowed, 'You can upload only 1 file.', 'You can upload only @count files.'));
  153. return;
  154. }
  155. }
  156. uploader.start();
  157. e.preventDefault();
  158. });
  159. // Initialize Plupload
  160. uploader.init();
  161. // Change weight values for images when reordering using sortable
  162. $(id +' .plup-list').bind('sortupdate', function(event, ui) {
  163. $(this).find('li').each(function(index) {
  164. $(this).find('input[name$="[weight]"]').val(index);
  165. });
  166. });
  167. });
  168. });
  169. }
  170. }
  171. })(jQuery);