fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

925 lines
29 KiB

7 years ago
(function () {
7 years ago
var _global;
if (typeof window !== "undefined") {
_global = window;
} else if (typeof global !== "undefined") {
_global = global;
} else if (typeof self !== "undefined") {
_global = self;
} else {
_global = this;
}
if (!_global.BI) {
_global.BI = {};
8 years ago
}
7 years ago
7 years ago
function isEmpty (value) {
8 years ago
// 判断是否为空值
var result = value === "" || value === null || value === undefined;
return result;
8 years ago
}
8 years ago
// 判断是否是无效的日期
7 years ago
function isInvalidDate (date) {
8 years ago
return date == "Invalid Date" || date == "NaN";
}
8 years ago
8 years ago
/**
7 years ago
* CHART-1400
* 使用数值计算的方式来获取任意数值的科学技术表示值
8 years ago
* 科学计数格式
*/
7 years ago
function _eFormat (text, fmt) {
7 years ago
text = +text;
return eFormat(text, fmt);
7 years ago
7 years ago
/**
* 科学计数格式具体计算过程
* @param num
* @param format {String}有两种形式
* 1"0.00E00"这样的字符串表示正常的科学计数表示只不过规定了数值精确到百分位
* 而数量级的绝对值如果是10以下的时候在前面补零
* 2 "##0.0E0"这样的字符串则规定用科学计数法表示之后的数值的整数部分是三位精确到十分位
* 数量级没有规定因为没见过实数里有用科学计数法表示之后E的后面会小于一位的情况0无所谓
* @returns {*}
*/
7 years ago
function eFormat (num, format) {
7 years ago
var neg = num < 0 ? (num *= -1, "-") : "",
magnitudeNeg = "";
var funcName = num > 0 && num < 1 ? "floor" : "ceil"; // -0.9999->-1
// 数量级
var magnitude = Math[funcName](Math.log(num) / Math.log(10));
if (!isFinite(magnitude)) {
7 years ago
return format.replace(/#/ig, "").replace(/\.e/ig, "E");
8 years ago
}
7 years ago
num = num / Math.pow(10, magnitude);
// 让num转化成[1, 10)区间上的数
if (num > 0 && num < 1) {
num *= 10;
magnitude -= 1;
8 years ago
}
7 years ago
// 计算出format中需要显示的整数部分的位数,然后更新这个数值,也更新数量级
var integerLen = getInteger(magnitude, format);
integerLen > 1 && (magnitude -= integerLen - 1, num *= Math.pow(10, integerLen - 1));
magnitude < 0 && (magnitudeNeg = "-", magnitude *= -1);
// 获取科学计数法精确到的位数
var precision = getPrecision(format);
// 判断num经过四舍五入之后是否有进位
var isValueCarry = isValueCarried(num);
num *= Math.pow(10, precision);
num = Math.round(num);
// 如果出现进位的情况,将num除以10
7 years ago
isValueCarry && (num /= 10, magnitude += magnitudeNeg === "-" ? -1 : 1);
7 years ago
num /= Math.pow(10, precision);
// 小数部分保留precision位
num = num.toFixed(precision);
// 格式化指数的部分
magnitude = formatExponential(format, magnitude, magnitudeNeg);
return neg + num + "E" + magnitude;
}
// 获取format格式规定的数量级的形式
7 years ago
function formatExponential (format, num, magnitudeNeg) {
7 years ago
num += "";
if (!/e/ig.test(format)) {
return num;
}
format = format.split(/e/ig)[1];
while (num.length < format.length) {
num = "0" + num;
}
// 如果magnitudeNeg是一个"-",而且num正好全是0,那么就别显示负号了
var isAllZero = true;
for (var i = 0, len = num.length; i < len; i++) {
if (!isAllZero) {
continue;
8 years ago
}
7 years ago
isAllZero = num.charAt(i) === "0";
}
magnitudeNeg = isAllZero ? "" : magnitudeNeg;
return magnitudeNeg + num;
}
// 获取format规定的科学计数法精确到的位数
7 years ago
function getPrecision (format) {
7 years ago
if (!/e/ig.test(format)) {
return 0;
}
var arr = format.split(/e/ig)[0].split(".");
return arr.length > 1 ? arr[1].length : 0;
}
// 获取数值科学计数法表示之后整数的位数
// 这边我们还需要考虑#和0的问题
7 years ago
function getInteger (magnitude, format) {
7 years ago
if (!/e/ig.test(format)) {
return 0;
8 years ago
}
7 years ago
// return format.split(/e/ig)[0].split(".")[0].length;
var formatLeft = format.split(/e/ig)[0].split(".")[0], i, f, len = formatLeft.length;
var valueLeftLen = 0;
7 years ago
for (i = 0; i < len; i++) {
7 years ago
f = formatLeft.charAt(i);
// "#"所在的位置到末尾长度小于等于值的整数部分长度,那么这个#才可以占位
7 years ago
if (f == 0 || (f == "#" && (len - i <= magnitude + 1))) {
7 years ago
valueLeftLen++;
8 years ago
}
}
7 years ago
return valueLeftLen;
}
// 判断num通过round函数之后是否有进位
7 years ago
function isValueCarried (num) {
7 years ago
var roundNum = Math.round(num);
num = (num + "").split(".")[0];
7 years ago
roundNum = (roundNum + "").split(".")[0];
7 years ago
return num.length !== roundNum.length;
}
}
//'#.##'之类的格式处理 1.324e-18 这种的科学数字
function _dealNumberPrecision (text, fright) {
if (/[eE]/.test(text)) {
var precision = 0, i = 0, ch;
if (/[%‰]$/.test(fright)) {
precision = /[%]$/.test(fright) ? 2 : 3;
8 years ago
}
7 years ago
for (var len = fright.length; i < len; i++) {
if ((ch = fright.charAt(i)) == "0" || ch == "#") {
precision++;
}
8 years ago
}
7 years ago
return Number(text).toFixed(precision);
8 years ago
}
7 years ago
8 years ago
return text;
8 years ago
}
8 years ago
/**
* 数字格式
*/
7 years ago
function _numberFormat (text, format) {
var text = text + "";
7 years ago
//在调用数字格式的时候如果text里没有任何数字则不处理
if (!(/[0-9]/.test(text)) || !format) {
return text;
}
7 years ago
// 数字格式,区分正负数
var numMod = format.indexOf(";");
8 years ago
if (numMod > -1) {
if (text >= 0) {
8 years ago
return _numberFormat(text + "", format.substring(0, numMod));
8 years ago
}
7 years ago
return _numberFormat((-text) + "", format.substr(numMod + 1));
7 years ago
7 years ago
} else {
// 兼容格式处理负数的情况(copy:fr-jquery.format.js)
if (+text < 0 && format.charAt(0) !== "-") {
return _numberFormat((-text) + "", "-" + format);
}
8 years ago
}
7 years ago
7 years ago
var fp = format.split("."), fleft = fp[0] || "", fright = fp[1] || "";
text = _dealNumberPrecision(text, fright);
var tp = text.split("."), tleft = tp[0] || "", tright = tp[1] || "";
7 years ago
// 百分比,千分比的小数点移位处理
8 years ago
if (/[%‰]$/.test(format)) {
7 years ago
var paddingZero = /[%]$/.test(format) ? "00" : "000";
8 years ago
tright += paddingZero;
tleft += tright.substr(0, paddingZero.length);
7 years ago
tleft = tleft.replace(/^0+/gi, "");
tright = tright.substr(paddingZero.length).replace(/0+$/gi, "");
8 years ago
}
8 years ago
var right = _dealWithRight(tright, fright);
8 years ago
if (right.leftPlus) {
7 years ago
// 小数点后有进位
tleft = parseInt(tleft) + 1 + "";
8 years ago
7 years ago
tleft = isNaN(tleft) ? "1" : tleft;
8 years ago
}
right = right.num;
8 years ago
var left = _dealWithLeft(tleft, fleft);
8 years ago
if (!(/[0-9]/.test(left))) {
7 years ago
left = left + "0";
8 years ago
}
if (!(/[0-9]/.test(right))) {
return left + right;
7 years ago
} else {
return left + "." + right;
8 years ago
}
8 years ago
}
8 years ago
/**
* 处理小数点右边小数部分
* @param tright 右边内容
* @param fright 右边格式
* @returns {JSON} 返回处理结果和整数部分是否需要进位
* @private
*/
7 years ago
function _dealWithRight (tright, fright) {
var right = "", j = 0, i = 0;
8 years ago
for (var len = fright.length; i < len; i++) {
var ch = fright.charAt(i);
var c = tright.charAt(j);
switch (ch) {
7 years ago
case "0":
8 years ago
if (isEmpty(c)) {
7 years ago
c = "0";
8 years ago
}
right += c;
j++;
break;
7 years ago
case "#":
8 years ago
right += c;
j++;
break;
default :
right += ch;
break;
}
8 years ago
}
8 years ago
var rll = tright.substr(j);
var result = {};
if (!isEmpty(rll) && rll.charAt(0) > 4) {
7 years ago
// 有多余字符,需要四舍五入
8 years ago
result.leftPlus = true;
var numReg = right.match(/^[0-9]+/);
if (numReg) {
var num = numReg[0];
var orilen = num.length;
7 years ago
var newnum = parseInt(num) + 1 + "";
// 进位到整数部分
8 years ago
if (newnum.length > orilen) {
newnum = newnum.substr(1);
} else {
newnum = BI.leftPad(newnum, orilen, "0");
8 years ago
result.leftPlus = false;
}
right = right.replace(/^[0-9]+/, newnum);
8 years ago
}
}
8 years ago
result.num = right;
return result;
8 years ago
}
8 years ago
8 years ago
/**
* 处理小数点左边整数部分
* @param tleft 左边内容
* @param fleft 左边格式
* @returns {string} 返回处理结果
* @private
*/
7 years ago
function _dealWithLeft (tleft, fleft) {
var left = "";
8 years ago
var j = tleft.length - 1;
var combo = -1, last = -1;
var i = fleft.length - 1;
for (; i >= 0; i--) {
var ch = fleft.charAt(i);
var c = tleft.charAt(j);
switch (ch) {
7 years ago
case "0":
8 years ago
if (isEmpty(c)) {
7 years ago
c = "0";
8 years ago
}
8 years ago
last = -1;
left = c + left;
j--;
break;
7 years ago
case "#":
8 years ago
last = i;
left = c + left;
j--;
break;
7 years ago
case ",":
8 years ago
if (!isEmpty(c)) {
7 years ago
// 计算一个,分隔区间的长度
8 years ago
var com = fleft.match(/,[#0]+/);
if (com) {
combo = com[0].length - 1;
}
7 years ago
left = "," + left;
8 years ago
}
break;
default :
left = ch + left;
break;
8 years ago
}
8 years ago
}
if (last > -1) {
7 years ago
// 处理剩余字符
8 years ago
var tll = tleft.substr(0, j + 1);
left = left.substr(0, last) + tll + left.substr(last);
}
if (combo > 0) {
7 years ago
// 处理,分隔区间
8 years ago
var res = left.match(/[0-9]+,/);
if (res) {
res = res[0];
7 years ago
var newstr = "", n = res.length - 1 - combo;
8 years ago
for (; n >= 0; n = n - combo) {
7 years ago
newstr = res.substr(n, combo) + "," + newstr;
8 years ago
}
var lres = res.substr(0, n + combo);
if (!isEmpty(lres)) {
7 years ago
newstr = lres + "," + newstr;
8 years ago
}
8 years ago
}
8 years ago
left = left.replace(/[0-9]+,/, newstr);
8 years ago
}
8 years ago
return left;
8 years ago
}
BI.cjkEncode = function (text) {
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的)
7 years ago
if (typeof text !== "string") {
8 years ago
return text;
}
var newText = "";
for (var i = 0; i < text.length; i++) {
var code = text.charCodeAt(i);
7 years ago
if (code >= 128 || code === 91 || code === 93) {// 91 is "[", 93 is "]".
8 years ago
newText += "[" + code.toString(16) + "]";
} else {
newText += text.charAt(i);
}
}
7 years ago
return newText;
8 years ago
};
/**
* 将cjkEncode处理过的字符串转化为原始字符串
*
* @static
* @param text 需要做解码的字符串
* @return {String} 解码后的字符串
*/
BI.cjkDecode = function (text) {
if (text == null) {
return "";
}
7 years ago
// 查找没有 "[", 直接返回. kunsnat:数字的时候, 不支持indexOf方法, 也是直接返回.
if (!isNaN(text) || text.indexOf("[") == -1) {
return text;
}
var newText = "";
for (var i = 0; i < text.length; i++) {
var ch = text.charAt(i);
7 years ago
if (ch == "[") {
var rightIdx = text.indexOf("]", i + 1);
if (rightIdx > i + 1) {
var subText = text.substring(i + 1, rightIdx);
7 years ago
// james:主要是考虑[CDATA[]]这样的值的出现
if (subText.length > 0) {
ch = String.fromCharCode(eval("0x" + subText));
}
i = rightIdx;
}
}
newText += ch;
}
return newText;
};
7 years ago
// replace the html special tags
BI.htmlEncode = function (text) {
return BI.isNull(text) ? "" : BI.replaceAll(text + "", "&|\"|<|>|\\s", function (v) {
switch (v) {
case "&":
return "&amp;";
case "\"":
return "&quot;";
case "<":
return "&lt;";
case ">":
return "&gt;";
case " ":
default:
return "&nbsp;";
}
});
};
7 years ago
// html decode
BI.htmlDecode = function (text) {
return BI.isNull(text) ? "" : BI.replaceAll(text + "", "&amp;|&quot;|&lt;|&gt;|&nbsp;", function (v) {
switch (v) {
case "&amp;":
return "&";
case "&quot;":
return "\"";
case "&lt;":
return "<";
case "&gt;":
return ">";
case "&nbsp;":
default:
return " ";
}
});
};
8 years ago
BI.cjkEncodeDO = function (o) {
if (BI.isPlainObject(o)) {
var result = {};
7 years ago
_.each(o, function (v, k) {
7 years ago
if (!(typeof v === "string")) {
8 years ago
v = BI.jsonEncode(v);
}
7 years ago
// wei:bug 43338,如果key是中文,cjkencode后o的长度就加了1,ie9以下版本死循环,所以新建对象result。
8 years ago
k = BI.cjkEncode(k);
result[k] = BI.cjkEncode(v);
});
return result;
}
return o;
};
BI.jsonEncode = function (o) {
7 years ago
// james:这个Encode是抄的EXT的
var useHasOwn = !!{}.hasOwnProperty;
8 years ago
// crashes Safari in some instances
7 years ago
// var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
8 years ago
var m = {
7 years ago
"\b": "\\b",
"\t": "\\t",
"\n": "\\n",
"\f": "\\f",
"\r": "\\r",
"\"": "\\\"",
"\\": "\\\\"
8 years ago
};
var encodeString = function (s) {
if (/["\\\x00-\x1f]/.test(s)) {
7 years ago
return "\"" + s.replace(/([\x00-\x1f\\"])/g, function (a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return "\\u00" +
7 years ago
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
7 years ago
}) + "\"";
8 years ago
}
7 years ago
return "\"" + s + "\"";
8 years ago
};
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) {
7 years ago
a.push(",");
8 years ago
}
a.push(v === null ? "null" : BI.jsonEncode(v));
b = true;
}
}
a.push("]");
return a.join("");
};
7 years ago
if (typeof o === "undefined" || o === null) {
8 years ago
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()
7 years ago
});
} else if (typeof o === "string") {
8 years ago
return encodeString(o);
7 years ago
} else if (typeof o === "number") {
8 years ago
return isFinite(o) ? String(o) : "null";
7 years ago
} else if (typeof o === "boolean") {
8 years ago
return String(o);
} else if (BI.isFunction(o)) {
return String(o);
7 years ago
}
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;
8 years ago
}
}
}
7 years ago
a.push("}");
return a.join("");
7 years ago
8 years ago
};
BI.jsonDecode = function (text) {
try {
// 注意0啊
7 years ago
// var jo = $.parseJSON(text) || {};
6 years ago
var jo = _global.$ ? _global.$.parseJSON(text) : _global.JSON.parse(text);
8 years ago
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) {
7 years ago
// do nothing
8 years ago
}
if (jo == null) {
jo = [];
}
}
if (!_hasDateInJson(text)) {
return jo;
}
7 years ago
function _hasDateInJson (json) {
8 years ago
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) {
7 years ago
if (o[a] == o || typeof o[a] === "object" || _.isFunction(o[a])) {
8 years ago
break;
}
o[a] = arguments.callee(o[a]);
}
return o;
})(jo);
8 years ago
};
8 years ago
BI.encodeURIComponent = function (url) {
BI.specialCharsMap = BI.specialCharsMap || {};
url = url || "";
url = BI.replaceAll(url + "", BI.keys(BI.specialCharsMap || []).join("|"), function (str) {
switch (str) {
case "\\":
return BI.specialCharsMap["\\\\"] || str;
default:
return BI.specialCharsMap[str] || str;
}
});
7 years ago
return _global.encodeURIComponent(url);
};
BI.decodeURIComponent = function (url) {
var reserveSpecialCharsMap = {};
BI.each(BI.specialCharsMap, function (initialChar, encodeChar) {
reserveSpecialCharsMap[encodeChar] = initialChar === "\\\\" ? "\\" : initialChar;
});
url = url || "";
url = BI.replaceAll(url + "", BI.keys(reserveSpecialCharsMap || []).join("|"), function (str) {
return reserveSpecialCharsMap[str] || str;
});
7 years ago
return _global.decodeURIComponent(url);
};
8 years ago
BI.contentFormat = function (cv, fmt) {
if (isEmpty(cv)) {
7 years ago
// 原值为空,返回空字符
return "";
8 years ago
}
var text = cv.toString();
if (isEmpty(fmt)) {
7 years ago
// 格式为空,返回原字符
8 years ago
return text;
}
if (fmt.match(/^T/)) {
7 years ago
// T - 文本格式
8 years ago
return text;
} else if (fmt.match(/^D/)) {
7 years ago
// D - 日期(时间)格式
8 years ago
if (!(cv instanceof Date)) {
7 years ago
if (typeof cv === "number") {
// 毫秒数类型
8 years ago
cv = new Date(cv);
} else {
7 years ago
//字符串类型转化为date类型
cv = new Date(Date.parse(("" + cv).replace(/-|\./g, "/")));
8 years ago
}
}
7 years ago
if (!isInvalidDate(cv) && !BI.isNull(cv)) {
8 years ago
var needTrim = fmt.match(/^DT/);
text = BI.date2Str(cv, fmt.substring(needTrim ? 2 : 1));
}
} else if (fmt.match(/E/)) {
7 years ago
// 科学计数格式
8 years ago
text = _eFormat(text, fmt);
} else {
7 years ago
// 数字格式
8 years ago
text = _numberFormat(text, fmt);
}
7 years ago
// ¤ - 货币格式
text = text.replace(/¤/g, "¥");
8 years ago
return text;
};
7 years ago
/**
* 将Java提供的日期格式字符串装换为JS识别的日期格式字符串
* @class FR.parseFmt
* @param fmt 日期格式
* @returns {String}
*/
BI.parseFmt = function (fmt) {
if (!fmt) {
return "";
}
//日期
fmt = String(fmt)
//年
.replace(/y{4,}/g, "%Y")//yyyy的时候替换为Y
.replace(/y{2}/g, "%y")//yy的时候替换为y
//月
.replace(/M{4,}/g, "%b")//MMMM的时候替换为b,八
.replace(/M{3}/g, "%B")//MMM的时候替换为M,八月
.replace(/M{2}/g, "%X")//MM的时候替换为X,08
.replace(/M{1}/g, "%x")//M的时候替换为x,8
.replace(/a{1}/g, "%p");
//天
if (new RegExp("d{2,}", "g").test(fmt)) {
fmt = fmt.replace(/d{2,}/g, "%d");//dd的时候替换为d
} else {
fmt = fmt.replace(/d{1}/g, "%e");//d的时候替换为j
}
//时
if (new RegExp("h{2,}", "g").test(fmt)) {//12小时制
fmt = fmt.replace(/h{2,}/g, "%I");
} else {
fmt = fmt.replace(/h{1}/g, "%I");
}
if (new RegExp("H{2,}", "g").test(fmt)) {//24小时制
fmt = fmt.replace(/H{2,}/g, "%H");
} else {
fmt = fmt.replace(/H{1}/g, "%H");
}
fmt = fmt.replace(/m{2,}/g, "%M")//分
//秒
.replace(/s{2,}/g, "%S");
return fmt;
};
/**
* 把字符串按照对应的格式转化成日期对象
*
* @example
7 years ago
* var result = BI.str2Date('2013-12-12', 'yyyy-MM-dd');//Thu Dec 12 2013 00:00:00 GMT+0800
7 years ago
*
7 years ago
* @class BI.str2Date
7 years ago
* @param str 字符串
* @param format 日期格式
* @returns {*}
*/
BI.str2Date = function (str, format) {
if (typeof str != "string" || typeof format != "string") {
return null;
}
var fmt = BI.parseFmt(format);
return BI.parseDateTime(str, fmt);
};
8 years ago
/**
* 把日期对象按照指定格式转化成字符串
*
* @example
* var date = new Date('Thu Dec 12 2013 00:00:00 GMT+0800');
* var result = BI.date2Str(date, 'yyyy-MM-dd');//2013-12-12
*
* @class BI.date2Str
* @param date 日期
* @param format 日期格式
* @returns {String}
*/
BI.date2Str = function (date, format) {
if (!date) {
7 years ago
return "";
8 years ago
}
8 years ago
// O(len(format))
7 years ago
var len = format.length, result = "";
8 years ago
if (len > 0) {
var flagch = format.charAt(0), start = 0, str = flagch;
for (var i = 1; i < len; i++) {
var ch = format.charAt(i);
if (flagch !== ch) {
result += compileJFmt({
7 years ago
char: flagch,
str: str,
len: i - start
8 years ago
}, date);
flagch = ch;
start = i;
str = flagch;
} else {
str += ch;
}
}
result += compileJFmt({
7 years ago
char: flagch,
str: str,
len: len - start
8 years ago
}, date);
}
return result;
7 years ago
function compileJFmt (jfmt, date) {
var str = jfmt.str, len = jfmt.len, ch = jfmt["char"];
8 years ago
switch (ch) {
7 years ago
case "E": // 星期
str = BI.Date._DN[date.getDay()];
8 years ago
break;
7 years ago
case "y": // 年
8 years ago
if (len <= 3) {
7 years ago
str = (date.getFullYear() + "").slice(2, 4);
8 years ago
} else {
str = date.getFullYear();
}
break;
7 years ago
case "M": // 月
8 years ago
if (len > 2) {
str = BI.Date._MN[date.getMonth()];
8 years ago
} else if (len < 2) {
str = date.getMonth() + 1;
} else {
str = BI.leftPad(date.getMonth() + 1 + "", 2, "0");
8 years ago
}
break;
7 years ago
case "d": // 日
8 years ago
if (len > 1) {
str = BI.leftPad(date.getDate() + "", 2, "0");
8 years ago
} else {
str = date.getDate();
8 years ago
}
break;
7 years ago
case "h": // 时(12)
8 years ago
var hour = date.getHours() % 12;
if (hour === 0) {
hour = 12;
}
if (len > 1) {
str = BI.leftPad(hour + "", 2, "0");
8 years ago
} else {
str = hour;
}
break;
7 years ago
case "H": // 时(24)
8 years ago
if (len > 1) {
str = BI.leftPad(date.getHours() + "", 2, "0");
8 years ago
} else {
str = date.getHours();
}
break;
7 years ago
case "m":
8 years ago
if (len > 1) {
str = BI.leftPad(date.getMinutes() + "", 2, "0");
8 years ago
} else {
str = date.getMinutes();
}
break;
7 years ago
case "s":
8 years ago
if (len > 1) {
str = BI.leftPad(date.getSeconds() + "", 2, "0");
8 years ago
} else {
str = date.getSeconds();
}
break;
7 years ago
case "a":
str = date.getHours() < 12 ? "am" : "pm";
8 years ago
break;
7 years ago
case "z":
str = BI.getTimezone(date);
8 years ago
break;
default:
str = jfmt.str;
break;
}
return str;
8 years ago
}
8 years ago
};
8 years ago
8 years ago
BI.object2Number = function (value) {
if (value == null) {
return 0;
}
7 years ago
if (typeof value === "number") {
8 years ago
return value;
8 years ago
}
7 years ago
var str = value + "";
if (str.indexOf(".") === -1) {
return parseInt(str);
}
return parseFloat(str);
8 years ago
};
8 years ago
8 years ago
BI.object2Date = function (obj) {
if (obj == null) {
return new Date();
8 years ago
}
8 years ago
if (obj instanceof Date) {
return obj;
7 years ago
} else if (typeof obj === "number") {
8 years ago
return new Date(obj);
}
7 years ago
var str = obj + "";
str = str.replace(/-/g, "/");
var dt = new Date(str);
if (!isInvalidDate(dt)) {
return dt;
}
return new Date();
7 years ago
8 years ago
};
8 years ago
8 years ago
BI.object2Time = function (obj) {
if (obj == null) {
return new Date();
8 years ago
}
8 years ago
if (obj instanceof Date) {
return obj;
7 years ago
}
var str = obj + "";
str = str.replace(/-/g, "/");
var dt = new Date(str);
if (!isInvalidDate(dt)) {
return dt;
}
if (str.indexOf("/") === -1 && str.indexOf(":") !== -1) {
dt = new Date("1970/01/01 " + str);
8 years ago
if (!isInvalidDate(dt)) {
8 years ago
return dt;
}
8 years ago
}
7 years ago
dt = BI.parseDateTime(str, "HH:mm:ss");
7 years ago
if (!isInvalidDate(dt)) {
return dt;
}
return new Date();
7 years ago
8 years ago
};
})();