var pm = pm || {};
/**
* 增加命名空间功能
*
* 使用方法:pm.ns('jQuery.bbb.ccc','jQuery.eee.fff');
*/
pm.ns = function() {
var o = {}, d;
for ( var i = 0; i < arguments.length; i++) {
d = arguments[i].split(".");
o = window[d[0]] = window[d[0]] || {};
for ( var k = 0; k < d.slice(1).length; k++) {
o = o[d[k + 1]] = o[d[k + 1]] || {};
}
}
return o;
};
/**
* 去字符串空格
*/
pm.trim = function(str) {
return str.replace(/(^\s*)|(\s*$)/g, '');
};
pm.ltrim = function(str) {
return str.replace(/(^\s*)/g, '');
};
pm.rtrim = function(str) {
return str.replace(/(\s*$)/g, '');
};
/**
* 判断开始字符是否是XX
*
*/
pm.startWith = function(source, str) {
var reg = new RegExp("^" + str);
return reg.test(source);
};
/**
* 判断结束字符是否是XX
*/
pm.endWith = function(source, str) {
var reg = new RegExp(str + "$");
return reg.test(source);
};
/**
* iframe自适应高度
* @param iframe
*/
pm.autoIframeHeight = function(iframe) {
iframe.style.height = iframe.contentWindow.document.body.scrollHeight
+ "px";
};
/**
* 设置iframe高度
*
* @param iframe
*/
pm.setIframeHeight = function(iframe, height) {
iframe.height = height;
};
// 获取一周前当前时间
pm.theDayBeforeWeek = function() {
var now = new Date();
var d = now.getTime();
var result = new Date();
result.setTime(d - 7 * 24 * 60 * 60 * 1000);
return result;
};
// 获取当前系统时间
pm.getNow = function() {
return new Date().Format("yyyy-MM-dd hh:mm:ss");
};
//格式化时间
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
pm.data = {
/**
* 2014-01-01 11:11:11转换成日期格式
*/
str2date : function(str) {
if (str) {
var dateArr = str.replace(/-/g, "/");
return new Date(dateArr);
} else {
return "";
}
},
/**
* 两个日期的差值(d1 - d2).
* @param d1
* 如:2014-01-01 11:11:11
* @param d2
* 如:2014-01-01 11:11:12
* @returns
*/
diff : function(d1, d2) {
try {
var data1 = pm.data.str2date(d1);
var data2 = pm.data.str2date(d2);
var cha = (data1.getTime() - data2.getTime()) / 1000;
return Math.ceil(cha);
} catch (e) {
return false;
}
}
};
/**
* 增加formatString功能
* @example pm.formatString('字符串{0}字符串{1}字符串','第一个变量','第二个变量');
* @returns 格式化后的字符串
*/
pm.formatString = function(str) {
for ( var i = 0; i < arguments.length - 1; i++) {
str = str.replace("{" + i + "}", arguments[i + 1]);
}
return str;
};
/**
* 接收一个以逗号分割的字符串,返回List,list里每一项都是一个字符串
* @returns list
*/
pm.stringToList = function(value) {
if (value != undefined && value != '') {
var values = [];
var t = value.split(',');
for ( var i = 0; i < t.length; i++) {
values.push('' + t[i]);/* 避免他将ID当成数字 */
}
return values;
} else {
return [];
}
};
/**
* JSON对象转换成String
* @param o
* @returns
*/
pm.jsonToString = function(o) {
var r = [];
if (typeof o == "string")
return "\""
+ o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n")
.replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
if (typeof o == "object") {
if (!o.sort) {
for ( var i in o)
r.push(i + ":" + pm.jsonToString(o[i]));
if (!!document.all
&& !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/
.test(o.toString)) {
r.push("toString:" + o.toString.toString());
}
r = "{" + r.join() + "}";
} else {
for ( var i = 0; i < o.length; i++)
r.push(pm.jsonToString(o[i]));
r = "[" + r.join() + "]";
}
return r;
}
return o.toString();
};