guy 8 years ago
parent
commit
568a88da08
  1. 581
      bi/core.js
  2. 581
      dist/core.js
  3. 581
      src/core/alias.js

581
bi/core.js

@ -3017,230 +3017,59 @@ if (!window.BI) {
return result; return result;
} }
BI.cjkEncode = function (text) { // 判断是否是无效的日期
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的) function isInvalidDate(date) {
if (typeof text !== 'string') { return date == "Invalid Date" || date == "NaN";
return text;
}
var newText = "";
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i);
if (code >= 128 || code === 91 || code === 93) {//91 is "[", 93 is "]".
newText += "[" + code.toString(16) + "]";
} else {
newText += text.charAt(i);
}
}
return newText
};
BI.cjkEncodeDO = function (o) {
if (BI.isPlainObject(o)) {
var result = {};
$.each(o, function (k, v) {
if (!(typeof v == "string")) {
v = BI.jsonEncode(v);
}
//wei:bug 43338,如果key是中文,cjkencode后o的长度就加了1,ie9以下版本死循环,所以新建对象result。
k = BI.cjkEncode(k);
result[k] = BI.cjkEncode(v);
});
return result;
}
return o;
};
BI.jsonEncode = function (o) {
//james:这个Encode是抄的EXT的
var useHasOwn = {}.hasOwnProperty ? true : false;
// crashes Safari in some instances
//var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
var m = {
"\b": '\\b',
"\t": '\\t',
"\n": '\\n',
"\f": '\\f',
"\r": '\\r',
'"': '\\"',
"\\": '\\\\'
};
var encodeString = function (s) {
if (/["\\\x00-\x1f]/.test(s)) {
return '"' + s.replace(/([\x00-\x1f\\"])/g, function (a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return "\\u00" +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + s + '"';
};
var encodeArray = function (o) {
var a = ["["], b, i, l = o.length, v;
for (i = 0; i < l; i += 1) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(v === null ? "null" : BI.jsonEncode(v));
b = true;
}
} }
a.push("]");
return a.join("");
};
if (typeof o == "undefined" || o === null) { /**
return "null"; * 科学计数格式
} else if (BI.isArray(o)) {
return encodeArray(o);
} else if (o instanceof Date) {
/*
* alex:原来只是把年月日时分秒简单地拼成一个String,无法decode
* 现在这么处理就可以decode了,但是JS.jsonDecode和Java.JSONObject也要跟着改一下
*/ */
return BI.jsonEncode({ function _eFormat(text, fmt) {
__time__: o.getTime() var e = fmt.indexOf("E");
}) var eleft = fmt.substr(0, e), eright = fmt.substr(e + 1);
} else if (typeof o == "string") { if (/^[0\.-]+$/.test(text)) {
return encodeString(o); text = BI._numberFormat(0.0, eleft) + 'E' + BI._numberFormat(0, eright)
} else if (typeof o == "number") {
return isFinite(o) ? String(o) : "null";
} else if (typeof o == "boolean") {
return String(o);
} else if (BI.isFunction(o)) {
return String(o);
} else { } else {
var a = ["{"], b, i, v; var isNegative = text < 0;
for (i in o) { if (isNegative) {
if (!useHasOwn || o.hasOwnProperty(i)) { text = text.substr(1);
v = o[i];
switch (typeof v) {
case "undefined":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(BI.jsonEncode(i), ":",
v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
}
a.push("}");
return a.join("");
}
};
BI.jsonDecode = function (text) {
try {
// 注意0啊
//var jo = $.parseJSON(text) || {};
var jo = $.parseJSON(text);
if (jo == null) {
jo = {};
}
} catch (e) {
/*
* richie:浏览器只支持标准的JSON字符串转换而jQuery会默认调用浏览器的window.JSON.parse()函数进行解析
* 比如var str = "{'a':'b'}",这种形式的字符串转换为JSON就会抛异常
*/
try {
jo = new Function("return " + text)() || {};
} catch (e) {
//do nothing
}
if (jo == null) {
jo = [];
}
}
if (!_hasDateInJson(text)) {
return jo;
}
function _hasDateInJson(json) {
if (!json || typeof json !== "string") {
return false;
}
return json.indexOf("__time__") != -1;
}
return (function (o) {
if (typeof o === "string") {
return o;
} }
if (o && o.__time__ != null) { var elvl = (eleft.split('.')[0] || '').length;
return new Date(o.__time__); var point = text.indexOf(".");
if (point < 0) {
point = text.length;
} }
for (var a in o) { var i = 0; //第一个不为0的数的位置
if (o[a] == o || typeof o[a] == 'object' || $.isFunction(o[a])) { text = text.replace('.', '');
for (var len = text.length; i < len; i++) {
var ech = text.charAt(i);
if (ech <= '9' && ech >= '1') {
break; break;
} }
o[a] = arguments.callee(o[a]);
}
return o;
})(jo);
}
BI.contentFormat = function (cv, fmt) {
if (isEmpty(cv)) {
//原值为空,返回空字符
return '';
} }
var text = cv.toString(); var right = point - i - elvl;
if (isEmpty(fmt)) { var left = text.substr(i, elvl);
//格式为空,返回原字符 var dis = i + elvl - text.length;
return text; if (dis > 0) {
//末位补全0
for (var k = 0; k < dis; k++) {
left += '0';
} }
if (fmt.match(/^T/)) {
//T - 文本格式
return text;
} else if (fmt.match(/^D/)) {
//D - 日期(时间)格式
if (!(cv instanceof Date)) {
if (typeof cv === 'number') {
//毫秒数类型
cv = new Date(cv);
} else { } else {
//字符串类型,如yyyyMMdd、MMddyyyy等这样无分隔符的结构 left += '.' + text.substr(i + elvl);
cv = Date.parseDate(cv + "", Date.patterns.ISO8601Long);
} }
left = left.replace(/^[0]+/, '');
if (right < 0 && eright.indexOf('-') < 0) {
eright += ';-' + eright;
} }
if (!BI.isNull(cv)) { text = BI._numberFormat(left, eleft) + 'E' + BI._numberFormat(right, eright);
var needTrim = fmt.match(/^DT/); if (isNegative) {
text = BI.date2Str(cv, fmt.substring(needTrim ? 2 : 1)); text = '-' + text;
} }
} else if (fmt.match(/E/)) {
//科学计数格式
text = BI._eFormat(text, fmt);
} else {
//数字格式
text = BI._numberFormat(text, fmt);
} }
//¤ - 货币格式
text = text.replace(/¤/g, '¥');
return text; return text;
}; }
/** /**
* 把日期对象按照指定格式转化成字符串 * 把日期对象按照指定格式转化成字符串
@ -3254,7 +3083,7 @@ if (!window.BI) {
* @param format 日期格式 * @param format 日期格式
* @returns {String} * @returns {String}
*/ */
date2Str = function (date, format) { function date2Str(date, format) {
if (!date) { if (!date) {
return ''; return '';
} }
@ -3363,7 +3192,7 @@ if (!window.BI) {
/** /**
* 数字格式 * 数字格式
*/ */
BI._numberFormat = function (text, format) { function _numberFormat(text, format) {
var text = text + ''; var text = text + '';
//数字格式,区分正负数 //数字格式,区分正负数
var numMod = format.indexOf(';'); var numMod = format.indexOf(';');
@ -3385,7 +3214,7 @@ if (!window.BI) {
tleft = tleft.replace(/^0+/gi, ''); tleft = tleft.replace(/^0+/gi, '');
tright = tright.substr(paddingZero.length).replace(/0+$/gi, ''); tright = tright.substr(paddingZero.length).replace(/0+$/gi, '');
} }
var right = BI._dealWithRight(tright, fright); var right = _dealWithRight(tright, fright);
if (right.leftPlus) { if (right.leftPlus) {
//小数点后有进位 //小数点后有进位
tleft = parseInt(tleft) + 1 + ''; tleft = parseInt(tleft) + 1 + '';
@ -3393,7 +3222,7 @@ if (!window.BI) {
tleft = isNaN(tleft) ? '1' : tleft; tleft = isNaN(tleft) ? '1' : tleft;
} }
right = right.num; right = right.num;
var left = BI._dealWithLeft(tleft, fleft); var left = _dealWithLeft(tleft, fleft);
if (!(/[0-9]/.test(left))) { if (!(/[0-9]/.test(left))) {
left = left + '0'; left = left + '0';
} }
@ -3410,7 +3239,7 @@ if (!window.BI) {
* @returns {JSON} 返回处理结果和整数部分是否需要进位 * @returns {JSON} 返回处理结果和整数部分是否需要进位
* @private * @private
*/ */
BI._dealWithRight = function (tright, fright) { function _dealWithRight(tright, fright) {
var right = '', j = 0, i = 0; var right = '', j = 0, i = 0;
for (var len = fright.length; i < len; i++) { for (var len = fright.length; i < len; i++) {
var ch = fright.charAt(i); var ch = fright.charAt(i);
@ -3441,7 +3270,7 @@ if (!window.BI) {
if (numReg) { if (numReg) {
var num = numReg[0]; var num = numReg[0];
var orilen = num.length; var orilen = num.length;
var newnum = BI.parseINT(num) + 1 + ''; var newnum = BI.parseInt(num) + 1 + '';
//进位到整数部分 //进位到整数部分
if (newnum.length > orilen) { if (newnum.length > orilen) {
newnum = newnum.substr(1); newnum = newnum.substr(1);
@ -3454,22 +3283,7 @@ if (!window.BI) {
} }
result.num = right; result.num = right;
return result; return result;
};
BI.parseINT = function (str) {
return parseInt(str, 10);
};
BI.leftPad = function (val, size, ch) {
var result = String(val);
if (!ch) {
ch = " ";
} }
while (result.length < size) {
result = ch + result;
}
return result.toString();
};
/** /**
* 处理小数点左边整数部分 * 处理小数点左边整数部分
@ -3478,7 +3292,7 @@ if (!window.BI) {
* @returns {string} 返回处理结果 * @returns {string} 返回处理结果
* @private * @private
*/ */
BI._dealWithLeft = function (tleft, fleft) { function _dealWithLeft(tleft, fleft) {
var left = ''; var left = '';
var j = tleft.length - 1; var j = tleft.length - 1;
var combo = -1, last = -1; var combo = -1, last = -1;
@ -3537,9 +3351,245 @@ if (!window.BI) {
left = left.replace(/[0-9]+,/, newstr); left = left.replace(/[0-9]+,/, newstr);
} }
return left; return left;
}; }
BI.object2Number = function (value) { BI.cjkEncode = function (text) {
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的)
if (typeof text !== 'string') {
return text;
}
var newText = "";
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i);
if (code >= 128 || code === 91 || code === 93) {//91 is "[", 93 is "]".
newText += "[" + code.toString(16) + "]";
} else {
newText += text.charAt(i);
}
}
return newText
};
BI.cjkEncodeDO = function (o) {
if (BI.isPlainObject(o)) {
var result = {};
$.each(o, function (k, v) {
if (!(typeof v == "string")) {
v = BI.jsonEncode(v);
}
//wei:bug 43338,如果key是中文,cjkencode后o的长度就加了1,ie9以下版本死循环,所以新建对象result。
k = BI.cjkEncode(k);
result[k] = BI.cjkEncode(v);
});
return result;
}
return o;
};
BI.jsonEncode = function (o) {
//james:这个Encode是抄的EXT的
var useHasOwn = {}.hasOwnProperty ? true : false;
// crashes Safari in some instances
//var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
var m = {
"\b": '\\b',
"\t": '\\t',
"\n": '\\n',
"\f": '\\f',
"\r": '\\r',
'"': '\\"',
"\\": '\\\\'
};
var encodeString = function (s) {
if (/["\\\x00-\x1f]/.test(s)) {
return '"' + s.replace(/([\x00-\x1f\\"])/g, function (a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return "\\u00" +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + s + '"';
};
var encodeArray = function (o) {
var a = ["["], b, i, l = o.length, v;
for (i = 0; i < l; i += 1) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
a.push("]");
return a.join("");
};
if (typeof o == "undefined" || o === null) {
return "null";
} else if (BI.isArray(o)) {
return encodeArray(o);
} else if (o instanceof Date) {
/*
* alex:原来只是把年月日时分秒简单地拼成一个String,无法decode
* 现在这么处理就可以decode了,但是JS.jsonDecode和Java.JSONObject也要跟着改一下
*/
return BI.jsonEncode({
__time__: o.getTime()
})
} else if (typeof o == "string") {
return encodeString(o);
} else if (typeof o == "number") {
return isFinite(o) ? String(o) : "null";
} else if (typeof o == "boolean") {
return String(o);
} else if (BI.isFunction(o)) {
return String(o);
} else {
var a = ["{"], b, i, v;
for (i in o) {
if (!useHasOwn || o.hasOwnProperty(i)) {
v = o[i];
switch (typeof v) {
case "undefined":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(BI.jsonEncode(i), ":",
v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
}
a.push("}");
return a.join("");
}
};
BI.jsonDecode = function (text) {
try {
// 注意0啊
//var jo = $.parseJSON(text) || {};
var jo = $.parseJSON(text);
if (jo == null) {
jo = {};
}
} catch (e) {
/*
* richie:浏览器只支持标准的JSON字符串转换而jQuery会默认调用浏览器的window.JSON.parse()函数进行解析
* 比如var str = "{'a':'b'}",这种形式的字符串转换为JSON就会抛异常
*/
try {
jo = new Function("return " + text)() || {};
} catch (e) {
//do nothing
}
if (jo == null) {
jo = [];
}
}
if (!_hasDateInJson(text)) {
return jo;
}
function _hasDateInJson(json) {
if (!json || typeof json !== "string") {
return false;
}
return json.indexOf("__time__") != -1;
}
return (function (o) {
if (typeof o === "string") {
return o;
}
if (o && o.__time__ != null) {
return new Date(o.__time__);
}
for (var a in o) {
if (o[a] == o || typeof o[a] == 'object' || $.isFunction(o[a])) {
break;
}
o[a] = arguments.callee(o[a]);
}
return o;
})(jo);
}
BI.contentFormat = function (cv, fmt) {
if (isEmpty(cv)) {
//原值为空,返回空字符
return '';
}
var text = cv.toString();
if (isEmpty(fmt)) {
//格式为空,返回原字符
return text;
}
if (fmt.match(/^T/)) {
//T - 文本格式
return text;
} else if (fmt.match(/^D/)) {
//D - 日期(时间)格式
if (!(cv instanceof Date)) {
if (typeof cv === 'number') {
//毫秒数类型
cv = new Date(cv);
} else {
//字符串类型,如yyyyMMdd、MMddyyyy等这样无分隔符的结构
cv = Date.parseDate(cv + "", Date.patterns.ISO8601Long);
}
}
if (!BI.isNull(cv)) {
var needTrim = fmt.match(/^DT/);
text = BI.date2Str(cv, fmt.substring(needTrim ? 2 : 1));
}
} else if (fmt.match(/E/)) {
//科学计数格式
text = _eFormat(text, fmt);
} else {
//数字格式
text = _numberFormat(text, fmt);
}
//¤ - 货币格式
text = text.replace(/¤/g, '¥');
return text;
};
BI.leftPad = function (val, size, ch) {
var result = String(val);
if (!ch) {
ch = " ";
}
while (result.length < size) {
result = ch + result;
}
return result.toString();
};
BI.object2Number = function (value) {
if (value == null) { if (value == null) {
return 0; return 0;
} }
@ -3567,7 +3617,7 @@ if (!window.BI) {
var str = obj + ""; var str = obj + "";
str = str.replace(/-/g, '/'); str = str.replace(/-/g, '/');
var dt = new Date(str); var dt = new Date(str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
@ -3575,10 +3625,6 @@ if (!window.BI) {
} }
}; };
BI.isArray = function (a) {
return Object.prototype.toString.call(a) == '[object Array]';
};
BI.object2Time = function (obj) { BI.object2Time = function (obj) {
if (obj == null) { if (obj == null) {
return new Date(); return new Date();
@ -3589,77 +3635,22 @@ if (!window.BI) {
var str = obj + ""; var str = obj + "";
str = str.replace(/-/g, '/'); str = str.replace(/-/g, '/');
var dt = new Date(str); var dt = new Date(str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
if (str.indexOf('/') === -1 && str.indexOf(':') !== -1) { if (str.indexOf('/') === -1 && str.indexOf(':') !== -1) {
dt = new Date("1970/01/01 " + str); dt = new Date("1970/01/01 " + str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
} }
dt = BI.str2Date(str, "HH:mm:ss"); dt = BI.str2Date(str, "HH:mm:ss");
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
return new Date(); return new Date();
} }
}; };
// 判断是否是无效的日期
BI.isInvalidDate = function (date) {
return date == "Invalid Date" || date == "NaN";
};
/**
* 科学计数格式
*/
BI._eFormat = function (text, fmt) {
var e = fmt.indexOf("E");
var eleft = fmt.substr(0, e), eright = fmt.substr(e + 1);
if (/^[0\.-]+$/.test(text)) {
text = BI._numberFormat(0.0, eleft) + 'E' + BI._numberFormat(0, eright)
} else {
var isNegative = text < 0;
if (isNegative) {
text = text.substr(1);
}
var elvl = (eleft.split('.')[0] || '').length;
var point = text.indexOf(".");
if (point < 0) {
point = text.length;
}
var i = 0; //第一个不为0的数的位置
text = text.replace('.', '');
for (var len = text.length; i < len; i++) {
var ech = text.charAt(i);
if (ech <= '9' && ech >= '1') {
break;
}
}
var right = point - i - elvl;
var left = text.substr(i, elvl);
var dis = i + elvl - text.length;
if (dis > 0) {
//末位补全0
for (var k = 0; k < dis; k++) {
left += '0';
}
} else {
left += '.' + text.substr(i + elvl);
}
left = left.replace(/^[0]+/, '');
if (right < 0 && eright.indexOf('-') < 0) {
eright += ';-' + eright;
}
text = BI._numberFormat(left, eleft) + 'E' + BI._numberFormat(right, eright);
if (isNegative) {
text = '-' + text;
}
}
return text;
};
})(); })();
/** /**
* 事件集合 * 事件集合

581
dist/core.js vendored

@ -19695,230 +19695,59 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
return result; return result;
} }
BI.cjkEncode = function (text) { // 判断是否是无效的日期
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的) function isInvalidDate(date) {
if (typeof text !== 'string') { return date == "Invalid Date" || date == "NaN";
return text;
}
var newText = "";
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i);
if (code >= 128 || code === 91 || code === 93) {//91 is "[", 93 is "]".
newText += "[" + code.toString(16) + "]";
} else {
newText += text.charAt(i);
}
}
return newText
};
BI.cjkEncodeDO = function (o) {
if (BI.isPlainObject(o)) {
var result = {};
$.each(o, function (k, v) {
if (!(typeof v == "string")) {
v = BI.jsonEncode(v);
}
//wei:bug 43338,如果key是中文,cjkencode后o的长度就加了1,ie9以下版本死循环,所以新建对象result。
k = BI.cjkEncode(k);
result[k] = BI.cjkEncode(v);
});
return result;
}
return o;
};
BI.jsonEncode = function (o) {
//james:这个Encode是抄的EXT的
var useHasOwn = {}.hasOwnProperty ? true : false;
// crashes Safari in some instances
//var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
var m = {
"\b": '\\b',
"\t": '\\t',
"\n": '\\n',
"\f": '\\f',
"\r": '\\r',
'"': '\\"',
"\\": '\\\\'
};
var encodeString = function (s) {
if (/["\\\x00-\x1f]/.test(s)) {
return '"' + s.replace(/([\x00-\x1f\\"])/g, function (a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return "\\u00" +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + s + '"';
};
var encodeArray = function (o) {
var a = ["["], b, i, l = o.length, v;
for (i = 0; i < l; i += 1) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(v === null ? "null" : BI.jsonEncode(v));
b = true;
}
} }
a.push("]");
return a.join("");
};
if (typeof o == "undefined" || o === null) { /**
return "null"; * 科学计数格式
} else if (BI.isArray(o)) {
return encodeArray(o);
} else if (o instanceof Date) {
/*
* alex:原来只是把年月日时分秒简单地拼成一个String,无法decode
* 现在这么处理就可以decode了,但是JS.jsonDecode和Java.JSONObject也要跟着改一下
*/ */
return BI.jsonEncode({ function _eFormat(text, fmt) {
__time__: o.getTime() var e = fmt.indexOf("E");
}) var eleft = fmt.substr(0, e), eright = fmt.substr(e + 1);
} else if (typeof o == "string") { if (/^[0\.-]+$/.test(text)) {
return encodeString(o); text = BI._numberFormat(0.0, eleft) + 'E' + BI._numberFormat(0, eright)
} else if (typeof o == "number") {
return isFinite(o) ? String(o) : "null";
} else if (typeof o == "boolean") {
return String(o);
} else if (BI.isFunction(o)) {
return String(o);
} else { } else {
var a = ["{"], b, i, v; var isNegative = text < 0;
for (i in o) { if (isNegative) {
if (!useHasOwn || o.hasOwnProperty(i)) { text = text.substr(1);
v = o[i];
switch (typeof v) {
case "undefined":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(BI.jsonEncode(i), ":",
v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
}
a.push("}");
return a.join("");
}
};
BI.jsonDecode = function (text) {
try {
// 注意0啊
//var jo = $.parseJSON(text) || {};
var jo = $.parseJSON(text);
if (jo == null) {
jo = {};
}
} catch (e) {
/*
* richie:浏览器只支持标准的JSON字符串转换而jQuery会默认调用浏览器的window.JSON.parse()函数进行解析
* 比如var str = "{'a':'b'}",这种形式的字符串转换为JSON就会抛异常
*/
try {
jo = new Function("return " + text)() || {};
} catch (e) {
//do nothing
}
if (jo == null) {
jo = [];
}
}
if (!_hasDateInJson(text)) {
return jo;
}
function _hasDateInJson(json) {
if (!json || typeof json !== "string") {
return false;
}
return json.indexOf("__time__") != -1;
}
return (function (o) {
if (typeof o === "string") {
return o;
} }
if (o && o.__time__ != null) { var elvl = (eleft.split('.')[0] || '').length;
return new Date(o.__time__); var point = text.indexOf(".");
if (point < 0) {
point = text.length;
} }
for (var a in o) { var i = 0; //第一个不为0的数的位置
if (o[a] == o || typeof o[a] == 'object' || $.isFunction(o[a])) { text = text.replace('.', '');
for (var len = text.length; i < len; i++) {
var ech = text.charAt(i);
if (ech <= '9' && ech >= '1') {
break; break;
} }
o[a] = arguments.callee(o[a]);
}
return o;
})(jo);
}
BI.contentFormat = function (cv, fmt) {
if (isEmpty(cv)) {
//原值为空,返回空字符
return '';
} }
var text = cv.toString(); var right = point - i - elvl;
if (isEmpty(fmt)) { var left = text.substr(i, elvl);
//格式为空,返回原字符 var dis = i + elvl - text.length;
return text; if (dis > 0) {
//末位补全0
for (var k = 0; k < dis; k++) {
left += '0';
} }
if (fmt.match(/^T/)) {
//T - 文本格式
return text;
} else if (fmt.match(/^D/)) {
//D - 日期(时间)格式
if (!(cv instanceof Date)) {
if (typeof cv === 'number') {
//毫秒数类型
cv = new Date(cv);
} else { } else {
//字符串类型,如yyyyMMdd、MMddyyyy等这样无分隔符的结构 left += '.' + text.substr(i + elvl);
cv = Date.parseDate(cv + "", Date.patterns.ISO8601Long);
} }
left = left.replace(/^[0]+/, '');
if (right < 0 && eright.indexOf('-') < 0) {
eright += ';-' + eright;
} }
if (!BI.isNull(cv)) { text = BI._numberFormat(left, eleft) + 'E' + BI._numberFormat(right, eright);
var needTrim = fmt.match(/^DT/); if (isNegative) {
text = BI.date2Str(cv, fmt.substring(needTrim ? 2 : 1)); text = '-' + text;
} }
} else if (fmt.match(/E/)) {
//科学计数格式
text = BI._eFormat(text, fmt);
} else {
//数字格式
text = BI._numberFormat(text, fmt);
} }
//¤ - 货币格式
text = text.replace(/¤/g, '¥');
return text; return text;
}; }
/** /**
* 把日期对象按照指定格式转化成字符串 * 把日期对象按照指定格式转化成字符串
@ -19932,7 +19761,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
* @param format 日期格式 * @param format 日期格式
* @returns {String} * @returns {String}
*/ */
date2Str = function (date, format) { function date2Str(date, format) {
if (!date) { if (!date) {
return ''; return '';
} }
@ -20041,7 +19870,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
/** /**
* 数字格式 * 数字格式
*/ */
BI._numberFormat = function (text, format) { function _numberFormat(text, format) {
var text = text + ''; var text = text + '';
//数字格式,区分正负数 //数字格式,区分正负数
var numMod = format.indexOf(';'); var numMod = format.indexOf(';');
@ -20063,7 +19892,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
tleft = tleft.replace(/^0+/gi, ''); tleft = tleft.replace(/^0+/gi, '');
tright = tright.substr(paddingZero.length).replace(/0+$/gi, ''); tright = tright.substr(paddingZero.length).replace(/0+$/gi, '');
} }
var right = BI._dealWithRight(tright, fright); var right = _dealWithRight(tright, fright);
if (right.leftPlus) { if (right.leftPlus) {
//小数点后有进位 //小数点后有进位
tleft = parseInt(tleft) + 1 + ''; tleft = parseInt(tleft) + 1 + '';
@ -20071,7 +19900,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
tleft = isNaN(tleft) ? '1' : tleft; tleft = isNaN(tleft) ? '1' : tleft;
} }
right = right.num; right = right.num;
var left = BI._dealWithLeft(tleft, fleft); var left = _dealWithLeft(tleft, fleft);
if (!(/[0-9]/.test(left))) { if (!(/[0-9]/.test(left))) {
left = left + '0'; left = left + '0';
} }
@ -20088,7 +19917,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
* @returns {JSON} 返回处理结果和整数部分是否需要进位 * @returns {JSON} 返回处理结果和整数部分是否需要进位
* @private * @private
*/ */
BI._dealWithRight = function (tright, fright) { function _dealWithRight(tright, fright) {
var right = '', j = 0, i = 0; var right = '', j = 0, i = 0;
for (var len = fright.length; i < len; i++) { for (var len = fright.length; i < len; i++) {
var ch = fright.charAt(i); var ch = fright.charAt(i);
@ -20119,7 +19948,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
if (numReg) { if (numReg) {
var num = numReg[0]; var num = numReg[0];
var orilen = num.length; var orilen = num.length;
var newnum = BI.parseINT(num) + 1 + ''; var newnum = BI.parseInt(num) + 1 + '';
//进位到整数部分 //进位到整数部分
if (newnum.length > orilen) { if (newnum.length > orilen) {
newnum = newnum.substr(1); newnum = newnum.substr(1);
@ -20132,22 +19961,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
} }
result.num = right; result.num = right;
return result; return result;
};
BI.parseINT = function (str) {
return parseInt(str, 10);
};
BI.leftPad = function (val, size, ch) {
var result = String(val);
if (!ch) {
ch = " ";
} }
while (result.length < size) {
result = ch + result;
}
return result.toString();
};
/** /**
* 处理小数点左边整数部分 * 处理小数点左边整数部分
@ -20156,7 +19970,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
* @returns {string} 返回处理结果 * @returns {string} 返回处理结果
* @private * @private
*/ */
BI._dealWithLeft = function (tleft, fleft) { function _dealWithLeft(tleft, fleft) {
var left = ''; var left = '';
var j = tleft.length - 1; var j = tleft.length - 1;
var combo = -1, last = -1; var combo = -1, last = -1;
@ -20215,9 +20029,245 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
left = left.replace(/[0-9]+,/, newstr); left = left.replace(/[0-9]+,/, newstr);
} }
return left; return left;
}; }
BI.object2Number = function (value) { BI.cjkEncode = function (text) {
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的)
if (typeof text !== 'string') {
return text;
}
var newText = "";
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i);
if (code >= 128 || code === 91 || code === 93) {//91 is "[", 93 is "]".
newText += "[" + code.toString(16) + "]";
} else {
newText += text.charAt(i);
}
}
return newText
};
BI.cjkEncodeDO = function (o) {
if (BI.isPlainObject(o)) {
var result = {};
$.each(o, function (k, v) {
if (!(typeof v == "string")) {
v = BI.jsonEncode(v);
}
//wei:bug 43338,如果key是中文,cjkencode后o的长度就加了1,ie9以下版本死循环,所以新建对象result。
k = BI.cjkEncode(k);
result[k] = BI.cjkEncode(v);
});
return result;
}
return o;
};
BI.jsonEncode = function (o) {
//james:这个Encode是抄的EXT的
var useHasOwn = {}.hasOwnProperty ? true : false;
// crashes Safari in some instances
//var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
var m = {
"\b": '\\b',
"\t": '\\t',
"\n": '\\n',
"\f": '\\f',
"\r": '\\r',
'"': '\\"',
"\\": '\\\\'
};
var encodeString = function (s) {
if (/["\\\x00-\x1f]/.test(s)) {
return '"' + s.replace(/([\x00-\x1f\\"])/g, function (a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return "\\u00" +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + s + '"';
};
var encodeArray = function (o) {
var a = ["["], b, i, l = o.length, v;
for (i = 0; i < l; i += 1) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
a.push("]");
return a.join("");
};
if (typeof o == "undefined" || o === null) {
return "null";
} else if (BI.isArray(o)) {
return encodeArray(o);
} else if (o instanceof Date) {
/*
* alex:原来只是把年月日时分秒简单地拼成一个String,无法decode
* 现在这么处理就可以decode了,但是JS.jsonDecode和Java.JSONObject也要跟着改一下
*/
return BI.jsonEncode({
__time__: o.getTime()
})
} else if (typeof o == "string") {
return encodeString(o);
} else if (typeof o == "number") {
return isFinite(o) ? String(o) : "null";
} else if (typeof o == "boolean") {
return String(o);
} else if (BI.isFunction(o)) {
return String(o);
} else {
var a = ["{"], b, i, v;
for (i in o) {
if (!useHasOwn || o.hasOwnProperty(i)) {
v = o[i];
switch (typeof v) {
case "undefined":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(BI.jsonEncode(i), ":",
v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
}
a.push("}");
return a.join("");
}
};
BI.jsonDecode = function (text) {
try {
// 注意0啊
//var jo = $.parseJSON(text) || {};
var jo = $.parseJSON(text);
if (jo == null) {
jo = {};
}
} catch (e) {
/*
* richie:浏览器只支持标准的JSON字符串转换而jQuery会默认调用浏览器的window.JSON.parse()函数进行解析
* 比如var str = "{'a':'b'}",这种形式的字符串转换为JSON就会抛异常
*/
try {
jo = new Function("return " + text)() || {};
} catch (e) {
//do nothing
}
if (jo == null) {
jo = [];
}
}
if (!_hasDateInJson(text)) {
return jo;
}
function _hasDateInJson(json) {
if (!json || typeof json !== "string") {
return false;
}
return json.indexOf("__time__") != -1;
}
return (function (o) {
if (typeof o === "string") {
return o;
}
if (o && o.__time__ != null) {
return new Date(o.__time__);
}
for (var a in o) {
if (o[a] == o || typeof o[a] == 'object' || $.isFunction(o[a])) {
break;
}
o[a] = arguments.callee(o[a]);
}
return o;
})(jo);
}
BI.contentFormat = function (cv, fmt) {
if (isEmpty(cv)) {
//原值为空,返回空字符
return '';
}
var text = cv.toString();
if (isEmpty(fmt)) {
//格式为空,返回原字符
return text;
}
if (fmt.match(/^T/)) {
//T - 文本格式
return text;
} else if (fmt.match(/^D/)) {
//D - 日期(时间)格式
if (!(cv instanceof Date)) {
if (typeof cv === 'number') {
//毫秒数类型
cv = new Date(cv);
} else {
//字符串类型,如yyyyMMdd、MMddyyyy等这样无分隔符的结构
cv = Date.parseDate(cv + "", Date.patterns.ISO8601Long);
}
}
if (!BI.isNull(cv)) {
var needTrim = fmt.match(/^DT/);
text = BI.date2Str(cv, fmt.substring(needTrim ? 2 : 1));
}
} else if (fmt.match(/E/)) {
//科学计数格式
text = _eFormat(text, fmt);
} else {
//数字格式
text = _numberFormat(text, fmt);
}
//¤ - 货币格式
text = text.replace(/¤/g, '¥');
return text;
};
BI.leftPad = function (val, size, ch) {
var result = String(val);
if (!ch) {
ch = " ";
}
while (result.length < size) {
result = ch + result;
}
return result.toString();
};
BI.object2Number = function (value) {
if (value == null) { if (value == null) {
return 0; return 0;
} }
@ -20245,7 +20295,7 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
var str = obj + ""; var str = obj + "";
str = str.replace(/-/g, '/'); str = str.replace(/-/g, '/');
var dt = new Date(str); var dt = new Date(str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
@ -20253,10 +20303,6 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
} }
}; };
BI.isArray = function (a) {
return Object.prototype.toString.call(a) == '[object Array]';
};
BI.object2Time = function (obj) { BI.object2Time = function (obj) {
if (obj == null) { if (obj == null) {
return new Date(); return new Date();
@ -20267,77 +20313,22 @@ BI.PopoverSection.EVENT_CLOSE = "EVENT_CLOSE";;(function () {
var str = obj + ""; var str = obj + "";
str = str.replace(/-/g, '/'); str = str.replace(/-/g, '/');
var dt = new Date(str); var dt = new Date(str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
if (str.indexOf('/') === -1 && str.indexOf(':') !== -1) { if (str.indexOf('/') === -1 && str.indexOf(':') !== -1) {
dt = new Date("1970/01/01 " + str); dt = new Date("1970/01/01 " + str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
} }
dt = BI.str2Date(str, "HH:mm:ss"); dt = BI.str2Date(str, "HH:mm:ss");
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
return new Date(); return new Date();
} }
}; };
// 判断是否是无效的日期
BI.isInvalidDate = function (date) {
return date == "Invalid Date" || date == "NaN";
};
/**
* 科学计数格式
*/
BI._eFormat = function (text, fmt) {
var e = fmt.indexOf("E");
var eleft = fmt.substr(0, e), eright = fmt.substr(e + 1);
if (/^[0\.-]+$/.test(text)) {
text = BI._numberFormat(0.0, eleft) + 'E' + BI._numberFormat(0, eright)
} else {
var isNegative = text < 0;
if (isNegative) {
text = text.substr(1);
}
var elvl = (eleft.split('.')[0] || '').length;
var point = text.indexOf(".");
if (point < 0) {
point = text.length;
}
var i = 0; //第一个不为0的数的位置
text = text.replace('.', '');
for (var len = text.length; i < len; i++) {
var ech = text.charAt(i);
if (ech <= '9' && ech >= '1') {
break;
}
}
var right = point - i - elvl;
var left = text.substr(i, elvl);
var dis = i + elvl - text.length;
if (dis > 0) {
//末位补全0
for (var k = 0; k < dis; k++) {
left += '0';
}
} else {
left += '.' + text.substr(i + elvl);
}
left = left.replace(/^[0]+/, '');
if (right < 0 && eright.indexOf('-') < 0) {
eright += ';-' + eright;
}
text = BI._numberFormat(left, eleft) + 'E' + BI._numberFormat(right, eright);
if (isNegative) {
text = '-' + text;
}
}
return text;
};
})(); })();
/** /**
* guy * guy

581
src/core/alias.js

@ -5,230 +5,59 @@
return result; return result;
} }
BI.cjkEncode = function (text) { // 判断是否是无效的日期
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的) function isInvalidDate(date) {
if (typeof text !== 'string') { return date == "Invalid Date" || date == "NaN";
return text;
}
var newText = "";
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i);
if (code >= 128 || code === 91 || code === 93) {//91 is "[", 93 is "]".
newText += "[" + code.toString(16) + "]";
} else {
newText += text.charAt(i);
}
}
return newText
};
BI.cjkEncodeDO = function (o) {
if (BI.isPlainObject(o)) {
var result = {};
$.each(o, function (k, v) {
if (!(typeof v == "string")) {
v = BI.jsonEncode(v);
}
//wei:bug 43338,如果key是中文,cjkencode后o的长度就加了1,ie9以下版本死循环,所以新建对象result。
k = BI.cjkEncode(k);
result[k] = BI.cjkEncode(v);
});
return result;
}
return o;
};
BI.jsonEncode = function (o) {
//james:这个Encode是抄的EXT的
var useHasOwn = {}.hasOwnProperty ? true : false;
// crashes Safari in some instances
//var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
var m = {
"\b": '\\b',
"\t": '\\t',
"\n": '\\n',
"\f": '\\f',
"\r": '\\r',
'"': '\\"',
"\\": '\\\\'
};
var encodeString = function (s) {
if (/["\\\x00-\x1f]/.test(s)) {
return '"' + s.replace(/([\x00-\x1f\\"])/g, function (a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return "\\u00" +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + s + '"';
};
var encodeArray = function (o) {
var a = ["["], b, i, l = o.length, v;
for (i = 0; i < l; i += 1) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(v === null ? "null" : BI.jsonEncode(v));
b = true;
}
} }
a.push("]");
return a.join("");
};
if (typeof o == "undefined" || o === null) { /**
return "null"; * 科学计数格式
} else if (BI.isArray(o)) {
return encodeArray(o);
} else if (o instanceof Date) {
/*
* alex:原来只是把年月日时分秒简单地拼成一个String,无法decode
* 现在这么处理就可以decode了,但是JS.jsonDecode和Java.JSONObject也要跟着改一下
*/ */
return BI.jsonEncode({ function _eFormat(text, fmt) {
__time__: o.getTime() var e = fmt.indexOf("E");
}) var eleft = fmt.substr(0, e), eright = fmt.substr(e + 1);
} else if (typeof o == "string") { if (/^[0\.-]+$/.test(text)) {
return encodeString(o); text = BI._numberFormat(0.0, eleft) + 'E' + BI._numberFormat(0, eright)
} else if (typeof o == "number") {
return isFinite(o) ? String(o) : "null";
} else if (typeof o == "boolean") {
return String(o);
} else if (BI.isFunction(o)) {
return String(o);
} else { } else {
var a = ["{"], b, i, v; var isNegative = text < 0;
for (i in o) { if (isNegative) {
if (!useHasOwn || o.hasOwnProperty(i)) { text = text.substr(1);
v = o[i];
switch (typeof v) {
case "undefined":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(BI.jsonEncode(i), ":",
v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
}
a.push("}");
return a.join("");
}
};
BI.jsonDecode = function (text) {
try {
// 注意0啊
//var jo = $.parseJSON(text) || {};
var jo = $.parseJSON(text);
if (jo == null) {
jo = {};
}
} catch (e) {
/*
* richie:浏览器只支持标准的JSON字符串转换而jQuery会默认调用浏览器的window.JSON.parse()函数进行解析
* 比如var str = "{'a':'b'}",这种形式的字符串转换为JSON就会抛异常
*/
try {
jo = new Function("return " + text)() || {};
} catch (e) {
//do nothing
}
if (jo == null) {
jo = [];
}
}
if (!_hasDateInJson(text)) {
return jo;
}
function _hasDateInJson(json) {
if (!json || typeof json !== "string") {
return false;
}
return json.indexOf("__time__") != -1;
}
return (function (o) {
if (typeof o === "string") {
return o;
} }
if (o && o.__time__ != null) { var elvl = (eleft.split('.')[0] || '').length;
return new Date(o.__time__); var point = text.indexOf(".");
if (point < 0) {
point = text.length;
} }
for (var a in o) { var i = 0; //第一个不为0的数的位置
if (o[a] == o || typeof o[a] == 'object' || $.isFunction(o[a])) { text = text.replace('.', '');
for (var len = text.length; i < len; i++) {
var ech = text.charAt(i);
if (ech <= '9' && ech >= '1') {
break; break;
} }
o[a] = arguments.callee(o[a]);
}
return o;
})(jo);
}
BI.contentFormat = function (cv, fmt) {
if (isEmpty(cv)) {
//原值为空,返回空字符
return '';
} }
var text = cv.toString(); var right = point - i - elvl;
if (isEmpty(fmt)) { var left = text.substr(i, elvl);
//格式为空,返回原字符 var dis = i + elvl - text.length;
return text; if (dis > 0) {
//末位补全0
for (var k = 0; k < dis; k++) {
left += '0';
} }
if (fmt.match(/^T/)) {
//T - 文本格式
return text;
} else if (fmt.match(/^D/)) {
//D - 日期(时间)格式
if (!(cv instanceof Date)) {
if (typeof cv === 'number') {
//毫秒数类型
cv = new Date(cv);
} else { } else {
//字符串类型,如yyyyMMdd、MMddyyyy等这样无分隔符的结构 left += '.' + text.substr(i + elvl);
cv = Date.parseDate(cv + "", Date.patterns.ISO8601Long);
} }
left = left.replace(/^[0]+/, '');
if (right < 0 && eright.indexOf('-') < 0) {
eright += ';-' + eright;
} }
if (!BI.isNull(cv)) { text = BI._numberFormat(left, eleft) + 'E' + BI._numberFormat(right, eright);
var needTrim = fmt.match(/^DT/); if (isNegative) {
text = BI.date2Str(cv, fmt.substring(needTrim ? 2 : 1)); text = '-' + text;
} }
} else if (fmt.match(/E/)) {
//科学计数格式
text = BI._eFormat(text, fmt);
} else {
//数字格式
text = BI._numberFormat(text, fmt);
} }
//¤ - 货币格式
text = text.replace(/¤/g, '¥');
return text; return text;
}; }
/** /**
* 把日期对象按照指定格式转化成字符串 * 把日期对象按照指定格式转化成字符串
@ -242,7 +71,7 @@
* @param format 日期格式 * @param format 日期格式
* @returns {String} * @returns {String}
*/ */
date2Str = function (date, format) { function date2Str(date, format) {
if (!date) { if (!date) {
return ''; return '';
} }
@ -351,7 +180,7 @@
/** /**
* 数字格式 * 数字格式
*/ */
BI._numberFormat = function (text, format) { function _numberFormat(text, format) {
var text = text + ''; var text = text + '';
//数字格式,区分正负数 //数字格式,区分正负数
var numMod = format.indexOf(';'); var numMod = format.indexOf(';');
@ -373,7 +202,7 @@
tleft = tleft.replace(/^0+/gi, ''); tleft = tleft.replace(/^0+/gi, '');
tright = tright.substr(paddingZero.length).replace(/0+$/gi, ''); tright = tright.substr(paddingZero.length).replace(/0+$/gi, '');
} }
var right = BI._dealWithRight(tright, fright); var right = _dealWithRight(tright, fright);
if (right.leftPlus) { if (right.leftPlus) {
//小数点后有进位 //小数点后有进位
tleft = parseInt(tleft) + 1 + ''; tleft = parseInt(tleft) + 1 + '';
@ -381,7 +210,7 @@
tleft = isNaN(tleft) ? '1' : tleft; tleft = isNaN(tleft) ? '1' : tleft;
} }
right = right.num; right = right.num;
var left = BI._dealWithLeft(tleft, fleft); var left = _dealWithLeft(tleft, fleft);
if (!(/[0-9]/.test(left))) { if (!(/[0-9]/.test(left))) {
left = left + '0'; left = left + '0';
} }
@ -398,7 +227,7 @@
* @returns {JSON} 返回处理结果和整数部分是否需要进位 * @returns {JSON} 返回处理结果和整数部分是否需要进位
* @private * @private
*/ */
BI._dealWithRight = function (tright, fright) { function _dealWithRight(tright, fright) {
var right = '', j = 0, i = 0; var right = '', j = 0, i = 0;
for (var len = fright.length; i < len; i++) { for (var len = fright.length; i < len; i++) {
var ch = fright.charAt(i); var ch = fright.charAt(i);
@ -429,7 +258,7 @@
if (numReg) { if (numReg) {
var num = numReg[0]; var num = numReg[0];
var orilen = num.length; var orilen = num.length;
var newnum = BI.parseINT(num) + 1 + ''; var newnum = BI.parseInt(num) + 1 + '';
//进位到整数部分 //进位到整数部分
if (newnum.length > orilen) { if (newnum.length > orilen) {
newnum = newnum.substr(1); newnum = newnum.substr(1);
@ -442,22 +271,7 @@
} }
result.num = right; result.num = right;
return result; return result;
};
BI.parseINT = function (str) {
return parseInt(str, 10);
};
BI.leftPad = function (val, size, ch) {
var result = String(val);
if (!ch) {
ch = " ";
} }
while (result.length < size) {
result = ch + result;
}
return result.toString();
};
/** /**
* 处理小数点左边整数部分 * 处理小数点左边整数部分
@ -466,7 +280,7 @@
* @returns {string} 返回处理结果 * @returns {string} 返回处理结果
* @private * @private
*/ */
BI._dealWithLeft = function (tleft, fleft) { function _dealWithLeft(tleft, fleft) {
var left = ''; var left = '';
var j = tleft.length - 1; var j = tleft.length - 1;
var combo = -1, last = -1; var combo = -1, last = -1;
@ -525,9 +339,245 @@
left = left.replace(/[0-9]+,/, newstr); left = left.replace(/[0-9]+,/, newstr);
} }
return left; return left;
}; }
BI.object2Number = function (value) { BI.cjkEncode = function (text) {
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的)
if (typeof text !== 'string') {
return text;
}
var newText = "";
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i);
if (code >= 128 || code === 91 || code === 93) {//91 is "[", 93 is "]".
newText += "[" + code.toString(16) + "]";
} else {
newText += text.charAt(i);
}
}
return newText
};
BI.cjkEncodeDO = function (o) {
if (BI.isPlainObject(o)) {
var result = {};
$.each(o, function (k, v) {
if (!(typeof v == "string")) {
v = BI.jsonEncode(v);
}
//wei:bug 43338,如果key是中文,cjkencode后o的长度就加了1,ie9以下版本死循环,所以新建对象result。
k = BI.cjkEncode(k);
result[k] = BI.cjkEncode(v);
});
return result;
}
return o;
};
BI.jsonEncode = function (o) {
//james:这个Encode是抄的EXT的
var useHasOwn = {}.hasOwnProperty ? true : false;
// crashes Safari in some instances
//var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
var m = {
"\b": '\\b',
"\t": '\\t',
"\n": '\\n',
"\f": '\\f',
"\r": '\\r',
'"': '\\"',
"\\": '\\\\'
};
var encodeString = function (s) {
if (/["\\\x00-\x1f]/.test(s)) {
return '"' + s.replace(/([\x00-\x1f\\"])/g, function (a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return "\\u00" +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + s + '"';
};
var encodeArray = function (o) {
var a = ["["], b, i, l = o.length, v;
for (i = 0; i < l; i += 1) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
a.push("]");
return a.join("");
};
if (typeof o == "undefined" || o === null) {
return "null";
} else if (BI.isArray(o)) {
return encodeArray(o);
} else if (o instanceof Date) {
/*
* alex:原来只是把年月日时分秒简单地拼成一个String,无法decode
* 现在这么处理就可以decode了,但是JS.jsonDecode和Java.JSONObject也要跟着改一下
*/
return BI.jsonEncode({
__time__: o.getTime()
})
} else if (typeof o == "string") {
return encodeString(o);
} else if (typeof o == "number") {
return isFinite(o) ? String(o) : "null";
} else if (typeof o == "boolean") {
return String(o);
} else if (BI.isFunction(o)) {
return String(o);
} else {
var a = ["{"], b, i, v;
for (i in o) {
if (!useHasOwn || o.hasOwnProperty(i)) {
v = o[i];
switch (typeof v) {
case "undefined":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(BI.jsonEncode(i), ":",
v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
}
a.push("}");
return a.join("");
}
};
BI.jsonDecode = function (text) {
try {
// 注意0啊
//var jo = $.parseJSON(text) || {};
var jo = $.parseJSON(text);
if (jo == null) {
jo = {};
}
} catch (e) {
/*
* richie:浏览器只支持标准的JSON字符串转换而jQuery会默认调用浏览器的window.JSON.parse()函数进行解析
* 比如var str = "{'a':'b'}",这种形式的字符串转换为JSON就会抛异常
*/
try {
jo = new Function("return " + text)() || {};
} catch (e) {
//do nothing
}
if (jo == null) {
jo = [];
}
}
if (!_hasDateInJson(text)) {
return jo;
}
function _hasDateInJson(json) {
if (!json || typeof json !== "string") {
return false;
}
return json.indexOf("__time__") != -1;
}
return (function (o) {
if (typeof o === "string") {
return o;
}
if (o && o.__time__ != null) {
return new Date(o.__time__);
}
for (var a in o) {
if (o[a] == o || typeof o[a] == 'object' || $.isFunction(o[a])) {
break;
}
o[a] = arguments.callee(o[a]);
}
return o;
})(jo);
}
BI.contentFormat = function (cv, fmt) {
if (isEmpty(cv)) {
//原值为空,返回空字符
return '';
}
var text = cv.toString();
if (isEmpty(fmt)) {
//格式为空,返回原字符
return text;
}
if (fmt.match(/^T/)) {
//T - 文本格式
return text;
} else if (fmt.match(/^D/)) {
//D - 日期(时间)格式
if (!(cv instanceof Date)) {
if (typeof cv === 'number') {
//毫秒数类型
cv = new Date(cv);
} else {
//字符串类型,如yyyyMMdd、MMddyyyy等这样无分隔符的结构
cv = Date.parseDate(cv + "", Date.patterns.ISO8601Long);
}
}
if (!BI.isNull(cv)) {
var needTrim = fmt.match(/^DT/);
text = BI.date2Str(cv, fmt.substring(needTrim ? 2 : 1));
}
} else if (fmt.match(/E/)) {
//科学计数格式
text = _eFormat(text, fmt);
} else {
//数字格式
text = _numberFormat(text, fmt);
}
//¤ - 货币格式
text = text.replace(/¤/g, '¥');
return text;
};
BI.leftPad = function (val, size, ch) {
var result = String(val);
if (!ch) {
ch = " ";
}
while (result.length < size) {
result = ch + result;
}
return result.toString();
};
BI.object2Number = function (value) {
if (value == null) { if (value == null) {
return 0; return 0;
} }
@ -555,7 +605,7 @@
var str = obj + ""; var str = obj + "";
str = str.replace(/-/g, '/'); str = str.replace(/-/g, '/');
var dt = new Date(str); var dt = new Date(str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
@ -563,10 +613,6 @@
} }
}; };
BI.isArray = function (a) {
return Object.prototype.toString.call(a) == '[object Array]';
};
BI.object2Time = function (obj) { BI.object2Time = function (obj) {
if (obj == null) { if (obj == null) {
return new Date(); return new Date();
@ -577,75 +623,20 @@
var str = obj + ""; var str = obj + "";
str = str.replace(/-/g, '/'); str = str.replace(/-/g, '/');
var dt = new Date(str); var dt = new Date(str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
if (str.indexOf('/') === -1 && str.indexOf(':') !== -1) { if (str.indexOf('/') === -1 && str.indexOf(':') !== -1) {
dt = new Date("1970/01/01 " + str); dt = new Date("1970/01/01 " + str);
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
} }
dt = BI.str2Date(str, "HH:mm:ss"); dt = BI.str2Date(str, "HH:mm:ss");
if (!BI.isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
return new Date(); return new Date();
} }
}; };
// 判断是否是无效的日期
BI.isInvalidDate = function (date) {
return date == "Invalid Date" || date == "NaN";
};
/**
* 科学计数格式
*/
BI._eFormat = function (text, fmt) {
var e = fmt.indexOf("E");
var eleft = fmt.substr(0, e), eright = fmt.substr(e + 1);
if (/^[0\.-]+$/.test(text)) {
text = BI._numberFormat(0.0, eleft) + 'E' + BI._numberFormat(0, eright)
} else {
var isNegative = text < 0;
if (isNegative) {
text = text.substr(1);
}
var elvl = (eleft.split('.')[0] || '').length;
var point = text.indexOf(".");
if (point < 0) {
point = text.length;
}
var i = 0; //第一个不为0的数的位置
text = text.replace('.', '');
for (var len = text.length; i < len; i++) {
var ech = text.charAt(i);
if (ech <= '9' && ech >= '1') {
break;
}
}
var right = point - i - elvl;
var left = text.substr(i, elvl);
var dis = i + elvl - text.length;
if (dis > 0) {
//末位补全0
for (var k = 0; k < dis; k++) {
left += '0';
}
} else {
left += '.' + text.substr(i + elvl);
}
left = left.replace(/^[0]+/, '');
if (right < 0 && eright.indexOf('-') < 0) {
eright += ';-' + eright;
}
text = BI._numberFormat(left, eleft) + 'E' + BI._numberFormat(right, eright);
if (isNegative) {
text = '-' + text;
}
}
return text;
};
})(); })();

Loading…
Cancel
Save