[879b32]: / qiita_pet / static / vendor / js / resumable.js

Download this file

2 lines (1 with data), 14.0 kB

1
(function(){"use strict";var e=function(t){function u(e,t){var n=this;n.opts={};n.getOpt=e.getOpt;n._prevProgress=0;n.resumableObj=e;n.file=t;n.fileName=t.fileName||t.name;n.size=t.size;n.relativePath=t.webkitRelativePath||n.fileName;n.uniqueIdentifier=r.generateUniqueIdentifier(t);n._pause=false;n.container="";var i=false;var s=function(e,t){switch(e){case"progress":n.resumableObj.fire("fileProgress",n);break;case"error":n.abort();i=true;n.chunks=[];n.resumableObj.fire("fileError",n,t);break;case"success":if(i)return;n.resumableObj.fire("fileProgress",n);if(n.isComplete()){n.resumableObj.fire("fileSuccess",n,t)}break;case"retry":n.resumableObj.fire("fileRetry",n);break}};n.chunks=[];n.abort=function(){var e=0;r.each(n.chunks,function(t){if(t.status()=="uploading"){t.abort();e++}});if(e>0)n.resumableObj.fire("fileProgress",n)};n.cancel=function(){var e=n.chunks;n.chunks=[];r.each(e,function(e){if(e.status()=="uploading"){e.abort();n.resumableObj.uploadNextChunk()}});n.resumableObj.removeFile(n);n.resumableObj.fire("fileProgress",n)};n.retry=function(){n.bootstrap();var e=false;n.resumableObj.on("chunkingComplete",function(){if(!e)n.resumableObj.upload();e=true})};n.bootstrap=function(){n.abort();i=false;n.chunks=[];n._prevProgress=0;var e=n.getOpt("forceChunkSize")?Math.ceil:Math.floor;var t=Math.max(e(n.file.size/n.getOpt("chunkSize")),1);for(var r=0;r<t;r++){(function(e){window.setTimeout(function(){n.chunks.push(new a(n.resumableObj,n,e,s));n.resumableObj.fire("chunkingProgress",n,e/t)},0)})(r)}window.setTimeout(function(){n.resumableObj.fire("chunkingComplete",n)},0)};n.progress=function(){if(i)return 1;var e=0;var t=false;r.each(n.chunks,function(n){if(n.status()=="error")t=true;e+=n.progress(true)});e=t?1:e>.999?1:e;e=Math.max(n._prevProgress,e);n._prevProgress=e;return e};n.isUploading=function(){var e=false;r.each(n.chunks,function(t){if(t.status()=="uploading"){e=true;return false}});return e};n.isComplete=function(){var e=false;r.each(n.chunks,function(t){var n=t.status();if(n=="pending"||n=="uploading"||t.preprocessState===1){e=true;return false}});return!e};n.pause=function(e){if(typeof e==="undefined"){n._pause=n._pause?false:true}else{n._pause=e}};n.isPaused=function(){return n._pause};n.resumableObj.fire("chunkingStart",n);n.bootstrap();return this}function a(e,t,n,i){var s=this;s.opts={};s.getOpt=e.getOpt;s.resumableObj=e;s.fileObj=t;s.fileObjSize=t.size;s.fileObjType=t.file.type;s.offset=n;s.callback=i;s.lastProgressCallback=new Date;s.tested=false;s.retries=0;s.pendingRetry=false;s.preprocessState=0;var o=s.getOpt("chunkSize");s.loaded=0;s.startByte=s.offset*o;s.endByte=Math.min(s.fileObjSize,(s.offset+1)*o);if(s.fileObjSize-s.endByte<o&&!s.getOpt("forceChunkSize")){s.endByte=s.fileObjSize}s.xhr=null;s.test=function(){s.xhr=new XMLHttpRequest;var e=function(e){s.tested=true;var t=s.status();if(t=="success"){s.callback(t,s.message());s.resumableObj.uploadNextChunk()}else{s.send()}};s.xhr.addEventListener("load",e,false);s.xhr.addEventListener("error",e,false);var t=[];var n=s.getOpt("query");if(typeof n=="function")n=n(s.fileObj,s);r.each(n,function(e,n){t.push([encodeURIComponent(e),encodeURIComponent(n)].join("="))});t.push(["resumableChunkNumber",encodeURIComponent(s.offset+1)].join("="));t.push(["resumableChunkSize",encodeURIComponent(s.getOpt("chunkSize"))].join("="));t.push(["resumableCurrentChunkSize",encodeURIComponent(s.endByte-s.startByte)].join("="));t.push(["resumableTotalSize",encodeURIComponent(s.fileObjSize)].join("="));t.push(["resumableType",encodeURIComponent(s.fileObjType)].join("="));t.push(["resumableIdentifier",encodeURIComponent(s.fileObj.uniqueIdentifier)].join("="));t.push(["resumableFilename",encodeURIComponent(s.fileObj.fileName)].join("="));t.push(["resumableRelativePath",encodeURIComponent(s.fileObj.relativePath)].join("="));t.push(["resumableTotalChunks",encodeURIComponent(s.fileObj.chunks.length)].join("="));s.xhr.open("GET",r.getTarget(t));s.xhr.timeout=s.getOpt("xhrTimeout");s.xhr.withCredentials=s.getOpt("withCredentials");r.each(s.getOpt("headers"),function(e,t){s.xhr.setRequestHeader(e,t)});s.xhr.send(null)};s.preprocessFinished=function(){s.preprocessState=2;s.send()};s.send=function(){var e=s.getOpt("preprocess");if(typeof e==="function"){switch(s.preprocessState){case 0:e(s);s.preprocessState=1;return;case 1:return;case 2:break}}if(s.getOpt("testChunks")&&!s.tested){s.test();return}s.xhr=new XMLHttpRequest;s.xhr.upload.addEventListener("progress",function(e){if(new Date-s.lastProgressCallback>s.getOpt("throttleProgressCallbacks")*1e3){s.callback("progress");s.lastProgressCallback=new Date}s.loaded=e.loaded||0},false);s.loaded=0;s.pendingRetry=false;s.callback("progress");var t=function(e){var t=s.status();if(t=="success"||t=="error"){s.callback(t,s.message());s.resumableObj.uploadNextChunk()}else{s.callback("retry",s.message());s.abort();s.retries++;var n=s.getOpt("chunkRetryInterval");if(n!==undefined){s.pendingRetry=true;setTimeout(s.send,n)}else{s.send()}}};s.xhr.addEventListener("load",t,false);s.xhr.addEventListener("error",t,false);var n={resumableChunkNumber:s.offset+1,resumableChunkSize:s.getOpt("chunkSize"),resumableCurrentChunkSize:s.endByte-s.startByte,resumableTotalSize:s.fileObjSize,resumableType:s.fileObjType,resumableIdentifier:s.fileObj.uniqueIdentifier,resumableFilename:s.fileObj.fileName,resumableRelativePath:s.fileObj.relativePath,resumableTotalChunks:s.fileObj.chunks.length};var i=s.getOpt("query");if(typeof i=="function")i=i(s.fileObj,s);r.each(i,function(e,t){n[e]=t});var o=s.fileObj.file.slice?"slice":s.fileObj.file.mozSlice?"mozSlice":s.fileObj.file.webkitSlice?"webkitSlice":"slice",u=s.fileObj.file[o](s.startByte,s.endByte),a=null,f=s.getOpt("target");if(s.getOpt("method")==="octet"){a=u;var l=[];r.each(n,function(e,t){l.push([encodeURIComponent(e),encodeURIComponent(t)].join("="))});f=r.getTarget(l)}else{a=new FormData;r.each(n,function(e,t){a.append(e,t)});a.append(s.getOpt("fileParameterName"),u)}s.xhr.open("POST",f);s.xhr.timeout=s.getOpt("xhrTimeout");s.xhr.withCredentials=s.getOpt("withCredentials");r.each(s.getOpt("headers"),function(e,t){s.xhr.setRequestHeader(e,t)});s.xhr.send(a)};s.abort=function(){if(s.xhr)s.xhr.abort();s.xhr=null};s.status=function(){if(s.pendingRetry){return"uploading"}else if(!s.xhr){return"pending"}else if(s.xhr.readyState<4){return"uploading"}else{if(s.xhr.status==200){return"success"}else if(r.contains(s.getOpt("permanentErrors"),s.xhr.status)||s.retries>=s.getOpt("maxChunkRetries")){return"error"}else{s.abort();return"pending"}}};s.message=function(){return s.xhr?s.xhr.responseText:""};s.progress=function(e){if(typeof e==="undefined")e=false;var t=e?(s.endByte-s.startByte)/s.fileObjSize:1;if(s.pendingRetry)return 0;var n=s.status();switch(n){case"success":case"error":return 1*t;case"pending":return 0*t;default:return s.loaded/(s.endByte-s.startByte)*t}};return this}if(!(this instanceof e)){return new e(t)}this.version=1;this.support=typeof File!=="undefined"&&typeof Blob!=="undefined"&&typeof FileList!=="undefined"&&(!!Blob.prototype.webkitSlice||!!Blob.prototype.mozSlice||!!Blob.prototype.slice||false);if(!this.support)return false;var n=this;n.files=[];n.defaults={chunkSize:1*1024*1024,forceChunkSize:false,simultaneousUploads:3,fileParameterName:"file",throttleProgressCallbacks:.5,query:{},headers:{},preprocess:null,method:"multipart",prioritizeFirstAndLastChunk:false,target:"/",testChunks:true,generateUniqueIdentifier:null,maxChunkRetries:undefined,chunkRetryInterval:undefined,permanentErrors:[404,415,500,501],maxFiles:undefined,withCredentials:false,xhrTimeout:0,maxFilesErrorCallback:function(e,t){var r=n.getOpt("maxFiles");alert("Please upload "+r+" file"+(r===1?"":"s")+" at a time.")},minFileSize:1,minFileSizeErrorCallback:function(e,t){alert(e.fileName||e.name+" is too small, please upload files larger than "+r.formatSize(n.getOpt("minFileSize"))+".")},maxFileSize:undefined,maxFileSizeErrorCallback:function(e,t){alert(e.fileName||e.name+" is too large, please upload files less than "+r.formatSize(n.getOpt("maxFileSize"))+".")},fileType:[],fileTypeErrorCallback:function(e,t){alert(e.fileName||e.name+" has type not allowed, please upload files of type "+n.getOpt("fileType")+".")}};n.opts=t||{};n.getOpt=function(t){var n=this;if(t instanceof Array){var i={};r.each(t,function(e){i[e]=n.getOpt(e)});return i}if(n instanceof a){if(typeof n.opts[t]!=="undefined"){return n.opts[t]}else{n=n.fileObj}}if(n instanceof u){if(typeof n.opts[t]!=="undefined"){return n.opts[t]}else{n=n.resumableObj}}if(n instanceof e){if(typeof n.opts[t]!=="undefined"){return n.opts[t]}else{return n.defaults[t]}}};n.events=[];n.on=function(e,t){n.events.push(e.toLowerCase(),t)};n.fire=function(){var e=[];for(var t=0;t<arguments.length;t++)e.push(arguments[t]);var r=e[0].toLowerCase();for(var t=0;t<=n.events.length;t+=2){if(n.events[t]==r)n.events[t+1].apply(n,e.slice(1));if(n.events[t]=="catchall")n.events[t+1].apply(null,e)}if(r=="fileerror")n.fire("error",e[2],e[1]);if(r=="fileprogress")n.fire("progress")};var r={stopEvent:function(e){e.stopPropagation();e.preventDefault()},each:function(e,t){if(typeof e.length!=="undefined"){for(var n=0;n<e.length;n++){if(t(e[n])===false)return}}else{for(n in e){if(t(n,e[n])===false)return}}},generateUniqueIdentifier:function(e){var t=n.getOpt("generateUniqueIdentifier");if(typeof t==="function"){return t(e)}var r=e.webkitRelativePath||e.fileName||e.name;var i=e.size;return i+"-"+r.replace(/[^0-9a-zA-Z_-]/img,"")},contains:function(e,t){var n=false;r.each(e,function(e){if(e==t){n=true;return false}return true});return n},formatSize:function(e){if(e<1024){return e+" bytes"}else if(e<1024*1024){return(e/1024).toFixed(0)+" KB"}else if(e<1024*1024*1024){return(e/1024/1024).toFixed(1)+" MB"}else{return(e/1024/1024/1024).toFixed(1)+" GB"}},getTarget:function(e){var t=n.getOpt("target");if(t.indexOf("?")<0){t+="?"}else{t+="&"}return t+e.join("&")}};var i=function(e){r.stopEvent(e);o(e.dataTransfer.files,e)};var s=function(e){e.preventDefault()};var o=function(e,t){var i=0;var s=n.getOpt(["maxFiles","minFileSize","maxFileSize","maxFilesErrorCallback","minFileSizeErrorCallback","maxFileSizeErrorCallback","fileType","fileTypeErrorCallback"]);if(typeof s.maxFiles!=="undefined"&&s.maxFiles<e.length+n.files.length){if(s.maxFiles===1&&n.files.length===1&&e.length===1){n.removeFile(n.files[0])}else{s.maxFilesErrorCallback(e,i++);return false}}var o=[];r.each(e,function(e){var a=e.name.split(".");var f=a[a.length-1].toLowerCase();if(s.fileType.length>0&&!r.contains(s.fileType,f)){s.fileTypeErrorCallback(e,i++);return false}if(typeof s.minFileSize!=="undefined"&&e.size<s.minFileSize){s.minFileSizeErrorCallback(e,i++);return false}if(typeof s.maxFileSize!=="undefined"&&e.size>s.maxFileSize){s.maxFileSizeErrorCallback(e,i++);return false}if(!n.getFromUniqueIdentifier(r.generateUniqueIdentifier(e))){(function(){console.log(e);var r=new u(n,e);window.setTimeout(function(){n.files.push(r);o.push(r);r.container=typeof t!="undefined"?t.srcElement:null;n.fire("fileAdded",r,t)},0)})()}});window.setTimeout(function(){n.fire("filesAdded",o)},0)};n.uploadNextChunk=function(){var e=false;if(n.getOpt("prioritizeFirstAndLastChunk")){r.each(n.files,function(t){if(t.chunks.length&&t.chunks[0].status()=="pending"&&t.chunks[0].preprocessState===0){t.chunks[0].send();e=true;return false}if(t.chunks.length>1&&t.chunks[t.chunks.length-1].status()=="pending"&&t.chunks[t.chunks.length-1].preprocessState===0){t.chunks[t.chunks.length-1].send();e=true;return false}});if(e)return true}r.each(n.files,function(t){if(t.isPaused()===false){r.each(t.chunks,function(t){if(t.status()=="pending"&&t.preprocessState===0){t.send();e=true;return false}})}if(e)return false});if(e)return true;var t=false;r.each(n.files,function(e){if(!e.isComplete()){t=true;return false}});if(!t){n.fire("complete")}return false};n.assignBrowse=function(e,t){if(typeof e.length=="undefined")e=[e];r.each(e,function(e){var r;if(e.tagName==="INPUT"&&e.type==="file"){r=e}else{r=document.createElement("input");r.setAttribute("type","file");r.style.display="none";e.addEventListener("click",function(){r.style.opacity=0;r.style.display="block";r.focus();r.click();r.style.display="none"},false);e.appendChild(r)}var i=n.getOpt("maxFiles");if(typeof i==="undefined"||i!=1){r.setAttribute("multiple","multiple")}else{r.removeAttribute("multiple")}if(t){r.setAttribute("webkitdirectory","webkitdirectory")}else{r.removeAttribute("webkitdirectory")}r.addEventListener("change",function(e){o(e.target.files,e);e.target.value=""},false)})};n.assignDrop=function(e){if(typeof e.length=="undefined")e=[e];r.each(e,function(e){e.addEventListener("dragover",s,false);e.addEventListener("drop",i,false)})};n.unAssignDrop=function(e){if(typeof e.length=="undefined")e=[e];r.each(e,function(e){e.removeEventListener("dragover",s);e.removeEventListener("drop",i)})};n.isUploading=function(){var e=false;r.each(n.files,function(t){if(t.isUploading()){e=true;return false}});return e};n.upload=function(){if(n.isUploading())return;n.fire("uploadStart");for(var e=1;e<=n.getOpt("simultaneousUploads");e++){n.uploadNextChunk()}};n.pause=function(){r.each(n.files,function(e){e.abort()});n.fire("pause")};n.cancel=function(){for(var e=n.files.length-1;e>=0;e--){n.files[e].cancel()}n.fire("cancel")};n.progress=function(){var e=0;var t=0;r.each(n.files,function(n){e+=n.progress()*n.size;t+=n.size});return t>0?e/t:0};n.addFile=function(e,t){o([e],t)};n.removeFile=function(e){for(var t=n.files.length-1;t>=0;t--){if(n.files[t]===e){n.files.splice(t,1)}}};n.getFromUniqueIdentifier=function(e){var t=false;r.each(n.files,function(n){if(n.uniqueIdentifier==e)t=n});return t};n.getSize=function(){var e=0;r.each(n.files,function(t){e+=t.size});return e};return this};if(typeof module!="undefined"){module.exports=e}else if(typeof define==="function"&&define.amd){define(function(){return e})}else{window.Resumable=e}})()