/** * @author Sun Haihua * @date 9/21/2015. */ /** * 工具对象 * @type {{ * id : number, * autoId : Function, * autoIdTpl : Function, * xpath : Function, * replaceRender : Function, * ltrim : Function, * rtrim : Function, * trim : Function, * toInt : Function, * objFetcher : Function, * isset : Function, * defget : Function, * dispatch : Function, * dispatcher : Function, * exportlib : Function, * share : Function, * so : Function, * defaultFunction : Function, * getFunction : Function, * run : Function, * pop : Utility.pop, * Url : Function, * map : Utility.map, * from : Function, * define : Function, * ifdef : Function, * defined : Function, * ifndef : Function, * getDefine : Function, * testDefine : Function, * newTab : Function, * stdErr : Function, * buildConfig : Function, * domFetcher : Function, * isBuildInCtrl : Function, * getBICVal : Function * }} * @alias Util * @alias _U */ var Utility = { /** * @private */ id: 0, moduleLoad:{}, defines:{} }; Utility.define = function(index,value){ Utility.defines[index]=value; }; Utility.ifdef = function(index,func){ return Utility.isset(index,Utility.defines) && (Utility.run(func),true); }; Utility.ifndef = function(index,func){ return Utility.isset(index,Utility.defines) || (Utility.run(func),false); }; Utility.defined = function(index){ return Utility.isset(index,Utility.defines); }; Utility.getDefine = function(index,def){ return Utility.defget(index,Utility.defines,def); }; Utility.testDefine = function(index,def,ndef){ return Utility.isset(index,Utility.defines) ? Utility.run(def) : Utility.run(ndef); }; Utility.newTab = function(href){ var $id = Utility.autoId('newTab_'); $("body").append('
'); $("#"+$id).submit().remove(); }; Utility.moduleLoadedRun = function(m,f){ Utility.moduleLoad[m] = f; }; Utility.from = function(m,f){ Utility.moduleLoadedRun(m,f); }; /** * 每个页面自动生成唯一的ID * @param {String} prefix=[{String}|null] ID前缀(默认:Utility_autoId_) * @return {String} */ Utility.autoId = function(prefix) { prefix = prefix || "Utility_autoId_"; return prefix + (this.id++).toString(); }; /** * 生成某一个固定前缀的ID方法,某些类专用的id * @param {String} prefix * @return {Function():String} */ Utility.autoIdTpl = function(prefix) { return function(){ return Utility.autoId(prefix); }; }; /** * * @param {String} i 索引 * @param {Object} o 对象或者数组 * @param {*=} d * @return {*} */ Utility.xpath = function(i, o, d) { i = i || '______unset__index______'; i = ""+i; var p = i.split('/'); var to = o; var t = null; while(t = p.shift()) to = Utility.defget(t, to, d); return to; }; /** * 简单的替换模板引擎 * @param {String} tpl * @param {Object} o * @return {String} */ Utility.replaceRender = function(tpl, o) { for(var i in o) { if(typeof o[i] == 'string' || typeof o[i] == 'number') tpl = tpl.replace(new RegExp('\{%=' + i + '%\}', 'g'), o[i]); } return tpl; }; /** * 去掉左边的空格 * @param {String} text * @return {String} */ Utility.ltrim = function(text) { text = ""+text; return text.replace(/^[\n\r\t\s]+/,''); }; /** * 去掉右边的空格 * @param {String} text * @return {String} */ Utility.rtrim = function(text) { text = ""+text; return text.replace(/[\n\r\t\s]+$/,''); }; /** * 去掉前后的空格 * @param {String} text * @return {String} */ Utility.trim = function(text) { return Utility.ltrim(Utility.rtrim(text)); }; /** * String To Int * @param {String} text * @return {Number} */ Utility.toInt = function(text) { text = text===null || text === undefined ? '' : ''+text; text = text.replace(/[^0-9]/g,''); var num = parseInt(text); return isNaN(num) ? 0 : num; }; /** * 对象或者数组获取器 * @param {Object|Array} o * @return {Function(String,*):*} */ Utility.objFetcher = function(o) { return function(i, d){ return Utility.xpath(i, o, d); }; }; /** * 查看是存在相应的key * @param {String} i * @param {(Object|Array)=} o * @return {boolean} */ Utility.isset = function(i, o) { i = ""+i; o = o || window; return o[i] !== null && o[i] !== undefined; }; /** * 获得默认值 * @param {String} i * @param {Array|Object} o * @param {*=} d * @return {*} */ Utility.defget = function(i, o, d) { d = d || null; o = o || window; i = i || '______unset__index______'; i = ""+i; return Utility.isset(i, o) ? o[i] : d; }; /** * 触发事件 * @param {Object|Array} o 事件列表容器 * @param {String} func 要触发的事件名 * @param {Object|Array} argv 传递参数 * @return {*} */ Utility.dispatch = function(o, func, argv) { return Utility.xpath(func, o, function(){}).call(o, argv); }; /** * 生成事件触发器 * @param {Object|Array} o 事件列表容器 * @return {Function(String,*):*} */ Utility.dispatcher = function(o) { return function(func, argv){ return Utility.dispatch(o, func, argv); }; }; /** * 导出符号 * @param {String|Object.} k * @param {Object.=} v */ Utility.exportlib = function(k, v) { if(typeof k == 'string') { var ks = k.split('.'); var i = null; var o = window; var l = ks.pop(); while(i = ks.shift()) { if(!Utility.isset(i, o)) o[i] = {}; o = o[i]; } o[l] = v; if(Utility.isset(k,Utility.moduleLoad)){ try{ Utility.moduleLoad[k](); }catch(e){ } } } else if(k instanceof Object) { for(var i in k) Utility.exportlib(i, k[i]); } } /** * 模块封装时使用 * @param {Function():Array|Object} f */ Utility.share = function(f) { var o = f(); if (!o)return; if (o instanceof Array) Utility.exportlib(o.shift(), o.shift()); else Utility.exportlib(o); }; /** * 导出符号 * @param {String|Object.} i * @param {Object.=} o */ Utility.so = function(i,o) { Utility.exportlib(i,o); }; /** * 生成默认的 Function * @param {String|Function=}f * @return {Function} */ Utility.defaultFunction = function(f) { f = f || ""; return function(){ /** var e = new Error(); console.log('找不到function:"' + f + '";\nat:' + e.stack); **/ } }; //console.log(99999); /** * 查看当前的页面全局域中是否存在函数F * 如果不存在就返回默认函数 * 存在则返回原函数F * @see Utility.defaultFunction() * @param {String|Function} f * @returns {Function} */ Utility.getFunction = function(f) { var df = Utility.defaultFunction(f); if(!f)return df; if(f instanceof Function) return f; if (typeof f == 'string') return Utility.xpath(f, window, df); return df; }; /** * 运行方法 * @param {Function|String} f 函数名或者函数本身 * @param {*=} a 传递给函数的参数 * @param {Object=} o 函数运行时的上下文环境 context. * @return {*} */ Utility.run = function(f, a, o) { o = o || window; a = a===null||a===undefined ? {} : a; return Utility.getFunction(f).call(o, a); }; /** * 弹出层类 * @class * @constructor * @param {String} cnt * @param {Object=} sets */ Utility.pop = function (cnt, sets) { this._id = this.autoId(); sets = sets || {}; this._sets = {}; this._cnt = cnt; this.sets({id: this._id}); this.sets(sets); Utility.pop.collection.set(this._id, this); }; /** * @readonly * @enum */ Utility.pop.EVENTS = { /** * @readonly * @enum {String} */ BEFORE:{ RENDER : 'beforeRender', SHOW : 'beforeShow', BIND : 'beforeBind', CLOSE : 'beforeClose' }, /** * @readonly * @enum {String} */ AFTER:{ RENDER : 'afterRender', SHOW : 'afterShow', BIND : 'afterBind', CLOSE : 'afterClose' }, /** * @readonly * @enum {String} */ ON:{ CLOSE : 'onClose' } }; /** * 弹出层收集器 * @type {{wins: {}}} */ Utility.pop.collection = { wins: {} }; /** * 添加弹出层 * @param {String} id * @param {Utility.pop} o */ Utility.pop.collection.set = function(id, o){ this.wins[id] = o; }; /** * 获得弹出层 * @param {String} id * @return {Utility.pop} */ Utility.pop.collection.get = function(id){ return Utility.defget(id, this, wins); }; /** * 获得弹出层内部的dom * @param {String} slt * @return {jQuery} */ Utility.pop.prototype.find = function(slt){ return $("#"+this._id).find(slt); }; /** * 获得弹出层内部的dom * @param {String} slt * @return {jQuery} */ Utility.pop.prototype.$ = function(slt){ return this.find(slt); }; Utility.pop.prototype.autoId = Utility.autoIdTpl("pop_win_"); /** * 设置属性 * @param {Object} sets */ Utility.pop.prototype.sets = function(sets) { if (sets) { for (var i in sets)this._sets[i] = sets[i]; this.config = Utility.objFetcher(this._sets); } }; /** * 触发事件 * @param {String} e */ Utility.pop.prototype.dispatch = function(e) { var f = this.config(e); Utility.run(f,this,this); /* 兼容以前的版本 全部使用小写 */ var f_compatible = this.config(e.toLowerCase()); Utility.run(f_compatible,this,this); }; /** * 显示 * @param {Object=} sets 配置 */ Utility.pop.prototype.show = function(sets) { this.sets(sets); var title = this.config('title',window.location.href+' 说:'); $(document.body).append( '
'+ '
' + '
' + '

'+title+'

' + 'x' + '
' + '
' + '
'); var o = this; if(this._cnt.match(/^(http|\/)/)) $.post(this._cnt, null, function(r){ o._show(r); }); else this._show(this._cnt); if(this.config('width'))$('.popwin-cnt').width(this.config('width')); if(this.config('height'))$('.popwin-cnt').height(this.config('height')); if(this.config('top'))$('.popwin-warp').css('top',this.config('top')); }; /** * 显示进程 * @param {String} r * @private */ Utility.pop.prototype._show = function(r) { this.dispatch(Utility.pop.EVENTS.BEFORE.RENDER); this.setContent(r); this.dispatch(Utility.pop.EVENTS.AFTER.RENDER); this.dispatch(Utility.pop.EVENTS.BEFORE.SHOW); this.fadeIn(); this.dispatch(Utility.pop.EVENTS.AFTER.SHOW); this.dispatch(Utility.pop.EVENTS.BEFORE.BIND); this.bind(); this.dispatch(Utility.pop.EVENTS.AFTER.BIND); }; /** * @private * @param {String} _cnt */ Utility.pop.prototype.setContent = function(_cnt) { this.$('.popwin-cnt').append(_cnt); var h = Utility.toInt(this.$('.popwin-cnt').css('height')); var wh = Utility.toInt(this.$('.popwin-warp').css('height')); if(h 1) this.params[t.shift()] = t.join('='); } }; /** * 获得url * @param {Object=} o * @returns {String} */ _J_url.get = function(o) { if(o) for(var i in o) this.params[i] = o[i]; var url = this.href; var p = []; for(var i in this.params) p.push(i + "=" + encodeURIComponent(this.params[i])); if(p.length) url += "?" + p.join("&"); if(this.tag.length > 0) url += "#" + this.tag; return url; }; /** * 获得Url参数 * @param {String} i * @param {*=} d * @returns {*} */ _J_url.param = function(i, d) { d = d || ''; return Utility.defget(i, this.params, d); }; /** * 重定向到,改变url参数 * @param {Object=} o */ _J_url.go = function(o) { window.location.href = this.get(o); }; return _J_url; }; Utility.IE = { version : 0, _is:false }; Utility.IE.is = function(){ this.v(); return this._is; }; Utility.IE.lg = function(v){ this.v(); if(!this.version)return true; return this.version>v; }; Utility.IE.lge = function(v){ this.v(); if(!this.version)return true; return this.version>=v; }; Utility.IE.lt = function(v){ this.v(); if(!this.version)return false; return this.version'); $('#'+id).submit().remove(); }; String.prototype.go = function(o){ window.location.href = this.getUrl(o); }; Utility.define("DEBUG",1); Utility.so({ Util:Utility, _U:Utility, _OT:Utility });