refactor(colorpicker): 使用 component 模块重构组件 (#2858)

* refactor(colorpicker): 使用 component 模块重构组件

* docs(colorpicker): 添加 component 基础接口说明

* chore: 剔除多余代码
pull/2884/head
贤心 2025-10-20 11:50:50 +08:00 committed by GitHub
parent 22d3b5ffad
commit a5678e2ef0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 256 additions and 281 deletions

View File

@ -2,7 +2,7 @@
title: 颜色选择器 colorpicker
toc: true
---
# 颜色选择器
> 颜色选择器 `colorpicker` 用于对颜色的快捷选择,支持 `hex,rgb,rgba` 三种颜色类型。
@ -20,6 +20,7 @@ toc: true
| API | 描述 |
| --- | --- |
| var colorpicker = layui.colorpicker | 获得 `colorpicker` 模块。 |
| [基础接口](../component/#export) <sup>2.13+</sup> | 该组件由 `component` 构建,因此继承其提供的基础接口。|
| [colorpicker.render(options)](#render) | colorpicker 组件渲染,核心方法。 |
@ -35,7 +36,7 @@ toc: true
<div class="class-test-colorpicker" lay-options="{color: '#333'}"></div>
<div class="class-test-colorpicker" lay-options="{color: '#777'}"></div>
<!-- import layui -->
<!-- import layui -->
<script>
layui.use(function(){
var colorpicker = layui.colorpicker;

View File

@ -3,63 +3,125 @@
* 颜色选择组件
*/
layui.define(['i18n', 'jquery', 'lay'], function(exports) {
layui.define(['i18n', 'component'], function(exports) {
"use strict";
var $ = layui.$;
var lay = layui.lay;
var hint = layui.hint();
var i18n = layui.i18n;
var device = layui.device();
var clickOrMousedown = (device.mobile ? 'click' : 'mousedown');
// 外部接口
var colorpicker = {
config: {},
index: layui.colorpicker ? (layui.colorpicker.index + 10000) : 0,
// 创建组件
var component = layui.component({
name: 'colorpicker',
// 设置全局项
set: function(options) {
var that = this;
that.config = $.extend({}, that.config, options);
return that;
// 默认配置
config: {
color: '', // 默认颜色,默认没有
size: null, // 选择器大小
alpha: false, // 是否开启透明度
format: 'hex', // 颜色显示/输入格式,可选 rgb,hex
predefine: false, // 预定义颜色是否开启
colors: [ // 默认预定义颜色列表
'#16baaa', '#16b777', '#1E9FFF', '#FF5722', '#FFB800',
'#01AAED', '#999', '#c00', '#ff8c00','#ffd700',
'#90ee90', '#00ced1', '#1e90ff', '#c71585', '#393D49',
'rgb(0, 186, 189)', 'rgb(255, 120, 0)', 'rgb(250, 212, 0)',
'rgba(0,0,0,.5)', 'rgba(255, 69, 0, 0.68)',
'rgba(144, 240, 144, 0.5)', 'rgba(31, 147, 255, 0.73)'
]
},
// 事件
on: function(events, callback) {
return layui.onevent.call(this, 'colorpicker', events, callback);
CONST: {
ELEM: 'layui-colorpicker',
ELEM_MAIN: '.layui-colorpicker-main',
ICON_PICKER_DOWN: 'layui-icon-down',
ICON_PICKER_CLOSE: 'layui-icon-close',
PICKER_TRIG_SPAN: 'layui-colorpicker-trigger-span',
PICKER_TRIG_I: 'layui-colorpicker-trigger-i',
PICKER_SIDE: 'layui-colorpicker-side',
PICKER_SIDE_SLIDER: 'layui-colorpicker-side-slider',
PICKER_BASIS: 'layui-colorpicker-basis',
PICKER_ALPHA_BG: 'layui-colorpicker-alpha-bgcolor',
PICKER_ALPHA_SLIDER: 'layui-colorpicker-alpha-slider',
PICKER_BASIS_CUR: 'layui-colorpicker-basis-cursor',
PICKER_INPUT: 'layui-colorpicker-main-input'
},
// 初始化之前
beforeInit: function() {
var that = this;
that.stopClickOutsideEvent = $.noop;
that.stopResizeEvent = $.noop;
CONST.PICKER_OPENED = CONST.MOD_ID + '-opened';
},
// 渲染之前
beforeRender: function() {
var that = this;
var options = that.config;
options.target = $('body'); // 后续考虑开放 target 元素
},
// 渲染
render: function() {
var that = this;
var options = that.config;
// 颜色选择框对象
var elemColorBox = $(['<div class="layui-unselect layui-colorpicker">',
'<span '+ (options.format == 'rgb' && options.alpha
? 'class="layui-colorpicker-trigger-bgcolor"'
: '') +'>',
'<span class="layui-colorpicker-trigger-span" ',
'lay-type="'+ (options.format == 'rgb' ? (options.alpha ? 'rgba' : 'torgb') : '') +'" ',
'style="'+ function() {
var bgstr = '';
if(options.color){
bgstr = options.color;
if((options.color.match(/[0-9]{1,3}/g) || []).length > 3){ //需要优化
if(!(options.alpha && options.format == 'rgb')){
bgstr = '#' + HSBToHEX(RGBToHSB(RGBSTo(options.color)))
}
}
return 'background: '+ bgstr;
}
return bgstr;
}() +'">',
'<i class="layui-icon layui-colorpicker-trigger-i '+ (options.color
? CONST.ICON_PICKER_DOWN
: CONST.ICON_PICKER_CLOSE) +'"></i>',
'</span>',
'</span>',
'</div>'].join(''));
// 初始化颜色选择框尺寸
var elem = options.elem;
options.size && elemColorBox.addClass('layui-colorpicker-'+ options.size);
// 插入颜色选择框
elem.addClass('layui-inline').html(
that.elemColorBox = elemColorBox
);
// 获取背景色值
that.color = that.elemColorBox.find('.'+ CONST.PICKER_TRIG_SPAN)[0].style.background;
}
};
});
// 操作当前实例
var thisModule = function() {
var that = this;
var options = that.config;
var id = options.id;
thisModule.that[id] = that; // 记录当前实例对象
return {
config: options
};
}
//字符常量
,MOD_NAME = 'colorpicker', SHOW = 'layui-show', THIS = 'layui-this', ELEM = 'layui-colorpicker'
,ELEM_MAIN = '.layui-colorpicker-main', ICON_PICKER_DOWN = 'layui-icon-down', ICON_PICKER_CLOSE = 'layui-icon-close'
,PICKER_TRIG_SPAN = 'layui-colorpicker-trigger-span', PICKER_TRIG_I = 'layui-colorpicker-trigger-i', PICKER_SIDE = 'layui-colorpicker-side', PICKER_SIDE_SLIDER = 'layui-colorpicker-side-slider'
,PICKER_BASIS = 'layui-colorpicker-basis', PICKER_ALPHA_BG = 'layui-colorpicker-alpha-bgcolor', PICKER_ALPHA_SLIDER = 'layui-colorpicker-alpha-slider', PICKER_BASIS_CUR = 'layui-colorpicker-basis-cursor', PICKER_INPUT = 'layui-colorpicker-main-input'
//RGB转HSB
,RGBToHSB = function(rgb){
// RGB 转 HSB
var RGBToHSB = function(rgb) {
var hsb = {h:0, s:0, b:0};
var min = Math.min(rgb.r, rgb.g, rgb.b);
var max = Math.max(rgb.r, rgb.g, rgb.b);
var delta = max - min;
hsb.b = max;
hsb.s = max !== 0 ? 255*delta/max : 0;
if(hsb.s !== 0){
if (hsb.s !== 0) {
if(rgb.r == max){ // 因 rgb 中返回的数字为 string 类型
hsb.h = (rgb.g - rgb.b) / delta;
}else if(rgb.g == max){
@ -67,10 +129,10 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
}else{
hsb.h = 4 + (rgb.r - rgb.g) / delta;
}
}else{
} else {
hsb.h = -1;
}
if(max === min){
if (max === min) {
hsb.h = 0;
}
hsb.h *= 60;
@ -80,29 +142,30 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
hsb.s *= 100/255;
hsb.b *= 100/255;
return hsb;
}
};
//HEX转HSB
,HEXToHSB = function(hex){
// HEX HSB
var HEXToHSB = function(hex) {
hex = hex.indexOf('#') > -1 ? hex.substring(1) : hex;
if(hex.length === 3){
if (hex.length === 3) {
var num = hex.split("");
hex = num[0]+num[0]+num[1]+num[1]+num[2]+num[2]
}
hex = parseInt(hex, 16);
var rgb = {r:hex >> 16, g:(hex & 0x00FF00) >> 8, b:(hex & 0x0000FF)};
return RGBToHSB(rgb);
}
};
//HSB转RGB
,HSBToRGB = function(hsb){
// HSB RGB
var HSBToRGB = function(hsb) {
var rgb = {};
var h = hsb.h;
var s = hsb.s*255/100;
var b = hsb.b*255/100;
if(s === 0){
if (s === 0) {
rgb.r = rgb.g = rgb.b = b;
}else{
} else {
var t1 = b;
var t2 = (255 - s) * b /255;
var t3 = (t1 - t2) * (h % 60) /60;
@ -116,10 +179,10 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
else {rgb.r=0; rgb.g=0; rgb.b=0}
}
return {r:Math.round(rgb.r), g:Math.round(rgb.g), b:Math.round(rgb.b)};
}
};
//HSB转HEX
,HSBToHEX = function(hsb){
// HSB HEX
var HSBToHEX = function(hsb) {
var rgb = HSBToRGB(hsb);
var hex = [
rgb.r.toString(16)
@ -132,117 +195,33 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
}
});
return hex.join('');
}
};
//转化成所需rgb格式
,RGBSTo = function(rgbs){
// 转化成所需 rgb 格式
var RGBSTo = function(rgbs){
var regexp = /[0-9]{1,3}/g;
var re = rgbs.match(regexp) || [];
return {r:re[0], g:re[1], b:re[2]};
}
,$win = $(window)
,$doc = $(document)
//构造器
,Class = function(options){
var that = this;
that.index = ++colorpicker.index;
that.config = $.extend({}, that.config, colorpicker.config, options);
that.render();
};
//默认配置
Class.prototype.config = {
color: '' //默认颜色,默认没有
,size: null //选择器大小
,alpha: false //是否开启透明度
,format: 'hex' //颜色显示/输入格式,可选 rgb,hex
,predefine: false //预定义颜色是否开启
,colors: [ //默认预定义颜色列表
'#16baaa', '#16b777', '#1E9FFF', '#FF5722', '#FFB800', '#01AAED', '#999', '#c00', '#ff8c00','#ffd700'
,'#90ee90', '#00ced1', '#1e90ff', '#c71585', 'rgb(0, 186, 189)', 'rgb(255, 120, 0)', 'rgb(250, 212, 0)', '#393D49', 'rgba(0,0,0,.5)', 'rgba(255, 69, 0, 0.68)', 'rgba(144, 240, 144, 0.5)', 'rgba(31, 147, 255, 0.73)'
]
};
var $win = $(window);
var $doc = $(document);
//初始颜色选择框
Class.prototype.render = function(){
var CONST = component.CONST;
/**
* 扩展组件原型方法
*/
var Class = component.Class;
// 渲染颜色选择器
Class.prototype.renderPicker = function(){
var that = this;
var options = that.config;
// 若 elem 非唯一,则拆分为多个实例
var elem = $(options.elem);
if(elem.length > 1){
layui.each(elem, function(){
colorpicker.render($.extend({}, options, {
elem: this
}));
});
return that;
}
// 合并 lay-options 属性上的配置信息
$.extend(options, lay.options(elem[0]));
//颜色选择框对象
var elemColorBox = $(['<div class="layui-unselect layui-colorpicker">'
,'<span '+ (options.format == 'rgb' && options.alpha
? 'class="layui-colorpicker-trigger-bgcolor"'
: '') +'>'
,'<span class="layui-colorpicker-trigger-span" '
,'lay-type="'+ (options.format == 'rgb' ? (options.alpha ? 'rgba' : 'torgb') : '') +'" '
,'style="'+ function(){
var bgstr = '';
if(options.color){
bgstr = options.color;
if((options.color.match(/[0-9]{1,3}/g) || []).length > 3){ //需要优化
if(!(options.alpha && options.format == 'rgb')){
bgstr = '#' + HSBToHEX(RGBToHSB(RGBSTo(options.color)))
}
}
return 'background: '+ bgstr;
}
return bgstr;
}() +'">'
,'<i class="layui-icon layui-colorpicker-trigger-i '+ (options.color
? ICON_PICKER_DOWN
: ICON_PICKER_CLOSE) +'"></i>'
,'</span>'
,'</span>'
,'</div>'].join(''))
//初始化颜色选择框
elem = options.elem = $(options.elem);
options.size && elemColorBox.addClass('layui-colorpicker-'+ options.size); //初始化颜色选择框尺寸
// 插入颜色选择框
elem.addClass('layui-inline').html(
that.elemColorBox = elemColorBox
);
// 初始化 id 属性 - 优先取 options > 元素 id > 自增索引
options.id = 'id' in options ? options.id : (
elem.attr('id') || that.index
);
// 获取背景色值
that.color = that.elemColorBox.find('.'+ PICKER_TRIG_SPAN)[0].style.background;
// 相关事件
that.events();
};
//渲染颜色选择器
Class.prototype.renderPicker = function(){
var that = this
,options = that.config
,elemColorBox = that.elemColorBox[0]
//颜色选择器对象
,elemPicker = that.elemPicker = $(['<div id="layui-colorpicker'+ that.index +'" data-index="'+ that.index +'" class="layui-anim layui-anim-downbit layui-colorpicker-main">'
// 颜色选择器对象
var elemPicker = that.elemPicker = $(['<div id="layui-colorpicker'+ that.index +'" data-index="'+ that.index +'" class="layui-anim layui-anim-downbit layui-colorpicker-main">'
//颜色面板
,'<div class="layui-colorpicker-main-wrapper">'
,'<div class="layui-colorpicker-basis">'
@ -256,7 +235,7 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
,'</div>'
//透明度条块
,'<div class="layui-colorpicker-main-alpha '+ (options.alpha ? SHOW : '') +'">'
,'<div class="layui-colorpicker-main-alpha '+ (options.alpha ? CONST.CLASS_SHOW : '') +'">'
,'<div class="layui-colorpicker-alpha-bgcolor">'
,'<div class="layui-colorpicker-alpha-slider"></div>'
,'</div>'
@ -290,37 +269,30 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
,'<button style="border-radius: 0; border-left: 0" class="layui-btn layui-btn-primary layui-btn-sm" colorpicker-events="confirm">' + i18n.$t('colorpicker.confirm') + '</button>'
,'</div'
,'</div>'
,'</div>'].join(''))
,'</div>'].join(''));
,elemColorBoxSpan = that.elemColorBox.find('.' + PICKER_TRIG_SPAN)[0];
//如果当前点击的颜色盒子已经存在选择器,则关闭
if($(ELEM_MAIN)[0] && $(ELEM_MAIN).data('index') == that.index){
that.removePicker(Class.thisElemInd);
} else { //插入颜色选择器
that.removePicker(Class.thisElemInd);
$('body').append(elemPicker);
}
// 记录当前执行的实例索引
colorpicker.thisId = options.id;
Class.thisElemInd = that.index; //记录最新打开的选择器索引
Class.thisColor = elemColorBox.style.background //记录最新打开的选择器颜色选中值
that.removePicker(options.id); // 若已存在则先移除
options.target.append(elemPicker);
options.elem.data(CONST.PICKER_OPENED, true); // 面板已打开的标记
that.position();
that.pickerEvents();
that.onClickOutside();
that.autoUpdatePosition();
};
//颜色选择器移除
// 颜色选择器移除
Class.prototype.removePicker = function(index){
var that = this;
var options = that.config;
var elem = $('#layui-colorpicker'+ (index || that.index));
if(elem[0]){
that.stopClickOutsideEvent();
that.stopResizeEvent();
if (elem[0]) {
elem.remove();
delete colorpicker.thisId;
options.elem.removeData(CONST.PICKER_OPENED);
// 面板关闭后的回调
typeof options.close === 'function' && options.close(that.color);
@ -329,35 +301,34 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
return that;
};
//定位算法
// 面板定位
Class.prototype.position = function(){
var that = this
,options = that.config;
var that = this;
var options = that.config;
lay.position(that.bindElem || that.elemColorBox[0], that.elemPicker[0], {
position: options.position
,align: 'center'
position: options.position,
align: 'center'
});
return that;
};
//颜色选择器赋值
// 颜色选择器赋值
Class.prototype.val = function(){
var that = this
,options = that.config
,elemColorBox = that.elemColorBox.find('.' + PICKER_TRIG_SPAN)
,elemPickerInput = that.elemPicker.find('.' + PICKER_INPUT)
,elemColorBox = that.elemColorBox.find('.' + CONST.PICKER_TRIG_SPAN)
,elemPickerInput = that.elemPicker.find('.' + CONST.PICKER_INPUT)
,e = elemColorBox[0]
,bgcolor = e.style.backgroundColor;
//判断是否有背景颜色
// 判断是否有背景颜色
if(bgcolor){
// 转化成 hsb 格式
var hsb = RGBToHSB(RGBSTo(bgcolor));
var type = elemColorBox.attr('lay-type');
//转化成hsb格式
var hsb = RGBToHSB(RGBSTo(bgcolor))
,type = elemColorBox.attr('lay-type');
//同步滑块的位置及颜色选择器的选择
// 同步滑块的位置及颜色选择器的选择
that.select(hsb.h, hsb.s, hsb.b);
// 若格式要求为rgb
@ -369,15 +340,15 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
// 若开启透明度而没有设置,则给默认值
if((bgcolor.match(/[0-9]{1,3}/g) || []).length === 3){
elemPickerInput.find('input').val('rgba('+ rgb.r +', '+ rgb.g +', '+ rgb.b +', 1)');
that.elemPicker.find('.'+ PICKER_ALPHA_SLIDER).css("left", 280);
that.elemPicker.find('.'+ CONST.PICKER_ALPHA_SLIDER).css("left", 280);
} else {
elemPickerInput.find('input').val(bgcolor);
var left = bgcolor.slice(bgcolor.lastIndexOf(",") + 1, bgcolor.length - 1) * 280;
that.elemPicker.find('.'+ PICKER_ALPHA_SLIDER).css("left", left);
that.elemPicker.find('.'+ CONST.PICKER_ALPHA_SLIDER).css("left", left);
}
// 设置 span 背景色
that.elemPicker.find('.'+ PICKER_ALPHA_BG)[0].style.background = 'linear-gradient(to right, rgba('+ rgb.r +', '+ rgb.g +', '+ rgb.b +', 0), rgb('+ rgb.r +', '+ rgb.g +', '+ rgb.b +'))';
that.elemPicker.find('.'+ CONST.PICKER_ALPHA_BG)[0].style.background = 'linear-gradient(to right, rgba('+ rgb.r +', '+ rgb.g +', '+ rgb.b +', 0), rgb('+ rgb.r +', '+ rgb.g +', '+ rgb.b +'))';
} else {
elemPickerInput.find('input').val('#'+ HSBToHEX(hsb));
}
@ -385,41 +356,41 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
// 若没有背景颜色则默认到最初始的状态
that.select(0,100,100);
elemPickerInput.find('input').val("");
that.elemPicker.find('.'+ PICKER_ALPHA_BG)[0].style.background = '';
that.elemPicker.find('.'+ PICKER_ALPHA_SLIDER).css("left", 280);
that.elemPicker.find('.'+ CONST.PICKER_ALPHA_BG)[0].style.background = '';
that.elemPicker.find('.'+ CONST.PICKER_ALPHA_SLIDER).css("left", 280);
}
};
//颜色选择器滑动 / 点击
// 颜色选择器滑动 / 点击
Class.prototype.side = function(){
var that = this
,options = that.config
,span = that.elemColorBox.find('.' + PICKER_TRIG_SPAN)
,span = that.elemColorBox.find('.' + CONST.PICKER_TRIG_SPAN)
,type = span.attr('lay-type')
,side = that.elemPicker.find('.' + PICKER_SIDE)
,slider = that.elemPicker.find('.' + PICKER_SIDE_SLIDER)
,basis = that.elemPicker.find('.' + PICKER_BASIS)
,choose = that.elemPicker.find('.' + PICKER_BASIS_CUR)
,alphacolor = that.elemPicker.find('.' + PICKER_ALPHA_BG)
,alphaslider = that.elemPicker.find('.' + PICKER_ALPHA_SLIDER)
,side = that.elemPicker.find('.' + CONST.PICKER_SIDE)
,slider = that.elemPicker.find('.' + CONST.PICKER_SIDE_SLIDER)
,basis = that.elemPicker.find('.' + CONST.PICKER_BASIS)
,choose = that.elemPicker.find('.' + CONST.PICKER_BASIS_CUR)
,alphacolor = that.elemPicker.find('.' + CONST.PICKER_ALPHA_BG)
,alphaslider = that.elemPicker.find('.' + CONST.PICKER_ALPHA_SLIDER)
,_h = slider[0].offsetTop/180*360
,_b = 100 - (choose[0].offsetTop)/180*100
,_s = (choose[0].offsetLeft)/260*100
,_a = Math.round(alphaslider[0].offsetLeft/280*100)/100
,i = that.elemColorBox.find('.' + PICKER_TRIG_I)
,i = that.elemColorBox.find('.' + CONST.PICKER_TRIG_I)
,pre = that.elemPicker.find('.layui-colorpicker-pre').children('div')
,change = function(x,y,z,a){
that.select(x, y, z);
var rgb = HSBToRGB({h:x, s:y, b:z});
var color = HSBToHEX({h:x, s:y, b:z});
var elemInput = that.elemPicker.find('.' + PICKER_INPUT).find('input');
var elemInput = that.elemPicker.find('.' + CONST.PICKER_INPUT).find('input');
i.addClass(ICON_PICKER_DOWN).removeClass(ICON_PICKER_CLOSE);
i.addClass(CONST.ICON_PICKER_DOWN).removeClass(CONST.ICON_PICKER_CLOSE);
span[0].style.background = 'rgb('+ rgb.r +', '+ rgb.g +', '+ rgb.b +')';
if(type === 'torgb'){
@ -435,7 +406,7 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
}
//回调更改的颜色
options.change && options.change($.trim(that.elemPicker.find('.' + PICKER_INPUT).find('input').val()));
options.change && options.change($.trim(that.elemPicker.find('.' + CONST.PICKER_INPUT).find('input').val()));
}
//拖拽元素
@ -559,7 +530,7 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
needTrigger && alphaslider.trigger('mousedown', e);
});
//预定义颜色选择
// 预定义颜色选择
pre.each(function(){
$(this).on('click', function(){
$(this).parent('.layui-colorpicker-pre').addClass('selected').siblings().removeClass('selected');
@ -612,7 +583,7 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
}
};
//颜色选择器hsb转换
// 颜色选择器hsb转换
Class.prototype.select = function(h, s, b, type){
var that = this;
var options = that.config;
@ -621,13 +592,13 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
var sidetop = h/360*180;
var top = 180 - b/100*180;
var left = s/100*260;
var basisElem = that.elemPicker.find('.' + PICKER_BASIS)[0];
var basisElem = that.elemPicker.find('.' + CONST.PICKER_BASIS)[0];
that.elemPicker.find('.' + PICKER_SIDE_SLIDER).css("top", sidetop); //滑块的top
that.elemPicker.find('.' + CONST.PICKER_SIDE_SLIDER).css("top", sidetop); //滑块的top
basisElem.style.background = '#' + hex; //颜色选择器的背景
//选择器的top left
that.elemPicker.find('.' + PICKER_BASIS_CUR).css({
that.elemPicker.find('.' + CONST.PICKER_BASIS_CUR).css({
"top": top / basisElem.offsetHeight * 100 + '%',
"left": left / basisElem.offsetWidth * 100 + '%'
});
@ -635,21 +606,21 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
// if(type === 'change') return;
// 选中的颜色
// that.elemPicker.find('.' + PICKER_INPUT).find('input').val('#'+ color);
// that.elemPicker.find('.' + CONST.PICKER_INPUT).find('input').val('#'+ color);
};
Class.prototype.pickerEvents = function(){
var that = this
,options = that.config
var that = this;
var options = that.config;
,elemColorBoxSpan = that.elemColorBox.find('.' + PICKER_TRIG_SPAN) //颜色盒子
,elemPickerInput = that.elemPicker.find('.' + PICKER_INPUT + ' input') //颜色选择器表单
var elemColorBoxSpan = that.elemColorBox.find('.' + CONST.PICKER_TRIG_SPAN); //颜色盒子
var elemPickerInput = that.elemPicker.find('.' + CONST.PICKER_INPUT + ' input'); //颜色选择器表单
,pickerEvents = {
//清空
var pickerEvents = {
// 清空
clear: function(othis){
elemColorBoxSpan[0].style.background ='';
that.elemColorBox.find('.' + PICKER_TRIG_I).removeClass(ICON_PICKER_DOWN).addClass(ICON_PICKER_CLOSE);
that.elemColorBox.find('.' + CONST.PICKER_TRIG_I).removeClass(CONST.ICON_PICKER_DOWN).addClass(CONST.ICON_PICKER_CLOSE);
that.color = '';
options.done && options.done('');
@ -669,14 +640,14 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
if((value.match(/[0-9]{1,3}/g) || []).length > 3 && elemColorBoxSpan.attr('lay-type') === 'rgba'){
var left = value.slice(value.lastIndexOf(",") + 1, value.length - 1) * 280;
that.elemPicker.find('.' + PICKER_ALPHA_SLIDER).css("left", left);
that.elemPicker.find('.' + CONST.PICKER_ALPHA_SLIDER).css("left", left);
elemColorBoxSpan[0].style.background = value;
colorValue = value;
}
} else {
hsb = HEXToHSB(value);
elemColorBoxSpan[0].style.background = (colorValue = '#' + HSBToHEX(hsb));
that.elemColorBox.find('.' + PICKER_TRIG_I).removeClass(ICON_PICKER_CLOSE).addClass(ICON_PICKER_DOWN);
that.elemColorBox.find('.' + CONST.PICKER_TRIG_I).removeClass(CONST.ICON_PICKER_CLOSE).addClass(CONST.ICON_PICKER_DOWN);
}
if(change === 'change'){
@ -691,101 +662,104 @@ layui.define(['i18n', 'jquery', 'lay'], function(exports) {
}
};
//选择器面板点击事件
// 选择器面板点击事件
that.elemPicker.on('click', '*[colorpicker-events]', function(){
var othis = $(this)
,attrEvent = othis.attr('colorpicker-events');
pickerEvents[attrEvent] && pickerEvents[attrEvent].call(this, othis);
});
//输入框事件
// 输入框事件
elemPickerInput.on('keyup', function(e){
var othis = $(this);
pickerEvents.confirm.call(this, othis, e.keyCode === 13 ? null : 'change');
});
}
// 颜色选择器输入
Class.prototype.events = function(){
// 事件
Class.prototype.events = function() {
var that = this;
var options = that.config;
// 弹出颜色选择器
that.elemColorBox.on('click' , function(){
that.renderPicker();
if($(ELEM_MAIN)[0]){
that.elemColorBox.on('click' , function() {
// 主面板是否已打开
var opened = options.elem.data(CONST.PICKER_OPENED);
// 根据主面板状态,自动切换打开与关闭
if (opened) {
that.removePicker();
} else {
that.renderPicker();
that.val();
that.side();
}
});
};
//全局事件
(function(){
//绑定关闭控件事件
$doc.on(clickOrMousedown, function(e){
if(!colorpicker.thisId) return;
var that = thisModule.getThis(colorpicker.thisId);
if(!that) return;
/**
* 点击面板外部时的事件
*/
Class.prototype.onClickOutside = function() {
var that = this;
var options = that.config;
var options = that.config;
var elemColorBoxSpan = that.elemColorBox.find('.' + PICKER_TRIG_SPAN);
that.stopClickOutsideEvent();
//如果点击的元素是颜色框
if($(e.target).hasClass(ELEM)
|| $(e.target).parents('.'+ELEM)[0]
) return;
var stop = lay.onClickOutside(
that.elemPicker[0],
function(e) {
var elemColorBoxSpan = that.elemColorBox.find('.' + CONST.PICKER_TRIG_SPAN);
//如果点击的元素是选择器
if($(e.target).hasClass(ELEM_MAIN.replace(/\./g, ''))
|| $(e.target).parents(ELEM_MAIN)[0]
) return;
if (that.color) {
var hsb = RGBToHSB(RGBSTo(that.color));
that.select(hsb.h, hsb.s, hsb.b);
} else {
that.elemColorBox.find('.' + CONST.PICKER_TRIG_I).removeClass(CONST.ICON_PICKER_DOWN).addClass(CONST.ICON_PICKER_CLOSE);
}
if(!that.elemPicker) return;
elemColorBoxSpan[0].style.background = that.color || '';
if(that.color){
var hsb = RGBToHSB(RGBSTo(that.color));
that.select(hsb.h, hsb.s, hsb.b);
} else {
that.elemColorBox.find('.' + PICKER_TRIG_I).removeClass(ICON_PICKER_DOWN).addClass(ICON_PICKER_CLOSE);
// 取消选择的回调
typeof options.cancel === 'function' && options.cancel(that.color);
// 移除面板
that.removePicker();
},
{
ignore: [options.elem[0]],
event: clickOrMousedown,
capture: false
}
elemColorBoxSpan[0].style.background = that.color || '';
);
// 取消选择的回调
typeof options.cancel === 'function' && options.cancel(that.color);
that.stopClickOutsideEvent = function(){
stop();
that.stopClickOutsideEvent = $.noop;
}
};
// 移除面板
that.removePicker();
});
/**
* 窗口大小变化时自动更新位置
*/
Class.prototype.autoUpdatePosition = function() {
var that = this;
var options = that.config;
var RESIZE_EVENT_NAME = 'resize.lay_colorpicker_resize';
//自适应定位
$win.on('resize', function(){
if(!colorpicker.thisId) return;
var that = thisModule.getThis(colorpicker.thisId);
if(!that) return;
that.stopResizeEvent();
if(!that.elemPicker || !$(ELEM_MAIN)[0]){
return false;
}
var windowResizeHandler = function() {
that.position();
});
})();
};
// 记录所有实例
thisModule.that = {}; // 记录所有实例对象
$win.on(RESIZE_EVENT_NAME, windowResizeHandler);
// 获取当前实例对象
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;
that.stopResizeEvent = function() {
$win.off(RESIZE_EVENT_NAME, windowResizeHandler);
that.stopResizeEvent = $.noop;
};
};
//核心入口
colorpicker.render = function(options){
var inst = new Class(options);
return thisModule.call(inst);
};
exports(MOD_NAME, colorpicker);
exports(CONST.MOD_NAME, component);
});