REFACTOR: admin-customize-colors-show (#16525)

- native class
- drops jquery
- @action
- uses clipboardCopy
- filterBy
This commit is contained in:
Joffrey JAFFEUX 2022-04-21 09:50:23 +02:00 committed by GitHub
parent c88ca23e8f
commit 9e2303427c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,36 +3,32 @@ import I18n from "I18n";
import bootbox from "bootbox"; import bootbox from "bootbox";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { later } from "@ember/runloop"; import { later } from "@ember/runloop";
import { action } from "@ember/object";
import { clipboardCopy } from "discourse/lib/utilities";
export default Controller.extend({ export default class AdminCustomizeColorsShowController extends Controller {
@discourseComputed("model.colors", "onlyOverridden") @discourseComputed("model.colors", "onlyOverridden")
colors(allColors, onlyOverridden) { colors(allColors, onlyOverridden) {
if (onlyOverridden) { if (onlyOverridden) {
return allColors.filter((color) => color.get("overridden")); return allColors.filterBy("overridden");
} else { } else {
return allColors; return allColors;
} }
}, }
actions: { @action
revert(color) { revert(color) {
color.revert(); color.revert();
}, }
@action
undo(color) { undo(color) {
color.undo(); color.undo();
}, }
@action
copyToClipboard() { copyToClipboard() {
$(".table.colors").hide(); if (clipboardCopy(this.model.schemeJson())) {
let area = $("<textarea id='copy-range'></textarea>");
$(".table.colors").after(area);
area.text(this.model.schemeJson());
let range = document.createRange();
range.selectNode(area[0]);
window.getSelection().addRange(range);
let successful = document.execCommand("copy");
if (successful) {
this.set( this.set(
"model.savingStatus", "model.savingStatus",
I18n.t("admin.customize.copied_to_clipboard") I18n.t("admin.customize.copied_to_clipboard")
@ -47,13 +43,9 @@ export default Controller.extend({
later(() => { later(() => {
this.set("model.savingStatus", null); this.set("model.savingStatus", null);
}, 2000); }, 2000);
}
window.getSelection().removeAllRanges(); @action
$(".table.colors").show();
$(area).remove();
},
copy() { copy() {
const newColorScheme = this.model.copy(); const newColorScheme = this.model.copy();
newColorScheme.set( newColorScheme.set(
@ -66,31 +58,32 @@ export default Controller.extend({
this.allColors.pushObject(newColorScheme); this.allColors.pushObject(newColorScheme);
this.replaceRoute("adminCustomize.colors.show", newColorScheme); this.replaceRoute("adminCustomize.colors.show", newColorScheme);
}); });
}, }
@action
save() { save() {
this.model.save(); this.model.save();
}, }
@action
applyUserSelectable() { applyUserSelectable() {
this.model.updateUserSelectable(this.get("model.user_selectable")); this.model.updateUserSelectable(this.get("model.user_selectable"));
}, }
@action
destroy() { destroy() {
const model = this.model;
return bootbox.confirm( return bootbox.confirm(
I18n.t("admin.customize.colors.delete_confirm"), I18n.t("admin.customize.colors.delete_confirm"),
I18n.t("no_value"), I18n.t("no_value"),
I18n.t("yes_value"), I18n.t("yes_value"),
(result) => { (result) => {
if (result) { if (result) {
model.destroy().then(() => { this.model.destroy().then(() => {
this.allColors.removeObject(model); this.allColors.removeObject(this.model);
this.replaceRoute("adminCustomize.colors"); this.replaceRoute("adminCustomize.colors");
}); });
} }
} }
); );
}, }
}, }
});