Initial commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @fileOverview Blob Html实现
|
||||
*/
|
||||
define([
|
||||
'./runtime',
|
||||
'../../lib/blob'
|
||||
], function( FlashRuntime, Blob ) {
|
||||
|
||||
return FlashRuntime.register( 'Blob', {
|
||||
slice: function( start, end ) {
|
||||
var blob = this.flashExec( 'Blob', 'slice', start, end );
|
||||
|
||||
return new Blob( this.getRuid(), blob );
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @fileOverview FilePicker
|
||||
*/
|
||||
define([
|
||||
'../../base',
|
||||
'./runtime'
|
||||
], function( Base, FlashRuntime ) {
|
||||
var $ = Base.$;
|
||||
|
||||
return FlashRuntime.register( 'FilePicker', {
|
||||
init: function( opts ) {
|
||||
var copy = $.extend({}, opts ),
|
||||
len, i;
|
||||
|
||||
// 修复Flash再没有设置title的情况下无法弹出flash文件选择框的bug.
|
||||
len = copy.accept && copy.accept.length;
|
||||
for ( i = 0; i < len; i++ ) {
|
||||
if ( !copy.accept[ i ].title ) {
|
||||
copy.accept[ i ].title = 'Files';
|
||||
}
|
||||
}
|
||||
|
||||
delete copy.button;
|
||||
delete copy.id;
|
||||
delete copy.container;
|
||||
|
||||
this.flashExec( 'FilePicker', 'init', copy );
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
this.flashExec( 'FilePicker', 'destroy' );
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @fileOverview 图片压缩
|
||||
*/
|
||||
define([
|
||||
'./runtime'
|
||||
], function( FlashRuntime ) {
|
||||
|
||||
return FlashRuntime.register( 'Image', {
|
||||
// init: function( options ) {
|
||||
// var owner = this.owner;
|
||||
|
||||
// this.flashExec( 'Image', 'init', options );
|
||||
// owner.on( 'load', function() {
|
||||
// debugger;
|
||||
// });
|
||||
// },
|
||||
|
||||
loadFromBlob: function( blob ) {
|
||||
var owner = this.owner;
|
||||
|
||||
owner.info() && this.flashExec( 'Image', 'info', owner.info() );
|
||||
owner.meta() && this.flashExec( 'Image', 'meta', owner.meta() );
|
||||
|
||||
this.flashExec( 'Image', 'loadFromBlob', blob.uid );
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @fileOverview Md5 flash实现
|
||||
*/
|
||||
define([
|
||||
'./runtime'
|
||||
], function( FlashRuntime ) {
|
||||
|
||||
return FlashRuntime.register( 'Md5', {
|
||||
init: function() {
|
||||
// do nothing.
|
||||
},
|
||||
|
||||
loadFromBlob: function( blob ) {
|
||||
return this.flashExec( 'Md5', 'loadFromBlob', blob.uid );
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,183 @@
|
||||
/**
|
||||
* @fileOverview FlashRuntime
|
||||
*/
|
||||
define([
|
||||
'../../base',
|
||||
'../runtime',
|
||||
'../compbase'
|
||||
], function( Base, Runtime, CompBase ) {
|
||||
|
||||
var $ = Base.$,
|
||||
type = 'flash',
|
||||
components = {};
|
||||
|
||||
|
||||
function getFlashVersion() {
|
||||
var version;
|
||||
|
||||
try {
|
||||
version = navigator.plugins[ 'Shockwave Flash' ];
|
||||
version = version.description;
|
||||
} catch ( ex ) {
|
||||
try {
|
||||
version = new ActiveXObject('ShockwaveFlash.ShockwaveFlash')
|
||||
.GetVariable('$version');
|
||||
} catch ( ex2 ) {
|
||||
version = '0.0';
|
||||
}
|
||||
}
|
||||
version = version.match( /\d+/g );
|
||||
return parseFloat( version[ 0 ] + '.' + version[ 1 ], 10 );
|
||||
}
|
||||
|
||||
function FlashRuntime() {
|
||||
var pool = {},
|
||||
clients = {},
|
||||
destroy = this.destroy,
|
||||
me = this,
|
||||
jsreciver = Base.guid('webuploader_');
|
||||
|
||||
Runtime.apply( me, arguments );
|
||||
me.type = type;
|
||||
|
||||
|
||||
// 这个方法的调用者,实际上是RuntimeClient
|
||||
me.exec = function( comp, fn/*, args...*/ ) {
|
||||
var client = this,
|
||||
uid = client.uid,
|
||||
args = Base.slice( arguments, 2 ),
|
||||
instance;
|
||||
|
||||
clients[ uid ] = client;
|
||||
|
||||
if ( components[ comp ] ) {
|
||||
if ( !pool[ uid ] ) {
|
||||
pool[ uid ] = new components[ comp ]( client, me );
|
||||
}
|
||||
|
||||
instance = pool[ uid ];
|
||||
|
||||
if ( instance[ fn ] ) {
|
||||
return instance[ fn ].apply( instance, args );
|
||||
}
|
||||
}
|
||||
|
||||
return me.flashExec.apply( client, arguments );
|
||||
};
|
||||
|
||||
function handler( evt, obj ) {
|
||||
var type = evt.type || evt,
|
||||
parts, uid;
|
||||
|
||||
parts = type.split('::');
|
||||
uid = parts[ 0 ];
|
||||
type = parts[ 1 ];
|
||||
|
||||
// console.log.apply( console, arguments );
|
||||
|
||||
if ( type === 'Ready' && uid === me.uid ) {
|
||||
me.trigger('ready');
|
||||
} else if ( clients[ uid ] ) {
|
||||
clients[ uid ].trigger( type.toLowerCase(), evt, obj );
|
||||
}
|
||||
|
||||
// Base.log( evt, obj );
|
||||
}
|
||||
|
||||
// flash的接受器。
|
||||
window[ jsreciver ] = function() {
|
||||
var args = arguments;
|
||||
|
||||
// 为了能捕获得到。
|
||||
setTimeout(function() {
|
||||
handler.apply( null, args );
|
||||
}, 1 );
|
||||
};
|
||||
|
||||
this.jsreciver = jsreciver;
|
||||
|
||||
this.destroy = function() {
|
||||
// @todo 删除池子中的所有实例
|
||||
return destroy && destroy.apply( this, arguments );
|
||||
};
|
||||
|
||||
this.flashExec = function( comp, fn ) {
|
||||
var flash = me.getFlash(),
|
||||
args = Base.slice( arguments, 2 );
|
||||
|
||||
return flash.exec( this.uid, comp, fn, args );
|
||||
};
|
||||
|
||||
// @todo
|
||||
}
|
||||
|
||||
Base.inherits( Runtime, {
|
||||
constructor: FlashRuntime,
|
||||
|
||||
init: function() {
|
||||
var container = this.getContainer(),
|
||||
opts = this.options,
|
||||
html;
|
||||
|
||||
// if not the minimal height, shims are not initialized
|
||||
// in older browsers (e.g FF3.6, IE6,7,8, Safari 4.0,5.0, etc)
|
||||
container.css({
|
||||
position: 'absolute',
|
||||
top: '-8px',
|
||||
left: '-8px',
|
||||
width: '9px',
|
||||
height: '9px',
|
||||
overflow: 'hidden'
|
||||
});
|
||||
|
||||
// insert flash object
|
||||
html = '<object id="' + this.uid + '" type="application/' +
|
||||
'x-shockwave-flash" data="' + opts.swf + '" ';
|
||||
|
||||
if ( Base.browser.ie ) {
|
||||
html += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
|
||||
}
|
||||
|
||||
html += 'width="100%" height="100%" style="outline:0">' +
|
||||
'<param name="movie" value="' + opts.swf + '" />' +
|
||||
'<param name="flashvars" value="uid=' + this.uid +
|
||||
'&jsreciver=' + this.jsreciver + '" />' +
|
||||
'<param name="wmode" value="transparent" />' +
|
||||
'<param name="allowscriptaccess" value="always" />' +
|
||||
'</object>';
|
||||
|
||||
container.html( html );
|
||||
},
|
||||
|
||||
getFlash: function() {
|
||||
if ( this._flash ) {
|
||||
return this._flash;
|
||||
}
|
||||
|
||||
this._flash = $( '#' + this.uid ).get( 0 );
|
||||
return this._flash;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
FlashRuntime.register = function( name, component ) {
|
||||
component = components[ name ] = Base.inherits( CompBase, $.extend({
|
||||
|
||||
// @todo fix this later
|
||||
flashExec: function() {
|
||||
var owner = this.owner,
|
||||
runtime = this.getRuntime();
|
||||
|
||||
return runtime.flashExec.apply( owner, arguments );
|
||||
}
|
||||
}, component ) );
|
||||
|
||||
return component;
|
||||
};
|
||||
|
||||
if ( getFlashVersion() >= 11.4 ) {
|
||||
Runtime.addRuntime( type, FlashRuntime );
|
||||
}
|
||||
|
||||
return FlashRuntime;
|
||||
});
|
||||
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* @fileOverview Transport flash实现
|
||||
*/
|
||||
define([
|
||||
'../../base',
|
||||
'./runtime',
|
||||
'../client'
|
||||
], function( Base, FlashRuntime, RuntimeClient ) {
|
||||
var $ = Base.$;
|
||||
|
||||
return FlashRuntime.register( 'Transport', {
|
||||
init: function() {
|
||||
this._status = 0;
|
||||
this._response = null;
|
||||
this._responseJson = null;
|
||||
},
|
||||
|
||||
send: function() {
|
||||
var owner = this.owner,
|
||||
opts = this.options,
|
||||
xhr = this._initAjax(),
|
||||
blob = owner._blob,
|
||||
server = opts.server,
|
||||
binary;
|
||||
|
||||
xhr.connectRuntime( blob.ruid );
|
||||
|
||||
if ( opts.sendAsBinary ) {
|
||||
server += (/\?/.test( server ) ? '&' : '?') +
|
||||
$.param( owner._formData );
|
||||
|
||||
binary = blob.uid;
|
||||
} else {
|
||||
$.each( owner._formData, function( k, v ) {
|
||||
xhr.exec( 'append', k, v );
|
||||
});
|
||||
|
||||
xhr.exec( 'appendBlob', opts.fileVal, blob.uid,
|
||||
opts.filename || owner._formData.name || '' );
|
||||
}
|
||||
|
||||
this._setRequestHeader( xhr, opts.headers );
|
||||
xhr.exec( 'send', {
|
||||
method: opts.method,
|
||||
url: server,
|
||||
forceURLStream: opts.forceURLStream,
|
||||
mimeType: 'application/octet-stream'
|
||||
}, binary );
|
||||
},
|
||||
|
||||
getStatus: function() {
|
||||
return this._status;
|
||||
},
|
||||
|
||||
getResponse: function() {
|
||||
return this._response || '';
|
||||
},
|
||||
|
||||
getResponseAsJson: function() {
|
||||
return this._responseJson;
|
||||
},
|
||||
|
||||
abort: function() {
|
||||
var xhr = this._xhr;
|
||||
|
||||
if ( xhr ) {
|
||||
xhr.exec('abort');
|
||||
xhr.destroy();
|
||||
this._xhr = xhr = null;
|
||||
}
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
this.abort();
|
||||
},
|
||||
|
||||
_initAjax: function() {
|
||||
var me = this,
|
||||
xhr = new RuntimeClient('XMLHttpRequest');
|
||||
|
||||
xhr.on( 'uploadprogress progress', function( e ) {
|
||||
var percent = e.loaded / e.total;
|
||||
percent = Math.min( 1, Math.max( 0, percent ) );
|
||||
return me.trigger( 'progress', percent );
|
||||
});
|
||||
|
||||
xhr.on( 'load', function() {
|
||||
var status = xhr.exec('getStatus'),
|
||||
readBody = false,
|
||||
err = '',
|
||||
p;
|
||||
|
||||
xhr.off();
|
||||
me._xhr = null;
|
||||
|
||||
if ( status >= 200 && status < 300 ) {
|
||||
readBody = true;
|
||||
} else if ( status >= 500 && status < 600 ) {
|
||||
readBody = true;
|
||||
err = 'server-'+status;
|
||||
} else {
|
||||
err = 'http-'+status;
|
||||
}
|
||||
|
||||
if ( readBody ) {
|
||||
me._response = xhr.exec('getResponse');
|
||||
me._response = decodeURIComponent( me._response );
|
||||
|
||||
// flash 处理可能存在 bug, 没辙只能靠 js 了
|
||||
// try {
|
||||
// me._responseJson = xhr.exec('getResponseAsJson');
|
||||
// } catch ( error ) {
|
||||
|
||||
p = function( s ) {
|
||||
try {
|
||||
if (window.JSON && window.JSON.parse) {
|
||||
return JSON.parse(s);
|
||||
}
|
||||
|
||||
return new Function('return ' + s).call();
|
||||
} catch ( err ) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
me._responseJson = me._response ? p(me._response) : {};
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
xhr.destroy();
|
||||
xhr = null;
|
||||
|
||||
return err ? me.trigger( 'error', err ) : me.trigger('load');
|
||||
});
|
||||
|
||||
xhr.on( 'error', function() {
|
||||
var status = xhr.exec('getStatus'),err = status?'http-'+status:'http';
|
||||
xhr.off();
|
||||
me._xhr = null;
|
||||
me.trigger( 'error', err );
|
||||
});
|
||||
|
||||
me._xhr = xhr;
|
||||
return xhr;
|
||||
},
|
||||
|
||||
_setRequestHeader: function( xhr, headers ) {
|
||||
$.each( headers, function( key, val ) {
|
||||
xhr.exec( 'setRequestHeader', key, val );
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user