From 50037826e6e1f1ede978be5449186d60e91a5482 Mon Sep 17 00:00:00 2001 From: yusanshi Date: Sat, 16 Nov 2019 21:41:46 +0800 Subject: [PATCH 1/3] Add support for pasting from clipboard --- dist/js/jquery.dm-uploader.min.js | 2 +- src/js/jquery.dm-uploader.js | 206 +++++++++++++++--------------- 2 files changed, 107 insertions(+), 101 deletions(-) diff --git a/dist/js/jquery.dm-uploader.min.js b/dist/js/jquery.dm-uploader.min.js index aca6194..9f8e8d2 100644 --- a/dist/js/jquery.dm-uploader.min.js +++ b/dist/js/jquery.dm-uploader.min.js @@ -8,4 +8,4 @@ * * @preserve */ -!function(e){"use strict";"function"==typeof define&&define.amd?define(["jquery"],e):"undefined"!=typeof exports?module.exports=e(require("jquery")):e(window.jQuery)}(function(e){"use strict";var t="dmUploader",n=0,i=1,s=2,o=3,r=4,u={auto:!0,queue:!0,dnd:!0,hookDocument:!0,multiple:!0,url:document.URL,method:"POST",extraData:{},headers:{},dataType:null,fieldName:"file",maxFileSize:0,allowedTypes:"*",extFilter:null,onInit:function(){},onComplete:function(){},onFallbackMode:function(){},onNewFile:function(){},onBeforeUpload:function(){},onUploadProgress:function(){},onUploadSuccess:function(){},onUploadCanceled:function(){},onUploadError:function(){},onUploadComplete:function(){},onFileTypeError:function(){},onFileSizeError:function(){},onFileExtError:function(){},onDragEnter:function(){},onDragLeave:function(){},onDocumentDragEnter:function(){},onDocumentDragLeave:function(){}},a=function(e,t){this.data=e,this.widget=t,this.jqXHR=null,this.status=n,this.id=Math.random().toString(36).substr(2)};a.prototype.upload=function(){var t=this;if(!t.canUpload())return t.widget.queueRunning&&t.status!==i&&t.widget.processQueue(),!1;var n=new FormData;n.append(t.widget.settings.fieldName,t.data);var s=t.widget.settings.extraData;return"function"==typeof s&&(s=s.call(t.widget.element,t.id)),e.each(s,function(e,t){n.append(e,t)}),t.status=i,t.widget.activeFiles++,t.widget.settings.onBeforeUpload.call(t.widget.element,t.id),t.jqXHR=e.ajax({url:t.widget.settings.url,type:t.widget.settings.method,dataType:t.widget.settings.dataType,data:n,headers:t.widget.settings.headers,cache:!1,contentType:!1,processData:!1,forceSync:!1,xhr:function(){return t.getXhr()},success:function(e){t.onSuccess(e)},error:function(e,n,i){t.onError(e,n,i)},complete:function(){t.onComplete()}}),!0},a.prototype.onSuccess=function(e){this.status=s,this.widget.settings.onUploadSuccess.call(this.widget.element,this.id,e)},a.prototype.onError=function(e,t,n){this.status!==r&&(this.status=o,this.widget.settings.onUploadError.call(this.widget.element,this.id,e,t,n))},a.prototype.onComplete=function(){this.widget.activeFiles--,this.status!==r&&this.widget.settings.onUploadComplete.call(this.widget.element,this.id),this.widget.queueRunning?this.widget.processQueue():this.widget.settings.queue&&0===this.widget.activeFiles&&this.widget.settings.onComplete.call(this.element)},a.prototype.getXhr=function(){var t=this,n=e.ajaxSettings.xhr();return n.upload&&n.upload.addEventListener("progress",function(e){var n=0,i=e.loaded||e.position,s=e.total||e.totalSize;e.lengthComputable&&(n=Math.ceil(i/s*100)),t.widget.settings.onUploadProgress.call(t.widget.element,t.id,n)},!1),n},a.prototype.cancel=function(e){e=void 0!==e&&e;var t=this.status;return!!(t===i||e&&t===n)&&(this.status=r,this.widget.settings.onUploadCanceled.call(this.widget.element,this.id),t===i&&this.jqXHR.abort(),!0)},a.prototype.canUpload=function(){return this.status===n||this.status===o};var l=function(t,n){return this.element=e(t),this.settings=e.extend({},u,n),this.checkSupport()?(this.init(),this):(e.error("Browser not supported by jQuery.dmUploader"),this.settings.onFallbackMode.call(this.element),!1)};l.prototype.checkSupport=function(){if(void 0===window.FormData)return!1;return!new RegExp("/(Android (1.0|1.1|1.5|1.6|2.0|2.1))|(Windows Phone (OS 7|8.0))|(XBLWP)|(ZuneWP)|(w(eb)?OSBrowser)|(webOS)|(Kindle/(1.0|2.0|2.5|3.0))/").test(window.navigator.userAgent)&&!e('').prop("disabled")},l.prototype.init=function(){var n=this;this.queue=[],this.queuePos=-1,this.queueRunning=!1,this.activeFiles=0,this.draggingOver=0,this.draggingOverDoc=0;var i=n.element.is("input[type=file]")?n.element:n.element.find("input[type=file]");return i.length>0&&(i.prop("multiple",this.settings.multiple),i.on("change."+t,function(t){var i=t.target&&t.target.files;i&&i.length&&(n.addFiles(i),e(this).val(""))})),this.settings.dnd&&this.initDnD(),0!==i.length||this.settings.dnd?(this.settings.onInit.call(this.element),this):(e.error("Markup error found by jQuery.dmUploader"),null)},l.prototype.initDnD=function(){var n=this;n.element.on("drop."+t,function(e){e.preventDefault(),n.draggingOver>0&&(n.draggingOver=0,n.settings.onDragLeave.call(n.element));var t=e.originalEvent&&e.originalEvent.dataTransfer;if(t&&t.files&&t.files.length){var i=[];n.settings.multiple?i=t.files:i.push(t.files[0]),n.addFiles(i)}}),n.element.on("dragenter."+t,function(e){e.preventDefault(),0===n.draggingOver&&n.settings.onDragEnter.call(n.element),n.draggingOver++}),n.element.on("dragleave."+t,function(e){e.preventDefault(),n.draggingOver--,0===n.draggingOver&&n.settings.onDragLeave.call(n.element)}),n.settings.hookDocument&&(e(document).off("drop."+t).on("drop."+t,function(e){e.preventDefault(),n.draggingOverDoc>0&&(n.draggingOverDoc=0,n.settings.onDocumentDragLeave.call(n.element))}),e(document).off("dragenter."+t).on("dragenter."+t,function(e){e.preventDefault(),0===n.draggingOverDoc&&n.settings.onDocumentDragEnter.call(n.element),n.draggingOverDoc++}),e(document).off("dragleave."+t).on("dragleave."+t,function(e){e.preventDefault(),n.draggingOverDoc--,0===n.draggingOverDoc&&n.settings.onDocumentDragLeave.call(n.element)}),e(document).off("dragover."+t).on("dragover."+t,function(e){e.preventDefault()}))},l.prototype.releaseEvents=function(){this.element.off("."+t),this.element.find("input[type=file]").off("."+t),this.settings.hookDocument&&e(document).off("."+t)},l.prototype.validateFile=function(t){if(this.settings.maxFileSize>0&&t.size>this.settings.maxFileSize)return this.settings.onFileSizeError.call(this.element,t),!1;if("*"!==this.settings.allowedTypes&&!t.type.match(this.settings.allowedTypes))return this.settings.onFileTypeError.call(this.element,t),!1;if(null!==this.settings.extFilter){var n=t.name.toLowerCase().split(".").pop();if(e.inArray(n,this.settings.extFilter)<0)return this.settings.onFileExtError.call(this.element,t),!1}return new a(t,this)},l.prototype.addFiles=function(e){for(var t=0,n=0;n=this.queue.length?(0===this.activeFiles&&this.settings.onComplete.call(this.element),this.queuePos=this.queue.length-1,this.queueRunning=!1,!1):(this.queueRunning=!0,this.queue[this.queuePos].upload())},l.prototype.restartQueue=function(){this.queuePos=-1,this.queueRunning=!1,this.processQueue()},l.prototype.findById=function(e){for(var t=!1,n=0;n').prop("disabled"))},l.prototype.init=function(){var t=this;this.queue=[],this.queuePos=-1,this.queueRunning=!1,this.activeFiles=0,this.draggingOver=0,this.draggingOverDoc=0;var n=t.element.is("input[type=file]")?t.element:t.element.find("input[type=file]");return n.length>0&&(n.prop("multiple",this.settings.multiple),n.on("change.dmUploader",function(n){var i=n.target&&n.target.files;i&&i.length&&(t.addFiles(i),e(this).val(""))})),this.settings.dnd&&this.initDnD(),this.settings.paste&&this.initPaste(),0!==n.length||this.settings.dnd||this.settings.paste?(this.settings.onInit.call(this.element),this):(e.error("Markup error found by jQuery.dmUploader"),null)},l.prototype.initDnD=function(){var t=this;t.element.on("drop.dmUploader",function(e){e.preventDefault(),t.draggingOver>0&&(t.draggingOver=0,t.settings.onDragLeave.call(t.element));var n=e.originalEvent&&e.originalEvent.dataTransfer;if(n&&n.files&&n.files.length){var i=[];t.settings.multiple?i=n.files:i.push(n.files[0]),t.addFiles(i)}}),t.element.on("dragenter.dmUploader",function(e){e.preventDefault(),0===t.draggingOver&&t.settings.onDragEnter.call(t.element),t.draggingOver++}),t.element.on("dragleave.dmUploader",function(e){e.preventDefault(),t.draggingOver--,0===t.draggingOver&&t.settings.onDragLeave.call(t.element)}),t.settings.hookDocument&&(e(document).off("drop.dmUploader").on("drop.dmUploader",function(e){e.preventDefault(),t.draggingOverDoc>0&&(t.draggingOverDoc=0,t.settings.onDocumentDragLeave.call(t.element))}),e(document).off("dragenter.dmUploader").on("dragenter.dmUploader",function(e){e.preventDefault(),0===t.draggingOverDoc&&t.settings.onDocumentDragEnter.call(t.element),t.draggingOverDoc++}),e(document).off("dragleave.dmUploader").on("dragleave.dmUploader",function(e){e.preventDefault(),t.draggingOverDoc--,0===t.draggingOverDoc&&t.settings.onDocumentDragLeave.call(t.element)}),e(document).off("dragover.dmUploader").on("dragover.dmUploader",function(e){e.preventDefault()}))},l.prototype.initPaste=function(){var t=this;e(document).on("paste.dmUploader",function(e){e.preventDefault();var n=e.originalEvent&&e.originalEvent.clipboardData;if(n&&n.files&&n.files.length){var i=[n.files[0]];t.addFiles(i)}}),t.settings.hookDocument},l.prototype.releaseEvents=function(){this.element.off(".dmUploader"),this.element.find("input[type=file]").off(".dmUploader"),this.settings.hookDocument&&e(document).off(".dmUploader")},l.prototype.validateFile=function(t){if(this.settings.maxFileSize>0&&t.size>this.settings.maxFileSize)return this.settings.onFileSizeError.call(this.element,t),!1;if("*"!==this.settings.allowedTypes&&!t.type.match(this.settings.allowedTypes))return this.settings.onFileTypeError.call(this.element,t),!1;if(null!==this.settings.extFilter){var n=t.name.toLowerCase().split(".").pop();if(e.inArray(n,this.settings.extFilter)<0)return this.settings.onFileExtError.call(this.element,t),!1}return new u(t,this)},l.prototype.addFiles=function(e){for(var t=0,n=0;n=this.queue.length?(0===this.activeFiles&&this.settings.onComplete.call(this.element),this.queuePos=this.queue.length-1,this.queueRunning=!1,!1):(this.queueRunning=!0,this.queue[this.queuePos].upload())},l.prototype.restartQueue=function(){this.queuePos=-1,this.queueRunning=!1,this.processQueue()},l.prototype.findById=function(e){for(var t=!1,n=0;n").prop("disabled"); }; - DmUploader.prototype.init = function() - { + DmUploader.prototype.init = function () { var widget = this; // Queue vars @@ -279,10 +269,10 @@ input.prop("multiple", this.settings.multiple); // Or does it has the input as a child - input.on("change." + pluginName, function(evt) { + input.on("change." + pluginName, function (evt) { var files = evt.target && evt.target.files; - if (!files || !files.length){ + if (!files || !files.length) { return; } @@ -296,7 +286,11 @@ this.initDnD(); } - if (input.length === 0 && !this.settings.dnd) { + if (this.settings.paste) { + this.initPaste(); + } + + if (input.length === 0 && !this.settings.dnd && !this.settings.paste) { // Trigger an error because if this happens the plugin wont do anything. $.error("Markup error found by jQuery.dmUploader"); @@ -309,15 +303,14 @@ return this; }; - DmUploader.prototype.initDnD = function() - { + DmUploader.prototype.initDnD = function () { var widget = this; // -- Now our own Drop widget.element.on("drop." + pluginName, function (evt) { evt.preventDefault(); - if (widget.draggingOver > 0){ + if (widget.draggingOver > 0) { widget.draggingOver = 0; widget.settings.onDragLeave.call(widget.element); } @@ -340,22 +333,22 @@ }); //-- These two events/callbacks are onlt to maybe do some fancy visual stuff - widget.element.on("dragenter." + pluginName, function(evt) { + widget.element.on("dragenter." + pluginName, function (evt) { evt.preventDefault(); - if (widget.draggingOver === 0){ + if (widget.draggingOver === 0) { widget.settings.onDragEnter.call(widget.element); } widget.draggingOver++; }); - widget.element.on("dragleave." + pluginName, function(evt) { + widget.element.on("dragleave." + pluginName, function (evt) { evt.preventDefault(); widget.draggingOver--; - if (widget.draggingOver === 0){ + if (widget.draggingOver === 0) { widget.settings.onDragLeave.call(widget.element); } }); @@ -365,41 +358,62 @@ } // Adding some off/namepacing to prevent some weird cases when people use multiple instances - $(document).off("drop." + pluginName).on("drop." + pluginName, function(evt) { + $(document).off("drop." + pluginName).on("drop." + pluginName, function (evt) { evt.preventDefault(); - if (widget.draggingOverDoc > 0){ + if (widget.draggingOverDoc > 0) { widget.draggingOverDoc = 0; widget.settings.onDocumentDragLeave.call(widget.element); } }); - $(document).off("dragenter." + pluginName).on("dragenter." + pluginName, function(evt) { + $(document).off("dragenter." + pluginName).on("dragenter." + pluginName, function (evt) { evt.preventDefault(); - if (widget.draggingOverDoc === 0){ + if (widget.draggingOverDoc === 0) { widget.settings.onDocumentDragEnter.call(widget.element); } widget.draggingOverDoc++; }); - $(document).off("dragleave." + pluginName).on("dragleave." + pluginName, function(evt) { + $(document).off("dragleave." + pluginName).on("dragleave." + pluginName, function (evt) { evt.preventDefault(); widget.draggingOverDoc--; - if (widget.draggingOverDoc === 0){ + if (widget.draggingOverDoc === 0) { widget.settings.onDocumentDragLeave.call(widget.element); } }); - $(document).off("dragover." + pluginName).on("dragover." + pluginName, function(evt) { + $(document).off("dragover." + pluginName).on("dragover." + pluginName, function (evt) { evt.preventDefault(); }); }; - DmUploader.prototype.releaseEvents = function() { + DmUploader.prototype.initPaste = function () { + var widget = this; + $(document).on("paste." + pluginName, function (evt) { + evt.preventDefault(); + + var clipboardData = evt.originalEvent && evt.originalEvent.clipboardData; + if (!clipboardData || !clipboardData.files || !clipboardData.files.length) { + return; + } + + // Clipboard has only one file (default name: image.png) + var files = [clipboardData.files[0]]; + widget.addFiles(files); + }); + + if (!widget.settings.hookDocument) { + return; + } + + }; + + DmUploader.prototype.releaseEvents = function () { // Leave everyone ALONE ;_; this.element.off("." + pluginName); @@ -410,11 +424,10 @@ } }; - DmUploader.prototype.validateFile = function(file) - { + DmUploader.prototype.validateFile = function (file) { // Check file size if ((this.settings.maxFileSize > 0) && - (file.size > this.settings.maxFileSize)) { + (file.size > this.settings.maxFileSize)) { this.settings.onFileSizeError.call(this.element, file); @@ -423,7 +436,7 @@ // Check file type if ((this.settings.allowedTypes !== "*") && - !file.type.match(this.settings.allowedTypes)){ + !file.type.match(this.settings.allowedTypes)) { this.settings.onFileTypeError.call(this.element, file); @@ -444,15 +457,13 @@ return new DmUploaderFile(file, this); }; - DmUploader.prototype.addFiles = function(files) - { + DmUploader.prototype.addFiles = function (files) { var nFiles = 0; - for (var i= 0; i < files.length; i++) - { + for (var i = 0; i < files.length; i++) { var file = this.validateFile(files[i]); - if (!file){ + if (!file) { continue; } @@ -468,7 +479,7 @@ } this.queue.push(file); - + nFiles++; } @@ -485,8 +496,7 @@ return this; }; - DmUploader.prototype.processQueue = function() - { + DmUploader.prototype.processQueue = function () { this.queuePos++; if (this.queuePos >= this.queue.length) { @@ -508,16 +518,14 @@ return this.queue[this.queuePos].upload(); }; - DmUploader.prototype.restartQueue = function() - { + DmUploader.prototype.restartQueue = function () { this.queuePos = -1; this.queueRunning = false; this.processQueue(); }; - DmUploader.prototype.findById = function(id) - { + DmUploader.prototype.findById = function (id) { var r = false; for (var i = 0; i < this.queue.length; i++) { @@ -530,8 +538,7 @@ return r; }; - DmUploader.prototype.cancelAll = function() - { + DmUploader.prototype.cancelAll = function () { var queueWasRunning = this.queueRunning; this.queueRunning = false; @@ -545,8 +552,7 @@ } }; - DmUploader.prototype.startAll = function() - { + DmUploader.prototype.startAll = function () { if (this.settings.queue) { // Resume queue this.restartQueue(); @@ -560,8 +566,8 @@ // Public API methods DmUploader.prototype.methods = { - start: function(id) { - if (this.queueRunning){ + start: function (id) { + if (this.queueRunning) { // Do not allow to manually upload Files when a queue is running return false; } @@ -578,7 +584,7 @@ return false; } } - + // Trying to Start an upload by ID if (file) { if (file.status === FileStatus.CANCELLED) { @@ -593,7 +599,7 @@ return true; }, - cancel: function(id) { + cancel: function (id) { var file = false; if (typeof id !== "undefined") { file = this.findById(id); @@ -611,12 +617,12 @@ } // With no id provided... - + this.cancelAll(); return true; }, - reset: function() { + reset: function () { this.cancelAll(); @@ -626,7 +632,7 @@ return true; }, - destroy: function() { + destroy: function () { this.cancelAll(); this.releaseEvents(); @@ -635,18 +641,18 @@ } }; - $.fn.dmUploader = function(options) { + $.fn.dmUploader = function (options) { var args = arguments; if (typeof options === "string") { - this.each(function() { + this.each(function () { var plugin = $.data(this, pluginName); if (plugin instanceof DmUploader) { if (typeof plugin.methods[options] === "function") { plugin.methods[options].apply(plugin, Array.prototype.slice.call(args, 1)); } else { - $.error("Method " + options + " does not exist in jQuery.dmUploader"); + $.error("Method " + options + " does not exist in jQuery.dmUploader"); } } else { $.error("Unknown plugin data found by jQuery.dmUploader"); From c575b37f27dd6cc1f2e20c8682b4c431a9e4654f Mon Sep 17 00:00:00 2001 From: yusanshi Date: Sat, 16 Nov 2019 21:48:39 +0800 Subject: [PATCH 2/3] Change line feed style for left brace --- src/js/jquery.dm-uploader.js | 57 ++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/js/jquery.dm-uploader.js b/src/js/jquery.dm-uploader.js index cc38b62..9a8abac 100644 --- a/src/js/jquery.dm-uploader.js +++ b/src/js/jquery.dm-uploader.js @@ -84,7 +84,8 @@ this.id = Math.random().toString(36).substr(2); }; - DmUploaderFile.prototype.upload = function () { + DmUploaderFile.prototype.upload = function () + { var file = this; if (!file.canUpload()) { @@ -135,12 +136,14 @@ return true; }; - DmUploaderFile.prototype.onSuccess = function (data) { + DmUploaderFile.prototype.onSuccess = function (data) + { this.status = FileStatus.COMPLETED; this.widget.settings.onUploadSuccess.call(this.widget.element, this.id, data); }; - DmUploaderFile.prototype.onError = function (xhr, status, errMsg) { + DmUploaderFile.prototype.onError = function (xhr, status, errMsg) + { // If the status is: cancelled (by the user) don't invoke the error callback if (this.status !== FileStatus.CANCELLED) { this.status = FileStatus.FAILED; @@ -148,7 +151,8 @@ } }; - DmUploaderFile.prototype.onComplete = function () { + DmUploaderFile.prototype.onComplete = function () + { this.widget.activeFiles--; if (this.status !== FileStatus.CANCELLED) { @@ -162,7 +166,8 @@ } }; - DmUploaderFile.prototype.getXhr = function () { + DmUploaderFile.prototype.getXhr = function () + { var file = this; var xhrobj = $.ajaxSettings.xhr(); @@ -182,7 +187,8 @@ return xhrobj; }; - DmUploaderFile.prototype.cancel = function (abort) { + DmUploaderFile.prototype.cancel = function (abort) + { // The abort flag is to track if we are calling this function directly (using the cancel Method, by id) // or the call comes from the 'gobal' method aka cancelAll. // THis mean that we don't want to trigger the cancel event on file that isn't uploading, UNLESS directly doing it @@ -206,7 +212,8 @@ return true; }; - DmUploaderFile.prototype.canUpload = function () { + DmUploaderFile.prototype.canUpload = function () + { return ( this.status === FileStatus.PENDING || this.status === FileStatus.FAILED @@ -230,7 +237,8 @@ return this; }; - DmUploader.prototype.checkSupport = function () { + DmUploader.prototype.checkSupport = function () + { // This one is mandatory for all modes if (typeof window.FormData === "undefined") { return false; @@ -250,7 +258,8 @@ return !$("").prop("disabled"); }; - DmUploader.prototype.init = function () { + DmUploader.prototype.init = function () + { var widget = this; // Queue vars @@ -303,7 +312,8 @@ return this; }; - DmUploader.prototype.initDnD = function () { + DmUploader.prototype.initDnD = function () + { var widget = this; // -- Now our own Drop @@ -392,7 +402,8 @@ }); }; - DmUploader.prototype.initPaste = function () { + DmUploader.prototype.initPaste = function () + { var widget = this; $(document).on("paste." + pluginName, function (evt) { evt.preventDefault(); @@ -413,7 +424,8 @@ }; - DmUploader.prototype.releaseEvents = function () { + DmUploader.prototype.releaseEvents = function () + { // Leave everyone ALONE ;_; this.element.off("." + pluginName); @@ -424,7 +436,8 @@ } }; - DmUploader.prototype.validateFile = function (file) { + DmUploader.prototype.validateFile = function (file) + { // Check file size if ((this.settings.maxFileSize > 0) && (file.size > this.settings.maxFileSize)) { @@ -457,7 +470,8 @@ return new DmUploaderFile(file, this); }; - DmUploader.prototype.addFiles = function (files) { + DmUploader.prototype.addFiles = function (files) + { var nFiles = 0; for (var i = 0; i < files.length; i++) { @@ -496,7 +510,8 @@ return this; }; - DmUploader.prototype.processQueue = function () { + DmUploader.prototype.processQueue = function () + { this.queuePos++; if (this.queuePos >= this.queue.length) { @@ -518,14 +533,16 @@ return this.queue[this.queuePos].upload(); }; - DmUploader.prototype.restartQueue = function () { + DmUploader.prototype.restartQueue = function () + { this.queuePos = -1; this.queueRunning = false; this.processQueue(); }; - DmUploader.prototype.findById = function (id) { + DmUploader.prototype.findById = function (id) + { var r = false; for (var i = 0; i < this.queue.length; i++) { @@ -538,7 +555,8 @@ return r; }; - DmUploader.prototype.cancelAll = function () { + DmUploader.prototype.cancelAll = function () + { var queueWasRunning = this.queueRunning; this.queueRunning = false; @@ -552,7 +570,8 @@ } }; - DmUploader.prototype.startAll = function () { + DmUploader.prototype.startAll = function () + { if (this.settings.queue) { // Resume queue this.restartQueue(); From 899d442da3522c308941890340007f4dc12b6668 Mon Sep 17 00:00:00 2001 From: yusanshi Date: Wed, 20 Nov 2019 03:51:25 +0800 Subject: [PATCH 3/3] Restore formatted code to its original style --- src/js/jquery.dm-uploader.js | 165 ++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 82 deletions(-) diff --git a/src/js/jquery.dm-uploader.js b/src/js/jquery.dm-uploader.js index 9a8abac..d13f024 100644 --- a/src/js/jquery.dm-uploader.js +++ b/src/js/jquery.dm-uploader.js @@ -11,7 +11,7 @@ /* global define, define, window, document, FormData */ -(function (factory) { +(function(factory) { "use strict"; if (typeof define === "function" && define.amd) { // AMD. Register as an anonymous module. @@ -22,7 +22,7 @@ // Browser globals factory(window.jQuery); } -}(function ($) { +}(function($) { "use strict"; var pluginName = "dmUploader"; @@ -52,26 +52,27 @@ maxFileSize: 0, allowedTypes: "*", extFilter: null, - onInit: function () { }, - onComplete: function () { }, - onFallbackMode: function () { }, - onNewFile: function () { }, //params: id, file - onBeforeUpload: function () { }, //params: id - onUploadProgress: function () { }, //params: id, percent - onUploadSuccess: function () { }, //params: id, data - onUploadCanceled: function () { }, //params: id - onUploadError: function () { }, //params: id, xhr, status, message - onUploadComplete: function () { }, //params: id - onFileTypeError: function () { }, //params: file - onFileSizeError: function () { }, //params: file - onFileExtError: function () { }, //params: file - onDragEnter: function () { }, - onDragLeave: function () { }, - onDocumentDragEnter: function () { }, - onDocumentDragLeave: function () { } + onInit: function(){}, + onComplete: function(){}, + onFallbackMode: function() {}, + onNewFile: function(){}, //params: id, file + onBeforeUpload: function(){}, //params: id + onUploadProgress: function(){}, //params: id, percent + onUploadSuccess: function(){}, //params: id, data + onUploadCanceled: function(){}, //params: id + onUploadError: function(){}, //params: id, xhr, status, message + onUploadComplete: function(){}, //params: id + onFileTypeError: function(){}, //params: file + onFileSizeError: function(){}, //params: file + onFileExtError: function(){}, //params: file + onDragEnter: function(){}, + onDragLeave: function(){}, + onDocumentDragEnter: function(){}, + onDocumentDragLeave: function(){} }; - - var DmUploaderFile = function (file, widget) { + + var DmUploaderFile = function(file, widget) + { this.data = file; this.widget = widget; @@ -84,7 +85,7 @@ this.id = Math.random().toString(36).substr(2); }; - DmUploaderFile.prototype.upload = function () + DmUploaderFile.prototype.upload = function() { var file = this; @@ -103,11 +104,11 @@ // Append extra Form Data var customData = file.widget.settings.extraData; - if (typeof (customData) === "function") { + if (typeof(customData) === "function") { customData = customData.call(file.widget.element, file.id); } - $.each(customData, function (exKey, exVal) { + $.each(customData, function(exKey, exVal) { fd.append(exKey, exVal); }); @@ -127,22 +128,22 @@ contentType: false, processData: false, forceSync: false, - xhr: function () { return file.getXhr(); }, - success: function (data) { file.onSuccess(data); }, - error: function (xhr, status, errMsg) { file.onError(xhr, status, errMsg); }, - complete: function () { file.onComplete(); }, + xhr: function() { return file.getXhr(); }, + success: function(data) { file.onSuccess(data); }, + error: function(xhr, status, errMsg) { file.onError(xhr, status, errMsg); }, + complete: function() { file.onComplete(); }, }); return true; }; - DmUploaderFile.prototype.onSuccess = function (data) + DmUploaderFile.prototype.onSuccess = function(data) { this.status = FileStatus.COMPLETED; this.widget.settings.onUploadSuccess.call(this.widget.element, this.id, data); }; - DmUploaderFile.prototype.onError = function (xhr, status, errMsg) + DmUploaderFile.prototype.onError = function(xhr, status, errMsg) { // If the status is: cancelled (by the user) don't invoke the error callback if (this.status !== FileStatus.CANCELLED) { @@ -151,7 +152,7 @@ } }; - DmUploaderFile.prototype.onComplete = function () + DmUploaderFile.prototype.onComplete = function() { this.widget.activeFiles--; @@ -166,13 +167,13 @@ } }; - DmUploaderFile.prototype.getXhr = function () + DmUploaderFile.prototype.getXhr = function() { var file = this; var xhrobj = $.ajaxSettings.xhr(); if (xhrobj.upload) { - xhrobj.upload.addEventListener("progress", function (event) { + xhrobj.upload.addEventListener("progress", function(event) { var percent = 0; var position = event.loaded || event.position; var total = event.total || event.totalSize; @@ -187,7 +188,7 @@ return xhrobj; }; - DmUploaderFile.prototype.cancel = function (abort) + DmUploaderFile.prototype.cancel = function(abort) { // The abort flag is to track if we are calling this function directly (using the cancel Method, by id) // or the call comes from the 'gobal' method aka cancelAll. @@ -212,7 +213,7 @@ return true; }; - DmUploaderFile.prototype.canUpload = function () + DmUploaderFile.prototype.canUpload = function() { return ( this.status === FileStatus.PENDING || @@ -220,7 +221,8 @@ ); }; - var DmUploader = function (element, options) { + var DmUploader = function(element, options) + { this.element = $(element); this.settings = $.extend({}, defaults, options); @@ -237,7 +239,7 @@ return this; }; - DmUploader.prototype.checkSupport = function () + DmUploader.prototype.checkSupport = function() { // This one is mandatory for all modes if (typeof window.FormData === "undefined") { @@ -246,9 +248,9 @@ // Test based on: Modernizr/feature-detects/forms/fileinput.js var exp = new RegExp( - "/(Android (1.0|1.1|1.5|1.6|2.0|2.1))|" + - "(Windows Phone (OS 7|8.0))|(XBLWP)|" + - "(ZuneWP)|(w(eb)?OSBrowser)|(webOS)|" + + "/(Android (1.0|1.1|1.5|1.6|2.0|2.1))|"+ + "(Windows Phone (OS 7|8.0))|(XBLWP)|"+ + "(ZuneWP)|(w(eb)?OSBrowser)|(webOS)|"+ "(Kindle\/(1.0|2.0|2.5|3.0))/"); if (exp.test(window.navigator.userAgent)) { @@ -258,7 +260,7 @@ return !$("").prop("disabled"); }; - DmUploader.prototype.init = function () + DmUploader.prototype.init = function() { var widget = this; @@ -278,10 +280,10 @@ input.prop("multiple", this.settings.multiple); // Or does it has the input as a child - input.on("change." + pluginName, function (evt) { + input.on("change." + pluginName, function(evt) { var files = evt.target && evt.target.files; - if (!files || !files.length) { + if (!files || !files.length){ return; } @@ -312,7 +314,7 @@ return this; }; - DmUploader.prototype.initDnD = function () + DmUploader.prototype.initDnD = function() { var widget = this; @@ -320,7 +322,7 @@ widget.element.on("drop." + pluginName, function (evt) { evt.preventDefault(); - if (widget.draggingOver > 0) { + if (widget.draggingOver > 0){ widget.draggingOver = 0; widget.settings.onDragLeave.call(widget.element); } @@ -343,22 +345,22 @@ }); //-- These two events/callbacks are onlt to maybe do some fancy visual stuff - widget.element.on("dragenter." + pluginName, function (evt) { + widget.element.on("dragenter." + pluginName, function(evt) { evt.preventDefault(); - if (widget.draggingOver === 0) { + if (widget.draggingOver === 0){ widget.settings.onDragEnter.call(widget.element); } widget.draggingOver++; }); - widget.element.on("dragleave." + pluginName, function (evt) { + widget.element.on("dragleave." + pluginName, function(evt) { evt.preventDefault(); widget.draggingOver--; - if (widget.draggingOver === 0) { + if (widget.draggingOver === 0){ widget.settings.onDragLeave.call(widget.element); } }); @@ -368,36 +370,36 @@ } // Adding some off/namepacing to prevent some weird cases when people use multiple instances - $(document).off("drop." + pluginName).on("drop." + pluginName, function (evt) { + $(document).off("drop." + pluginName).on("drop." + pluginName, function(evt) { evt.preventDefault(); - if (widget.draggingOverDoc > 0) { + if (widget.draggingOverDoc > 0){ widget.draggingOverDoc = 0; widget.settings.onDocumentDragLeave.call(widget.element); } }); - $(document).off("dragenter." + pluginName).on("dragenter." + pluginName, function (evt) { + $(document).off("dragenter." + pluginName).on("dragenter." + pluginName, function(evt) { evt.preventDefault(); - if (widget.draggingOverDoc === 0) { + if (widget.draggingOverDoc === 0){ widget.settings.onDocumentDragEnter.call(widget.element); } widget.draggingOverDoc++; }); - $(document).off("dragleave." + pluginName).on("dragleave." + pluginName, function (evt) { + $(document).off("dragleave." + pluginName).on("dragleave." + pluginName, function(evt) { evt.preventDefault(); widget.draggingOverDoc--; - if (widget.draggingOverDoc === 0) { + if (widget.draggingOverDoc === 0){ widget.settings.onDocumentDragLeave.call(widget.element); } }); - $(document).off("dragover." + pluginName).on("dragover." + pluginName, function (evt) { + $(document).off("dragover." + pluginName).on("dragover." + pluginName, function(evt) { evt.preventDefault(); }); }; @@ -421,11 +423,9 @@ if (!widget.settings.hookDocument) { return; } - }; - DmUploader.prototype.releaseEvents = function () - { + DmUploader.prototype.releaseEvents = function() { // Leave everyone ALONE ;_; this.element.off("." + pluginName); @@ -436,11 +436,11 @@ } }; - DmUploader.prototype.validateFile = function (file) + DmUploader.prototype.validateFile = function(file) { // Check file size if ((this.settings.maxFileSize > 0) && - (file.size > this.settings.maxFileSize)) { + (file.size > this.settings.maxFileSize)) { this.settings.onFileSizeError.call(this.element, file); @@ -449,7 +449,7 @@ // Check file type if ((this.settings.allowedTypes !== "*") && - !file.type.match(this.settings.allowedTypes)) { + !file.type.match(this.settings.allowedTypes)){ this.settings.onFileTypeError.call(this.element, file); @@ -470,14 +470,15 @@ return new DmUploaderFile(file, this); }; - DmUploader.prototype.addFiles = function (files) + DmUploader.prototype.addFiles = function(files) { var nFiles = 0; - for (var i = 0; i < files.length; i++) { + for (var i= 0; i < files.length; i++) + { var file = this.validateFile(files[i]); - if (!file) { + if (!file){ continue; } @@ -493,7 +494,7 @@ } this.queue.push(file); - + nFiles++; } @@ -510,7 +511,7 @@ return this; }; - DmUploader.prototype.processQueue = function () + DmUploader.prototype.processQueue = function() { this.queuePos++; @@ -533,7 +534,7 @@ return this.queue[this.queuePos].upload(); }; - DmUploader.prototype.restartQueue = function () + DmUploader.prototype.restartQueue = function() { this.queuePos = -1; this.queueRunning = false; @@ -541,7 +542,7 @@ this.processQueue(); }; - DmUploader.prototype.findById = function (id) + DmUploader.prototype.findById = function(id) { var r = false; @@ -555,7 +556,7 @@ return r; }; - DmUploader.prototype.cancelAll = function () + DmUploader.prototype.cancelAll = function() { var queueWasRunning = this.queueRunning; this.queueRunning = false; @@ -570,7 +571,7 @@ } }; - DmUploader.prototype.startAll = function () + DmUploader.prototype.startAll = function() { if (this.settings.queue) { // Resume queue @@ -585,8 +586,8 @@ // Public API methods DmUploader.prototype.methods = { - start: function (id) { - if (this.queueRunning) { + start: function(id) { + if (this.queueRunning){ // Do not allow to manually upload Files when a queue is running return false; } @@ -603,7 +604,7 @@ return false; } } - + // Trying to Start an upload by ID if (file) { if (file.status === FileStatus.CANCELLED) { @@ -618,7 +619,7 @@ return true; }, - cancel: function (id) { + cancel: function(id) { var file = false; if (typeof id !== "undefined") { file = this.findById(id); @@ -636,12 +637,12 @@ } // With no id provided... - + this.cancelAll(); return true; }, - reset: function () { + reset: function() { this.cancelAll(); @@ -651,7 +652,7 @@ return true; }, - destroy: function () { + destroy: function() { this.cancelAll(); this.releaseEvents(); @@ -660,18 +661,18 @@ } }; - $.fn.dmUploader = function (options) { + $.fn.dmUploader = function(options) { var args = arguments; if (typeof options === "string") { - this.each(function () { + this.each(function() { var plugin = $.data(this, pluginName); if (plugin instanceof DmUploader) { if (typeof plugin.methods[options] === "function") { plugin.methods[options].apply(plugin, Array.prototype.slice.call(args, 1)); } else { - $.error("Method " + options + " does not exist in jQuery.dmUploader"); + $.error("Method " + options + " does not exist in jQuery.dmUploader"); } } else { $.error("Unknown plugin data found by jQuery.dmUploader"); @@ -685,4 +686,4 @@ }); } }; -})); \ No newline at end of file +}));