var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
* @module annie
*/
var annie;
(function (annie) {
/**
* <h4><font color="red">小游戏不支持 小程序不支持</font></h4>
* 资源加载类,后台请求,加载资源和后台交互都可以使用此类
* @class annie.URLLoader
* @extends annie.EventDispatcher
* @public
* @since 1.0.0
* @example
* var urlLoader = new annie.URLLoader();
* urlLoader.addEventListener('onComplete', function (e) {
* //console.log(e.data.response);
* var bitmapData = e.data.response,//bitmap图片数据
* bitmap = new annie.Bitmap(bitmapData);//实例化bitmap对象
* //居中对齐
* bitmap.x = (s.stage.desWidth - bitmap.width) / 2;
* bitmap.y = (s.stage.desHeight - bitmap.height) / 2;
* s.addChild(bitmap);
* });
* urlLoader.load('http://test.annie2x.com/biglong/logo.jpg');//载入外部图片
*/
var URLLoader = (function (_super) {
__extends(URLLoader, _super);
//Event
/**
* 完成事件
* @event annie.Event.COMPLETE
* @since 1.0.0
*/
/**
* annie.URLLoader加载过程事件
* @event annie.Event.PROGRESS
* @since 1.0.0
*/
/**
* annie.URLLoader出错事件
* @event annie.Event.ERROR
* @since 1.0.0
*/
/**
* annie.URLLoader中断事件
* @event annie.Event.ABORT
* @since 1.0.0
*/
/**
* annie.URLLoader开始事件
* @event annie.Event.START
* @since 1.0.0
*/
/**
* 构造函数
* @method URLLoader
* @param type text json js xml image sound css svg video unKnow
*/
function URLLoader() {
_super.call(this);
this._req = null;
this.headers = [];
/**
* 后台返回来的数据类型
* @property responseType
* @type {string}
* @default null
* @public
* @since 1.0.0
*/
this.responseType = "";
/**
* 请求的url地址
* @property url
* @public
* @since 1.0.0
* @type {string}
*/
this.url = "";
/**
* 请求后台的类型 get post
* @property method
* @type {string}
* @default get
* @public
* @since 1.0.0
*/
this.method = "get";
/**
* 需要向后台传送的数据对象
* @property data
* @public
* @since 1.0.0
* @default null
* @type {Object}
*/
this.data = null;
//格式化post请求参数
this._fqs = function (data, query) {
var params = [];
if (data instanceof Object) {
for (var n in data) {
params.push(encodeURIComponent(n) + "=" + encodeURIComponent(data[n]));
}
}
if (query) {
params = params.concat(query);
}
return params.join("&");
};
//formatURIString
//格式化get 请求参数
this._fus = function (src, data) {
var s = this;
if (data == void 0) {
return src;
}
var query = [];
var idx = src.indexOf("?");
if (idx != -1) {
var q = src.slice(idx + 1);
query = query.concat(q.split("&"));
return src.slice(0, idx) + "?" + s._fqs(data, query);
}
else {
return src + "?" + s._fqs(data, query);
}
};
this._instanceType = "annie.URLLoader";
}
/**
* 取消加载
* @method loadCancel
* @public
* @since 1.0.0
*/
URLLoader.prototype.loadCancel = function () {
var s = this;
if (s._req) {
s._req.abort();
}
};
/**
* 加载或请求数据
* @method load
* @public
* @since 1.0.0
* @param {string} url
* @param {string} contentType 如果请求类型需要设置主体类型,有form json binary jsonp等,请设置 默认为form
*/
URLLoader.prototype.load = function (url, contentType) {
if (contentType === void 0) { contentType = "form"; }
var s = this;
s.loadCancel();
if (s.responseType == "") {
//看看是什么后缀
var urlSplit = url.split(".");
var extStr = urlSplit[urlSplit.length - 1];
var ext = extStr.split("?")[0].toLocaleLowerCase();
if ("." + ext == annie.suffixName) {
if (annie._isReleased) {
s.responseType = "swf";
}
else {
s.responseType = "js";
}
}
else if (ext == "mp3" || ext == "ogg" || ext == "wav") {
s.responseType = "sound";
}
else if (ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "gif") {
s.responseType = "image";
}
else if (ext == "css") {
s.responseType = "css";
}
else if (ext == "mp4") {
s.responseType = "video";
}
else if (ext == "svg") {
s.responseType = "svg";
}
else if (ext == "xml") {
s.responseType = "xml";
}
else if (ext == "json") {
s.responseType = "json";
}
else if (ext == "txt") {
s.responseType = "text";
}
else if (ext == "js") {
s.responseType = "js";
}
else {
s.responseType = "unKnow";
}
}
var reSendTimes = 0;
if (!(s._req instanceof XMLHttpRequest)) {
s._req = new XMLHttpRequest();
s._req.withCredentials = false;
s._req.onprogress = function (event) {
s.dispatchEvent("onProgress", { loadedBytes: event.loaded, totalBytes: event.total });
};
s._req.onerror = function (event) {
reSendTimes++;
if (reSendTimes > 2) {
s.dispatchEvent("onError", { id: 2, msg: event["message"] });
}
else {
//断线重连
s._req.abort();
if (s.data instanceof Object) {
s._req.send(s.data);
}
else {
s._req.send();
}
}
};
s._req.onload = function () {
//是否异步
var e = new annie.Event("onComplete");
var result = s._req.response;
e.data = { type: s.responseType, response: null };
var item;
switch (s.responseType) {
case "css":
item = document.createElement("link");
item.rel = "stylesheet";
item.href = s.url;
break;
case "video":
item = s.url;
break;
case "json":
item = JSON.parse(result);
break;
case "js":
item = "JS_CODE";
annie.Eval(result);
break;
default:
item = result;
break;
}
e.data["response"] = item;
s.data = null;
s.responseType = "";
s.dispatchEvent(e);
};
}
if (s.data instanceof Object && s.method.toLocaleLowerCase() == "get") {
s.url = s._fus(url, s.data);
s.data = null;
}
else {
s.url = url;
}
if (s.responseType == "swf" || s.responseType == "image" || s.responseType == "sound") {
s._req.responseType = "blob";
}
else {
s._req.responseType = "";
}
s._req.open(s.method, s.url, true);
if (s.headers.length > 0) {
for (var h = 0; h < s.headers.length; h += 2) {
s._req.setRequestHeader(s.headers[h], s.headers[h + 1]);
}
s.headers.length = 0;
}
if (contentType == "form") {
s._req.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
s._req.send(s._fqs(s.data, null));
}
else {
var type = "application/json";
if (contentType != "json") {
type = "multipart/form-data";
}
s._req.setRequestHeader("Content-type", type + ";charset=UTF-8");
s._req.send(s.data);
}
};
/**
* 添加自定义头
* @method addHeader
* @param name
* @param value
*/
URLLoader.prototype.addHeader = function (name, value) {
this.headers.push(name, value);
};
URLLoader.prototype.destroy = function () {
var s = this;
s.loadCancel();
s.headers = null;
s.data = null;
_super.prototype.destroy.call(this);
};
return URLLoader;
}(annie.EventDispatcher));
annie.URLLoader = URLLoader;
})(annie || (annie = {}));