Initial commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @fileOverview Blob
|
||||
*/
|
||||
define([
|
||||
'../base',
|
||||
'../runtime/client'
|
||||
], function( Base, RuntimeClient ) {
|
||||
|
||||
function Blob( ruid, source ) {
|
||||
var me = this;
|
||||
|
||||
me.source = source;
|
||||
me.ruid = ruid;
|
||||
this.size = source.size || 0;
|
||||
|
||||
// 如果没有指定 mimetype, 但是知道文件后缀。
|
||||
if ( !source.type && this.ext &&
|
||||
~'jpg,jpeg,png,gif,bmp'.indexOf( this.ext ) ) {
|
||||
this.type = 'image/' + (this.ext === 'jpg' ? 'jpeg' : this.ext);
|
||||
} else {
|
||||
this.type = source.type || 'application/octet-stream';
|
||||
}
|
||||
|
||||
RuntimeClient.call( me, 'Blob' );
|
||||
this.uid = source.uid || this.uid;
|
||||
|
||||
if ( ruid ) {
|
||||
me.connectRuntime( ruid );
|
||||
}
|
||||
}
|
||||
|
||||
Base.inherits( RuntimeClient, {
|
||||
constructor: Blob,
|
||||
|
||||
slice: function( start, end ) {
|
||||
return this.exec( 'slice', start, end );
|
||||
},
|
||||
|
||||
getSource: function() {
|
||||
return this.source;
|
||||
}
|
||||
});
|
||||
|
||||
return Blob;
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @fileOverview 错误信息
|
||||
*/
|
||||
define([
|
||||
'../base',
|
||||
'../mediator',
|
||||
'../runtime/client'
|
||||
], function( Base, Mediator, RuntimeClent ) {
|
||||
|
||||
var $ = Base.$;
|
||||
|
||||
function DragAndDrop( opts ) {
|
||||
opts = this.options = $.extend({}, DragAndDrop.options, opts );
|
||||
|
||||
opts.container = $( opts.container );
|
||||
|
||||
if ( !opts.container.length ) {
|
||||
return;
|
||||
}
|
||||
|
||||
RuntimeClent.call( this, 'DragAndDrop' );
|
||||
}
|
||||
|
||||
DragAndDrop.options = {
|
||||
accept: null,
|
||||
disableGlobalDnd: false
|
||||
};
|
||||
|
||||
Base.inherits( RuntimeClent, {
|
||||
constructor: DragAndDrop,
|
||||
|
||||
init: function() {
|
||||
var me = this;
|
||||
|
||||
me.connectRuntime( me.options, function() {
|
||||
me.exec('init');
|
||||
me.trigger('ready');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Mediator.installTo( DragAndDrop.prototype );
|
||||
|
||||
return DragAndDrop;
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 为了统一化Flash的File和HTML5的File而存在。
|
||||
* 以至于要调用Flash里面的File,也可以像调用HTML5版本的File一下。
|
||||
* @fileOverview File
|
||||
*/
|
||||
define([
|
||||
'../base',
|
||||
'./blob'
|
||||
], function( Base, Blob ) {
|
||||
|
||||
var uid = 1,
|
||||
rExt = /\.([^.]+)$/;
|
||||
|
||||
function File( ruid, file ) {
|
||||
var ext;
|
||||
|
||||
this.name = file.name || ('untitled' + uid++);
|
||||
ext = rExt.exec( file.name ) ? RegExp.$1.toLowerCase() : '';
|
||||
|
||||
// todo 支持其他类型文件的转换。
|
||||
// 如果有 mimetype, 但是文件名里面没有找出后缀规律
|
||||
if ( !ext && file.type ) {
|
||||
ext = /\/(jpg|jpeg|png|gif|bmp)$/i.exec( file.type ) ?
|
||||
RegExp.$1.toLowerCase() : '';
|
||||
this.name += '.' + ext;
|
||||
}
|
||||
|
||||
this.ext = ext;
|
||||
this.lastModifiedDate = file.lastModifiedDate ||
|
||||
(new Date()).toLocaleString();
|
||||
|
||||
Blob.apply( this, arguments );
|
||||
}
|
||||
|
||||
return Base.inherits( Blob, File );
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @fileOverview 错误信息
|
||||
*/
|
||||
define([
|
||||
'../base',
|
||||
'../mediator',
|
||||
'../runtime/client'
|
||||
], function( Base, Mediator, RuntimeClent ) {
|
||||
|
||||
var $ = Base.$;
|
||||
|
||||
function FilePaste( opts ) {
|
||||
opts = this.options = $.extend({}, opts );
|
||||
opts.container = $( opts.container || document.body );
|
||||
RuntimeClent.call( this, 'FilePaste' );
|
||||
}
|
||||
|
||||
Base.inherits( RuntimeClent, {
|
||||
constructor: FilePaste,
|
||||
|
||||
init: function() {
|
||||
var me = this;
|
||||
|
||||
me.connectRuntime( me.options, function() {
|
||||
me.exec('init');
|
||||
me.trigger('ready');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Mediator.installTo( FilePaste.prototype );
|
||||
|
||||
return FilePaste;
|
||||
});
|
||||
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* @fileOverview 错误信息
|
||||
*/
|
||||
define([
|
||||
'../base',
|
||||
'../runtime/client',
|
||||
'./file'
|
||||
], function( Base, RuntimeClient, File ) {
|
||||
|
||||
var $ = Base.$;
|
||||
|
||||
function FilePicker( opts ) {
|
||||
opts = this.options = $.extend({}, FilePicker.options, opts );
|
||||
|
||||
opts.container = $( opts.id );
|
||||
|
||||
if ( !opts.container.length ) {
|
||||
throw new Error('按钮指定错误');
|
||||
}
|
||||
|
||||
opts.innerHTML = opts.innerHTML || opts.label ||
|
||||
opts.container.html() || '';
|
||||
|
||||
opts.button = $( opts.button || document.createElement('div') );
|
||||
opts.button.html( opts.innerHTML );
|
||||
opts.container.html( opts.button );
|
||||
|
||||
RuntimeClient.call( this, 'FilePicker', true );
|
||||
}
|
||||
|
||||
FilePicker.options = {
|
||||
button: null,
|
||||
container: null,
|
||||
label: null,
|
||||
innerHTML: null,
|
||||
multiple: true,
|
||||
accept: null,
|
||||
name: 'file',
|
||||
style: 'webuploader-pick' //pick element class attribute, default is "webuploader-pick"
|
||||
};
|
||||
|
||||
Base.inherits( RuntimeClient, {
|
||||
constructor: FilePicker,
|
||||
|
||||
init: function() {
|
||||
var me = this,
|
||||
opts = me.options,
|
||||
button = opts.button,
|
||||
style = opts.style;
|
||||
|
||||
if (style)
|
||||
button.addClass('webuploader-pick');
|
||||
|
||||
me.on( 'all', function( type ) {
|
||||
var files;
|
||||
|
||||
switch ( type ) {
|
||||
case 'mouseenter':
|
||||
if (style)
|
||||
button.addClass('webuploader-pick-hover');
|
||||
break;
|
||||
|
||||
case 'mouseleave':
|
||||
if (style)
|
||||
button.removeClass('webuploader-pick-hover');
|
||||
break;
|
||||
|
||||
case 'change':
|
||||
files = me.exec('getFiles');
|
||||
me.trigger( 'select', $.map( files, function( file ) {
|
||||
file = new File( me.getRuid(), file );
|
||||
|
||||
// 记录来源。
|
||||
file._refer = opts.container;
|
||||
return file;
|
||||
}), opts.container );
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
me.connectRuntime( opts, function() {
|
||||
me.refresh();
|
||||
me.exec( 'init', opts );
|
||||
me.trigger('ready');
|
||||
});
|
||||
|
||||
this._resizeHandler = Base.bindFn( this.refresh, this );
|
||||
$( window ).on( 'resize', this._resizeHandler );
|
||||
},
|
||||
|
||||
refresh: function() {
|
||||
var shimContainer = this.getRuntime().getContainer(),
|
||||
button = this.options.button,
|
||||
width = button.outerWidth ?
|
||||
button.outerWidth() : button.width(),
|
||||
|
||||
height = button.outerHeight ?
|
||||
button.outerHeight() : button.height(),
|
||||
|
||||
pos = button.offset();
|
||||
|
||||
width && height && shimContainer.css({
|
||||
bottom: 'auto',
|
||||
right: 'auto',
|
||||
width: width + 'px',
|
||||
height: height + 'px'
|
||||
}).offset( pos );
|
||||
},
|
||||
|
||||
enable: function() {
|
||||
var btn = this.options.button;
|
||||
|
||||
btn.removeClass('webuploader-pick-disable');
|
||||
this.refresh();
|
||||
},
|
||||
|
||||
disable: function() {
|
||||
var btn = this.options.button;
|
||||
|
||||
this.getRuntime().getContainer().css({
|
||||
top: '-99999px'
|
||||
});
|
||||
|
||||
btn.addClass('webuploader-pick-disable');
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
var btn = this.options.button;
|
||||
$( window ).off( 'resize', this._resizeHandler );
|
||||
btn.removeClass('webuploader-pick-disable webuploader-pick-hover ' +
|
||||
'webuploader-pick');
|
||||
}
|
||||
});
|
||||
|
||||
return FilePicker;
|
||||
});
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* @fileOverview Image
|
||||
*/
|
||||
define([
|
||||
'../base',
|
||||
'../runtime/client',
|
||||
'./blob'
|
||||
], function( Base, RuntimeClient, Blob ) {
|
||||
var $ = Base.$;
|
||||
|
||||
// 构造器。
|
||||
function Image( opts ) {
|
||||
this.options = $.extend({}, Image.options, opts );
|
||||
RuntimeClient.call( this, 'Image' );
|
||||
|
||||
this.on( 'load', function() {
|
||||
this._info = this.exec('info');
|
||||
this._meta = this.exec('meta');
|
||||
});
|
||||
}
|
||||
|
||||
// 默认选项。
|
||||
Image.options = {
|
||||
|
||||
// 默认的图片处理质量
|
||||
quality: 90,
|
||||
|
||||
// 是否裁剪
|
||||
crop: false,
|
||||
|
||||
// 是否保留头部信息
|
||||
preserveHeaders: false,
|
||||
|
||||
// 是否允许放大。
|
||||
allowMagnify: false
|
||||
};
|
||||
|
||||
// 继承RuntimeClient.
|
||||
Base.inherits( RuntimeClient, {
|
||||
constructor: Image,
|
||||
|
||||
info: function( val ) {
|
||||
|
||||
// setter
|
||||
if ( val ) {
|
||||
this._info = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
// getter
|
||||
return this._info;
|
||||
},
|
||||
|
||||
meta: function( val ) {
|
||||
|
||||
// setter
|
||||
if ( val ) {
|
||||
this._meta = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
// getter
|
||||
return this._meta;
|
||||
},
|
||||
|
||||
loadFromBlob: function( blob ) {
|
||||
var me = this,
|
||||
ruid = blob.getRuid();
|
||||
|
||||
this.connectRuntime( ruid, function() {
|
||||
me.exec( 'init', me.options );
|
||||
me.exec( 'loadFromBlob', blob );
|
||||
});
|
||||
},
|
||||
|
||||
resize: function() {
|
||||
var args = Base.slice( arguments );
|
||||
return this.exec.apply( this, [ 'resize' ].concat( args ) );
|
||||
},
|
||||
|
||||
crop: function() {
|
||||
var args = Base.slice( arguments );
|
||||
return this.exec.apply( this, [ 'crop' ].concat( args ) );
|
||||
},
|
||||
|
||||
getAsDataUrl: function( type ) {
|
||||
return this.exec( 'getAsDataUrl', type );
|
||||
},
|
||||
|
||||
getAsBlob: function( type ) {
|
||||
var blob = this.exec( 'getAsBlob', type );
|
||||
|
||||
return new Blob( this.getRuid(), blob );
|
||||
}
|
||||
});
|
||||
|
||||
return Image;
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @fileOverview Md5
|
||||
*/
|
||||
define([
|
||||
'../runtime/client',
|
||||
'../mediator'
|
||||
], function( RuntimeClient, Mediator ) {
|
||||
|
||||
function Md5() {
|
||||
RuntimeClient.call( this, 'Md5' );
|
||||
}
|
||||
|
||||
// 让 Md5 具备事件功能。
|
||||
Mediator.installTo( Md5.prototype );
|
||||
|
||||
Md5.prototype.loadFromBlob = function( blob ) {
|
||||
var me = this;
|
||||
|
||||
if ( me.getRuid() ) {
|
||||
me.disconnectRuntime();
|
||||
}
|
||||
|
||||
// 连接到blob归属的同一个runtime.
|
||||
me.connectRuntime( blob.ruid, function() {
|
||||
me.exec('init');
|
||||
me.exec( 'loadFromBlob', blob );
|
||||
});
|
||||
};
|
||||
|
||||
Md5.prototype.getResult = function() {
|
||||
return this.exec('getResult');
|
||||
};
|
||||
|
||||
return Md5;
|
||||
});
|
||||
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* @fileOverview Transport
|
||||
*/
|
||||
define([
|
||||
'../base',
|
||||
'../runtime/client',
|
||||
'../mediator'
|
||||
], function( Base, RuntimeClient, Mediator ) {
|
||||
|
||||
var $ = Base.$;
|
||||
|
||||
function Transport( opts ) {
|
||||
var me = this;
|
||||
|
||||
opts = me.options = $.extend( true, {}, Transport.options, opts || {} );
|
||||
RuntimeClient.call( this, 'Transport' );
|
||||
|
||||
this._blob = null;
|
||||
this._formData = opts.formData || {};
|
||||
this._headers = opts.headers || {};
|
||||
|
||||
this.on( 'progress', this._timeout );
|
||||
this.on( 'load error', function() {
|
||||
me.trigger( 'progress', 1 );
|
||||
clearTimeout( me._timer );
|
||||
});
|
||||
}
|
||||
|
||||
Transport.options = {
|
||||
server: '',
|
||||
method: 'POST',
|
||||
|
||||
// 跨域时,是否允许携带cookie, 只有html5 runtime才有效
|
||||
withCredentials: false,
|
||||
fileVal: 'file',
|
||||
timeout: 2 * 60 * 1000, // 2分钟
|
||||
formData: {},
|
||||
headers: {},
|
||||
sendAsBinary: false
|
||||
};
|
||||
|
||||
$.extend( Transport.prototype, {
|
||||
|
||||
// 添加Blob, 只能添加一次,最后一次有效。
|
||||
appendBlob: function( key, blob, filename ) {
|
||||
var me = this,
|
||||
opts = me.options;
|
||||
|
||||
if ( me.getRuid() ) {
|
||||
me.disconnectRuntime();
|
||||
}
|
||||
|
||||
// 连接到blob归属的同一个runtime.
|
||||
me.connectRuntime( blob.ruid, function() {
|
||||
me.exec('init');
|
||||
});
|
||||
|
||||
me._blob = blob;
|
||||
opts.fileVal = key || opts.fileVal;
|
||||
opts.filename = filename || opts.filename;
|
||||
},
|
||||
|
||||
// 添加其他字段
|
||||
append: function( key, value ) {
|
||||
if ( typeof key === 'object' ) {
|
||||
$.extend( this._formData, key );
|
||||
} else {
|
||||
this._formData[ key ] = value;
|
||||
}
|
||||
},
|
||||
|
||||
setRequestHeader: function( key, value ) {
|
||||
if ( typeof key === 'object' ) {
|
||||
$.extend( this._headers, key );
|
||||
} else {
|
||||
this._headers[ key ] = value;
|
||||
}
|
||||
},
|
||||
|
||||
send: function( method ) {
|
||||
this.exec( 'send', method );
|
||||
this._timeout();
|
||||
},
|
||||
|
||||
abort: function() {
|
||||
clearTimeout( this._timer );
|
||||
return this.exec('abort');
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
this.trigger('destroy');
|
||||
this.off();
|
||||
this.exec('destroy');
|
||||
this.disconnectRuntime();
|
||||
},
|
||||
|
||||
getResponse: function() {
|
||||
return this.exec('getResponse');
|
||||
},
|
||||
|
||||
getResponseAsJson: function() {
|
||||
return this.exec('getResponseAsJson');
|
||||
},
|
||||
|
||||
getStatus: function() {
|
||||
return this.exec('getStatus');
|
||||
},
|
||||
|
||||
_timeout: function() {
|
||||
var me = this,
|
||||
duration = me.options.timeout;
|
||||
|
||||
if ( !duration ) {
|
||||
return;
|
||||
}
|
||||
|
||||
clearTimeout( me._timer );
|
||||
me._timer = setTimeout(function() {
|
||||
me.abort();
|
||||
me.trigger( 'error', 'timeout' );
|
||||
}, duration );
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// 让Transport具备事件功能。
|
||||
Mediator.installTo( Transport.prototype );
|
||||
|
||||
return Transport;
|
||||
});
|
||||
Reference in New Issue
Block a user