-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcolorpicker.js
142 lines (125 loc) · 3.76 KB
/
colorpicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* 颜色拾取器插件
* @author Ly
* @date 2013/01/07
*/
;Neter.namespace('Neter.ColorPicker');
/**
* @class
* @name Neter.ColorPicker
* @require farbtastic
* @see <a href="http://acko.net/blog/farbtastic-jquery-color-picker-plug-in/">Farbtastic</a>
* @param {Object} options 自定义配置信息,默认配置信息如下:
<pre>
options = {
container : $('body'), // 插件容器,默认为body
acceptor : null, // 颜色接收器
width : 150, // 插件宽度,默认150px
defaultColor : '#808080' // 默认颜色,默认#808080
}
</pre>
*/
;Neter.ColorPicker = function(options) {
var _this = this;
this.defaults = {
container : $('body'),
acceptor : null,
width : 150,
defaultColor : '#FFF'
};
Neter.apply(this.defaults, options);
this.handler = {
container : this.defaults.container,
colorPicker : null,
farbtastic : null
};
this.defaults.container = null;
/** @ignore */
this.method = {
/** @ignore */
create : function() {
var defaults = _this.defaults,
handler = _this.handler;
handler.colorPicker = $('<div></div>').addClass('neter-color-picker').appendTo(handler.container);
handler.farbtastic = handler.colorPicker.farbtastic({
callback : defaults.acceptor,
width : defaults.width
}).setColor(defaults.defaultColor);
return this;
},
/** @ignore */
initLayout : function() {
return this;
},
/** @ignore */
bindEvents : function() {
var defaults = _this.defaults,
handler = _this.handler;
defaults.acceptor
.on('click', function(event) {
event.stopPropagation();
_this.show();
});
handler.colorPicker
.on('click', function(event) {
event.stopPropagation();
});
handler.container
.on('click', function() {
_this.hide();
});
}
};
};
;Neter.apply(Neter.ColorPicker.prototype, {
/**
* 渲染插件
* @function
* @name Neter.ColorPicker.prototype.render
* @return {Neter.ColorPicker} 返回插件引用
*/
render : function() {
this.method.create().initLayout().bindEvents();
return this;
},
/**
* 隐藏插件
* @function
* @name Neter.ColorPicker.prototype.hide
* @return {Neter.ColorPicker} 返回插件引用
*/
hide : function() {
this.handler.colorPicker.hide();
return this;
},
/**
* 显示插件
* @function
* @name Neter.ColorPicker.prototype.show
* @return {Neter.ColorPicker} 返回插件引用
*/
show : function() {
var handler = this.handler,
acceptor = this.defaults.acceptor,
offset = acceptor.offset();
handler.colorPicker.css({
top : offset.top + acceptor.outerHeight(),
left : offset.left
}).show();
return this;
},
/**
* 设置颜色
* @function
* @name Neter.ColorPicker.prototype.setColor
* @param {String} color 16进制的颜色字符串
* @return {Neter.ColorPicker} 返回插件引用
*/
setColor : function(color) {
if (/^#[0-9a-fA-F]+/.test(color)) {
this.defaults.defaultColor = color;
this.handler.farbtastic.setColor(color);
}
return this;
}
});