|
|
|
@ -6,26 +6,29 @@
|
|
|
|
|
* @extends BI.Single |
|
|
|
|
* @abstract |
|
|
|
|
*/ |
|
|
|
|
((function (document) { |
|
|
|
|
/** |
|
|
|
|
import { shortcut, Widget, some, extend } from "../../../core"; |
|
|
|
|
import { Msg } from "../../foundation/message"; |
|
|
|
|
|
|
|
|
|
const document = _global.document || {}; |
|
|
|
|
/** |
|
|
|
|
* @description normalize input.files. create if not present, add item method if not present |
|
|
|
|
* @param Object generated wrap object |
|
|
|
|
* @return Object the wrap object itself |
|
|
|
|
*/ |
|
|
|
|
var F = ((function (item) { |
|
|
|
|
return function (input) { |
|
|
|
|
var files = input.files || [input]; |
|
|
|
|
const F = (((item) => { |
|
|
|
|
return (input) => { |
|
|
|
|
const files = input.files || [input]; |
|
|
|
|
if (!files.item) { |
|
|
|
|
files.item = item; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return files; |
|
|
|
|
}; |
|
|
|
|
})(function (i) { |
|
|
|
|
})((i) => { |
|
|
|
|
return this[i]; |
|
|
|
|
})); |
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
var event = { |
|
|
|
|
const event = { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @description add an event via addEventListener or attachEvent |
|
|
|
@ -35,12 +38,12 @@
|
|
|
|
|
* @return Object noswfupload.event |
|
|
|
|
*/ |
|
|
|
|
add: document.addEventListener ? |
|
|
|
|
function (node, name, callback) { |
|
|
|
|
(node, name, callback) => { |
|
|
|
|
node.addEventListener(name, callback, false); |
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
|
} : |
|
|
|
|
function (node, name, callback) { |
|
|
|
|
(node, name, callback) => { |
|
|
|
|
node.attachEvent("on" + name, callback); |
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
@ -54,12 +57,12 @@
|
|
|
|
|
* @return Object noswfupload.event |
|
|
|
|
*/ |
|
|
|
|
del: document.removeEventListener ? |
|
|
|
|
function (node, name, callback) { |
|
|
|
|
(node, name, callback) => { |
|
|
|
|
node.removeEventListener(name, callback, false); |
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
|
} : |
|
|
|
|
function (node, name, callback) { |
|
|
|
|
(node, name, callback) => { |
|
|
|
|
node.detachEvent("on" + name, callback); |
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
@ -70,7 +73,7 @@
|
|
|
|
|
* @param void generated event or undefined |
|
|
|
|
* @return Boolean false |
|
|
|
|
*/ |
|
|
|
|
stop: function (e) { |
|
|
|
|
stop(e) { |
|
|
|
|
if (!e) { |
|
|
|
|
if (self.event) { |
|
|
|
|
event.returnValue = !(event.cancelBubble = true); |
|
|
|
@ -82,15 +85,15 @@
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var sendFile = (function (toString) { |
|
|
|
|
var split = "onabort.onerror.onloadstart.onprogress".split("."), |
|
|
|
|
const sendFile = (((toString) => { |
|
|
|
|
const split = "onabort.onerror.onloadstart.onprogress".split("."), |
|
|
|
|
length = split.length, |
|
|
|
|
CRLF = "\r\n", |
|
|
|
|
xhr = new XMLHttpRequest, |
|
|
|
|
CRLF = "\r\n"; |
|
|
|
|
let xhr = new XMLHttpRequest, |
|
|
|
|
sendFile; |
|
|
|
|
function multipart(boundary, name, file) { |
|
|
|
|
const multipart = (boundary, name, file) => { |
|
|
|
|
return "--".concat( |
|
|
|
|
boundary, CRLF, |
|
|
|
|
"Content-Disposition: form-data; name=\"", name, "\"; filename=\"", _global.encodeURIComponent(file.fileName), "\"", CRLF, |
|
|
|
@ -100,15 +103,15 @@
|
|
|
|
|
"--", boundary, "--", CRLF |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
function isFunction (Function) { |
|
|
|
|
const isFunction = (Function) => { |
|
|
|
|
return toString.call(Function) === "[object Function]"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FireFox 3+, Safari 4 beta (Chrome 2 beta file is buggy and will not work)
|
|
|
|
|
if (xhr.upload || xhr.sendAsBinary) { |
|
|
|
|
sendFile = function (handler, maxSize, width, height) { |
|
|
|
|
var current = handler.current; |
|
|
|
|
sendFile = (handler, maxSize, width, height) => { |
|
|
|
|
const current = handler.current; |
|
|
|
|
if (-1 < maxSize && maxSize < handler.file.fileSize) { |
|
|
|
|
if (isFunction(handler.onerror)) { |
|
|
|
|
handler.onerror(); |
|
|
|
@ -116,46 +119,44 @@
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
for (var xhr = new XMLHttpRequest, |
|
|
|
|
const xhr = new XMLHttpRequest, |
|
|
|
|
upload = xhr.upload || { |
|
|
|
|
addEventListener: function (event, callback) { |
|
|
|
|
addEventListener(event, callback) { |
|
|
|
|
this["on" + event] = callback; |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
i = 0; |
|
|
|
|
i < length; |
|
|
|
|
i++ |
|
|
|
|
) { |
|
|
|
|
}; |
|
|
|
|
for (let i = 0;i < length;i++) { |
|
|
|
|
upload.addEventListener( |
|
|
|
|
split[i].substring(2), |
|
|
|
|
// eslint-disable-next-line no-loop-func
|
|
|
|
|
(function (event) { |
|
|
|
|
return function (rpe) { |
|
|
|
|
(((event) => { |
|
|
|
|
return (rpe) => { |
|
|
|
|
if (isFunction(handler[event])) { |
|
|
|
|
handler[event](rpe, xhr); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}(split[i])), |
|
|
|
|
})(split[i])), |
|
|
|
|
false |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
upload.addEventListener( |
|
|
|
|
"load", |
|
|
|
|
function (rpe) { |
|
|
|
|
(rpe) => { |
|
|
|
|
if (handler.onreadystatechange === false) { |
|
|
|
|
if (isFunction(handler.onload)) { |
|
|
|
|
handler.onload(rpe, xhr); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
setTimeout(function () { |
|
|
|
|
const callback = () => { |
|
|
|
|
if (xhr.readyState === 4) { |
|
|
|
|
if (isFunction(handler.onload)) { |
|
|
|
|
handler.onload(rpe, xhr); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
setTimeout(arguments.callee, 15); |
|
|
|
|
setTimeout(callback, 15); |
|
|
|
|
} |
|
|
|
|
}, 15); |
|
|
|
|
} |
|
|
|
|
setTimeout(callback, 15); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
false |
|
|
|
@ -164,21 +165,21 @@
|
|
|
|
|
filename: _global.encodeURIComponent(handler.file.fileName), |
|
|
|
|
}), true); |
|
|
|
|
if (!xhr.upload) { |
|
|
|
|
var rpe = { loaded: 0, total: handler.file.fileSize || handler.file.size, simulation: true }; |
|
|
|
|
rpe.interval = setInterval(function () { |
|
|
|
|
const rpe = { loaded: 0, total: handler.file.fileSize || handler.file.size, simulation: true }; |
|
|
|
|
rpe.interval = setInterval(() => { |
|
|
|
|
rpe.loaded += 1024 / 4; |
|
|
|
|
if (rpe.total <= rpe.loaded) { |
|
|
|
|
rpe.loaded = rpe.total; |
|
|
|
|
} |
|
|
|
|
upload.onprogress(rpe); |
|
|
|
|
}, 100); |
|
|
|
|
xhr.onabort = function () { |
|
|
|
|
xhr.onabort = () => { |
|
|
|
|
upload.onabort({}); |
|
|
|
|
}; |
|
|
|
|
xhr.onerror = function () { |
|
|
|
|
xhr.onerror = () => { |
|
|
|
|
upload.onerror({}); |
|
|
|
|
}; |
|
|
|
|
xhr.onreadystatechange = function () { |
|
|
|
|
xhr.onreadystatechange = () => { |
|
|
|
|
switch (xhr.readyState) { |
|
|
|
|
case 2: |
|
|
|
|
case 3: |
|
|
|
@ -194,7 +195,7 @@
|
|
|
|
|
upload.onprogress(rpe); |
|
|
|
|
if (199 < xhr.status && xhr.status < 400) { |
|
|
|
|
upload.onload({}); |
|
|
|
|
var attachO = BI.jsonDecode(xhr.responseText); |
|
|
|
|
const attachO = BI.jsonDecode(xhr.responseText); |
|
|
|
|
attachO.filename = handler.file.fileName; |
|
|
|
|
if (handler.file.type.indexOf("image") !== -1) { |
|
|
|
|
attachO.attach_type = "image"; |
|
|
|
@ -210,10 +211,10 @@
|
|
|
|
|
}; |
|
|
|
|
upload.onloadstart(rpe); |
|
|
|
|
} else { |
|
|
|
|
xhr.onreadystatechange = function () { |
|
|
|
|
xhr.onreadystatechange = () => { |
|
|
|
|
switch (xhr.readyState) { |
|
|
|
|
case 4: |
|
|
|
|
var attachO = BI.jsonDecode(xhr.responseText); |
|
|
|
|
const attachO = BI.jsonDecode(xhr.responseText); |
|
|
|
|
if (handler.file.type.indexOf("image") !== -1) { |
|
|
|
|
attachO.attach_type = "image"; |
|
|
|
|
} |
|
|
|
@ -233,7 +234,7 @@
|
|
|
|
|
upload.onloadstart(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
var boundary = "AjaxUploadBoundary" + (new Date).getTime(); |
|
|
|
|
const boundary = "AjaxUploadBoundary" + (new Date).getTime(); |
|
|
|
|
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary); |
|
|
|
|
if (handler.file.getAsBinary) { |
|
|
|
|
xhr[xhr.sendAsBinary ? "sendAsBinary" : "send"](multipart(boundary, handler.name, handler.file)); |
|
|
|
@ -241,7 +242,7 @@
|
|
|
|
|
xhr.setRequestHeader("Content-Type", "multipart/form-data"); |
|
|
|
|
// xhr.setRequestHeader("X-Name", handler.name);
|
|
|
|
|
// xhr.setRequestHeader("X-File-Name", handler.file.fileName);
|
|
|
|
|
var form = new FormData(); |
|
|
|
|
const form = new FormData(); |
|
|
|
|
form.append("FileData", handler.file); |
|
|
|
|
xhr.send(form); |
|
|
|
|
} |
|
|
|
@ -250,11 +251,12 @@
|
|
|
|
|
}; |
|
|
|
|
} else { |
|
|
|
|
// Internet Explorer, Opera, others
|
|
|
|
|
sendFile = function (handler, maxSize, width, height) { |
|
|
|
|
var current = handler.current, iframe, form; |
|
|
|
|
var url = handler.url.concat(-1 === handler.url.indexOf("?") ? "?" : "&", "AjaxUploadFrame=true"), |
|
|
|
|
sendFile = (handler, maxSize, width, height) => { |
|
|
|
|
const current = handler.current; |
|
|
|
|
let iframe, form; |
|
|
|
|
const url = handler.url.concat(-1 === handler.url.indexOf("?") ? "?" : "&", "AjaxUploadFrame=true"), |
|
|
|
|
rpe = { |
|
|
|
|
loaded: 1, total: 100, simulation: true, interval: setInterval(function () { |
|
|
|
|
loaded: 1, total: 100, simulation: true, interval: setInterval(() => { |
|
|
|
|
if (rpe.loaded < rpe.total) { |
|
|
|
|
++rpe.loaded; |
|
|
|
|
} |
|
|
|
@ -264,15 +266,15 @@
|
|
|
|
|
}, 100), |
|
|
|
|
}, |
|
|
|
|
target = ["AjaxUpload", (new Date).getTime(), String(Math.random()).substring(2)].join("_"); |
|
|
|
|
function onload() { |
|
|
|
|
const onload = () => { |
|
|
|
|
iframe.onreadystatechange = iframe.onload = iframe.onerror = null; |
|
|
|
|
form.parentNode.removeChild(form); |
|
|
|
|
form = null; |
|
|
|
|
clearInterval(rpe.interval); |
|
|
|
|
// rpe.loaded = rpe.total;
|
|
|
|
|
const responseText = (iframe.contentWindow.document || iframe.contentWindow.contentDocument).body.innerHTML; |
|
|
|
|
try { |
|
|
|
|
var responseText = (iframe.contentWindow.document || iframe.contentWindow.contentDocument).body.innerHTML; |
|
|
|
|
var attachO = BI.jsonDecode(responseText); |
|
|
|
|
const attachO = BI.jsonDecode(responseText); |
|
|
|
|
if (handler.file.type.indexOf("image") !== -1) { |
|
|
|
|
attachO.attach_type = "image"; |
|
|
|
|
} |
|
|
|
@ -300,10 +302,10 @@
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { // IE < 8 does not accept enctype attribute ...
|
|
|
|
|
var form = document.createElement("<form enctype=\"multipart/form-data\"></form>"), |
|
|
|
|
const form = document.createElement("<form enctype=\"multipart/form-data\"></form>"), |
|
|
|
|
iframe = handler.iframe || (handler.iframe = document.createElement("<iframe id=\"" + target + "\" name=\"" + target + "\" src=\"" + url + "\"></iframe>")); |
|
|
|
|
} catch (e) { |
|
|
|
|
var form = document.createElement("form"), |
|
|
|
|
const form = document.createElement("form"), |
|
|
|
|
iframe = handler.iframe || (handler.iframe = document.createElement("iframe")); |
|
|
|
|
form.setAttribute("enctype", "multipart/form-data"); |
|
|
|
|
iframe.setAttribute("name", iframe.id = target); |
|
|
|
@ -312,12 +314,12 @@
|
|
|
|
|
iframe.style.position = "absolute"; |
|
|
|
|
iframe.style.left = iframe.style.top = "-10000px"; |
|
|
|
|
iframe.onload = onload; |
|
|
|
|
iframe.onerror = function (event) { |
|
|
|
|
iframe.onerror = (event) => { |
|
|
|
|
if (isFunction(handler.onerror)) { |
|
|
|
|
handler.onerror(rpe, event || _global.event); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
iframe.onreadystatechange = function () { |
|
|
|
|
iframe.onreadystatechange = () => { |
|
|
|
|
if (/loaded|complete/i.test(iframe.readyState)) { |
|
|
|
|
onload(); |
|
|
|
|
|
|
|
|
@ -344,7 +346,7 @@
|
|
|
|
|
if (isFunction(handler.onloadstart)) { |
|
|
|
|
handler.onloadstart(rpe, {}); |
|
|
|
|
} |
|
|
|
|
var d = document.body || document.documentElement; |
|
|
|
|
const d = document.body || document.documentElement; |
|
|
|
|
d.appendChild(iframe); |
|
|
|
|
d.appendChild(form); |
|
|
|
|
form.submit(); |
|
|
|
@ -355,10 +357,10 @@
|
|
|
|
|
xhr = null; |
|
|
|
|
|
|
|
|
|
return sendFile; |
|
|
|
|
}(Object.prototype.toString)); |
|
|
|
|
})(Object.prototype.toString)); |
|
|
|
|
|
|
|
|
|
function sendFiles(handler, maxSize, width, height) { |
|
|
|
|
var length = handler.files.length, |
|
|
|
|
const sendFiles = (handler, maxSize, width, height) => { |
|
|
|
|
const length = handler.files.length, |
|
|
|
|
onload = handler.onload, |
|
|
|
|
onloadstart = handler.onloadstart; |
|
|
|
|
handler.current = 0; |
|
|
|
@ -371,24 +373,23 @@
|
|
|
|
|
handler.current = 0; |
|
|
|
|
if (length && handler.files[0].fileSize !== -1) { |
|
|
|
|
handler.file = handler.files[handler.current]; |
|
|
|
|
|
|
|
|
|
sendFile(handler, maxSize, width, height).onload = function (rpe, xhr) { |
|
|
|
|
const callback = (rpe, xhr) => { |
|
|
|
|
handler.onloadstart = null; |
|
|
|
|
handler.sent += (handler.files[handler.current].fileSize || handler.files[handler.current].size); |
|
|
|
|
if (++handler.current < length) { |
|
|
|
|
handler.file = handler.files[handler.current]; |
|
|
|
|
sendFile(handler, maxSize, width, height).onload = arguments.callee; |
|
|
|
|
sendFile(handler, maxSize, width, height).onload = callback; |
|
|
|
|
} else if (onload) { |
|
|
|
|
handler.onloadstart = onloadstart; |
|
|
|
|
handler.onload = onload; |
|
|
|
|
handler.onload(rpe, xhr); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
sendFile(handler, maxSize, width, height).onload = callback; |
|
|
|
|
} else if (length) { |
|
|
|
|
handler.total = length * 100; |
|
|
|
|
handler.file = handler.files[handler.current]; |
|
|
|
|
sendFile(handler, maxSize, width, height).onload = function (rpe, xhr) { |
|
|
|
|
var callee = arguments.callee; |
|
|
|
|
const callback = (rpe, xhr) => { |
|
|
|
|
handler.onloadstart = null; |
|
|
|
|
handler.sent += 100; |
|
|
|
|
if (++handler.current < length) { |
|
|
|
@ -396,12 +397,12 @@
|
|
|
|
|
handler.iframe.parentNode.removeChild(handler.iframe); |
|
|
|
|
handler.iframe = null; |
|
|
|
|
} |
|
|
|
|
setTimeout(function () { |
|
|
|
|
setTimeout(() => { |
|
|
|
|
handler.file = handler.files[handler.current]; |
|
|
|
|
sendFile(handler, maxSize, width, height).onload = callee; |
|
|
|
|
sendFile(handler, maxSize, width, height).onload = callback; |
|
|
|
|
}, 15); |
|
|
|
|
} else if (onload) { |
|
|
|
|
setTimeout(function () { |
|
|
|
|
setTimeout(() => { |
|
|
|
|
handler.iframe.parentNode.removeChild(handler.iframe); |
|
|
|
|
handler.iframe = null; |
|
|
|
|
handler.onloadstart = onloadstart; |
|
|
|
@ -410,31 +411,32 @@
|
|
|
|
|
}, 15); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
sendFile(handler, maxSize, width, height).onload = callback; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return handler; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var r1 = /\.([^.]+)$/; // .png
|
|
|
|
|
var r2 = /\/([^/]+)$/; // image/png
|
|
|
|
|
const r1 = /\.([^.]+)$/; // .png
|
|
|
|
|
const r2 = /\/([^/]+)$/; // image/png
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
/** |
|
|
|
|
* 校验文件类型是否合法,同时兼容旧版形式 |
|
|
|
|
* @param fileName |
|
|
|
|
* @param fileType |
|
|
|
|
* @returns {boolean} |
|
|
|
|
*/ |
|
|
|
|
function fileTypeValidate(fileName, fileType) { |
|
|
|
|
const fileTypeValidate = (fileName, fileType) => { |
|
|
|
|
if (!fileType) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
var mimes = fileType.split(","); |
|
|
|
|
const mimes = fileType.split(","); |
|
|
|
|
if (mimes[0] === fileType) { |
|
|
|
|
mimes = (fileType + "").split(";"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return BI.some(mimes, function (index, mime) { |
|
|
|
|
var matches; |
|
|
|
|
return some(mimes, (index, mime) => { |
|
|
|
|
let matches; |
|
|
|
|
matches = mime.match(r1); |
|
|
|
|
if (matches) { |
|
|
|
|
return fileName.toLowerCase().indexOf(matches[1]) > -1; |
|
|
|
@ -444,13 +446,22 @@
|
|
|
|
|
return matches[1] === "*" ? true : fileName.toLowerCase().indexOf(matches[1]) > -1; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@shortcut() |
|
|
|
|
export class File extends Widget { |
|
|
|
|
static xtype = "bi.file"; |
|
|
|
|
|
|
|
|
|
static EVENT_CHANGE = "EVENT_CHANGE"; |
|
|
|
|
static EVENT_UPLOADSTART = "EVENT_UPLOADSTART"; |
|
|
|
|
static EVENT_ERROR = "EVENT_ERROR"; |
|
|
|
|
static EVENT_PROGRESS = "EVENT_PROGRESS"; |
|
|
|
|
static EVENT_UPLOADED = "EVENT_UPLOADED"; |
|
|
|
|
|
|
|
|
|
BI.File = BI.inherit(BI.Widget, { |
|
|
|
|
_defaultConfig: function () { |
|
|
|
|
var conf = BI.File.superclass._defaultConfig.apply(this, arguments); |
|
|
|
|
_defaultConfig() { |
|
|
|
|
const conf = super._defaultConfig(arguments); |
|
|
|
|
|
|
|
|
|
return BI.extend(conf, { |
|
|
|
|
return extend(conf, { |
|
|
|
|
baseCls: (conf.baseCls || "") + " bi-file display-block", |
|
|
|
|
tagName: "input", |
|
|
|
|
attributes: { |
|
|
|
@ -464,39 +475,39 @@
|
|
|
|
|
maxLength: -1, // 无限制, 与multiple配合使用
|
|
|
|
|
errorText: BI.emptyFn, |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
render: function () { |
|
|
|
|
var o = this.options; |
|
|
|
|
if (o.multiple === true) { |
|
|
|
|
render() { |
|
|
|
|
const { multiple, name, title, accept } = this.options; |
|
|
|
|
if (multiple === true) { |
|
|
|
|
this.element.attr("multiple", "multiple"); |
|
|
|
|
} |
|
|
|
|
this.element.attr("name", o.name || this.getName()); |
|
|
|
|
this.element.attr("title", o.title || ""); |
|
|
|
|
this.element.attr("accept", o.accept); |
|
|
|
|
}, |
|
|
|
|
this.element.attr("name", name || this.getName()); |
|
|
|
|
this.element.attr("title", title || ""); |
|
|
|
|
this.element.attr("accept", accept); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
created: function () { |
|
|
|
|
var self = this, o = this.options; |
|
|
|
|
created() { |
|
|
|
|
const { maxSize, url, accept } = this.options; |
|
|
|
|
// create the noswfupload.wrap Object
|
|
|
|
|
// wrap.maxSize 文件大小限制
|
|
|
|
|
// wrap.maxLength 文件个数限制
|
|
|
|
|
var _wrap = this.wrap = this._wrap(this.element[0], o.maxSize); |
|
|
|
|
const _wrap = this.wrap = this._wrap(this.element[0], maxSize); |
|
|
|
|
// fileType could contain whatever text but filter checks *.{extension}
|
|
|
|
|
// if present
|
|
|
|
|
|
|
|
|
|
// handlers
|
|
|
|
|
// handlerszhe
|
|
|
|
|
|
|
|
|
|
_wrap.onloadstart = function (rpe, xhr) { |
|
|
|
|
self.fireEvent(BI.File.EVENT_UPLOADSTART, arguments); |
|
|
|
|
_wrap.onloadstart = (...args) => { |
|
|
|
|
this.fireEvent(File.EVENT_UPLOADSTART, ...args);
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
_wrap.onprogress = function (rpe, xhr) { |
|
|
|
|
_wrap.onprogress = (rpe, xhr) => { |
|
|
|
|
// percent for each bar
|
|
|
|
|
|
|
|
|
|
// fileSize is -1 only if browser does not support file info access
|
|
|
|
|
// this if splits recent browsers from others
|
|
|
|
|
if (this.file.fileSize !== -1) { |
|
|
|
|
if (_wrap.file.fileSize !== -1) { |
|
|
|
|
// simulation property indicates when the progress event is fake
|
|
|
|
|
if (rpe.simulation) { |
|
|
|
|
// empty
|
|
|
|
@ -509,7 +520,7 @@
|
|
|
|
|
// files sent via Ajax (XMLHttpRequest)
|
|
|
|
|
// We can still show some information
|
|
|
|
|
} |
|
|
|
|
self.fireEvent(BI.File.EVENT_PROGRESS, { |
|
|
|
|
this.fireEvent(File.EVENT_PROGRESS, { |
|
|
|
|
file: this.file, |
|
|
|
|
total: rpe.total, |
|
|
|
|
loaded: rpe.loaded, |
|
|
|
@ -518,79 +529,78 @@
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// generated if there is something wrong during upload
|
|
|
|
|
_wrap.onerror = function () { |
|
|
|
|
_wrap.onerror = () => { |
|
|
|
|
// just inform the user something was wrong
|
|
|
|
|
self.fireEvent(BI.File.EVENT_ERROR); |
|
|
|
|
this.fireEvent(File.EVENT_ERROR); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// generated when every file has been sent (one or more, it does not
|
|
|
|
|
// matter)
|
|
|
|
|
_wrap.onload = function (rpe, xhr) { |
|
|
|
|
var self_ = this; |
|
|
|
|
_wrap.onload = (rpe, xhr) => { |
|
|
|
|
// just show everything is fine ...
|
|
|
|
|
// ... and after a second reset the component
|
|
|
|
|
setTimeout(function () { |
|
|
|
|
self_.clean(); // remove files from list
|
|
|
|
|
self_.hide(); // hide progress bars and enable input file
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
_wrap.clean(); // remove files from list
|
|
|
|
|
_wrap.hide(); // hide progress bars and enable input file
|
|
|
|
|
// enable again the submit button/element
|
|
|
|
|
}, 100); |
|
|
|
|
if (200 > xhr.status || xhr.status > 399) { |
|
|
|
|
BI.Msg.toast(BI.i18nText("BI-Upload_File_Error"), { level: "error" }); |
|
|
|
|
self.fireEvent(BI.File.EVENT_ERROR); |
|
|
|
|
Msg.toast(BI.i18nText("BI-Upload_File_Error"), { level: "error" }); |
|
|
|
|
this.fireEvent(File.EVENT_ERROR); |
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
var error = BI.some(_wrap.attach_array, function (index, attach) { |
|
|
|
|
const error = some(_wrap.attach_array, (index, attach) => { |
|
|
|
|
if (attach.errorCode) { |
|
|
|
|
BI.Msg.toast(BI.i18nText(attach.errorMsg), { level: "error" }); |
|
|
|
|
self.fireEvent(BI.File.EVENT_ERROR, attach); |
|
|
|
|
Msg.toast(BI.i18nText(attach.errorMsg), { level: "error" }); |
|
|
|
|
this.fireEvent(File.EVENT_ERROR, attach); |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
!error && self.fireEvent(BI.File.EVENT_UPLOADED); |
|
|
|
|
!error && this.fireEvent(File.EVENT_UPLOADED); |
|
|
|
|
}; |
|
|
|
|
_wrap.url = o.url; |
|
|
|
|
_wrap.fileType = o.accept; // 文件类型限制
|
|
|
|
|
_wrap.url = url; |
|
|
|
|
_wrap.fileType = accept; // 文件类型限制
|
|
|
|
|
_wrap.attach_array = []; |
|
|
|
|
_wrap.attach_names = []; |
|
|
|
|
_wrap.attachNum = 0; |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_events: function (wrap) { |
|
|
|
|
var self = this, o = this.options; |
|
|
|
|
event.add(wrap.dom.input, "change", function () { |
|
|
|
|
event.del(wrap.dom.input, "change", arguments.callee); |
|
|
|
|
var input = wrap.dom.input.cloneNode(true); |
|
|
|
|
var files = F(wrap.dom.input); |
|
|
|
|
if (o.maxLength !== -1 && o.maxLength < files.length) { |
|
|
|
|
self.fireEvent(BI.File.EVENT_ERROR, { |
|
|
|
|
_events(wrap) { |
|
|
|
|
const { maxLength, errorText } = this.options; |
|
|
|
|
const callback = () => { |
|
|
|
|
event.del(wrap.dom.input, "change", callback); |
|
|
|
|
const input = wrap.dom.input.cloneNode(true); |
|
|
|
|
const files = F(wrap.dom.input); |
|
|
|
|
if (maxLength !== -1 && maxLength < files.length) { |
|
|
|
|
this.fireEvent(File.EVENT_ERROR, { |
|
|
|
|
errorType: 2, |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
for (var i = 0; i < files.length; i++) { |
|
|
|
|
var item = files.item(i); |
|
|
|
|
var tempFile = item.value || item.name; |
|
|
|
|
var value = item.fileName || (item.fileName = tempFile.split("\\").pop()), |
|
|
|
|
for (let i = 0; i < files.length; i++) { |
|
|
|
|
const item = files.item(i); |
|
|
|
|
const tempFile = item.value || item.name; |
|
|
|
|
const value = item.fileName || (item.fileName = tempFile.split("\\").pop()), |
|
|
|
|
size = item.fileSize || item.size; |
|
|
|
|
var validateFileType = fileTypeValidate(value, wrap.fileType); |
|
|
|
|
const validateFileType = fileTypeValidate(value, wrap.fileType); |
|
|
|
|
if (!validateFileType) { |
|
|
|
|
// 文件类型不支持
|
|
|
|
|
BI.Msg.toast(o.errorText({ |
|
|
|
|
Msg.toast(errorText({ |
|
|
|
|
errorType: 0, |
|
|
|
|
file: item, |
|
|
|
|
}) || BI.i18nText("BI-Upload_File_Type_Error", wrap.fileType), { level: "error" }); |
|
|
|
|
self.fireEvent(BI.File.EVENT_ERROR, { |
|
|
|
|
this.fireEvent(File.EVENT_ERROR, { |
|
|
|
|
errorType: 0, |
|
|
|
|
file: item, |
|
|
|
|
}); |
|
|
|
|
} else if (wrap.maxSize !== -1 && size && wrap.maxSize < size) { |
|
|
|
|
// 文件大小不支持
|
|
|
|
|
BI.Msg.toast(o.errorText({ |
|
|
|
|
Msg.toast(errorText({ |
|
|
|
|
errorType: 1, |
|
|
|
|
file: item, |
|
|
|
|
}) || BI.i18nText("BI-Upload_File_Size_Error", Math.ceil(wrap.maxSize / 1024 / 1024)), { level: "error" }); |
|
|
|
|
self.fireEvent(BI.File.EVENT_ERROR, { |
|
|
|
|
this.fireEvent(File.EVENT_ERROR, { |
|
|
|
|
errorType: 1, |
|
|
|
|
file: item, |
|
|
|
|
}); |
|
|
|
@ -599,23 +609,24 @@
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
wrap.files.length > 0 && self.fireEvent(BI.File.EVENT_CHANGE, { |
|
|
|
|
wrap.files.length > 0 && this.fireEvent(File.EVENT_CHANGE, { |
|
|
|
|
files: wrap.files, |
|
|
|
|
}); |
|
|
|
|
input.value = ""; |
|
|
|
|
wrap.dom.input.parentNode.replaceChild(input, wrap.dom.input); |
|
|
|
|
wrap.dom.input = input; |
|
|
|
|
event.add(wrap.dom.input, "change", arguments.callee); |
|
|
|
|
}); |
|
|
|
|
event.add(wrap.dom.input, "change", callback); |
|
|
|
|
} |
|
|
|
|
event.add(wrap.dom.input, "change", callback); |
|
|
|
|
|
|
|
|
|
return wrap; |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_wrap: function () { |
|
|
|
|
var o = this.options; |
|
|
|
|
_wrap() { |
|
|
|
|
const { multiple, maxSize, maxLength } = this.options; |
|
|
|
|
// be sure input accept multiple files
|
|
|
|
|
var input = this.element[0]; |
|
|
|
|
if (o.multiple === true) { |
|
|
|
|
const input = this.element[0]; |
|
|
|
|
if (multiple === true) { |
|
|
|
|
this.element.attr("multiple", "multiple"); |
|
|
|
|
} |
|
|
|
|
input.value = ""; |
|
|
|
@ -630,21 +641,21 @@
|
|
|
|
|
}, |
|
|
|
|
name: input.name, // name to send for each file ($_FILES[{name}] in the server)
|
|
|
|
|
// maxSize is the maximum amount of bytes for each file
|
|
|
|
|
maxSize: o.maxSize ? o.maxSize >> 0 : -1, |
|
|
|
|
maxLength: o.maxLength, |
|
|
|
|
maxSize: maxSize ? maxSize >> 0 : -1, |
|
|
|
|
maxLength, |
|
|
|
|
files: [], // file list
|
|
|
|
|
|
|
|
|
|
// remove every file from the noswfupload component
|
|
|
|
|
clean: function () { |
|
|
|
|
clean() { |
|
|
|
|
this.files = []; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// upload one file a time (which make progress possible rather than all files in one shot)
|
|
|
|
|
// the handler is an object injected into the wrap one, could be the wrap itself or
|
|
|
|
|
// something like {onload:function(){alert("OK")},onerror:function(){alert("Error")}, etc ...}
|
|
|
|
|
upload: function (handler) { |
|
|
|
|
upload(handler) { |
|
|
|
|
if (handler) { |
|
|
|
|
for (var key in handler) { |
|
|
|
|
for (let key in handler) { |
|
|
|
|
this[key] = handler[key]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -654,7 +665,7 @@
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// hide progress bar (total + current) and enable files selection
|
|
|
|
|
hide: function () { |
|
|
|
|
hide() { |
|
|
|
|
if (this.dom.disabled) { |
|
|
|
|
this.dom.disabled = false; |
|
|
|
|
this.dom.input.removeAttribute("disabled"); |
|
|
|
@ -664,76 +675,69 @@
|
|
|
|
|
// show progress bar and disable file selection (used during upload)
|
|
|
|
|
// total and current are pixels used to style bars
|
|
|
|
|
// totalProp and currentProp are properties to change, "height" by default
|
|
|
|
|
show: function (total, current, totalProp, currentProp) { |
|
|
|
|
show(total, current, totalProp, currentProp) { |
|
|
|
|
if (!this.dom.disabled) { |
|
|
|
|
this.dom.disabled = true; |
|
|
|
|
this.dom.input.setAttribute("disabled", "disabled"); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setUrl: function(v) { |
|
|
|
|
setUrl(v) { |
|
|
|
|
this.options.url = v; |
|
|
|
|
if (this.wrap) { |
|
|
|
|
this.wrap.url = v; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setMaxFileLength: function (v) { |
|
|
|
|
setMaxFileLength(v) { |
|
|
|
|
this.options.maxLength = v; |
|
|
|
|
if (this.wrap) { |
|
|
|
|
this.wrap.maxLength = v; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
select: function () { |
|
|
|
|
this.wrap && BI.Widget._renderEngine.createElement(this.wrap.dom.input).click(); |
|
|
|
|
}, |
|
|
|
|
select() { |
|
|
|
|
this.wrap && Widget._renderEngine.createElement(this.wrap.dom.input).click(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
upload: function (handler) { |
|
|
|
|
upload(handler) { |
|
|
|
|
this.wrap && this.wrap.upload(handler); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getValue: function () { |
|
|
|
|
getValue() { |
|
|
|
|
return this.wrap ? this.wrap.attach_array : []; |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getQueue: function () { |
|
|
|
|
getQueue() { |
|
|
|
|
return this.wrap.files; |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
reset: function () { |
|
|
|
|
reset() { |
|
|
|
|
if (this.wrap) { |
|
|
|
|
this.wrap.attach_array = []; |
|
|
|
|
this.wrap.attach_names = []; |
|
|
|
|
this.wrap.attachNum = 0; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sendFiles: function (files) { |
|
|
|
|
sendFiles(files) { |
|
|
|
|
if (!this.wrap) return; |
|
|
|
|
|
|
|
|
|
this.wrap.dom.input.files = files; |
|
|
|
|
|
|
|
|
|
var event = new CustomEvent("change"); |
|
|
|
|
const event = new CustomEvent("change"); |
|
|
|
|
|
|
|
|
|
this.wrap.dom.input.dispatchEvent(event); |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_setEnable: function (enable) { |
|
|
|
|
BI.File.superclass._setEnable.apply(this, arguments); |
|
|
|
|
_setEnable(enable) { |
|
|
|
|
super._setEnable(arguments); |
|
|
|
|
if (enable === true) { |
|
|
|
|
this.element.removeAttr("disabled"); |
|
|
|
|
} else { |
|
|
|
|
this.element.attr("disabled", "disabled"); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
BI.File.EVENT_CHANGE = "EVENT_CHANGE"; |
|
|
|
|
BI.File.EVENT_UPLOADSTART = "EVENT_UPLOADSTART"; |
|
|
|
|
BI.File.EVENT_ERROR = "EVENT_ERROR"; |
|
|
|
|
BI.File.EVENT_PROGRESS = "EVENT_PROGRESS"; |
|
|
|
|
BI.File.EVENT_UPLOADED = "EVENT_UPLOADED"; |
|
|
|
|
BI.shortcut("bi.file", BI.File); |
|
|
|
|
})(_global.document || {})); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|