(function() {
/**
* update:
* 1.0: show(pos), 去loading...动画
* 1.1: 去掉loading动画背景,否则在等待js加载时点不到东东
* 1.2: window.App = new Object() script.async = true; loadJs+loading
* 1.3: showLoading可控制在加载js时是否显示loading动画
* 1.4: 去掉 keepFlow,后台已经作了保持会话的功能
* 1.41: cild_direct由 upingan改为 upingannew 临时不用cdn [注释1.41]
* 1.49: 屏蔽对移动设备的判断条件if(!IS_NATIVE)
*/
var version = "Upa1.50";
//
window.App = new Object();
var showLoading = false;
var cild_direct = "upingannew/";
//这个也用于搜索替换
var ua = navigator.userAgent.toUpperCase();
var IS_ANDROID = ua.indexOf('ANDROID') != -1;
// 当前环境是否为IOS平台
var IS_IOS = ua.indexOf('IPHONE OS') != -1;
var IS_NATIVE = (IS_ANDROID || IS_IOS) ? true : false;
// alert('IS_IOS: '+IS_IOS)
if(IS_IOS && ua.indexOf('SAFARI') != -1) {
IS_NATIVE = false;
IS_IOS = false;
}
if(IS_ANDROID && !window.android) {
IS_NATIVE = false;
IS_ANDROID = false;
}
/**
* 获取URL参数
* @return {String} name
*/
var getmtParam = function(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg);
if(r) {
return decodeURI(r[2]);
}
return null;
}
var mtype = null;
var isInitPage = true;
/**
* Cookie操作
*/
var Cookie = function(key, value, options) {
if(arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
options = options || {};
if(value === null || value === undefined) {
options.expires = -1;
}
if( typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
value = String(value);
return (document.cookie = [encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value), options.expires ? '; expires=' + options.expires.toUTCString() : '', options.path ? '; path=' + options.path : '', options.domain ? '; domain=' + options.domain : '', options.secure ? '; secure' : ''].join(''));
}
options = value || {};
var decode = options.raw ? function(s) {
return s;
} : decodeURIComponent;
var pairs = document.cookie.split('; ');
for(var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
if(decode(pair[0]) === key)
return decode(pair[1] || '');
}
return null;
}
//--- 外部app修复 IS_NATIVE的标识
//--- 外部app修复 IS_NATIVE的标识
if(IS_ANDROID || IS_IOS) {
mtype = getmtParam('mt') || Cookie('mt') || null;
if(mtype == null) {//来自与平安应用,无参数
IS_NATIVE = true;
if(IS_ANDROID) {
IS_ANDROID = true;
IS_IOS = false;
}
if(IS_IOS) {
IS_ANDROID = false;
IS_IOS = true;
}
} else {//来自与平安应用
IS_NATIVE = false;
IS_IOS = false;
}
}
/**
* 页面初始化, 动态加载
*/
var location = window.location;
// 协议
var protocol = location.protocol;
// 主机名
var host = location.host;
//stz+20130718 协议和域名
function getHostWithProtocol() {
var origin = location.origin, host = location.host;
if(!origin) {
var protocol = location.protocol;
protocol = protocol == "http:" ? "http://" : protocol == "https:" ? "https://" : "file:///";
origin = protocol + (host || location.hostname);
}
return origin;
}
// 加载一个脚本文件
function _loadJsFile(url, callback) {
var script = document.createElement("script");
if(script.readyState) {
script.onreadystatechange = function() {
if(script.readyState == "loaded" || script.readyState == "complete") {
callback.call();
}
}
} else {
script.onload = callback;
}
script.type = "text/javascript";
script.async = true;
//+20130815
script.src = url;
//url.indexOf('?') != -1 ? url + '&v=' + version : url + '?v=' + version;
document.getElementsByTagName("head")[0].appendChild(script);
}
// 加载脚本文件列表
function _loadJsList(urls, statechange, _index) {
if(showLoading && AheadHandle.loadingBee && AheadHandle.loadingBee.canvas) {
AheadHandle.loadingBee.show();
showLoading = false; //130826 show一次就够了
}
var index = _index || 0;
if(urls[index]) {
_loadJsFile(urls[index], function() {
_loadJsList(urls, statechange, index + 1);
});
}
if(statechange) {
statechange(index);
}
}
var allHost = getHostWithProtocol();
// 根据域名解析文件url
function _parse(urls, type) {
var _urls = [], url = "";
if( typeof urls == "string") {
urls = [urls];
}
for(var i = 0, len = urls.length; i < len; i++) {
url = urls[i];
if(!url) {
//
} else if(/^(http|https)/.test(url)) {// 完整的URL
_urls.push(url);
} else if(/^\//.test(url)) {// 以根目录为路径
_urls.push(protocol + "//" + host + url);
} else {
_urls.push(url);
}
}
return _urls;
}
// 加载一个css文件
function loadCSS(urls) {
var html = [];
urls = _parse(urls, "css");
for(var i = 0, len = urls.length; i < len; i++) {
html.push("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + urls[i] + "\" />");
}
document.write(html.join(""));
}
// 加载脚本文件
function loadJs(urls, callback, dontevent, showLoad) {
showLoading = showLoad;
urls = _parse(urls, "js");
if(!dontevent) {
var _callback = callback, len = urls.length;
callback = function(index) {
if(_callback) {
_callback(index);
}
if(index == len) {
if(AheadHandle.loadingBee && AheadHandle.loadingBee.canvas) {
AheadHandle.loadingBee.hide();
}
//App.onload(); //stz
//App.alert('body:'+ document.getElementsByTagName('body') +' , links: '+ document.getElementsByTagName('link').length);
// keepFlow();
}
}
}
_loadJsList(urls, callback);
}
window.loadCSS = loadCSS;
window.loadJs = loadJs;
window.getDiffFrameUrl = function(prefix) {
var b = navigator.userAgent.toLowerCase();
browser = {
safari : /webkit/.test(b),
opera : /opera/.test(b),
msie : /msie/.test(b) && !/opera/.test(b),
mozilla : /mozilla/.test(b) && !/compatible/.test(b),
winphone : window.navigator.msPointerEnabled
};
prefix = !prefix ? '' : prefix;
var url = prefix+'../../mobile/common_js/zepto.min.1.0rc1.js';
//js/third-party/zepto.min.1.0rc1.js
//http://a.tbcdn.cn/mw/base/libs/zepto/1.0.0/zepto.js
if(browser.msie || browser.winphone) {
url = prefix+'http://script2.pingan.com/app_js/toubao/v20/js/jquery-1.4.4.js';
}
return url;
}
/**
* ============================================= 蜜蜂类
*/
function loadingBee(options) {
var options = options || {};
this.path = options.path || '../../common_images/';
this.images = options.images || ['loading.gif'];
this.width = options.width || 230;
this.height = options.height || 230;
this.FPS = options.width || 5;
this._curFrame = 0;
this._playTime = 0;
this.canvas = null;
this.loadingBg = null;
this._init();
}
loadingBee.prototype = {
_init : function() {
var loadedNum = 0, arrowImage = [];
var self = this;
for(var i = 0, len = this.images.length; i < len; i++) {
arrowImage[i] = new Image();
//打算用背景方式,无需关心图片是否加载成功了,因为不会出现异常,最多是某帧不出现而已
arrowImage[i].src = this._getCDNPicURL(this.path + this.images[i]);
if(i == len - 1) {
self.images = arrowImage.slice(0);
self._creatMovie();
}
}
},
/**
* 获取cdn图片地址
* @param {String} fileName 图片在本地相对于根目录的位置,如 images/themes/default/images/member/drawUpIcon.png
* @return {String} 真实地址
*/
_getCDNPicURL : function(fileName) {
var oldfileName = fileName;
var origin = location.origin, host = location.host;
if(!origin) {//android 不识别 location.origin
var protocol = location.protocol;
protocol = protocol == "http:" ? "http://" : protocol == "https:" ? "https://" : "file:///";
origin = protocol + (host || location.hostname);
}
return fileName;
},
/**
* 通过毕包实现的事件代理
* @param {Function} func
* @param {Object} scope
*/
// _delegate : function(func, scope) {
// scope = scope || window;
// if(arguments.length > 2) {
// var args = Array.prototype.slice.call(arguments, 2);
// return function() {
// return func.apply(scope, args);
// }
// } else {
// return function() {
// return func.call(scope);
// }
// }
// },
_creatMovie : function() {
this.movieCreated = true;
var screenW = 640, screenH = document.documentElement.clientHeight;
//stz mf 201300730: ie10设置固定宽度才行!
//var screenW = document.body.clientWidth, screenH = document.body.clientHeight; //stz mf 201300730
var canvas = document.createElement('div');
var canvasCont = document.createElement('div');
canvasCont.className = 'comBeeLoading';
// canvasCont.innerText = 'loading';
// this.canvasCont = canvasCont;
canvasCont.innerHTML = '<div class="comTable"><span class="comTableCell loadingWord"> </span><span class="comTableCell loadingDot"><span></span></span></div>'
canvas.appendChild(canvasCont);
this.loadingDot = canvasCont.children[0].children[1].children[0];
document.body.appendChild(canvas);
canvas.callTimes = 0;
this.canvas = canvas;
// this.loadingBg = document.getElementById('loadingBeeBg');
canvas.style.cssText = 'width:' + this.width + 'px; height:' + this.height + 'px;position:absolute;display:none;z-index:1002;left:' + (screenW - this.width) / 2 + 'px; top:' + (screenH - this.height) / 2 + 'px;';
loadingBee.instance = this;
},
show : function(pos) {//pos + 2013-08-10 {top:200px, left:100px}默认居中
var canvas = this.canvas, ctx = canvas.style;
canvas.callTimes++;
if(!loadingBee.movIntval) {
canvas.style.display = 'block';
// this.loadingBg.style.display = 'block';
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
//stz:+201300730||document.documentElement.scrollTop兼容ie10.因html5是属于非怪异(非quirk)模式,这时documentElement有值,body没有。而在怪异模式正好相反
// if(scrollTop>0){
// ctx.top = scrollTop+(document.documentElement.clientHeight-this.height)/2+20+'px';
// }else{ //+ 2013521: 蜜蜂已经下去又拖到顶部时 重新把蜜蜂提上来点
// ctx.top = (document.documentElement.clientHeight-this.height)/2+20+'px';
// }
if(pos && pos.top) {
ctx.top = pos.top + 'px';
} else {
ctx.top = scrollTop + (document.documentElement.clientHeight - this.height) / 2 + 30 + 'px';
}
ctx.background = "url(" + this.images[0].src + ") no-repeat center center";
// var self = this, frameInval = parseInt(1000/this.FPS), frameLen = 3;
// var teststz = document.getElementById('teststz');
// loadingBee.movIntval = setInterval(function(){
// ctx.top = scrollTop+(document.documentElement.clientHeight-self.height)/2+30+'px';
// // console.log(self._curFrame)
// if(self._curFrame==0){
// self.loadingDot.innerText = ''; //canvasCont
// }else{
// self.loadingDot.innerText = self.loadingDot.innerText+'.';
// }
// self._curFrame++;
// self._curFrame%=frameLen+1;
// self._playTime+=frameInval;
// }, frameInval);
loadingBee.movIntval = true;
}
},
hide : function() {//return;
var canvas = this.canvas;
canvas.callTimes--;
if(canvas.callTimes > 0) {
return;
} else {
canvas.callTimes = 0;
}
canvas.style.display = 'none';
// this.loadingBg.style.display = 'none';
if(loadingBee.movIntval) {
clearInterval(loadingBee.movIntval);
loadingBee.movIntval = null;
this._curFrame = 0;
this._playTime = 0;
}
},
hideAll : function() {//return;
var canvas = this.canvas;
canvas.callTimes = 0;
canvas.style.display = 'none';
// this.loadingBg.style.display = 'none';
if(loadingBee.movIntval) {
clearInterval(loadingBee.movIntval);
loadingBee.movIntval = null;
this._curFrame = 0;
this._playTime = 0;
}
}
};
//if(!IS_NATIVE) { //屏蔽对移动设备的判断条件
window.AheadHandle = {};
window.bodyLoadStart_inv = setInterval(function() {
if(document.body) {//||!document.body document.getElementsByTagName('header')
clearInterval(window.bodyLoadStart_inv);
window.bodyLoadStart_inv = null;
// var canvasbg = document.createElement('div');
// canvasbg.id = 'loadingBeeBg';
// canvasbg.className = 'com_shadowbg';
// // canvasbg.style.display = 'block';
// document.body.insertBefore(canvasbg,null); //prepend
// // if(!IS_NATIVE) {
// // AheadHandle.loadingBee.show();
// // }
// // AheadHandle.loadingBee.show();
AheadHandle.loadingBee = loadingBee.instance ? loadingBee.instance : new loadingBee();
// window.loadingCreat_inv = setInterval(function(){
// if( AheadHandle.loadingBee.canvas ){
// clearInterval(window.loadingCreat_inv);
// window.loadingCreat_inv = null;
// AheadHandle.loadingBee.show();
// }
// },50);
}
}, 0);
window.getmtParam = getmtParam();
//}
// function bodyLoadStart(){
// if(!AheadHandle.loadingBee.canvas){ //||!document.body
// setTimeout(bodyLoadStart,50);
// } else{
// AheadHandle.loadingBee.show();
// }
// }
// bodyLoadStart();
})();