diff --git a/examples/colorpicker.html b/examples/colorpicker.html index 22a36e0e..2243ca83 100644 --- a/examples/colorpicker.html +++ b/examples/colorpicker.html @@ -60,12 +60,19 @@ body{padding:20px;} //,predefine: true //开启预定义颜色 //,colors: ['#F00','#0F0','#00F','rgb(255, 69, 0)','rgba(255, 69, 0, 0.5)'] ,change: function(color){ - document.body.style.backgroundColor = color; + this.done(color); } ,done: function(color){ layui.$('#LAY-test1').val(color); document.body.style.backgroundColor = color; } + ,cancel: function(color){ + this.done(color); + console.log('cancel', color); + } + ,close: function(color){ + console.log('close', color); + } }); colorpicker.render({ diff --git a/examples/element.tab.html b/examples/element.tab.html index 363e9689..c1af1927 100644 --- a/examples/element.tab.html +++ b/examples/element.tab.html @@ -113,6 +113,10 @@ layui.use(['element', 'form'], function(){ element.on('tab(test)', function(data){ console.log(this, data); }); + + element.on('tabDelete(tabDemo)', function(data){ + console.log(this, data); + }); }); diff --git a/src/css/layui.css b/src/css/layui.css index 12ba1d30..51ffc0ae 100644 --- a/src/css/layui.css +++ b/src/css/layui.css @@ -900,7 +900,7 @@ a cite{font-style: normal; *cursor:pointer;} /* 小表格 */ .layui-table[lay-size="sm"] th, .layui-table[lay-size="sm"] td{padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; font-size: 12px;} -.layui-table-view .layui-table[lay-size="sm"] .layui-table-cell{height: 30px; line-height: 20px; padding-top: 5px; padding-right: 5px;} +.layui-table-view .layui-table[lay-size="sm"] .layui-table-cell{height: 30px; line-height: 20px; padding-top: 5px; padding-left: 5px; padding-right: 5px;} /* 数据表格 */ .layui-table[lay-data]{display: none;} diff --git a/src/modules/colorpicker.js b/src/modules/colorpicker.js index 765f9d51..97f374b6 100644 --- a/src/modules/colorpicker.js +++ b/src/modules/colorpicker.js @@ -6,13 +6,14 @@ layui.define(['jquery', 'lay'], function(exports){ "use strict"; - var $ = layui.jquery - ,lay = layui.lay - ,device = layui.device() - ,clickOrMousedown = (device.mobile ? 'click' : 'mousedown') + var $ = layui.$; + var lay = layui.lay; + var hint = layui.hint(); + var device = layui.device(); + var clickOrMousedown = (device.mobile ? 'click' : 'mousedown'); //外部接口 - ,colorpicker = { + var colorpicker = { config: {} ,index: layui.colorpicker ? (layui.colorpicker.index + 10000) : 0 @@ -27,16 +28,19 @@ layui.define(['jquery', 'lay'], function(exports){ ,on: function(events, callback){ return layui.onevent.call(this, 'colorpicker', events, callback); } - } + }; - //操作当前实例 - ,thisColorPicker = function(){ - var that = this - ,options = that.config; + // 操作当前实例 + var thisModule = function(){ + var that = this; + var options = that.config; + var id = options.id; + + thisModule.that[id] = that; // 记录当前实例对象 return { config: options - } + }; } //字符常量 @@ -213,15 +217,18 @@ layui.define(['jquery', 'lay'], function(exports){ var othis = options.elem = $(options.elem); options.size && elemColorBox.addClass('layui-colorpicker-'+ options.size); //初始化颜色选择框尺寸 - //插入颜色选择框 + // 插入颜色选择框 othis.addClass('layui-inline').html( that.elemColorBox = elemColorBox ); + + //初始化 id 参数 + options.id = ('id' in options) ? options.id : that.index; - //获取背景色值 + // 获取背景色值 that.color = that.elemColorBox.find('.'+ PICKER_TRIG_SPAN)[0].style.background; - //相关事件 + // 相关事件 that.events(); }; @@ -291,6 +298,9 @@ layui.define(['jquery', 'lay'], function(exports){ that.removePicker(Class.thisElemInd); $('body').append(elemPicker); } + + // 记录当前执行的实例索引 + colorpicker.thisId = options.id; Class.thisElemInd = that.index; //记录最新打开的选择器索引 Class.thisColor = elemColorBox.style.background //记录最新打开的选择器颜色选中值 @@ -301,9 +311,18 @@ layui.define(['jquery', 'lay'], function(exports){ //颜色选择器移除 Class.prototype.removePicker = function(index){ - var that = this - ,options = that.config; - $('#layui-colorpicker'+ (index || that.index)).remove(); + var that = this; + var options = that.config; + var elem = $('#layui-colorpicker'+ (index || that.index)); + + if(elem[0]){ + elem.remove(); + delete colorpicker.thisId; + + // 面板关闭后的回调 + typeof options.close === 'function' && options.close(that.color); + } + return that; }; @@ -640,14 +659,12 @@ layui.define(['jquery', 'lay'], function(exports){ }); } - //颜色选择器输入 + // 颜色选择器输入 Class.prototype.events = function(){ - var that = this - ,options = that.config - - ,elemColorBoxSpan = that.elemColorBox.find('.' + PICKER_TRIG_SPAN) - - //弹出颜色选择器 + var that = this; + var options = that.config; + + // 弹出颜色选择器 that.elemColorBox.on('click' , function(){ that.renderPicker(); if($(ELEM_MAIN)[0]){ @@ -655,11 +672,19 @@ layui.define(['jquery', 'lay'], function(exports){ that.side(); }; }); - - if(!options.elem[0] || that.elemColorBox[0].eventHandler) return; - + }; + + //全局事件 + (function(){ //绑定关闭控件事件 $doc.on(clickOrMousedown, function(e){ + if(!colorpicker.thisId) return; + var that = thisModule.getThis(colorpicker.thisId); + if(!that) return; + + var options = that.config; + var elemColorBoxSpan = that.elemColorBox.find('.' + PICKER_TRIG_SPAN); + //如果点击的元素是颜色框 if($(e.target).hasClass(ELEM) || $(e.target).parents('.'+ELEM)[0] @@ -680,24 +705,40 @@ layui.define(['jquery', 'lay'], function(exports){ } elemColorBoxSpan[0].style.background = that.color || ''; + // 取消选择的回调 + typeof options.cancel === 'function' && options.cancel(that.color); + + // 移除面板 that.removePicker(); }); //自适应定位 $win.on('resize', function(){ + if(!colorpicker.thisId) return; + var that = thisModule.getThis(colorpicker.thisId); + if(!that) return; + if(!that.elemPicker || !$(ELEM_MAIN)[0]){ return false; } that.position(); }); - - that.elemColorBox[0].eventHandler = true; + })(); + + // 记录所有实例 + thisModule.that = {}; // 记录所有实例对象 + + // 获取当前实例对象 + thisModule.getThis = function(id){ + var that = thisModule.that[id]; + if(!that) hint.error(id ? (MOD_NAME +' instance with ID \''+ id +'\' not found') : 'ID argument required'); + return that; }; //核心入口 colorpicker.render = function(options){ var inst = new Class(options); - return thisColorPicker.call(inst); + return thisModule.call(inst); }; exports(MOD_NAME, colorpicker); diff --git a/src/modules/dropdown.js b/src/modules/dropdown.js index 9bf76a8b..bfaf1a12 100644 --- a/src/modules/dropdown.js +++ b/src/modules/dropdown.js @@ -36,9 +36,9 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){ //操作当前实例 ,thisModule = function(){ - var that = this - ,options = that.config - ,id = options.id; + var that = this; + var options = that.config; + var id = options.id; thisModule.that[id] = that; //记录当前实例对象 diff --git a/src/modules/layedit.js b/src/modules/layedit.js deleted file mode 100644 index 87cad7ab..00000000 --- a/src/modules/layedit.js +++ /dev/null @@ -1,570 +0,0 @@ -/** - * layedit 富文本编辑器(该组件已被弃用,为了向下兼容而保留) - */ - -layui.define(['layer', 'form'], function(exports){ - "use strict"; - - var $ = layui.$ - ,layer = layui.layer - ,form = layui.form - ,hint = layui.hint() - ,device = layui.device() - - ,MOD_NAME = 'layedit', THIS = 'layui-this', SHOW = 'layui-show', ABLED = 'layui-disabled' - - ,Edit = function(){ - var that = this; - that.index = 0; - - //全局配置 - that.config = { - //默认工具bar - tool: [ - 'strong', 'italic', 'underline', 'del' - ,'|' - ,'left', 'center', 'right' - ,'|' - ,'link', 'unlink' - ] - ,hideTool: [] - ,height: 280 //默认高 - }; - }; - - //全局设置 - Edit.prototype.set = function(options){ - var that = this; - $.extend(true, that.config, options); - return that; - }; - - //事件 - Edit.prototype.on = function(events, callback){ - return layui.onevent(MOD_NAME, events, callback); - }; - - //建立编辑器 - Edit.prototype.build = function(id, settings){ - settings = settings || {}; - - var that = this - ,config = that.config - ,ELEM = 'layui-layedit', textArea = $(typeof(id)=='string'?'#'+id:id) - ,name = 'LAY_layedit_'+ (++that.index) - ,haveBuild = textArea.next('.'+ELEM) - - ,set = $.extend({}, config, settings) - - ,tool = function(){ - var node = [], hideTools = {}; - layui.each(set.hideTool, function(_, item){ - hideTools[item] = true; - }); - layui.each(set.tool, function(_, item){ - if(tools[item] && !hideTools[item]){ - node.push(tools[item]); - } - }); - return node.join(''); - }() - - - ,editor = $(['
'); - } - }); - - //给textarea同步内容 - $(textArea).parents('form').on('submit', function(){ - var html = body.html(); - //IE8下将标签处理成小写 - if(device.ie == 8){ - html = html.replace(/<.+>/g, function(str){ - return str.toLowerCase(); - }); - } - textArea.value = html; - }); - - //处理粘贴 - body.on('paste', function(e){ - iframeDOM.execCommand('formatBlock', false, '
'); - setTimeout(function(){ - filter.call(iframeWin, body); - textArea.value = body.html(); - }, 100); - }); - } - - //标签过滤 - ,filter = function(body){ - var iframeWin = this - ,iframeDOM = iframeWin.document; - - //清除影响版面的css属性 - body.find('*[style]').each(function(){ - var textAlign = this.style.textAlign; - this.removeAttribute('style'); - $(this).css({ - 'text-align': textAlign || '' - }) - }); - - //修饰表格 - body.find('table').addClass('layui-table'); - - //移除不安全的标签 - body.find('script,link').remove(); - } - - //Range对象兼容性处理 - ,Range = function(iframeDOM){ - return iframeDOM.selection - ? iframeDOM.selection.createRange() - : iframeDOM.getSelection().getRangeAt(0); - } - - //当前Range对象的endContainer兼容性处理 - ,getContainer = function(range){ - return range.endContainer || range.parentElement().childNodes[0] - } - - //在选区插入内联元素 - ,insertInline = function(tagName, attr, range){ - var iframeDOM = this.document - ,elem = document.createElement(tagName) - for(var key in attr){ - elem.setAttribute(key, attr[key]); - } - elem.removeAttribute('text'); - - if(iframeDOM.selection){ //IE - var text = range.text || attr.text; - if(tagName === 'a' && !text) return; - if(text){ - elem.innerHTML = text; - } - range.pasteHTML($(elem).prop('outerHTML')); - range.select(); - } else { //非IE - var text = range.toString() || attr.text; - if(tagName === 'a' && !text) return; - if(text){ - elem.innerHTML = text; - } - range.deleteContents(); - range.insertNode(elem); - } - } - - //工具选中 - ,toolCheck = function(tools, othis){ - var iframeDOM = this.document - ,CHECK = 'layedit-tool-active' - ,container = getContainer(Range(iframeDOM)) - ,item = function(type){ - return tools.find('.layedit-tool-'+type) - } - - if(othis){ - othis[othis.hasClass(CHECK) ? 'removeClass' : 'addClass'](CHECK); - } - - tools.find('>i').removeClass(CHECK); - item('unlink').addClass(ABLED); - - $(container).parents().each(function(){ - var tagName = this.tagName.toLowerCase() - ,textAlign = this.style.textAlign; - - //文字 - if(tagName === 'b' || tagName === 'strong'){ - item('b').addClass(CHECK) - } - if(tagName === 'i' || tagName === 'em'){ - item('i').addClass(CHECK) - } - if(tagName === 'u'){ - item('u').addClass(CHECK) - } - if(tagName === 'strike'){ - item('d').addClass(CHECK) - } - - //对齐 - if(tagName === 'p'){ - if(textAlign === 'center'){ - item('center').addClass(CHECK); - } else if(textAlign === 'right'){ - item('right').addClass(CHECK); - } else { - item('left').addClass(CHECK); - } - } - - //超链接 - if(tagName === 'a'){ - item('link').addClass(CHECK); - item('unlink').removeClass(ABLED); - } - }); - } - - //触发工具 - ,toolActive = function(iframeWin, editor, set){ - var iframeDOM = iframeWin.document - ,body = $(iframeDOM.body) - ,toolEvent = { - //超链接 - link: function(range){ - var container = getContainer(range) - ,parentNode = $(container).parent(); - - link.call(body, { - href: parentNode.attr('href') - ,target: parentNode.attr('target') - }, function(field){ - var parent = parentNode[0]; - if(parent.tagName === 'A'){ - parent.href = field.url; - } else { - insertInline.call(iframeWin, 'a', { - target: field.target - ,href: field.url - ,text: field.url - }, range); - } - }); - } - //清除超链接 - ,unlink: function(range){ - iframeDOM.execCommand('unlink'); - } - //插入代码 - ,code: function(range){ - code.call(body, function(pre){ - insertInline.call(iframeWin, 'pre', { - text: pre.code - ,'lay-lang': pre.lang - }, range); - }); - } - //帮助 - ,help: function(){ - layer.open({ - type: 2 - ,title: '帮助' - ,area: ['600px', '380px'] - ,shadeClose: true - ,shade: 0.1 - ,skin: 'layui-layer-msg' - ,content: ['', 'no'] - }); - } - } - ,tools = editor.find('.layui-layedit-tool') - - ,click = function(){ - var othis = $(this) - ,events = othis.attr('layedit-event') - ,command = othis.attr('lay-command'); - - if(othis.hasClass(ABLED)) return; - - body.focus(); - - var range = Range(iframeDOM) - ,container = range.commonAncestorContainer - - if(command){ - iframeDOM.execCommand(command); - if(/justifyLeft|justifyCenter|justifyRight/.test(command)){ - iframeDOM.execCommand('formatBlock', false, '
'); - } - setTimeout(function(){ - body.focus(); - }, 10); - } else { - toolEvent[events] && toolEvent[events].call(this, range); - } - toolCheck.call(iframeWin, tools, othis); - } - - ,isClick = /image/ - - tools.find('>i').on('mousedown', function(){ - var othis = $(this) - ,events = othis.attr('layedit-event'); - if(isClick.test(events)) return; - click.call(this) - }).on('click', function(){ - var othis = $(this) - ,events = othis.attr('layedit-event'); - if(!isClick.test(events)) return; - click.call(this) - }); - - //触发内容区域 - body.on('click', function(){ - toolCheck.call(iframeWin, tools); - }); - } - - //超链接面板 - ,link = function(options, callback){ - var body = this, index = layer.open({ - type: 1 - ,id: 'LAY_layedit_link' - ,area: '350px' - ,shade: 0.05 - ,shadeClose: true - ,moveType: 1 - ,title: '超链接' - ,skin: 'layui-layer-msg' - ,content: ['