Skip to content

Commit

Permalink
Found multiple issues when using aliases with multiple instances of c…
Browse files Browse the repository at this point in the history
…olorpicker. Changes are explained in comments
  • Loading branch information
soltmar committed Nov 15, 2016
1 parent 6f216a5 commit 7c4f649
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dist/css/bootstrap-colorpicker.min.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 24 additions & 8 deletions dist/js/bootstrap-colorpicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
};
this.origFormat = null; // original string format
if (predefinedColors) {
$.extend(this.colors, predefinedColors);
// We don't want to share aliases across instances so we extend new object
this.colors = $.extend({}, this.colors_lib, predefinedColors);
// Let's keep user defined aliases in separate object
this.user_colors = predefinedColors;
}
if (val) {
if (val.toLowerCase !== undefined) {
Expand All @@ -51,8 +54,10 @@

Color.prototype = {
constructor: Color,
user_colors: {},
colors: {},
// 140 predefined colors from the HTML Colors spec
colors: {
colors_lib: {
"aliceblue": "#f0f8ff",
"antiquewhite": "#faebd7",
"aqua": "#00ffff",
Expand Down Expand Up @@ -340,8 +345,15 @@
},
toAlias: function(r, g, b, a) {
var rgb = this.toHex(r, g, b, a);
for (var alias in this.colors) {
if (this.colors[alias] === rgb) {
//first check if there is user defined alias
for (var ualias in this.user_colors) {
if (this.user_colors[ualias] === rgb) {
return ualias;
}
}
//if not then iterate through pre-defined colors
for (var alias in this.colors_lib) {
if (this.colors_lib[alias] === rgb) {
return alias;
}
}
Expand Down Expand Up @@ -846,20 +858,24 @@
return val;
},
updateComponent: function(val) {
val = val || this.color.toString(this.format);
if (val !== undefined) {
val = new Color(val, this.options.colorSelectors);
} else {
val = this.color;
}
if (this.component !== false) {
var icn = this.component.find('i').eq(0);
if (icn.length > 0) {
icn.css({
'backgroundColor': val
'backgroundColor': val.toHex() // We always wan it to be valid value
});
} else {
this.component.css({
'backgroundColor': val
'backgroundColor': val.toHex() // We always wan it to be valid value
});
}
}
return val;
return val.toString(this.format);
},
update: function(force) {
var val;
Expand Down
Loading

0 comments on commit 7c4f649

Please sign in to comment.