PluginProbe
Contact Forms by Cimatti / 2.3.6
Contact Forms by Cimatti v2.3.6
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
contact-forms / assets / vendor / jqColorPicker.js

jqColorPicker.js in Contact Forms by Cimatti 2.3.6, at assets/vendor/jqColorPicker.js

341 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function (root, factory) {
2 if (typeof exports === 'object') {
3 module.exports = factory(root, require('jquery'), require('colors'));
4 } else if (typeof define === 'function' && define.amd) {
5 define(['jquery', 'colors'], function (jQuery, Colors) {
6 return factory(root, jQuery, Colors);
7 });
8 } else {
9 factory(root, root.jQuery, root.Colors);
10 }
11 }(this, function(window, $, Colors, undefined){
12 'use strict';
13
14 var $document = $(document),
15 _instance = $(),
16 _colorPicker,
17 _color,
18 _options,
19
20 _$trigger, _$UI,
21 _$z_slider, _$xy_slider,
22 _$xy_cursor, _$z_cursor , _$alpha , _$alpha_cursor,
23
24 _pointermove = 'touchmove.tcp mousemove.tcp pointermove.tcp',
25 _pointerdown = 'touchstart.tcp mousedown.tcp pointerdown.tcp',
26 _pointerup = 'touchend.tcp mouseup.tcp pointerup.tcp',
27 _GPU = false,
28 _animate = window.requestAnimationFrame ||
29 window.webkitRequestAnimationFrame || function(cb){cb()},
30 _html = '<div class="cp-color-picker"><div class="cp-z-slider"><div c' +
31 'lass="cp-z-cursor"></div></div><div class="cp-xy-slider"><div cl' +
32 'ass="cp-white"></div><div class="cp-xy-cursor"></div></div><div ' +
33 'class="cp-alpha"><div class="cp-alpha-cursor"></div></div></div>',
34 // 'grunt-contrib-uglify' puts all this back to one single string...
35 _css = '.cp-color-picker{position:absolute;overflow:hidden;padding:6p' +
36 'x 6px 0;background-color:#444;color:#bbb;font-family:Arial,Helve' +
37 'tica,sans-serif;font-size:12px;font-weight:400;cursor:default;bo' +
38 'rder-radius:5px}.cp-color-picker>div{position:relative;overflow:' +
39 'hidden}.cp-xy-slider{float:left;height:128px;width:128px;margin-' +
40 'bottom:6px;background:linear-gradient(to right,#FFF,rgba(255,255' +
41 ',255,0))}.cp-white{height:100%;width:100%;background:linear-grad' +
42 'ient(rgba(0,0,0,0),#000)}.cp-xy-cursor{position:absolute;top:0;w' +
43 'idth:10px;height:10px;margin:-5px;border:1px solid #fff;border-r' +
44 'adius:100%;box-sizing:border-box}.cp-z-slider{float:right;margin' +
45 '-left:6px;height:128px;width:20px;background:linear-gradient(red' +
46 ' 0,#f0f 17%,#00f 33%,#0ff 50%,#0f0 67%,#ff0 83%,red 100%)}.cp-z-' +
47 'cursor{position:absolute;margin-top:-4px;width:100%;border:4px s' +
48 'olid #fff;border-color:transparent #fff;box-sizing:border-box}.c' +
49 'p-alpha{clear:both;width:100%;height:16px;margin:6px 0;backgroun' +
50 'd:linear-gradient(to right,#444,rgba(0,0,0,0))}.cp-alpha-cursor{' +
51 'position:absolute;margin-left:-4px;height:100%;border:4px solid ' +
52 '#fff;border-color:#fff transparent;box-sizing:border-box}',
53
54 ColorPicker = function(options) {
55 _color = this.color = new Colors(options);
56 _options = _color.options;
57 _colorPicker = this;
58 };
59
60 ColorPicker.prototype = {
61 render: preRender,
62 toggle: toggle
63 };
64
65 function extractValue(elm) {
66 return elm.value || elm.getAttribute('value') ||
67 $(elm).css('background-color') || '#FFF';
68 }
69
70 function resolveEventType(event) {
71 event = event.originalEvent && event.originalEvent.touches ?
72 event.originalEvent.touches[0] : event;
73
74 return event.originalEvent ? event.originalEvent : event;
75 }
76
77 function findElement($elm) {
78 return $($elm.find(_options.doRender)[0] || $elm[0]);
79 }
80
81 function toggle(event) {
82 var $this = $(this),
83 position = $this.offset(),
84 $window = $(window),
85 gap = _options.gap;
86
87 if (event) {
88 _$trigger = findElement($this);
89 _$trigger._colorMode = _$trigger.data('colorMode');
90
91 _colorPicker.$trigger = $this;
92
93 (_$UI || build()).css(
94 _options.positionCallback.call(_colorPicker, $this) ||
95 {'left': (_$UI._left = position.left) -
96 ((_$UI._left += _$UI._width -
97 ($window.scrollLeft() + $window.width())) + gap > 0 ?
98 _$UI._left + gap : 0),
99 'top': (_$UI._top = position.top + $this.outerHeight()) -
100 ((_$UI._top += _$UI._height -
101 ($window.scrollTop() + $window.height())) + gap > 0 ?
102 _$UI._top + gap : 0)
103 }).show(_options.animationSpeed, function() {
104 if (event === true) { // resize, scroll
105 return;
106 }
107 _$alpha.toggle(!!_options.opacity)._width = _$alpha.width();
108 _$xy_slider._width = _$xy_slider.width();
109 _$xy_slider._height = _$xy_slider.height();
110 _$z_slider._height = _$z_slider.height();
111 _color.setColor(extractValue(_$trigger[0]));
112
113 preRender(true);
114 })
115 .off('.tcp').on(_pointerdown,
116 '.cp-xy-slider,.cp-z-slider,.cp-alpha', pointerdown);
117 } else if (_colorPicker.$trigger) {
118 $(_$UI).hide(_options.animationSpeed, function() {
119 preRender(false);
120 _colorPicker.$trigger = null;
121 }).off('.tcp');
122 }
123 }
124
125 function build() {
126 $('head').append('<style type="text/css" id="tinyColorPickerStyles">' +
127 (_options.css || _css) + (_options.cssAddon || '') + '</style>');
128
129 return $(_html).css({'margin': _options.margin})
130 .appendTo('body')
131 .show(0, function() {
132 _colorPicker.$UI = _$UI = $(this);
133
134 _GPU = _options.GPU && _$UI.css('perspective') !== undefined;
135 _$z_slider = $('.cp-z-slider', this);
136 _$xy_slider = $('.cp-xy-slider', this);
137 _$xy_cursor = $('.cp-xy-cursor', this);
138 _$z_cursor = $('.cp-z-cursor', this);
139 _$alpha = $('.cp-alpha', this);
140 _$alpha_cursor = $('.cp-alpha-cursor', this);
141 _options.buildCallback.call(_colorPicker, _$UI);
142 _$UI.prepend('<div>').children().eq(0).css('width',
143 _$UI.children().eq(0).width()); // stabilizer
144 _$UI._width = this.offsetWidth;
145 _$UI._height = this.offsetHeight;
146 }).hide();
147 }
148
149 function pointerdown(e) {
150 var action = this.className
151 .replace(/cp-(.*?)(?:\s*|$)/, '$1').replace('-', '_');
152
153 if ((e.button || e.which) > 1) return;
154
155 e.preventDefault && e.preventDefault();
156 e.returnValue = false;
157
158 _$trigger._offset = $(this).offset();
159
160 (action = action === 'xy_slider' ? xy_slider :
161 action === 'z_slider' ? z_slider : alpha)(e);
162 preRender();
163
164 $document.on(_pointerup, function(e) {
165 $document.off('.tcp');
166 }).on(_pointermove, function(e) {
167 action(e);
168 preRender();
169 });
170 }
171
172 function xy_slider(event) {
173 var e = resolveEventType(event),
174 x = e.pageX - _$trigger._offset.left,
175 y = e.pageY - _$trigger._offset.top;
176
177 _color.setColor({
178 s: x / _$xy_slider._width * 100,
179 v: 100 - (y / _$xy_slider._height * 100)
180 }, 'hsv');
181 }
182
183 function z_slider(event) {
184 var z = resolveEventType(event).pageY - _$trigger._offset.top;
185
186 _color.setColor({h: 360 - (z / _$z_slider._height * 360)}, 'hsv');
187 }
188
189 function alpha(event) {
190 var x = resolveEventType(event).pageX - _$trigger._offset.left,
191 alpha = x / _$alpha._width;
192
193 _color.setColor({}, 'rgb', alpha);
194 }
195
196 function preRender(toggled) {
197 var colors = _color.colors,
198 hueRGB = colors.hueRGB,
199 RGB = colors.RND.rgb,
200 HSL = colors.RND.hsl,
201 dark = _options.dark,
202 light = _options.light,
203 colorText = _color.toString(_$trigger._colorMode, _options.forceAlpha),
204 HUEContrast = colors.HUELuminance > 0.22 ? dark : light,
205 alphaContrast = colors.rgbaMixBlack.luminance > 0.22 ? dark : light,
206 h = (1 - colors.hsv.h) * _$z_slider._height,
207 s = colors.hsv.s * _$xy_slider._width,
208 v = (1 - colors.hsv.v) * _$xy_slider._height,
209 a = colors.alpha * _$alpha._width,
210 translate3d = _GPU ? 'translate3d' : '',
211 triggerValue = _$trigger[0].value,
212 hasNoValue = _$trigger[0].hasAttribute('value') && // question this
213 triggerValue === '' && toggled !== undefined;
214
215 _$xy_slider._css = {
216 backgroundColor: 'rgb(' +
217 hueRGB.r + ',' + hueRGB.g + ',' + hueRGB.b + ')'};
218 _$xy_cursor._css = {
219 transform: translate3d + '(' + s + 'px, ' + v + 'px, 0)',
220 left: !_GPU ? s : '',
221 top: !_GPU ? v : '',
222 borderColor : colors.RGBLuminance > 0.22 ? dark : light
223 };
224 _$z_cursor._css = {
225 transform: translate3d + '(0, ' + h + 'px, 0)',
226 top: !_GPU ? h : '',
227 borderColor : 'transparent ' + HUEContrast
228 };
229 _$alpha._css = {backgroundColor: '#' + colors.HEX};
230 _$alpha_cursor._css = {
231 transform: translate3d + '(' + a + 'px, 0, 0)',
232 left: !_GPU ? a : '',
233 borderColor : alphaContrast + ' transparent'
234 };
235 _$trigger._css = {
236 backgroundColor : hasNoValue ? '' : colorText,
237 color: hasNoValue ? '' :
238 colors.rgbaMixBGMixCustom.luminance > 0.22 ? dark : light
239 };
240 _$trigger.text = hasNoValue ? '' : triggerValue !== colorText ? colorText : '';
241
242 toggled !== undefined ? render(toggled) : _animate(render);
243 }
244
245 // As _animate() is actually requestAnimationFrame(), render() gets called
246 // decoupled from any pointer action (whenever the browser decides to do
247 // so) as an event. preRender() is coupled to toggle() and all pointermove
248 // actions; that's where all the calculations happen. render() can now be
249 // called without extra calculations which results in faster rendering.
250 function render(toggled) {
251 _$xy_slider.css(_$xy_slider._css);
252 _$xy_cursor.css(_$xy_cursor._css);
253 _$z_cursor.css(_$z_cursor._css);
254 _$alpha.css(_$alpha._css);
255 _$alpha_cursor.css(_$alpha_cursor._css);
256
257 _options.doRender && _$trigger.css(_$trigger._css);
258 _$trigger.text && _$trigger.val(_$trigger.text);
259
260 _options.renderCallback.call(
261 _colorPicker,
262 _$trigger,
263 typeof toggled === 'boolean' ? toggled : undefined
264 );
265 }
266
267 $.fn.colorPicker = function(options) {
268 var _this = this,
269 noop = function(){};
270
271 options = $.extend({
272 animationSpeed: 150,
273 GPU: true,
274 doRender: true,
275 customBG: '#FFF',
276 opacity: true,
277 renderCallback: noop,
278 buildCallback: noop,
279 positionCallback: noop,
280 body: document.body,
281 scrollResize: true,
282 gap: 4,
283 dark: '#222',
284 light: '#DDD'
285 // forceAlpha: undefined,
286 // css: '',
287 // cssAddon: '',
288 // margin: '',
289 // preventFocus: false
290 }, options);
291
292 !_colorPicker && options.scrollResize && $(window)
293 .on('resize.tcp scroll.tcp', function() {
294 if (_colorPicker.$trigger) {
295 _colorPicker.toggle.call(_colorPicker.$trigger[0], true);
296 }
297 });
298 _instance = _instance.add(this);
299 this.colorPicker = _colorPicker || new ColorPicker(options);
300 this.options = options;
301
302 $(options.body).off('.tcp').on(_pointerdown, function(e) {
303 _instance.add(_$UI).add($(_$UI).find(e.target)).
304 index(e.target) === -1 && toggle();
305 });
306
307 return this.on('focusin.tcp click.tcp', function(event) {
308 _colorPicker.color.options = // swap options to fake new instance
309 $.extend(_colorPicker.color.options, _options = _this.options);
310 toggle.call(this, event);
311 })
312 .on('change.tcp', function() {
313 _color.setColor(this.value || '#FFF');
314 _this.colorPicker.render(true);
315 })
316 .each(function() {
317 var value = extractValue(this),
318 mode = value.split('('),
319 $elm = findElement($(this));
320
321 $elm.data('colorMode', mode[1] ? mode[0].substr(0, 3) : 'HEX')
322 .attr('readonly', _options.preventFocus);
323 options.doRender &&
324 $elm.css({'background-color': value,
325 'color': function() {
326 return _color.setColor(value)
327 .rgbaMixBGMixCustom.luminance > 0.22 ?
328 options.dark : options.light
329 }
330 });
331 });
332 };
333
334 $.fn.colorPicker.destroy = function() {
335 $('*').off('.tcp'); // slower but saver
336 _colorPicker.toggle(false);
337 _instance = $();
338 // destroy _colorPicker
339 };
340
341 }));