Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 9.75 KB | None | 0 0
  1. //Image Upload Button
  2.     $("#image_file_input").change(function() {
  3.        // alert($('#image_file_input')[0].files[0]);
  4.        $('.uploader-loader').show();
  5.        $('.error-container').empty().hide();
  6.        $('.add-to-cart-form').hide();      
  7.  
  8.         var fd = new FormData();
  9.         var img = $('#image_file_input')[0].files[0];
  10.         var imgType = img.type;
  11.         var imgSize = img.size;
  12.        
  13.         if(imgType=='image/jpeg' || imgType=='image/png' ) {
  14.                var allowedSize = '20971520'; //1MB = 1000000bytes //20MB Max Allowed
  15.                 if(imgSize <=allowedSize ) {                          
  16.                         /* Upload Image*/
  17.                         fd.append( "file", $('#image_file_input')[0].files[0]);
  18.                         fd.append( "action", 'upload_image_enhanced');  
  19.                         $.ajax({
  20.                             url: ajaxjs.ajax_url,
  21.                             type: "POST",
  22.                             contentType: false,
  23.                             processData:false,
  24.                             data: fd,
  25.                             success: function(val) {
  26.                                 $.ajax({
  27.                                     url: ajaxjs.ajax_url,
  28.                                     type: "POST",
  29.                                     data: {action:'get_uploaded_html_preview','id':val,'size':'ssmall'},
  30.                                     success: function(html) {                        
  31.                                         getImgSizes(img);
  32.                                         getImgOrientation(img);
  33.                                         $('.add-to-cart-form').show();                    
  34.                                         $('#image-container').empty().html(html);
  35.                                         $("#image-container img").imageScale();
  36.                                         $('#attachment_id').val(val);
  37.                                         $('#product-title').val('Window Print');
  38.                                         $('.uploader-loader').hide();
  39.  
  40. //------------------------------------------------------------------------------
  41.  
  42.             //Since the image is now uploaded and displayed on the page as a preview, as well as the ".add-to-cart" form is now unhidden, we should select the image with Jcrop and perform functions. Not sure how to add that to this code here                                      
  43.                                                        
  44.                                     },
  45.                                     error: function() {
  46.                                         $('.uploader-loader').hide();                        
  47.                                         alert("An error occured, please try again.");        
  48.                                     }
  49.                                 });  
  50.                                
  51.                             },
  52.                             error: function() {
  53.                                 alert("An error occured, please try again.");        
  54.                             }
  55.                         });        
  56.                     } else {
  57.                     $('.uploader-loader').hide();            
  58.                     $('.error-container').empty().show().html('The uploaded file size is too high.');  
  59.                 }
  60.                
  61.         } else {
  62.             $('.uploader-loader').hide();            
  63.             $('.error-container').empty().show().html('The uploaded file format is not supported.');
  64.         }
  65.     });
  66.      
  67.     /*$('#cart-size').change(function(){
  68.         var size=$(this).val();
  69.         if(size==1) {
  70.             $('#cart-price').val('10');
  71.         }
  72.         if(size==2) {
  73.             $('#cart-price').val('20');
  74.         }
  75.         if(size==3) {
  76.             $('#cart-price').val('30');
  77.         }
  78.         if(size==4) {
  79.             $('#cart-price').val('40');
  80.         }
  81.         if(size==5) {
  82.             $('#cart-price').val('50');
  83.         }
  84.     });*/
  85.    
  86.     $('.crop').bind('click',function(){
  87.         $('#image-container').croppie(
  88.            
  89.         );
  90.        
  91.         return false;
  92.     });
  93.    
  94.     $('#add-to-cart').click(function(){
  95.         var attach_id = $('#attachment_id').val();
  96.         var productName = $('#product-title').val();
  97.         var cartSize = $('#cart-size').val();
  98.         var cartPrice = $('#cart-price').val();
  99.         var cartOrientation = $('#cart-orientation').val();
  100.        
  101.         $.ajax({
  102.             url: ajaxjs.ajax_url,
  103.             type: "POST",
  104.             data: {action:'add_product_to_buy','attach_id':attach_id,'productName':productName,'cartSize':cartSize,'cartPrice':cartPrice,'cartOrientation':cartOrientation},
  105.             success: function(text) {                        
  106.                 //After Add to cart redirect to Cart page
  107.                 //alert(text);
  108.                 $('.add-to-cart-form').hide();
  109.                 $('.error-container').show();
  110.                 $('.error-container').empty().html('<p>'+text+'</p>');
  111.             },
  112.             error: function() {
  113.                 alert("An error occured, please try again.");        
  114.             }
  115.         });
  116.         return false;
  117.     });
  118.    
  119. });
  120.  
  121.  
  122.  
  123. //---------------------------------------------------------------------------------------------
  124.  
  125. //Jcrop bit to add
  126.  
  127.  
  128. function initJcrop()//{{{
  129. {
  130.   // Hide any interface elements that require Jcrop
  131.   // (This is for the local user interface portion.)
  132.   $('.requiresjcrop').hide();
  133.  
  134.   // Invoke Jcrop in typical fashion
  135.   $('#target').Jcrop({
  136.     onRelease: releaseCheck
  137.   },function(){
  138.  
  139.     jcrop_api = this;
  140.     jcrop_api.animateTo([100,100,400,300]);
  141.  
  142.     // Setup and dipslay the interface for "enabled"
  143.     $('#can_click,#can_move,#can_size').attr('checked','checked');
  144.     $('#ar_lock,#size_lock,#bg_swap').attr('checked',false);
  145.     $('.requiresjcrop').show();
  146.  
  147.   });
  148.  
  149. };
  150. //}}}
  151.  
  152.  
  153. function getRandom() {
  154.   var dim = jcrop_api.getBounds();
  155.   return [
  156.     Math.round(Math.random() * dim[0]),
  157.     Math.round(Math.random() * dim[1]),
  158.     Math.round(Math.random() * dim[0]),
  159.     Math.round(Math.random() * dim[1])
  160.   ];
  161. };
  162.  
  163. // This function is bound to the onRelease handler...
  164. // In certain circumstances (such as if you set minSize
  165. // and aspectRatio together), you can inadvertently lose
  166. // the selection. This callback re-enables creating selections
  167. // in such a case. Although the need to do this is based on a
  168. // buggy behavior, it's recommended that you in some way trap
  169. // the onRelease callback if you use allowSelect: false
  170. function releaseCheck()
  171. {
  172.   jcrop_api.setOptions({ allowSelect: true });
  173.   $('#can_click').attr('checked',false);
  174. };
  175.  
  176. // Attach interface buttons
  177. // This may appear to be a lot of code but it's simple stuff
  178. $('#setSelect').click(function(e) {
  179.   // Sets a random selection
  180.   jcrop_api.setSelect(getRandom());
  181. });
  182. $('#animateTo').click(function(e) {
  183.   // Animates to a random selection
  184.   jcrop_api.animateTo(getRandom());
  185. });
  186. $('#release').click(function(e) {
  187.   // Release method clears the selection
  188.   jcrop_api.release();
  189. });
  190. $('#disable').click(function(e) {
  191.   // Disable Jcrop instance
  192.   jcrop_api.disable();
  193.   // Update the interface to reflect disabled state
  194.   $('#enable').show();
  195.   $('.requiresjcrop').hide();
  196. });
  197. $('#enable').click(function(e) {
  198.   // Re-enable Jcrop instance
  199.   jcrop_api.enable();
  200.   // Update the interface to reflect enabled state
  201.   $('#enable').hide();
  202.   $('.requiresjcrop').show();
  203. });
  204. $('#rehook').click(function(e) {
  205.   // This button is visible when Jcrop has been destroyed
  206.   // It performs the re-attachment and updates the UI
  207.   $('#rehook,#enable').hide();
  208.   initJcrop();
  209.   $('#unhook,.requiresjcrop').show();
  210.   return false;
  211. });
  212. $('#unhook').click(function(e) {
  213.   // Destroy Jcrop widget, restore original state
  214.   jcrop_api.destroy();
  215.   // Update the interface to reflect un-attached state
  216.   $('#unhook,#enable,.requiresjcrop').hide();
  217.   $('#rehook').show();
  218.   return false;
  219. });
  220.  
  221. // Hook up the three image-swapping buttons
  222. $('#img1').click(function(e) {
  223.   $(this).addClass('active').closest('.btn-group')
  224.     .find('button.active').not(this).removeClass('active');
  225.  
  226.   jcrop_api.setImage('demo_files/sago.jpg');
  227.   jcrop_api.setOptions({ bgOpacity: .6 });
  228.   return false;
  229. });
  230. $('#img2').click(function(e) {
  231.   $(this).addClass('active').closest('.btn-group')
  232.     .find('button.active').not(this).removeClass('active');
  233.  
  234.   jcrop_api.setImage('demo_files/pool.jpg');
  235.   jcrop_api.setOptions({ bgOpacity: .6 });
  236.   return false;
  237. });
  238. $('#img3').click(function(e) {
  239.   $(this).addClass('active').closest('.btn-group')
  240.     .find('button.active').not(this).removeClass('active');
  241.  
  242.   jcrop_api.setImage('demo_files/sago.jpg',function(){
  243.     this.setOptions({
  244.       bgOpacity: 1,
  245.       outerImage: 'demo_files/sagomod.jpg'
  246.     });
  247.     this.animateTo(getRandom());
  248.   });
  249.   return false;
  250. });
  251.  
  252. // The checkboxes simply set options based on it's checked value
  253. // Options are changed by passing a new options object
  254.  
  255. // Also, to prevent strange behavior, they are initially checked
  256. // This matches the default initial state of Jcrop
  257.  
  258. $('#can_click').change(function(e) {
  259.   jcrop_api.setOptions({ allowSelect: !!this.checked });
  260.   jcrop_api.focus();
  261. });
  262. $('#can_move').change(function(e) {
  263.   jcrop_api.setOptions({ allowMove: !!this.checked });
  264.   jcrop_api.focus();
  265. });
  266. $('#can_size').change(function(e) {
  267.   jcrop_api.setOptions({ allowResize: !!this.checked });
  268.   jcrop_api.focus();
  269. });
  270. $('#ar_lock').change(function(e) {
  271.   jcrop_api.setOptions(this.checked?
  272.     { aspectRatio: 4/3 }: { aspectRatio: 0 });
  273.   jcrop_api.focus();
  274. });
  275. $('#size_lock').change(function(e) {
  276.   jcrop_api.setOptions(this.checked? {
  277.     minSize: [ 80, 80 ],
  278.     maxSize: [ 350, 350 ]
  279.   }: {
  280.     minSize: [ 0, 0 ],
  281.     maxSize: [ 0, 0 ]
  282.   });
  283.   jcrop_api.focus();
  284. });
  285.  
  286. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement