diff --git a/app/assets/javascripts/admin/components/color-input.js.es6 b/app/assets/javascripts/admin/components/color-input.js.es6 index 8648506bf98..44fe738b43d 100644 --- a/app/assets/javascripts/admin/components/color-input.js.es6 +++ b/app/assets/javascripts/admin/components/color-input.js.es6 @@ -1,5 +1,6 @@ import { schedule } from "@ember/runloop"; import Component from "@ember/component"; +import { computed, action } from "@ember/object"; import loadScript, { loadCSS } from "discourse/lib/load-script"; import { observes } from "discourse-common/utils/decorators"; @@ -13,28 +14,45 @@ import { observes } from "discourse-common/utils/decorators"; export default Component.extend({ classNames: ["color-picker"], + onlyHex: true, + + styleSelection: true, + + maxlength: computed("onlyHex", function() { + return this.onlyHex ? 6 : null; + }), + + @action + onHexInput(color) { + this.attrs.onChangeColor && + this.attrs.onChangeColor((color || "").replace(/^#/, "")); + }, + @observes("hexValue", "brightnessValue", "valid") hexValueChanged: function() { - var hex = this.hexValue; + const hex = this.hexValue; let text = this.element.querySelector("input.hex-input"); + this.attrs.onChangeColor && this.attrs.onChangeColor(hex); + if (this.valid) { - text.setAttribute( - "style", - "color: " + - (this.brightnessValue > 125 ? "black" : "white") + - "; background-color: #" + - hex + - ";" - ); + this.styleSelection && + text.setAttribute( + "style", + "color: " + + (this.brightnessValue > 125 ? "black" : "white") + + "; background-color: #" + + hex + + ";" + ); if (this.pickerLoaded) { $(this.element.querySelector(".picker")).spectrum({ - color: "#" + this.hexValue + color: "#" + hex }); } } else { - text.setAttribute("style", ""); + this.styleSelection && text.setAttribute("style", ""); } }, @@ -51,8 +69,6 @@ export default Component.extend({ }); }); }); - schedule("afterRender", () => { - this.hexValueChanged(); - }); + schedule("afterRender", () => this.hexValueChanged()); } }); diff --git a/app/assets/javascripts/admin/components/site-settings/color.js.es6 b/app/assets/javascripts/admin/components/site-settings/color.js.es6 new file mode 100644 index 00000000000..5b7e40e34c4 --- /dev/null +++ b/app/assets/javascripts/admin/components/site-settings/color.js.es6 @@ -0,0 +1,49 @@ +import Component from "@ember/component"; +import { computed, action } from "@ember/object"; + +function RGBToHex(rgb) { + // Choose correct separator + let sep = rgb.indexOf(",") > -1 ? "," : " "; + // Turn "rgb(r,g,b)" into [r,g,b] + rgb = rgb + .substr(4) + .split(")")[0] + .split(sep); + + let r = (+rgb[0]).toString(16), + g = (+rgb[1]).toString(16), + b = (+rgb[2]).toString(16); + + if (r.length === 1) r = "0" + r; + if (g.length === 1) g = "0" + g; + if (b.length === 1) b = "0" + b; + + return "#" + r + g + b; +} + +export default Component.extend({ + valid: computed("value", function() { + let value = this.value.toLowerCase(); + + let testColor = new Option().style; + testColor.color = value; + + if (!testColor.color && !value.startsWith("#")) { + value = `#${value}`; + testColor = new Option().style; + testColor.color = value; + } + + let hexifiedColor = RGBToHex(testColor.color); + if (hexifiedColor.includes("NaN")) { + hexifiedColor = testColor.color; + } + + return testColor.color && hexifiedColor === value; + }), + + @action + onChangeColor(color) { + this.set("value", color); + } +}); diff --git a/app/assets/javascripts/admin/mixins/setting-component.js.es6 b/app/assets/javascripts/admin/mixins/setting-component.js.es6 index 70384c1a277..200ec00c1f8 100644 --- a/app/assets/javascripts/admin/mixins/setting-component.js.es6 +++ b/app/assets/javascripts/admin/mixins/setting-component.js.es6 @@ -22,7 +22,8 @@ const CUSTOM_TYPES = [ "secret_list", "upload", "group_list", - "tag_list" + "tag_list", + "color" ]; const AUTO_REFRESH_ON_SAVE = ["logo", "logo_small", "large_icon"]; diff --git a/app/assets/javascripts/admin/templates/components/site-settings/color.hbs b/app/assets/javascripts/admin/templates/components/site-settings/color.hbs new file mode 100644 index 00000000000..33c66076a24 --- /dev/null +++ b/app/assets/javascripts/admin/templates/components/site-settings/color.hbs @@ -0,0 +1,9 @@ +{{color-input + hexValue=(readonly value) + valid=valid + onlyHex=false + styleSelection=false + onChangeColor=(action "onChangeColor") +}} +{{setting-validation-message message=validationMessage}} +