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
1 changed files with 69 additions and 76 deletions

View File

@ -3,94 +3,87 @@ 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();
}, }
undo(color) { @action
color.undo(); undo(color) {
}, color.undo();
}
copyToClipboard() { @action
$(".table.colors").hide(); copyToClipboard() {
let area = $("<textarea id='copy-range'></textarea>"); if (clipboardCopy(this.model.schemeJson())) {
$(".table.colors").after(area); this.set(
area.text(this.model.schemeJson()); "model.savingStatus",
let range = document.createRange(); I18n.t("admin.customize.copied_to_clipboard")
range.selectNode(area[0]);
window.getSelection().addRange(range);
let successful = document.execCommand("copy");
if (successful) {
this.set(
"model.savingStatus",
I18n.t("admin.customize.copied_to_clipboard")
);
} else {
this.set(
"model.savingStatus",
I18n.t("admin.customize.copy_to_clipboard_error")
);
}
later(() => {
this.set("model.savingStatus", null);
}, 2000);
window.getSelection().removeAllRanges();
$(".table.colors").show();
$(area).remove();
},
copy() {
const newColorScheme = this.model.copy();
newColorScheme.set(
"name",
I18n.t("admin.customize.colors.copy_name_prefix") +
" " +
this.get("model.name")
); );
newColorScheme.save().then(() => { } else {
this.allColors.pushObject(newColorScheme); this.set(
this.replaceRoute("adminCustomize.colors.show", newColorScheme); "model.savingStatus",
}); I18n.t("admin.customize.copy_to_clipboard_error")
}, );
}
save() { later(() => {
this.model.save(); this.set("model.savingStatus", null);
}, }, 2000);
}
applyUserSelectable() { @action
this.model.updateUserSelectable(this.get("model.user_selectable")); copy() {
}, const newColorScheme = this.model.copy();
newColorScheme.set(
"name",
I18n.t("admin.customize.colors.copy_name_prefix") +
" " +
this.get("model.name")
);
newColorScheme.save().then(() => {
this.allColors.pushObject(newColorScheme);
this.replaceRoute("adminCustomize.colors.show", newColorScheme);
});
}
destroy() { @action
const model = this.model; save() {
return bootbox.confirm( this.model.save();
I18n.t("admin.customize.colors.delete_confirm"), }
I18n.t("no_value"),
I18n.t("yes_value"), @action
(result) => { applyUserSelectable() {
if (result) { this.model.updateUserSelectable(this.get("model.user_selectable"));
model.destroy().then(() => { }
this.allColors.removeObject(model);
this.replaceRoute("adminCustomize.colors"); @action
}); destroy() {
} return bootbox.confirm(
I18n.t("admin.customize.colors.delete_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
(result) => {
if (result) {
this.model.destroy().then(() => {
this.allColors.removeObject(this.model);
this.replaceRoute("adminCustomize.colors");
});
} }
); }
}, );
}, }
}); }