REFACTOR: color-scheme-color model (#7019)

This commit is contained in:
Joffrey JAFFEUX 2019-02-19 09:31:26 +01:00 committed by GitHub
parent cba0dd33ee
commit 15fd875855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 36 deletions

View File

@ -1,48 +1,50 @@
const ColorSchemeColor = Discourse.Model.extend({ import {
init: function() { default as computed,
this._super(...arguments); observes,
this.startTrackingChanges(); on
}, } from "ember-addons/ember-computed-decorators";
import { propertyNotEqual, i18n } from "discourse/lib/computed";
startTrackingChanges: function() { const ColorSchemeColor = Discourse.Model.extend({
@on("init")
startTrackingChanges() {
this.set("originals", { hex: this.get("hex") || "FFFFFF" }); this.set("originals", { hex: this.get("hex") || "FFFFFF" });
this.notifyPropertyChange("hex"); // force changed property to be recalculated
// force changed property to be recalculated
this.notifyPropertyChange("hex");
}, },
// Whether value has changed since it was last saved. // Whether value has changed since it was last saved.
changed: function() { @computed("hex")
if (!this.originals) return false; changed(hex) {
if (this.get("hex") !== this.originals["hex"]) return true; if (!this.get("originals")) return false;
if (hex !== this.get("originals").hex) return true;
return false; return false;
}.property("hex"), },
// Whether the current value is different than Discourse's default color scheme. // Whether the current value is different than Discourse's default color scheme.
overridden: function() { overridden: propertyNotEqual("hex", "default_hex"),
return this.get("hex") !== this.get("default_hex");
}.property("hex", "default_hex"),
// Whether the saved value is different than Discourse's default color scheme. // Whether the saved value is different than Discourse's default color scheme.
savedIsOverriden: function() { @computed("default_hex", "hex")
return this.get("originals").hex !== this.get("default_hex"); savedIsOverriden(defaultHex) {
}.property("hex", "default_hex"), return this.get("originals").hex !== defaultHex;
},
revert: function() { revert() {
this.set("hex", this.get("default_hex")); this.set("hex", this.get("default_hex"));
}, },
undo: function() { undo() {
if (this.originals) this.set("hex", this.originals["hex"]); if (this.get("originals")) {
this.set("hex", this.get("originals").hex);
}
}, },
translatedName: function() { translatedName: i18n("name", "admin.customize.colors.%@.name"),
return I18n.t("admin.customize.colors." + this.get("name") + ".name");
}.property("name"),
description: function() { description: i18n("name", "admin.customize.colors.%@.description"),
return I18n.t(
"admin.customize.colors." + this.get("name") + ".description"
);
}.property("name"),
/** /**
brightness returns a number between 0 (darkest) to 255 (brightest). brightness returns a number between 0 (darkest) to 255 (brightest).
@ -50,8 +52,8 @@ const ColorSchemeColor = Discourse.Model.extend({
@property brightness @property brightness
**/ **/
brightness: function() { @computed("hex")
var hex = this.get("hex"); brightness(hex) {
if (hex.length === 6 || hex.length === 3) { if (hex.length === 6 || hex.length === 3) {
if (hex.length === 3) { if (hex.length === 3) {
hex = hex =
@ -69,9 +71,10 @@ const ColorSchemeColor = Discourse.Model.extend({
1000 1000
); );
} }
}.property("hex"), },
hexValueChanged: function() { @observes("hex")
hexValueChanged() {
if (this.get("hex")) { if (this.get("hex")) {
this.set( this.set(
"hex", "hex",
@ -80,11 +83,12 @@ const ColorSchemeColor = Discourse.Model.extend({
.replace(/[^0-9a-fA-F]/g, "") .replace(/[^0-9a-fA-F]/g, "")
); );
} }
}.observes("hex"), },
valid: function() { @computed("hex")
return this.get("hex").match(/^([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/) !== null; valid(hex) {
}.property("hex") return hex.match(/^([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/) !== null;
}
}); });
export default ColorSchemeColor; export default ColorSchemeColor;