/**
* 请不要删除 init 方法, 否在外面拿不到 upload 实例
*/
/* ```````````````````````````````````````````````````````````````````````````
' +
'
删除').appendTo( $li ),
$prgress = $li.find('p.progress span'),
$wrap = $li.find( 'p.imgWrap' ),
$info = $('
'),
showError = function( code ) {
switch( code ) {
case 'exceed_size':
text = '文件大小超出';
break;
case 'interrupt':
text = '上传暂停';
break;
default:
text = '上传失败,请重试';
break;
}
$info.text( text ).appendTo( $li );
};
if ( file.getStatus() === 'invalid' ) {
showError( file.statusText );
} else {
$wrap.text( '预览中' );
self.uploader.makeThumb( file, function( error, src ) {
var img;
if ( error ) {
$wrap.text( '不能预览' );
return;
}
if( self.isSupportBase64 ) {
img = $('

');
$wrap.empty().append( img );
} else {
$.ajax('../../server/preview.php', {
method: 'POST',
data: src,
dataType:'json'
}).done(function( response ) {
if (response.result) {
img = $('

');
$wrap.empty().append( img );
} else {
$wrap.text("预览出错");
}
});
}
}, self.thumbnailWidth, self.thumbnailHeight );
self.percentages[ file.id ] = [ file.size, 0 ];
file.rotation = 0;
}
file.on('statuschange', function( cur, prev ) {
if ( prev === 'progress' ) {
$prgress.hide().width(0);
} else if ( prev === 'queued' ) {
$li.off( 'mouseenter mouseleave' );
$btns.remove();
}
// 成功
if ( cur === 'error' || cur === 'invalid' ) {
showError( file.statusText );
self.percentages[ file.id ][ 1 ] = 1;
} else if ( cur === 'interrupt' ) {
showError( 'interrupt' );
} else if ( cur === 'queued' ) {
$info.remove();
$prgress.css('display', 'block');
self.percentages[ file.id ][ 1 ] = 0;
} else if ( cur === 'progress' ) {
$info.remove();
$prgress.css('display', 'block');
} else if ( cur === 'complete' ) {
$prgress.hide().width(0);
$li.append( '
' );
}
$li.removeClass( 'state-' + prev ).addClass( 'state-' + cur );
});
// 鼠标进入
$li.on( 'mouseenter', function() {
$btns.stop().animate({height: 30});
});
// 鼠标离开
$li.on( 'mouseleave', function() {
$btns.stop().animate({height: 0});
});
// 预览上按钮点击事件
$btns.on( 'click', 'span', function() {
var index = $(this).index(),
deg;
switch ( index ) {
case 0:
self.uploader.removeFile( file );
return;
}
if ( supportTransition ) {
deg = 'rotate(' + file.rotation + 'deg)';
$wrap.css({
'-webkit-transform': deg,
'-mos-transform': deg,
'-o-transform': deg,
'transform': deg
});
} else {
$wrap.css( 'filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ (~~((file.rotation/90)%4 + 4)%4) +')');
}
});
$li.appendTo( self.$queue );
}
// 负责view的销毁
removeFile( file ) {
var $li = $('#'+file.id);
delete this.percentages[ file.id ];
this.updateTotalProgress();
$li.off().find('.file-panel').off().end().remove();
}
// 更新总进度
updateTotalProgress() {
let self = this;
var loaded = 0,
total = 0,
spans = this.$progress.children(),
percent;
$.each( this.percentages, function( k, v ) {
total += v[ 0 ];
loaded += v[ 0 ] * v[ 1 ];
} );
percent = total ? loaded / total : 0;
spans.eq( 0 ).text( Math.round( percent * 100 ) + '%' );
spans.eq( 1 ).css( 'width', Math.round( percent * 100 ) + '%' );
this.updateStatus();
}
setState( val ) {
let self = this;
var file, stats;
if ( val === self.state ) {
return;
}
this.$upload.removeClass( 'state-' + self.state );
this.$upload.addClass( 'state-' + val );
self.state = val;
switch ( self.state ) {
case 'pedding':
self.$placeHolder.removeClass( 'element-invisible' );
self.$queue.parent().removeClass('filled');
self.$queue.hide();
self.$statusBar.addClass( 'element-invisible' );
self.uploader.refresh();
break;
case 'ready':
self.$placeHolder.addClass( 'element-invisible' );
$('#filePicker2' ).removeClass( 'element-invisible');
self.$queue.parent().addClass('filled');
self.$queue.show();
self.$statusBar.removeClass('element-invisible');
self.uploader.refresh();
break;
case 'uploading':
$('#filePicker2' ).addClass( 'element-invisible' );
self.$progress.show();
self.$progress.show();
self.$upload.text( '暂停上传' );
break;
case 'paused':
self.$progress.show();
self.$upload.text( '继续上传' );
break;
case 'confirm':
self.$progress.hide();
$('#filePicker2' ).removeClass( 'element-invisible' );
self.$upload.text( '开始上传' );
stats = self.uploader.getStats();
if ( stats.successNum && !stats.uploadFailNum ) {
self.setState( 'finish' );
return;
}
break;
case 'finish':
stats = self.uploader.getStats();
if ( stats.successNum ) {
// alert( '上传成功' );
} else {
// 没有成功的图片,重设
self.state = 'done';
location.reload();
}
break;
}
self.updateStatus();
}
updateStatus(){
var text = '', stats;
if ( this.state === 'ready' ) {
text = '选中' + this.fileCount + '张图片,共' +
WebUploader.formatSize( this.fileSize ) + '。';
} else if ( this.state === 'confirm' ) {
stats = this.uploader.getStats();
if ( stats.uploadFailNum ) {
text = '已成功上传' + stats.successNum+ '张照片,'+
stats.uploadFailNum + '张照片上传失败';
}
} else {
stats = this.uploader.getStats();
text = '共' + this.fileCount + '张(' +
WebUploader.formatSize( this.fileSize ) +
'),已上传' + stats.successNum + '张';
if ( stats.uploadFailNum ) {
text += ',失败' + stats.uploadFailNum + '张';
}
}
this.$info.html( text );
}
// 判断浏览器是否支持图片的base64
isSupportBase64 (){
var data = new Image();
var support = true;
data.onload = data.onerror = function() {
if( this.width != 1 || this.height != 1 ) {
support = false;
}
}
data.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
return support;
}
}
window.MyWebUpload = MyWebUpload;