@ -6,24 +6,24 @@
* @ extends BI . Single
* @ abstract
* /
( function ( document ) {
( ( function ( 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 ) {
var F = ( ( function ( item ) {
return function ( input ) {
var files = input . files || [ input ] ;
if ( ! files . item ) {
files . item = item ;
}
return files ;
} ;
} ) ( function ( i ) {
return this [ i ] ;
} ) ;
} ) ) ;
var event = {
@ -37,10 +37,12 @@
add : document . addEventListener ?
function ( node , name , callback ) {
node . addEventListener ( name , callback , false ) ;
return this ;
} :
function ( node , name , callback ) {
node . attachEvent ( "on" + name , callback ) ;
return this ;
} ,
@ -54,10 +56,12 @@
del : document . removeEventListener ?
function ( node , name , callback ) {
node . removeEventListener ( name , callback , false ) ;
return this ;
} :
function ( node , name , callback ) {
node . detachEvent ( "on" + name , callback ) ;
return this ;
} ,
@ -77,11 +81,16 @@
}
return false ;
}
} ,
} ;
var sendFile = ( function ( toString ) {
var multipart = function ( boundary , name , file ) {
var split = "onabort.onerror.onloadstart.onprogress" . split ( "." ) ,
length = split . length ,
CRLF = "\r\n" ,
xhr = XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject ( "Microsoft.XMLHTTP" ) ,
sendFile ;
function multipart ( boundary , name , file ) {
return "--" . concat (
boundary , CRLF ,
"Content-Disposition: form-data; name=\"" , name , "\"; filename=\"" , _global . encodeURIComponent ( file . fileName ) , "\"" , CRLF ,
@ -90,15 +99,11 @@
file . getAsBinary ( ) , CRLF ,
"--" , boundary , "--" , CRLF
) ;
} ,
isFunction = f unction ( Function ) {
}
function isF unction ( Function ) {
return toString . call ( Function ) === "[object Function]" ;
} ,
split = "onabort.onerror.onloadstart.onprogress" . split ( "." ) ,
length = split . length ,
CRLF = "\r\n" ,
xhr = XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject ( "Microsoft.XMLHTTP" ) ,
sendFile ;
}
// FireFox 3+, Safari 4 beta (Chrome 2 beta file is buggy and will not work)
if ( xhr . upload || xhr . sendAsBinary ) {
@ -108,14 +113,15 @@
if ( isFunction ( handler . onerror ) ) {
handler . onerror ( ) ;
}
return ;
return ;
}
for ( var
xhr = new XMLHttpRequest ,
upload = xhr . upload || {
addEventListener : function ( event , callback ) {
this [ "on" + event ] = callback ;
}
} ,
} ,
i = 0 ;
i < length ;
@ -129,7 +135,7 @@
handler [ event ] ( rpe , xhr ) ;
}
} ;
} ) ( split [ i ] ) ,
} ( split [ i ] ) ) ,
false
) ;
}
@ -174,49 +180,53 @@
} ;
xhr . onreadystatechange = function ( ) {
switch ( xhr . readyState ) {
case 2 :
case 3 :
case 2 :
case 3 :
if ( rpe . total <= rpe . loaded ) {
rpe . loaded = rpe . total ;
}
upload . onprogress ( rpe ) ;
break ;
case 4 :
case 4 :
clearInterval ( rpe . interval ) ;
rpe . interval = 0 ;
rpe . loaded = rpe . total ;
upload . onprogress ( rpe ) ;
if ( 199 < xhr . status && xhr . status < 400 ) {
upload [ "onload" ] ( { } ) ;
upload . onload ( { } ) ;
var attachO = BI . jsonDecode ( xhr . responseText ) ;
attachO . filename = handler . file . fileName ;
if ( handler . file . type . indexOf ( "image" ) != - 1 ) {
if ( handler . file . type . indexOf ( "image" ) !== - 1 ) {
attachO . attach _type = "image" ;
}
handler . attach _array [ current ] = attachO ;
} else {
upload [ "onerror" ] ( { } ) ;
upload . onerror ( { } ) ;
}
break ;
default :
break ;
}
} ;
upload . onloadstart ( rpe ) ;
} else {
xhr . onreadystatechange = function ( ) {
switch ( xhr . readyState ) {
case 4 :
case 4 :
var attachO = BI . jsonDecode ( xhr . responseText ) ;
if ( handler . file . type . indexOf ( "image" ) != - 1 ) {
if ( handler . file . type . indexOf ( "image" ) !== - 1 ) {
attachO . attach _type = "image" ;
}
attachO . filename = handler . file . fileName ;
if ( handler . maxLength == 1 ) {
if ( handler . maxLength === 1 ) {
handler . attach _array [ 0 ] = attachO ;
// handler.attach_array.push(attachO);
} else {
handler . attach _array [ current ] = attachO ;
}
break ;
default :
break ;
}
} ;
if ( isFunction ( upload . onloadstart ) ) {
@ -235,11 +245,11 @@
form . append ( "FileData" , handler . file ) ;
xhr . send ( form ) ;
}
return handler ;
} ;
}
// Internet Explorer, Opera, others
else {
} else {
// Internet Explorer, Opera, others
sendFile = function ( handler , maxSize , width , height ) {
var current = handler . current ;
var url = handler . url . concat ( - 1 === handler . url . indexOf ( "?" ) ? "?" : "&" , "AjaxUploadFrame=true" ) ,
@ -251,9 +261,10 @@
if ( isFunction ( handler . onprogress ) ) {
handler . onprogress ( rpe , { } ) ;
}
} , 100 )
} , 100 ) ,
} ,
onload = function ( ) {
target = [ "AjaxUpload" , ( new Date ) . getTime ( ) , String ( Math . random ( ) ) . substring ( 2 ) ] . join ( "_" ) ;
function onload ( ) {
iframe . onreadystatechange = iframe . onload = iframe . onerror = null ;
form . parentNode . removeChild ( form ) ;
form = null ;
@ -262,7 +273,7 @@
try {
var responseText = ( iframe . contentWindow . document || iframe . contentWindow . contentDocument ) . body . innerHTML ;
var attachO = BI . jsonDecode ( responseText ) ;
if ( handler . file . type . indexOf ( "image" ) != - 1 ) {
if ( handler . file . type . indexOf ( "image" ) !== - 1 ) {
attachO . attach _type = "image" ;
}
@ -273,7 +284,7 @@
} catch ( e ) {
attachO . filename = handler . file . fileName ;
}
if ( handler . maxLength == 1 ) {
if ( handler . maxLength === 1 ) {
handler . attach _array [ 0 ] = attachO ;
} else {
handler . attach _array [ current ] = attachO ;
@ -286,8 +297,8 @@
if ( isFunction ( handler . onload ) ) {
handler . onload ( rpe , { responseText : responseText } ) ;
}
} ,
target = [ "AjaxUpload" , ( new Date ) . getTime ( ) , String ( Math . random ( ) ) . substring ( 2 ) ] . join ( "_" ) ;
}
try { // IE < 8 does not accept enctype attribute ...
var form = document . createElement ( "<form enctype=\"multipart/form-data\"></form>" ) ,
iframe = handler . iframe || ( handler . iframe = document . createElement ( "<iframe id=\"" + target + "\" name=\"" + target + "\" src=\"" + url + "\"></iframe>" ) ) ;
@ -320,8 +331,8 @@
loading : 2 ,
interactive : 3 ,
loaded : 4 ,
complete : 4
} [ iframe . readyState ] || 1
complete : 4 ,
} [ iframe . readyState ] || 1 ,
} ) ;
}
} ;
@ -342,13 +353,12 @@
} ;
}
xhr = null ;
return sendFile ;
} ) ( Object . prototype . toString ) ;
var sendFiles = function ( handler , maxSize , width , height ) {
} ( Object . prototype . toString ) ) ;
function sendFiles ( handler , maxSize , width , height ) {
var length = handler . files . length ,
i = 0 ,
onload = handler . onload ,
onloadstart = handler . onloadstart ;
handler . current = 0 ;
@ -401,11 +411,12 @@
}
} ;
}
return handler ;
} ;
}
var r1 = /\.([^.]+)$/ ; // .png
var r2 = /\/([^/]+)$/ ; // image/png
var r1 = /\.([^.]+)$/ ; // .png
var r2 = /\/([^/]+)$/ ; // image/png
/ * *
* 校验文件类型是否合法 , 同时兼容旧版形式
@ -413,7 +424,7 @@
* @ param fileType
* @ returns { boolean }
* /
var fileTypeValidate = function ( fileName , fileType ) {
function fileTypeValidate ( fileName , fileType ) {
if ( ! fileType ) {
return true ;
}
@ -421,25 +432,29 @@
if ( mimes [ 0 ] === fileType ) {
mimes = ( fileType + "" ) . split ( ";" ) ;
}
return BI . some ( mimes , function ( index , mime ) {
var matches ;
if ( matches = mime . match ( r1 ) ) {
matches = mime . match ( r1 ) ;
if ( matches ) {
return fileName . toLowerCase ( ) . indexOf ( matches [ 1 ] ) > - 1 ;
}
if ( matches = mime . match ( r2 ) ) {
matches = mime . match ( r2 ) ;
if ( matches ) {
return matches [ 1 ] === "*" ? true : fileName . toLowerCase ( ) . indexOf ( matches [ 1 ] ) > - 1 ;
}
} ) ;
} ;
}
BI . File = BI . inherit ( BI . Widget , {
_defaultConfig : function ( ) {
var conf = BI . File . superclass . _defaultConfig . apply ( this , arguments ) ;
return BI . extend ( conf , {
baseCls : ( conf . baseCls || "" ) + " bi-file display-block" ,
tagName : "input" ,
attributes : {
type : "file"
type : "file" ,
} ,
name : "" ,
url : "" ,
@ -452,7 +467,7 @@
} ,
render : function ( ) {
var self = this , o = this . options ;
var o = this . options ;
if ( o . multiple === true ) {
this . element . attr ( "multiple" , "multiple" ) ;
}
@ -498,7 +513,7 @@
file : this . file ,
total : rpe . total ,
loaded : rpe . loaded ,
simulation : rpe . simulation
simulation : rpe . simulation ,
} ) ;
} ;
@ -522,19 +537,21 @@
if ( 200 > xhr . status || xhr . status > 399 ) {
BI . Msg . toast ( BI . i18nText ( "BI-Upload_File_Error" ) , { level : "error" } ) ;
self . fireEvent ( BI . File . EVENT _ERROR ) ;
return ;
}
var error = BI . some ( _wrap . attach _array , function ( index , attach ) {
if ( attach . errorCode ) {
BI . Msg . toast ( BI . i18nText ( attach . errorMsg ) , { level : "error" } ) ;
self . fireEvent ( BI . File . EVENT _ERROR , attach ) ;
return true ;
}
} ) ;
! error && self . fireEvent ( BI . File . EVENT _UPLOADED ) ;
} ;
_wrap . url = o . url ;
_wrap . fileType = o . accept ; // 文件类型限制
_wrap . fileType = o . accept ; // 文件类型限制
_wrap . attach _array = [ ] ;
_wrap . attach _names = [ ] ;
_wrap . attachNum = 0 ;
@ -548,35 +565,34 @@
var files = F ( wrap . dom . input ) ;
if ( o . maxLength !== - 1 && o . maxLength < files . length ) {
self . fireEvent ( BI . File . EVENT _ERROR , {
errorType : 2
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 ( ) ) ,
ext = - 1 !== value . indexOf ( "." ) ? value . split ( "." ) . pop ( ) . toLowerCase ( ) : "unknown" ,
size = item . fileSize || item . size ;
var validateFileType = fileTypeValidate ( value , wrap . fileType ) ;
if ( ! validateFileType ) {
// 文件类型不支持
BI . Msg . toast ( o . errorText ( {
errorType : 0 ,
file : item
file : item ,
} ) || BI . i18nText ( "BI-Upload_File_Type_Error" , wrap . fileType ) , { level : "error" } ) ;
self . fireEvent ( BI . File . EVENT _ERROR , {
errorType : 0 ,
file : item
file : item ,
} ) ;
} else if ( wrap . maxSize !== - 1 && size && wrap . maxSize < size ) {
// 文件大小不支持
BI . Msg . toast ( o . errorText ( {
errorType : 1 ,
file : item
file : item ,
} ) || BI . i18nText ( "BI-Upload_File_Size_Error" , Math . ceil ( wrap . maxSize / 1024 / 1024 ) ) , { level : "error" } ) ;
self . fireEvent ( BI . File . EVENT _ERROR , {
errorType : 1 ,
file : item
file : item ,
} ) ;
} else {
wrap . files . unshift ( item ) ;
@ -584,18 +600,19 @@
}
}
wrap . files . length > 0 && self . fireEvent ( BI . File . EVENT _CHANGE , {
files : wrap . files
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 ) ;
} ) ;
return wrap ;
} ,
_wrap : function ( ) {
var self = this , o = this . options ;
var o = this . options ;
// be sure input accept multiple files
var input = this . element [ 0 ] ;
if ( o . multiple === true ) {
@ -608,14 +625,14 @@
// DOM namespace
dom : {
input : input , // input file
disabled : false // internal use, checks input file state
input : input , // input file
disabled : false , // internal use, checks input file state
} ,
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 : o . maxSize ? o . maxSize >> 0 : - 1 ,
maxLength : o . maxLength ,
files : [ ] , // file list
files : [ ] , // file list
// remove every file from the noswfupload component
clean : function ( ) {
@ -632,6 +649,7 @@
}
}
sendFiles ( this , this . maxSize ) ;
return this ;
} ,
@ -651,7 +669,7 @@
this . dom . disabled = true ;
this . dom . input . setAttribute ( "disabled" , "disabled" ) ;
}
}
} ,
} ) ;
} ,
@ -710,7 +728,7 @@
} else {
this . element . attr ( "disabled" , "disabled" ) ;
}
}
} ,
} ) ;
BI . File . EVENT _CHANGE = "EVENT_CHANGE" ;
BI . File . EVENT _UPLOADSTART = "EVENT_UPLOADSTART" ;
@ -718,4 +736,4 @@
BI . File . EVENT _PROGRESS = "EVENT_PROGRESS" ;
BI . File . EVENT _UPLOADED = "EVENT_UPLOADED" ;
BI . shortcut ( "bi.file" , BI . File ) ;
} ) ( _global . document || { } ) ;
} ) ( _global . document || { } ) ) ;