/*! @title: suncnui @site: www.suncn.com.cn @author: qpf */ ;!function(win){ "use strict"; var doc = document, config = { modules: {} //记录模块物理路径 ,status: {} //记录模块加载状态 ,timeout: 10 //符合规范的模块请求最长等待秒数 ,event: {} //记录模块自定义事件 } ,suncnui = function(){ this.v = '2.5.4'; //版本号 } ,mod_name = "suncn-ui" //获取suncnui所在目录 ,getpath = function(){ var jspath = doc.currentscript ? doc.currentscript.src : function(){ var js = doc.scripts ,last = js.length - 1 ,src; for(var i = last; i > 0; i--){ if(js[i].readystate === 'interactive'){ src = js[i].src; break; } } return src || js[last].src; }(); return jspath.substring(0, jspath.lastindexof('/') + 1); }() //异常提示 ,error = function(msg){ win.console && console.error && console.error('suncnui hint: ' + msg); } ,isopera = typeof opera !== 'undefined' && opera.tostring() === '[object opera]' //内置模块 ,modules = { layer: 'modules/layer' //弹层 ,laydate: 'modules/laydate' //日期 ,colorpicker: 'modules/colorpicker' //颜色选择器 ,jquery: 'modules/jquery' //dom库(第三方) ,laypage: 'modules/laypage' //分页 ,laytpl: 'modules/laytpl' //模板引擎 ,layedit: 'modules/layedit' //富文本编辑器 ,form: 'modules/form' //表单集 ,upload: 'modules/upload' //上传 ,mailupload: 'modules/mailupload' //邮件附件上传 ,transfer: 'modules/transfer' //穿梭框 ,table: 'modules/table' //表格 ,element: 'modules/element' //常用元素操作 ,rate: 'modules/rate' //评分组件 ,tag: 'modules/tag' //tag标签 ,slider: 'modules/slider' //滑块 ,seluser: 'modules/seluser' //用户选择 ,selupunit: 'modules/selupunit' //上报单位选择 ,carousel: 'modules/carousel' //轮播 ,flow: 'modules/flow' //流加载 ,util: 'modules/util' //工具块 ,code: 'modules/code' //代码修饰器 ,tree: 'modules/tree' //树形组件 ,treeselect: 'modules/treeselect' //下拉树 ,treetable: 'modules/treetable' //下拉树 ,notice: 'modules/notice' //消息弹窗 ,popover: 'modules/popover' //气泡框 ,list: 'modules/list' //列表页面 ,formpage: 'modules/formpage' //表单页面 ,formflow: 'modules/formflow' ,choosetemp: 'modules/choosetemp' ,weboffice: 'modules/weboffice' //weboffice插件 ,senddoc: 'modules/senddoc' //发文脚本 ,seluserandgroup: 'modules/seluserandgroup' //选通知通告发布组插件 ,seldocsendtemplet: 'modules/seldocsendtemplet' //选公文发布组插件 ,seldeptuser:'modules/seldeptuser'//本部门选择 ,ofd: 'modules/ofd' //pdf或ofd插件 ,selinfoupunit: 'modules/selinfoupunit' //选报送机构插件 ,wpsinvoke: 'modules/wpsinvoke' // ,wps: 'modules/wps' // ,picture: 'modules/picture' //图片裁剪 ,selcolumn: 'modules/selcolumn' //选栏目插件 ,selctcontacts: 'modules/selctcontacts' ,filedataupload: 'modules/filedataupload' ,viewimg:'modules/viewimg' }; //记录基础数据 suncnui.prototype.cache = config; suncnui.prototype.realpath =function(){ var curwwwpath=window.document.location.href; var pathname=window.document.location.pathname; var pos=curwwwpath.indexof(pathname); var localhostpaht=curwwwpath.substring(0,pos); var projectname=pathname.substring(0,pathname.substr(1).indexof('/')+1); var realpath=localhostpaht+projectname; return realpath; }() //定义模块 suncnui.prototype.define = function(deps, factory){ var that = this ,type = typeof deps === 'function' ,callback = function(){ var setapp = function(app, exports){ suncnui[app] = exports; config.status[app] = true; }; typeof factory === 'function' && factory(function(app, exports){ setapp(app, exports); config.callback[app] = function(){ factory(setapp); } }); return this; }; type && ( factory = deps, deps = [] ); if((!suncnui['suncnui.all'] && suncnui['suncnui.mobile'])){ return callback.call(that); } that.use(deps, callback); return that; }; //使用特定模块 suncnui.prototype.use = function(apps, callback, exports){ var that = this ,dir = config.dir = config.dir ? config.dir : getpath ,head = doc.getelementsbytagname('head')[0]; apps = typeof apps === 'string' ? [apps] : apps; //如果页面已经存在jquery1.7+库且所定义的模块依赖jquery,则不加载内部jquery模块 if(window.jquery && jquery.fn.on){ that.each(apps, function(index, item){ if(item === 'jquery'){ apps.splice(index, 1); } }); suncnui.jquery = suncnui.$ = jquery; } var item = apps[0] ,timeout = 0; exports = exports || []; //静态资源host config.host = config.host || (dir.match(/\/\/([\s\s]+?)\//)||['//'+ location.host +'/'])[0]; //加载完毕 function onscriptload(e, url){ var readyregexp = navigator.platform === 'playstation 3' ? /^complete$/ : /^(complete|loaded)$/ if (e.type === 'load' || (readyregexp.test((e.currenttarget || e.srcelement).readystate))) { config.modules[item] = url; head.removechild(node); (function poll() { if(++timeout > config.timeout * 1000 / 4){ return error(item + ' is not a valid module'); }; config.status[item] ? oncallback() : settimeout(poll, 4); }()); } } //回调 function oncallback(){ exports.push(suncnui[item]); apps.length > 1 ? that.use(apps.slice(1), callback, exports) : ( typeof callback === 'function' && callback.apply(suncnui, exports) ); } //如果引入了完整库(suncnui.all.js),内置的模块则不必再加载 if(apps.length === 0 || (suncnui['suncnui.all'] && modules[item]) || (!suncnui['suncnui.all'] && suncnui['suncnui.mobile'] && modules[item]) ){ return oncallback(), that; } //首次加载模块 if(!config.modules[item]){ var node = doc.createelement('script') //如果是内置模块,则按照 dir 参数拼接模块路径 //如果是扩展模块,则判断模块路径值是否为 {/} 开头, //如果路径值是 {/} 开头,则模块路径即为后面紧跟的字符。 //否则,则按照 base 参数拼接模块路径 ,url = ( modules[item] ? (dir + 'lay/') : (/^\{\/\}/.test(that.modules[item]) ? '' : (config.base || '')) ) + (that.modules[item] || item) + '.js'; url = url.replace(/^\{\/\}/, ''); node.async = true; node.charset = 'utf-8'; node.src = url + function(){ var version = config.version === true ? (config.v || (new date()).gettime()) : (config.version||''); return version ? ('?v=' + version) : ''; }(); head.appendchild(node); if(node.attachevent && !(node.attachevent.tostring && node.attachevent.tostring().indexof('[native code') < 0) && !isopera){ node.attachevent('onreadystatechange', function(e){ onscriptload(e, url); }); } else { node.addeventlistener('load', function(e){ onscriptload(e, url); }, false); } config.modules[item] = url; } else { //缓存 (function poll() { if(++timeout > config.timeout * 1000 / 4){ return error(item + ' is not a valid module'); }; (typeof config.modules[item] === 'string' && config.status[item]) ? oncallback() : settimeout(poll, 4); }()); } return that; }; //获取节点的style属性值 suncnui.prototype.getstyle = function(node, name){ var style = node.currentstyle ? node.currentstyle : win.getcomputedstyle(node, null); return style[style.getpropertyvalue ? 'getpropertyvalue' : 'getattribute'](name); }; //css外部加载器 suncnui.prototype.link = function(href, fn, cssname){ var that = this ,link = doc.createelement('link') ,head = doc.getelementsbytagname('head')[0]; if(typeof fn === 'string') cssname = fn; var app = (cssname || href).replace(/\.|\//g, '') ,id = link.id = 'suncnuicss-'+app ,timeout = 0; link.rel = 'stylesheet'; link.href = href + (config.debug ? '?v='+new date().gettime() : ''); link.media = 'all'; if(!doc.getelementbyid(id)){ head.appendchild(link); } if(typeof fn !== 'function') return that; //轮询css是否加载完毕 (function poll() { if(++timeout > config.timeout * 1000 / 100){ return error(href + ' timeout'); }; parseint(that.getstyle(doc.getelementbyid(id), 'width')) === 1989 ? function(){ fn(); }() : settimeout(poll, 100); }()); return that; }; //存储模块的回调 config.callback = {}; //重新执行模块的工厂函数 suncnui.prototype.factory = function(modname){ if(suncnui[modname]){ return typeof config.callback[modname] === 'function' ? config.callback[modname] : null; } }; //css内部加载器 suncnui.prototype.addcss = function(firename, fn, cssname){ return suncnui.link(config.dir + 'css/' + firename, fn, cssname); }; //图片预加载 suncnui.prototype.img = function(url, callback, error) { var img = new image(); img.src = url; if(img.complete){ return callback(img); } img.onload = function(){ img.onload = null; typeof callback === 'function' && callback(img); }; img.onerror = function(e){ img.onerror = null; typeof error === 'function' && error(e); }; }; //全局配置 suncnui.prototype.config = function(options){ options = options || {}; for(var key in options){ config[key] = options[key]; } return this; }; //记录全部模块 suncnui.prototype.modules = function(){ var clone = {}; for(var o in modules){ clone[o] = modules[o]; } return clone; }(); //拓展模块 suncnui.prototype.extend = function(options){ var that = this; //验证模块是否被占用 options = options || {}; for(var o in options){ if(that[o] || that.modules[o]){ error('\u6a21\u5757\u540d '+ o +' \u5df2\u88ab\u5360\u7528'); } else { that.modules[o] = options[o]; } } return that; }; //路由解析 suncnui.prototype.router = function(hash){ var that = this ,hash = hash || location.hash ,data = { path: [] ,search: {} ,hash: (hash.match(/[^#](#.*$)/) || [])[1] || '' }; if(!/^#\//.test(hash)) return data; //禁止非路由规范 hash = hash.replace(/^#\//, ''); data.href = '/' + hash; hash = hash.replace(/([^#])(#.*$)/, '$1').split('/') || []; //提取hash结构 that.each(hash, function(index, item){ /^\w+=/.test(item) ? function(){ item = item.split('='); data.search[item[0]] = item[1]; }() : data.path.push(item); }); return data; }; //本地持久性存储 suncnui.prototype.data = function(table, settings, storage){ table = table || 'suncnui'; storage = storage || localstorage; if(!win.json || !win.json.parse) return; //如果settings为null,则删除表 if(settings === null){ return delete storage[table]; } settings = typeof settings === 'object' ? settings : {key: settings}; try{ var data = json.parse(storage[table]); } catch(e){ var data = {}; } if('value' in settings) data[settings.key] = settings.value; if(settings.remove) delete data[settings.key]; storage[table] = json.stringify(data); return settings.key ? data[settings.key] : data; }; //本地会话性存储 suncnui.prototype.sessiondata = function(table, settings){ return this.data(table, settings, sessionstorage); } //设备信息 suncnui.prototype.device = function(key){ var agent = navigator.useragent.tolowercase() //获取版本号 ,getversion = function(label){ var exp = new regexp(label + '/([^\\s\\_\\-]+)'); label = (agent.match(exp)||[])[1]; return label || false; } //返回结果集 ,result = { os: function(){ //底层操作系统 if(/windows/.test(agent)){ return 'windows'; } else if(/linux/.test(agent)){ return 'linux'; } else if(/iphone|ipod|ipad|ios/.test(agent)){ return 'ios'; } else if(/mac/.test(agent)){ return 'mac'; } }() ,ie: function(){ //ie版本 return (!!win.activexobject || "activexobject" in win) ? ( (agent.match(/msie\s(\d+)/) || [])[1] || '11' //由于ie11并没有msie的标识 ) : false; }() ,weixin: getversion('micromessenger') //是否微信 }; //任意的key if(key && !result[key]){ result[key] = getversion(key); } //移动设备 result.android = /android/.test(agent); result.ios = result.os === 'ios'; return result; }; //提示 suncnui.prototype.hint = function(){ return { error: error } }; //遍历 suncnui.prototype.each = function(obj, fn){ var key ,that = this; if(typeof fn !== 'function') return that; obj = obj || []; if(obj.constructor === object){ for(key in obj){ if(fn.call(obj[key], key, obj[key])) break; } } else { for(key = 0; key < obj.length; key++){ if(fn.call(obj[key], key, obj[key])) break; } } return that; }; //将数组中的对象按其某个成员排序 suncnui.prototype.sort = function(obj, key, desc){ var clone = json.parse( json.stringify(obj || []) ); if(!key) return clone; //如果是数字,按大小排序,如果是非数字,按字典序排序 clone.sort(function(o1, o2){ var isnum = /^-?\d+$/ ,v1 = o1[key] ,v2 = o2[key]; if(isnum.test(v1)) v1 = parsefloat(v1); if(isnum.test(v2)) v2 = parsefloat(v2); if(v1 && !v2){ return 1; } else if(!v1 && v2){ return -1; } if(v1 > v2){ return 1; } else if (v1 < v2) { return -1; } else { return 0; } }); desc && clone.reverse(); //倒序 return clone; }; //阻止事件冒泡 suncnui.prototype.stope = function(thisevent){ thisevent = thisevent || win.event; try { thisevent.stoppropagation() } catch(e){ thisevent.cancelbubble = true; } }; //自定义模块事件 suncnui.prototype.onevent = function(modname, events, callback){ if(typeof modname !== 'string' || typeof callback !== 'function') return this; return suncnui.event(modname, events, null, callback); }; //表单事件监听 suncnui.prototype.on = function(events, callback){ return suncnui.onevent.call(this, mod_name, events, callback); }; //刷新页面 suncnui.prototype.refresh = function(domid){ var result = suncnui.event.call(this, mod_name, 'refresh('+ domid +')', {}); //没有找到自定义刷新方法,采用统一方式刷新 if(result != 'true'){ var dom = getdom(domid); var form = $(dom).find("form")[0]; if(form){ var search_type = $("#search_type_"+domid).val(); if(search_type && search_type.indexof("_m")>0){//高级检索 //禁用快速查询值 $(".suncnui-list-search-simple",form).find("input").attr("disabled","disabled"); $(".suncnui-list-search-simple",form).find("select").attr("disabled","disabled"); }else{//快捷检索 //禁用高级查询值 $(".suncnui-list-search-more",form).find("input").attr("disabled","disabled"); $(".suncnui-list-search-more",form).find("select").attr("disabled","disabled"); } var page = $(form).find(".suncnui-laypage-skip input[type='text']").val(); if(typeof(page) != 'undefined'){ var url = $(form).attr("action"); if(url.indexof('?')>-1){ url += "¤tpage="; } else{ url += "?currentpage="; } url = url + page; } $(form).attr("action",url); var method = $(form).attr("method"); if(method=="get"){ $(form).get(dom); }else{ $(form).post(dom); } } } }; //执行自定义方法 suncnui.prototype.execute = function(events){ var args = null; if(arguments.length > 1){ args=new array(); for(var i=1;i