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