forked from PitPik/colorPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_temp.js
More file actions
174 lines (154 loc) · 5.08 KB
/
Copy path_temp.js
File metadata and controls
174 lines (154 loc) · 5.08 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* [kolor-picker]{@link https://github.com/emn178/kolor-picker}
*
* @version 0.2.0
* @author Yi-Cyuan Chen [emn178@gmail.com]
* @copyright Yi-Cyuan Chen 2015-2016
* @license MIT
*/
(function ($) {
'use strict';
var KEY = 'kolor-picker';
var wrapper;
function Wrapper(element, colorPicker) {
this.element = element;
this.colorPicker = colorPicker;
this.previewElement = $('<div class="kolor-picker"><div class="sampler"></div><div class="preview-block"><input type="text"/><div class="preview-wrapper"><div class="preview" /></div></div></div>');
this.element.append(this.previewElement);
var elements = {
preview: this.previewElement.find('.preview'),
input: this.previewElement.find('input'),
sampler: this.previewElement.find('.sampler'),
alpha: this.element.find('.cp-alpha')
};
this.elements = elements;
elements.sampler.click(this.enableSampler.bind(this));
this.sampling = false;
this.lastToggled = false;
}
Wrapper.prototype.enableSampler = function () {
if (!this.kolorPicker.canvas) {
return;
}
this.kolorPicker.canvas.colorSampler('enable');
this.sampling = true;
this.colorPicker.toggle(false);
};
Wrapper.prototype.setKolorPicker = function (kolorPicker) {
this.kolorPicker = kolorPicker;
this.element.attr('data-theme', kolorPicker.theme);
this.elements.sampler.toggle(!!kolorPicker.canvas);
this.elements.alpha.toggle(kolorPicker.options.opacity !== false);
if (kolorPicker.options.doRender === undefined) {
this.colorPicker.color.options.doRender = true;
} else {
this.colorPicker.color.options.doRender = kolorPicker.options.doRender;
}
};
Wrapper.prototype.getColor = function () {
return this.color.toString();
// var rgb = this.colorPicker.color.colors.rgb;
// return 'rgba(' + [parseInt(rgb.r * 255), parseInt(rgb.g * 255), parseInt(rgb.b * 255), this.colorPicker.color.colors.alpha.toFixed(2)].join(',') + ')';
};
Wrapper.prototype.updateColor = function () {
var color = this.getColor();
this.elements.preview.css('background-color', color);
this.elements.input.val(color);
this.kolorPicker.changeColor(color);
};
Wrapper.prototype.render = function (toggled) {
if (toggled === undefined) {
this.updateColor();
} else if (this.lastToggled === toggled) {
return;
}
this.lastToggled = toggled;
if (toggled === false) {
if (!this.sampling) {
var color = this.getColor();
this.kolorPicker.selectColor(color);
}
} else {
this.updateColor();
}
};
function KolorPicker(element, options) {
this.element = element;
this.options = options || {};
this.canvas = this.options.canvas;
this.theme = this.options.theme || $.kolorPicker.theme;
if (this.canvas) {
this.canvas = $(this.canvas);
this.canvas.colorSampler().colorSampler('disable')
.bind('sampler:preview', this.onSamplerPreview.bind(this))
.bind('sampler:select', this.onSamplerSelect.bind(this));
}
}
KolorPicker.prototype.onSamplerSelect = function (e, color) {
if (wrapper.kolorPicker != this) {
return;
}
wrapper.sampling = false;
this.canvas.colorSampler('disable');
this.setColor(color);
color = wrapper.getColor();
this.selectColor(color);
};
KolorPicker.prototype.onSamplerPreview = function (e, color) {
if (wrapper.kolorPicker != this) {
return;
}
this.element.css('background-color', color);
color = wrapper.getColor();
this.changeColor(color);
};
KolorPicker.prototype.selectColor = function (color) {
if ($.isFunction(this.options.onSelect)) {
this.options.onSelect.call(this.element, color);
}
};
KolorPicker.prototype.changeColor = function (color) {
if ($.isFunction(this.options.onChange)) {
this.options.onChange.call(this.element, color);
}
};
KolorPicker.prototype.setColor = function (color) {
wrapper.colorPicker.color.setColor(color);
wrapper.colorPicker.render();
};
var KolorPickerOptions = {
buildCallback: function (element) {
wrapper = new Wrapper(element, this);
},
renderCallback: function (element, toggled) {
wrapper.setKolorPicker($(element).data(KEY));
wrapper.render(toggled);
}
};
var PublicMethods = ['setColor'];
$.fn.kolorPicker = function (options) {
if (typeof (options) == 'string') {
if ($.inArray(options, PublicMethods) != -1) {
var args = Array.prototype.splice.call(arguments, 1);
this.each(function () {
var kolorPicker = $(this).data(KEY);
if (kolorPicker) {
return kolorPicker[options].apply(kolorPicker, args);
}
});
}
} else {
this.each(function () {
var element = $(this);
if (!element.data(KEY)) {
return element.data(KEY, new KolorPicker(element, options))
.colorPicker($.extend({ cssAddon: $.kolorPicker.css }, options, KolorPickerOptions));
}
});
}
return this;
};
$.kolorPicker = {
theme: ''
};
})(jQuery);