Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #187 from marsoltys/master
Browse files Browse the repository at this point in the history
Fixes issues when working with user-defined aliases

Fixes multiple issues when using custom aliases with multiple instances of colorpicker.
  • Loading branch information
itsjavi authored Dec 28, 2016
2 parents 6623984 + 7c4f649 commit 499549a
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 499549a

Please sign in to comment.