Support for multiple instances

- Multiple instances based on jQuery instances
 - Backwards compatible (no changes in API)
 - Options now have to be set on every instantiation.
 - Option opacity is now toggled automatically.
pull/67/head 1.0.7
peterd 8 years ago
parent 7204cad5f4
commit 9d7ea525f7

@ -34,7 +34,7 @@ tinyColorPicker can be received by bower:
bower install tinyColorPicker bower install tinyColorPicker
``` ```
##jqColorPicker.js ## jqColorPicker.js
colorPicker uses an instance of Colors and passes the options to it, so some values are the same... colorPicker uses an instance of Colors and passes the options to it, so some values are the same...
@ -59,7 +59,7 @@ $('.color').colorPicker({
forceAlpha: // force printing alpha channel (undefined = auto; false = never print alpha) forceAlpha: // force printing alpha channel (undefined = auto; false = never print alpha)
}); });
``` ```
####Some tips #### Some tips
The renderCallback can be used as openCallback and closeCallback: The renderCallback can be used as openCallback and closeCallback:
@ -69,6 +69,8 @@ renderCallback: function($elm, toggled) {
// ... like an open callback // ... like an open callback
} else if (toggled === false) { } else if (toggled === false) {
// ... like a close callback // ... like a close callback
} else {
// rendering...
} }
} }
``` ```
@ -77,7 +79,7 @@ Here you can fire events if necessary or check for a className or data attribute
this.$UI.find('.cp-alpha').toggle(!$elm.hasClass('no-alpha')); this.$UI.find('.cp-alpha').toggle(!$elm.hasClass('no-alpha'));
``` ```
##colors.js ## colors.js
This section only shows the options for color.js. They are picked up automatically if set in $('.color').colorPicker This section only shows the options for color.js. They are picked up automatically if set in $('.color').colorPicker
@ -92,7 +94,7 @@ Colors({ // all options have a default value...
}); });
``` ```
##The color model, the methods and more ## The color model, the methods and more
After initializing Color or ColorPicker you'll get a clean but rhich model of the instance: After initializing Color or ColorPicker you'll get a clean but rhich model of the instance:
@ -129,7 +131,7 @@ myColorPicker: {
``` ```
The color model ## The color model
```javascript ```javascript
HEX: // current color as HEX (upper case, 6 digits) HEX: // current color as HEX (upper case, 6 digits)

@ -93,5 +93,5 @@ $(function(){
window.myColorPicker = window.myColorPicker =
$('.color').colorPicker(options); $('.color').colorPicker(options);
$('.trigger').colorPicker(); $('.trigger').colorPicker(options);
}); });

@ -408,17 +408,17 @@
var that = this; var that = this;
$elm.append('<div class="cp-patch"><div></div></div><div class="cp-disp"></div>'); $elm.append('<div class="cp-patch"><div></div></div><div class="cp-disp"></div>');
$('.trigger').parent().on('click', '.trigger', function(e) { // $('.trigger').parent().on('click', '.trigger', function(e) {
if (e.target === this && $(this).hasClass('active')) { // if (e.target === this && $(this).hasClass('active')) {
e.cancelBubble = true; // e.cancelBubble = true;
e.stopPropagation && e.stopPropagation(); // e.stopPropagation && e.stopPropagation();
that.toggle(); // that.toggle();
} // }
}); // });
// if input type="color" // if input type="color"
$('.color').on('click', function(e){ // $('.color').on('click', function(e){
e.preventDefault && e.preventDefault(); // e.preventDefault && e.preventDefault();
}); // });
}, },
cssAddon: // could also be in a css file instead cssAddon: // could also be in a css file instead
@ -567,6 +567,6 @@
window.myColorPicker = $('.color').colorPicker( window.myColorPicker = $('.color').colorPicker(
plugin[type] || plugin.desktop plugin[type] || plugin.desktop
); );
$('.trigger').colorPicker(); $('.trigger').colorPicker(plugin[type] || plugin.desktop);
$('pre').colorPicker({doRender: false}); $('pre').colorPicker(plugin[type] || plugin.desktop);
})(window, jQuery); })(window, jQuery);

@ -105,7 +105,7 @@
if (event === true) { // resize, scroll if (event === true) { // resize, scroll
return; return;
} }
_$alpha._width = _$alpha.width(); _$alpha.toggle(!!_options.opacity)._width = _$alpha.width();
_$xy_slider._width = _$xy_slider.width(); _$xy_slider._width = _$xy_slider.width();
_$xy_slider._height = _$xy_slider.height(); _$xy_slider._height = _$xy_slider.height();
_$z_slider._height = _$z_slider.height(); _$z_slider._height = _$z_slider.height();
@ -138,7 +138,7 @@
_$xy_slider = $('.cp-xy-slider', this); _$xy_slider = $('.cp-xy-slider', this);
_$xy_cursor = $('.cp-xy-cursor', this); _$xy_cursor = $('.cp-xy-cursor', this);
_$z_cursor = $('.cp-z-cursor', this); _$z_cursor = $('.cp-z-cursor', this);
_$alpha = $('.cp-alpha', this).toggle(!!_options.opacity); _$alpha = $('.cp-alpha', this);
_$alpha_cursor = $('.cp-alpha-cursor', this); _$alpha_cursor = $('.cp-alpha-cursor', this);
_options.buildCallback.call(_colorPicker, $this); _options.buildCallback.call(_colorPicker, $this);
$this.prepend('<div>').children().eq(0).css('width', $this.prepend('<div>').children().eq(0).css('width',
@ -268,7 +268,8 @@
} }
$.fn.colorPicker = function(options) { $.fn.colorPicker = function(options) {
var noop = function(){}; var _this = this,
noop = function(){};
options = $.extend({ options = $.extend({
animationSpeed: 150, animationSpeed: 150,
@ -295,18 +296,22 @@
} }
}); });
_instance = _instance.add(this); _instance = _instance.add(this);
this.colorPicker = _instance.colorPicker = this.colorPicker = _colorPicker || new ColorPicker(options);
_colorPicker || new ColorPicker(options); this.options = options;
$(options.body).off('.tcp').on(_pointerdown, function(e) { $(options.body).off('.tcp').on(_pointerdown, function(e) {
!_instance.add(_$UI).find(e.target) !_instance.add(_$UI).find(e.target)
.add(_instance.filter(e.target))[0] && toggle(); .add(_instance.filter(e.target))[0] && toggle();
}); });
return this.on('focusin.tcp click.tcp', toggle) return this.on('focusin.tcp click.tcp', function(event) {
_colorPicker.color.options = // swap options to fake new instance
$.extend(_colorPicker.color.options, _options = _this.options);
toggle.call(this, event);
})
.on('change.tcp', function() { .on('change.tcp', function() {
_color.setColor(this.value || '#FFF'); _color.setColor(this.value || '#FFF');
_instance.colorPicker.render(true); _this.colorPicker.render(true);
}) })
.each(function() { .each(function() {
var value = extractValue(this), var value = extractValue(this),

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -1,6 +1,6 @@
{ {
"name": "tinyColorPicker", "name": "tinyColorPicker",
"version": "1.0.4", "version": "1.0.7",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "http://github.com/PitPik/tinyColorPicker.git" "url": "http://github.com/PitPik/tinyColorPicker.git"

Loading…
Cancel
Save