diff --git a/examples/code.html b/examples/code.html index 6ba67880..c0a68807 100644 --- a/examples/code.html +++ b/examples/code.html @@ -14,7 +14,27 @@ pre{margin-bottom: 20px;}
-++ //路由 + LAY.fn.router = function(hash){ + var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; + var item, param = { + dir: [] + }; + for(var i = 0; i < hashs.length; i++){ + item = hashs[i].split('='); + /^\w+=/.test(hashs[i]) ? function(){ + if(item[0] !== 'dir'){ + param[item[0]] = item[1]; + } + }() : param.dir.push(hashs[i]); + item = null; + } + return param; + }; ++ +//路由 LAY.fn.router = function(hash){ var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; @@ -34,7 +54,7 @@ LAY.fn.router = function(hash){ };-+//路由 LAY.fn.router = function(hash){ var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; @@ -54,21 +74,28 @@ LAY.fn.router = function(hash){ };-+var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; var item, param = { dir: [] }; -+-//代码中的代码 var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; var item, param = { dir: [] }; ++ //代码中的代码 + var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; + var item, param = { + dir: [] + }; ++//路由 LAY.fn.router = function(hash){ var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; @@ -86,19 +113,19 @@ LAY.fn.router = function(hash){ } return param; }; -+//代码中的代码 var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; var item, param = { dir: [] }; -+//代码中的代码 var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; var item, param = { dir: [] }; -+//代码中的代码 var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; var item, param = { @@ -110,18 +137,36 @@ var item, param = {-++123+ about ++ + diff --git a/src/css/modules/code.css b/src/css/modules/code.css index c6a23917..e8c709bf 100644 --- a/src/css/modules/code.css +++ b/src/css/modules/code.css @@ -11,18 +11,20 @@ html #layuicss-skincodecss{display:none; position: absolute; width:1989px;} /* 默认风格 */ .layui-code-view{display: block; position: relative; margin: 10px 0; padding: 0; border: 1px solid #eee; border-left-width: 6px; background-color: #FAFAFA; color: #333; font-family: Courier New; font-size: 13px;} -.layui-code-h3{position: relative; padding: 0 10px; height: 40px; line-height: 40px; border-bottom: 1px solid #eee; font-size: 12px;} -.layui-code-h3 a{position: absolute; right: 10px; top: 0; color: #999;} -.layui-code-view .layui-code-ol{position: relative; overflow: auto;} -.layui-code-view .layui-code-ol li{position: relative; margin-left: 45px; line-height: 20px; padding: 0 10px; border-left: 1px solid #e2e2e2; list-style-type: decimal-leading-zero; *list-style-type: decimal; background-color: #fff;} -.layui-code-view .layui-code-ol li:first-child{padding-top: 10px;} -.layui-code-view .layui-code-ol li:last-child{padding-bottom: 10px;} +.layui-code-title{position: relative; padding: 0 10px; height: 40px; line-height: 40px; border-bottom: 1px solid #eee; font-size: 12px;} +.layui-code-title > .layui-code-about{position: absolute; right: 10px; top: 0; color: #999;} +.layui-code-view > .layui-code-ol, .layui-code-view > .layui-code-ul{position: relative; overflow: auto;} +.layui-code-view > .layui-code-ol > li{position: relative; margin-left: 45px; line-height: 20px; padding: 0 10px; border-left: 1px solid #e2e2e2; list-style-type: decimal-leading-zero; *list-style-type: decimal; background-color: #fff;} +.layui-code-view > .layui-code-ol > li:first-child, .layui-code-view > .layui-code-ul > li:first-child{padding-top: 10px;} +.layui-code-view > .layui-code-ol > li:last-child, .layui-code-view > .layui-code-ul > li:last-child{padding-bottom: 10px;} +.layui-code-view > .layui-code-ul > li{position: relative; line-height: 20px; padding: 0 10px; list-style-type: none; *list-style-type: none; background-color: #fff;} .layui-code-view pre{margin: 0;} /* notepadd++ 风格 */ .layui-code-notepad{border: 1px solid #0C0C0C; border-left-color: #3F3F3F; background-color: #0C0C0C; color: #C2BE9E} -.layui-code-notepad .layui-code-h3{border-bottom: none;} -.layui-code-notepad .layui-code-ol li{background-color: #3F3F3F; border-left: none;} +.layui-code-notepad > .layui-code-title{border-bottom: none;} +.layui-code-notepad > .layui-code-ol > li, .layui-code-notepad > .layui-code-ul > li{background-color: #3F3F3F; border-left: none;} +.layui-code-notepad > .layui-code-ul > li{margin-left: 6px;} /* 代码预览 */ .layui-code-demo .layui-code{visibility: visible !important; margin: -15px; border-top: none; border-right: none; border-bottom: none;} diff --git a/src/modules/code.js b/src/modules/code.js index fda08bcb..920d97ec 100644 --- a/src/modules/code.js +++ b/src/modules/code.js @@ -17,8 +17,7 @@ layui.define(['util'], function(exports){ options = options || {}; options.elem = $(options.elem||'.layui-code'); - options.lang = 'lang' in options ? options.lang : 'code'; - + options.elem.each(function(){ elems.push(this); }); @@ -26,22 +25,30 @@ layui.define(['util'], function(exports){ layui.each(elems.reverse(), function(index, item){ var othis = $(item); var html = trim(othis.html()); - var about = othis.attr('lay-about') || options.about || ( - othis.attr('lay-lang') || options.lang - ) || ''; - + var about = othis.attr('lay-about') || options.about || othis.attr('lay-lang') || options.lang || ''; + + //是否显示行号 + var lineNo; + if(othis.attr('lay-line-no')) + { + lineNo = othis.attr('lay-line-no').toLowerCase() === 'true'; + } + lineNo = options.lineNo === undefined ? lineNo === undefined ? false : lineNo : options.lineNo; + // 转义 HTML 标签 if(othis.attr('lay-encode') || options.encode){ html = util.escape(html); } - othis.html('') + var list = lineNo ? 'ol' : 'ul'; + othis.html('<' + list + ' class="layui-code-' + list + '">
- ' + html.replace(/[\r\t\n]+/g, '
- ') + '