Advertisement
Zane-wego

Droppable

May 16th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.84 KB | None | 0 0
  1. $.fn.droppable = function(func, options){
  2.  
  3.         if(typeof this != "undefined"){
  4.  
  5.             if(typeof $.fn.droppable.files == "undefined")
  6.                 $.fn.droppable.files = {};
  7.  
  8.             if(typeof $.fn.droppable.index == "undefined")
  9.                 $.fn.droppable.index = 0;
  10.            
  11.             var elem = this;
  12.  
  13.             // console.debug(options);
  14.             var settings = $.extend({
  15.                 upload_on_drop: true,
  16.                 show_upload_button: false,
  17.                 form: elem.find("form").first(),
  18.                 url: "/upload.php",
  19.                 backgroundColor: "rgba(150,150,150,0.7)",
  20.                 elementsColor: "#FFF",
  21.                 selector: ".droppable",
  22.                 arrayName: "files",
  23.                 method: "POST",
  24.                 dataType: "json",
  25.                 customData:{},
  26.                 headers: {},
  27.                 onSuccess: function(){},
  28.                 onError: function(){},
  29.                 onStart: function(e, files, from){},
  30.                 onProgress: function(e, files, from){},
  31.                 onDownloadProgress: function(e, files, from){}
  32.             }, options);
  33.  
  34.             if(func == null){
  35.  
  36.                 if(typeof this.attr("data-droppable") == "undefined"){
  37.                     this.attr("data-droppable", $.fn.droppable.index);
  38.                     $.fn.droppable.files[this.attr("data-droppable")] = [];
  39.                     $.fn.droppable.index+=1;
  40.  
  41.                     this.on("dragover", function(e){
  42.  
  43.                         e.preventDefault();
  44.                         e.stopPropagation();
  45.  
  46.                         var dt = e.originalEvent.dataTransfer;                    
  47.                         e.originalEvent.dataTransfer.dropEffect = 'copy';
  48.                         if (dt.types && (dt.types.indexOf ? dt.types.indexOf('Files') != -1 : dt.types.contains('Files'))) {
  49.                            
  50.                             elem.css("background-color", settings.backgroundColor);
  51.                             elem.find("*").css("color", settings.elementsColor);                        
  52.                         }
  53.                     });
  54.  
  55.                     this.on("dragleave", function(e){
  56.  
  57.                         var dt = e.originalEvent.dataTransfer;
  58.                         e.preventDefault();
  59.                         e.stopPropagation();
  60.                         elem.css("background-color", "");
  61.                         elem.find("*").css("color", "");
  62.                        
  63.                     });
  64.  
  65.                     this.on("dragend", function(e){
  66.  
  67.                         var dt = e.originalEvent.dataTransfer;
  68.                         e.preventDefault();
  69.                         e.stopPropagation();                    
  70.                         elem.css("background-color", "");
  71.                         elem.find("*").css("color", "");                                                
  72.                        
  73.                     });
  74.  
  75.                     this.on("drop", function(e){
  76.  
  77.                         var dt = e.originalEvent.dataTransfer;
  78.  
  79.                         e.preventDefault();
  80.                         e.stopPropagation();
  81.                         elem.css("background-color", "");
  82.                         elem.find("*").css("color", "");  
  83.  
  84.                         if (dt.types && (dt.types.indexOf ? dt.types.indexOf('Files') != -1 : dt.types.contains('Files'))) {
  85.                                                        
  86.                             for(f in dt.files){
  87.  
  88.                                 if(typeof dt.files[f] == "object" && dt.files[f] instanceof File)                                
  89.                                     $.fn.droppable.files[elem.attr("data-droppable")].push(dt.files[f]);
  90.                             }                            
  91.                         }
  92.  
  93.                         if(settings.upload_on_drop === true && $.fn.droppable.files[elem.attr("data-droppable")].length > 0){
  94.  
  95.                             elem.droppable("upload", settings);
  96.                         }
  97.                     });
  98.  
  99.                     // console.debug($(settings.form));
  100.                     if($(settings.form).length > 0){                        
  101.  
  102.                         $(settings.form).find("input[type='file']").on("change", function(e){                            
  103.  
  104.                             if($(this).val() != ""){
  105.  
  106.                                 var fil= $(this)[0].files;
  107.                                
  108.                                 for(f in fil)
  109.                                     $.fn.droppable.files[elem.attr("data-droppable")].push(fil[f]);
  110.  
  111.                                 if(settings.upload_on_drop == true){
  112.  
  113.                                     elem.droppable("upload", settings);
  114.                                 }
  115.                                
  116.                             }
  117.                         });                        
  118.                     }
  119.                 }
  120.             }
  121.  
  122.             if(func == "upload"){
  123.  
  124.                
  125.                 var files = $.fn.droppable.files[this.attr("data-droppable")];
  126.                 var fData = new FormData();
  127.                
  128.                 for(f in files){
  129.                    
  130.                     if(typeof files[f] == "object" && files[f] instanceof File)
  131.                         fData.append(settings.arrayName+"[]",files[f]);
  132.                 }
  133.  
  134.                 for(s in settings.customData){
  135.                     fData.append(s, settings.customData[s]);
  136.                 }
  137.  
  138.  
  139.  
  140.                 ajaxCall(settings.form, fData, {
  141.                     type: settings.method,
  142.                     dataType: settings.dataType,
  143.                     url: settings.url,
  144.                     beforeSend: function(e) {
  145.                         settings.onStart.bind(elem).call(elem, e, files, settings.form);
  146.                     },
  147.                     contentType: false,
  148.                     processData: false,
  149.                     xhr: function(e){
  150.                         var xhr = $.ajaxSettings.xhr();
  151.  
  152.                         xhr.upload.onprogress = function(e) {
  153.                             settings.onProgress.call(elem, e, files, settings.form);
  154.                             // console.log(Math.floor(e.loaded / e.total *100) + '%');
  155.                         };
  156.  
  157.                         xhr.onprogress = function(e){
  158.                             settings.onDownloadProgress.call(elem, e, files, settings.from);
  159.                         };
  160.  
  161.                         return xhr;
  162.                     },
  163.                     success:settings.onSuccess.bind(settings.form),
  164.                     error:settings.onError.bind(settings.form)
  165.                 });
  166.  
  167.                 $.fn.droppable.files[this.attr("data-droppable")] = [];
  168.  
  169.                 return this;
  170.             }
  171.  
  172.             return this;
  173.         }
  174.  
  175.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement