forked from rachel-carvalho/simple-color-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.simple-color-picker.js
100 lines (83 loc) · 3.79 KB
/
jquery.simple-color-picker.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
$.fn.simpleColorPicker = function(options) {
var defaults = {
colorsPerLine: 11,
colors: ['#ffffff', '#000000', '#eeece1', '#1f497d', '#4f81bd', '#c0504d', '#9bbb59', '#8064a2', '#4bacc6', '#f79646', '#ffff00',
'#f2f2f2', '#7f7f7f', '#ddd9c3', '#c6d9f0', '#dbe5f1', '#f2dcdb', '#ebf1dd', '#e5e0ec', '#dbeef3', '#fdeada', '#fff2ca',
'#d8d8d8', '#595959', '#c4bd97', '#8db3e2', '#b8cce4', '#e5b9b7', '#d7e3bc', '#ccc1d9', '#b7dde8', '#fbd5b5', '#ffe694',
'#bfbfbf', '#3f3f3f', '#938953', '#548dd4', '#95b3d7', '#d99694', '#c3d69b', '#b2a2c7', '#b7dde8', '#fac08f', '#f2c314',
'#a5a5a5', '#262626', '#494429', '#17365d', '#366092', '#953734', '#76923c', '#5f497a', '#92cddc', '#e36c09', '#c09100'],
showEffect: '',
hideEffect: '',
onChangeColor: false
};
var opts = $.extend(defaults, options);
return this.each(function() {
var txt = $(this);
var colorsMarkup = '';
var prefix = txt.attr('id').replace(/-/g, '') + '_';
for(var i = 0; i < opts.colors.length; i++){
var item = opts.colors[i];
var breakLine = '';
if (i % opts.colorsPerLine == 0)
breakLine = 'clear: both; ';
if (i > 0 && breakLine && $.browser && $.browser.msie && $.browser.version <= 7) {
breakLine = '';
colorsMarkup += '<li style="float: none; clear: both; overflow: hidden; background-color: #fff; display: block; height: 1px; line-height: 1px; font-size: 1px; margin-bottom: -2px;"></li>';
}
colorsMarkup += '<li id="' + prefix + 'color-' + i + '" class="color-box" style="' + breakLine + 'background-color: ' + item + '" title="' + item + '"></li>';
}
var box = $('<div id="' + prefix + 'color-picker" class="color-picker" style="position: absolute; left: 0px; top: 0px;"><ul>' + colorsMarkup + '</ul><div style="clear: both;"></div></div>');
$('body').append(box);
box.hide();
box.find('li.color-box').click(function() {
if (txt.is('input')) {
txt.val(opts.colors[this.id.substr(this.id.indexOf('-') + 1)]);
txt.blur();
}
if ($.isFunction(defaults.onChangeColor)) {
defaults.onChangeColor.call(txt, opts.colors[this.id.substr(this.id.indexOf('-') + 1)]);
}
hideBox(box);
});
$('body').live('click', function() {
hideBox(box);
});
box.click(function(event) {
event.stopPropagation();
});
var positionAndShowBox = function(box) {
var pos = txt.offset();
var left = pos.left + txt.outerWidth() - box.outerWidth();
if (left < pos.left) left = pos.left;
box.css({ left: left, top: (pos.top + txt.outerHeight()) });
showBox(box);
}
txt.click(function(event) {
event.stopPropagation();
if (!txt.is('input')) {
// element is not an input so probably a link or div which requires the color box to be shown
positionAndShowBox(box);
}
});
txt.focus(function() {
positionAndShowBox(box);
});
function hideBox(box) {
if (opts.hideEffect == 'fade')
box.fadeOut();
else if (opts.hideEffect == 'slide')
box.slideUp();
else
box.hide();
}
function showBox(box) {
if (opts.showEffect == 'fade')
box.fadeIn();
else if (opts.showEffect == 'slide')
box.slideDown();
else
box.show();
}
});
};