Created
October 27, 2014 13:26
-
-
Save cnnewjohn/78a007f4aadeaf636ea4 to your computer and use it in GitHub Desktop.
common.js 通用js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* common.js 通用js | |
* charset: utf-8 | |
*/ | |
/** | |
* $.ns ,为jq增加ns方法,用来自定义命名空间 | |
*/ | |
$.ns = function(name, obj){ | |
var nodes = name.split("."); | |
var node = window; | |
for (var i = 0; i < nodes.length; i ++) { | |
var node_name = nodes[i]; | |
if(typeof(node[node_name]) == 'undefined') { | |
node[node_name] = (i == (nodes.length - 1)) ? obj : {}; | |
} | |
node = node[node_name]; | |
} | |
return node; | |
}; | |
// 时间戳格式化 | |
$.ns('helper.date', function(format, timestamp){ | |
var date = new Date(parseInt(timestamp) * 1000); | |
var o = { | |
"M+" :date.getMonth() + 1, // month | |
"d+" :date.getDate(), // day | |
"h+" :date.getHours(), // hour | |
"m+" :date.getMinutes(), // minute | |
"s+" :date.getSeconds(), // second | |
"q+" :Math.floor((date.getMonth() + 3) / 3), // quarter | |
"S" :date.getMilliseconds() | |
}; | |
if (/(y+)/.test(format)) { | |
format = format.replace(RegExp.$1, (date.getFullYear() + "") | |
.substr(4 - RegExp.$1.length)); | |
} | |
for ( var k in o) { | |
if (new RegExp("(" + k + ")").test(format)) { | |
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] | |
: ("00" + o[k]).substr(("" + o[k]).length)); | |
} | |
} | |
return format; | |
}); | |
// 分页 | |
$.ns('helper.page', function(items_total, page_total, current_page, cb){ | |
var count_out = 3, count_in = 5; | |
var n1 = 1; | |
var n2 = Math.min(count_out, page_total); | |
var n7 = Math.max(1, page_total - count_out + 1); | |
var n8 = page_total; | |
var n4 = Math.max(n2 + 1, current_page - count_in); | |
var n5 = Math.min(n7 - 1, current_page + count_in); | |
var use_mid = (n5 > n4); | |
var n3 = parseInt((n2 + n4) / 2); | |
var use_n3 = (use_mid && ((n4 - n2) > 1)); | |
var n6 = parseInt((n5 + n7) / 2); | |
var use_n6 = (use_mid && ((n7 - n5) > 1)); | |
var links = {}; | |
var i = 0; | |
for (i = n1; i <= n2; i ++) { | |
links[i] = i; | |
} | |
if (use_n3) { | |
links[n3] = '…'; | |
} | |
for (i = n4; i <= n5; i ++) { | |
links[i] = i; | |
} | |
if (use_n6) { | |
links[n6] = '…'; | |
} | |
for (i = n7; i <= n8; i ++) { | |
links[i] = i; | |
} | |
cb = cb || 'page.logic.getlist'; | |
var html = '<li><a href="javascript:'+cb+'(1);">首页</a></li>'; | |
for (var idx in links){ | |
html = html + '<li' + (idx == current_page ? ' class="active" ' : '') + '><a href="javascript:'+cb+'('+idx+');">' + idx + '</a></li>'; | |
} | |
html = html + '<li><a href="javascript:'+cb+'('+page_total+');">末页</a></li><li class="disabled"><span>共'+page_total+'页'+items_total+'条</span></li>'; | |
return items_total > 0 ? html : ''; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment