Skip to content

Commit 9d7ea52

Browse files
committed
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.
1 parent 7204cad commit 9d7ea52

File tree

7 files changed

+37
-30
lines changed

7 files changed

+37
-30
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ tinyColorPicker can be received by bower:
3434
bower install tinyColorPicker
3535
```
3636

37-
##jqColorPicker.js
37+
## jqColorPicker.js
3838

3939
colorPicker uses an instance of Colors and passes the options to it, so some values are the same...
4040

@@ -59,7 +59,7 @@ $('.color').colorPicker({
5959
forceAlpha: // force printing alpha channel (undefined = auto; false = never print alpha)
6060
});
6161
```
62-
####Some tips
62+
#### Some tips
6363

6464
The renderCallback can be used as openCallback and closeCallback:
6565

@@ -69,6 +69,8 @@ renderCallback: function($elm, toggled) {
6969
// ... like an open callback
7070
} else if (toggled === false) {
7171
// ... like a close callback
72+
} else {
73+
// rendering...
7274
}
7375
}
7476
```
@@ -77,7 +79,7 @@ Here you can fire events if necessary or check for a className or data attribute
7779
this.$UI.find('.cp-alpha').toggle(!$elm.hasClass('no-alpha'));
7880
```
7981

80-
##colors.js
82+
## colors.js
8183

8284
This section only shows the options for color.js. They are picked up automatically if set in $('.color').colorPicker
8385

@@ -92,7 +94,7 @@ Colors({ // all options have a default value...
9294
});
9395
```
9496

95-
##The color model, the methods and more
97+
## The color model, the methods and more
9698

9799
After initializing Color or ColorPicker you'll get a clean but rhich model of the instance:
98100

@@ -129,7 +131,7 @@ myColorPicker: {
129131
```
130132

131133

132-
The color model
134+
## The color model
133135

134136
```javascript
135137
HEX: // current color as HEX (upper case, 6 digits)

demo/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ $(function(){
9393

9494
window.myColorPicker =
9595
$('.color').colorPicker(options);
96-
$('.trigger').colorPicker();
96+
$('.trigger').colorPicker(options);
9797
});

index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -408,17 +408,17 @@
408408
var that = this;
409409

410410
$elm.append('<div class="cp-patch"><div></div></div><div class="cp-disp"></div>');
411-
$('.trigger').parent().on('click', '.trigger', function(e) {
412-
if (e.target === this && $(this).hasClass('active')) {
413-
e.cancelBubble = true;
414-
e.stopPropagation && e.stopPropagation();
415-
that.toggle();
416-
}
417-
});
411+
// $('.trigger').parent().on('click', '.trigger', function(e) {
412+
// if (e.target === this && $(this).hasClass('active')) {
413+
// e.cancelBubble = true;
414+
// e.stopPropagation && e.stopPropagation();
415+
// that.toggle();
416+
// }
417+
// });
418418
// if input type="color"
419-
$('.color').on('click', function(e){
420-
e.preventDefault && e.preventDefault();
421-
});
419+
// $('.color').on('click', function(e){
420+
// e.preventDefault && e.preventDefault();
421+
// });
422422
},
423423

424424
cssAddon: // could also be in a css file instead
@@ -567,6 +567,6 @@
567567
window.myColorPicker = $('.color').colorPicker(
568568
plugin[type] || plugin.desktop
569569
);
570-
$('.trigger').colorPicker();
571-
$('pre').colorPicker({doRender: false});
570+
$('.trigger').colorPicker(plugin[type] || plugin.desktop);
571+
$('pre').colorPicker(plugin[type] || plugin.desktop);
572572
})(window, jQuery);

jqColorPicker.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
_color,
1818
_options,
1919

20-
_$trigger, _$UI,
20+
_$trigger, _$UI,
2121
_$z_slider, _$xy_slider,
2222
_$xy_cursor, _$z_cursor , _$alpha , _$alpha_cursor,
2323

@@ -105,7 +105,7 @@
105105
if (event === true) { // resize, scroll
106106
return;
107107
}
108-
_$alpha._width = _$alpha.width();
108+
_$alpha.toggle(!!_options.opacity)._width = _$alpha.width();
109109
_$xy_slider._width = _$xy_slider.width();
110110
_$xy_slider._height = _$xy_slider.height();
111111
_$z_slider._height = _$z_slider.height();
@@ -138,7 +138,7 @@
138138
_$xy_slider = $('.cp-xy-slider', this);
139139
_$xy_cursor = $('.cp-xy-cursor', this);
140140
_$z_cursor = $('.cp-z-cursor', this);
141-
_$alpha = $('.cp-alpha', this).toggle(!!_options.opacity);
141+
_$alpha = $('.cp-alpha', this);
142142
_$alpha_cursor = $('.cp-alpha-cursor', this);
143143
_options.buildCallback.call(_colorPicker, $this);
144144
$this.prepend('<div>').children().eq(0).css('width',
@@ -268,7 +268,8 @@
268268
}
269269

270270
$.fn.colorPicker = function(options) {
271-
var noop = function(){};
271+
var _this = this,
272+
noop = function(){};
272273

273274
options = $.extend({
274275
animationSpeed: 150,
@@ -295,18 +296,22 @@
295296
}
296297
});
297298
_instance = _instance.add(this);
298-
this.colorPicker = _instance.colorPicker =
299-
_colorPicker || new ColorPicker(options);
299+
this.colorPicker = _colorPicker || new ColorPicker(options);
300+
this.options = options;
300301

301302
$(options.body).off('.tcp').on(_pointerdown, function(e) {
302303
!_instance.add(_$UI).find(e.target)
303304
.add(_instance.filter(e.target))[0] && toggle();
304305
});
305306

306-
return this.on('focusin.tcp click.tcp', toggle)
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+
})
307312
.on('change.tcp', function() {
308313
_color.setColor(this.value || '#FFF');
309-
_instance.colorPicker.render(true);
314+
_this.colorPicker.render(true);
310315
})
311316
.each(function() {
312317
var value = extractValue(this),

jqColorPicker.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jqColorPicker.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tinyColorPicker",
3-
"version": "1.0.4",
3+
"version": "1.0.7",
44
"repository": {
55
"type": "git",
66
"url": "http://github.com/PitPik/tinyColorPicker.git"

0 commit comments

Comments
 (0)