Guest User

Untitled

a guest
Aug 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. <%= form_for :vehicle_listing_images, :html => { :multipart => true } do |f| %>
  2. <%= hidden_field_tag :listing_id, @listing_id %>
  3. <div id="file_upload_area">
  4. </div>
  5. <% end %>
  6.  
  7. <div id="image-container">
  8. <button id="primary_photo" class="btn right"><strong>Select Primary photo</strong></button>
  9. <input type="hidden" name="vehicle_listing[primary_photo_id]" value="<%= @listing.primary_photo_id %>" id="primary_photo_id" />
  10. <ul id="images">
  11. <% unless @images.blank? %>
  12. <% @images.each do |image| %>
  13. <li>
  14. <a class="image_link" href="<%= image.vehicle.url %>">
  15. <img class="<%= 'primary_photo' if @listing.primary_photo_id == image.id %>" src="<%= image.vehicle.url(:thumb) %>" />
  16. </a>
  17. <a href="<%= vehicle_listing_image_path(image) %>" class="remove_image">Remove</a>
  18. </li>
  19. <% end %>
  20. <% end %>
  21. </ul>
  22. </div>
  23. <%= stylesheet_link_tag "jquery.plupload.queue"%>
  24. <script type="text/javascript" src="//bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
  25. <script type="text/javascript">
  26. // Convert divs to queue widgets when the DOM is ready
  27. <% if !current_user.blank? %>
  28. <% if current_user.is_dealer? || current_user.admin? %>
  29. var image_limit = "10";
  30. <% else %>
  31. var image_limit = "<%= @product.product_vehicle_listing_detail.photos %>";
  32. <% end %>
  33. <% else %>
  34. var image_limit = "<%= @product.product_vehicle_listing_detail.photos %>";
  35. <% end %>
  36. var image_count = 0;
  37. $(function() {
  38. //RoR - capture authenticity token
  39. var atoken = $("input[name=authenticity_token]").val();
  40. $("#file_upload_area").pluploadQueue({
  41. // General settings
  42. runtimes : 'html5,gears,browserplus,html4',
  43. url : "<%= vehicle_listing_images_path %>",
  44. max_file_size : '10mb',
  45. chunk_size : '1mb',
  46. unique_names : true,
  47. multiple_queues : true,
  48. //RoR - make sure form is multipart
  49. multipart: true,
  50. //RoR - make sure to send authenticity token and any other parameters that are on the plain html form
  51. multipart_params : {"[listing_id]" : "<%= @listing.id %>", "[cart_id]" : "<%= @cart.id %>", authenticity_token : atoken},
  52.  
  53. // Specify what files to browse for
  54. filters : [
  55. {title : "Image files", extensions : "jpg,gif,png"},
  56. {title : "Zip files", extensions : "zip"}
  57. ],
  58. init: {
  59. BeforeUpload: function(up, file) {
  60. if (image_count >= image_limit) {
  61. up.stop();
  62. alert('Sorry, you may only upload a maximum of ' + image_limit + ' images');
  63. $(".plupload_upload_status").fadeOut(800, function(){
  64. $(".plupload_buttons").fadeIn(800);
  65. });
  66. }
  67. },
  68. FileUploaded: function(up, file, response) {
  69. image_count++
  70. data = jQuery.parseJSON(response.response);
  71. jQuery('#images').append('<li><a class="image_link" href="' + data.image_link + '">'
  72. + '<img src="' + data.image + '" /></a>'
  73. + '<input type="hidden" class="image_id" value="' + data.image_id + '" name="image_id" />'
  74. + '<a href="/vehicle_listing_images/' + data.image_id + '" class="remove_image">Remove</a></li>');
  75. }
  76. },
  77. });
  78.  
  79.  
  80.  
  81. // Client side form validation
  82. $('form').submit(function(e) {
  83. var uploader = $('#file_upload').pluploadQueue();
  84.  
  85. // Validate number of uploaded files
  86. if (uploader.total.uploaded == 0) {
  87. // Files in queue upload them first
  88. if (uploader.files.length > 0) {
  89. // When all files are uploaded submit form
  90. uploader.bind('UploadProgress', function() {
  91. if (uploader.total.uploaded == uploader.files.length)
  92. $('form').submit();
  93. });
  94.  
  95. uploader.start();
  96. } else
  97. alert('You must at least upload one file.');
  98.  
  99. e.preventDefault();
  100. }
  101. });
  102. jQuery('.remove_image').live('click', function(event) {
  103. event.preventDefault();
  104. var image = this;
  105. if (confirm('Are you sure you want to delete this image?')) {
  106. jQuery.post(this.href, { _method: 'delete' }, function(data) {
  107. if (data.status == 'success') {
  108. jQuery(image).parent().fadeOut('slow', function() {
  109. jQuery(this).remove();
  110. image_count--
  111. });
  112. } else {
  113. alert('We were unable to Delete the image, please try again');
  114. }
  115. }, 'json');
  116. }
  117. });
  118.  
  119. jQuery('.image_link').live('click', function(event) {
  120. event.preventDefault();
  121. if (!$(this).hasClass('primary_hover')) {
  122. jQuery.colorbox({ href: this.href });
  123. }
  124. });
  125. });
  126. </script>
Add Comment
Please, Sign In to add comment