(function (mod) { if (typeof exports == "object" && typeof module == "object") // commonjs 规范 module.exports = mod; else if (typeof define == "function" && define.amd) // amd 规范 return define(['jquery', 'jquery.validate'], mod); else // plain browser env 浏览器 this.tools = mod(); })(function () { var tools = {}; // 检测空对象 // 空则返回true tools.isemptyvalue = function (value) { var type; if (value === null) { // 等同于 value === undefined || value === null return true; } type = object.prototype.tostring.call(value).slice(8, -1); switch (type) { case 'string': return !!$.trim(value); case 'array': return !value.length; case 'object': // return $.isemptyobject(value); return !value.length; default: return false; } }; tools.isempty = function (v) { if (v instanceof string) { var r = /^\s*$/; return r.test(v); } else if (v instanceof array) { return !v.length; } else if (v instanceof object) { return v === {}; } else { return false; } }; tools.is_set = function (value) { if (typeof (value) == "undefined") { return false; } else { return true; } }; // 解析url路径 tools.parseurl = function (url) { var a = document.createelement('a'); a.href = url; return { source: url, protocol: a.protocol.replace(':', ''), host: a.hostname, port: a.port, query: a.search, params: (function () { var ret = [], seg = a.search.replace(/^\?/, '').split('&'), len = seg.length, i = 0, s; for (; i < len; i++) { if (!seg[i]) { continue; } s = seg[i].split('='); ret[i] = {"key": s[0], "val": s[1]}; } return ret; })(), file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1], hash: a.hash.replace('#', ''), path: a.pathname.replace(/^([^\/])/, '/$1'), relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1], segments: a.pathname.replace(/^\//, '').split('/') }; }; // todo: url params change tools.imgready = (function () { var list = []; var intervalid = null; // 用来执行队列 var tick = function () { var i = 0; for (; i < list.length; i++) { list[i].end ? list.splice(i--, 1) : list[i](); } !list.length && stop(); }; // 停止所有定时器队列 var stop = function () { clearinterval(intervalid); intervalid = null; }; return function (url, ready, load, error) { var onready, width, height, newwidth, newheight, img = new image(); img.src = url; // 如果图片被缓存,则直接返回缓存数据 if (img.complete) { ready.call(img); load && load.call(img); return; } width = img.width; height = img.height; // 加载错误后的事件 img.onerror = function () { error && error.call(img); onready.end = true; img = img.onload = img.onerror = null; }; // 图片尺寸就绪 onready = function () { newwidth = img.width; newheight = img.height; if (newwidth !== width || newheight !== height || // 如果图片已经在其他地方加载可使用面积检测 newwidth * newheight > 1024 ) { ready.call(img); onready.end = true; } }; onready(); // 完全加载完毕的事件 img.onload = function () { // onload在定时器时间差范围内可能比onready快 // 这里进行检查并保证onready优先执行 !onready.end && onready(); load && load.call(img); // ie gif动画会循环执行onload,置空onload即可 img = img.onload = img.onerror = null; }; // 加入队列中定期执行 if (!onready.end) { list.push(onready); // 无论何时只允许出现一个定时器,减少浏览器性能损耗 if (intervalid === null) intervalid = setinterval(tick, 40); } ; }; })(); // 一部加载页面对分页按钮处理 ,id 带有 # tools.load_page = function (_url, nodeid) { var _load_page = function (__url) { $.ajax({ url: __url, type: 'get', datatype: 'html' }) .done(function (data) { console.log("success"); $(nodeid).html(data); $(nodeid + " .pagination a").each(function (index, el) { var elhref = $(el).attr('href'); if (elhref == '#') { elhref = __url; $(el).attr('data-href', __url); } else { $(el).attr('data-href', elhref); } $(el).attr('href', 'javascript:void(0)'); // $(el).attr('href',"javascript:tools.load_page('"+elhref+"','"+nodeid+"')"); }); }) .fail(function () { // console.log("error"); }) .always(function () { // console.log("complete"); }); }; _load_page(_url); $(nodeid).delegate('a[data-href]', 'click', function () { _load_page($(this).attr('data-href')); }); }; /** * @brief 创建表单验证 * 注意引用 jquery.validate.js 和 jquery.validate.lang.cn.js * @param frm 表单 id * @param rules 验证规则 */ tools.make_validate = function (frm, rules, messages, inline) { if (inline === 0) { inline = "block"; } else { inline = "inline"; } $('#' + frm).validate({ ignore: ".ignore", // 忽略 rules: rules, onchange: true, onblur: true, messages: messages, errorclass: "help-" + inline + " validate", errorelement: "span", highlight: function (element, errorclass, validclass) { $(element).parents('.control-group').removeclass('success').addclass('error'); $(element).removeclass('success').addclass('error'); $(element).nextall('span.validate').remove(); }, unhighlight: function (element, errorclass, validclass) { $(element).parents('.control-group').removeclass('error').addclass('success'); $(element).removeclass('error').addclass('success'); $(element).nextall('span.validate').remove(); }, errorplacement: function (error, element) { if (inline == "block") { error.appendto(element.parents(".control-group")); } else { element.after(error); } } }); }; /** * @brief 创建表单验证 带有返回函数, 可以验证后直接用ajax处理 * 注意引用 jquery.validate.js 和 jquery.validate.lang.cn.js * @param frm 表单 id * @param rules 验证规则 */ tools.make_validate_submit = function (frm, rules, messages, submit_fun, inline) { if (inline === 0) { inline = "block"; } else { inline = "inline"; } $('#' + frm).validate({ ignore: ".ignore", // 忽略 rules: rules, onchange: true, onblur: true, messages: messages, errorclass: "help-inline validate", errorelement: "span", highlight: function (element, errorclass, validclass) { $(element).parents('.control-group').removeclass('success').addclass('error'); $(element).removeclass('success').addclass('error'); $(element).nextall('span.validate').remove(); }, unhighlight: function (element, errorclass, validclass) { $(element).parents('.control-group').removeclass('error').addclass('success'); $(element).removeclass('error').addclass('success'); $(element).nextall('span.validate').remove(); }, errorplacement: function (error, element) { if (inline == "block") { error.appendto(element.parents(".control-group")); } else { element.after(error); } }, submithandler: function (form) { submit_fun(form); } }); }; return tools; });