mirror of
https://github.com/discourse/discourse.git
synced 2024-12-04 20:33:38 +08:00
a23d0f9961
Currently the process of adding a custom image to badge is quite clunky; you have to upload your image to a topic, and then copy the image URL and pasting it in a text field. Besides being clucky, if the topic or post that contains the image is deleted, the image will be garbage-collected in a few days and the badge will lose the image because the application is not that the image is referenced by a badge. This commit improves that by adding a proper image uploader widget for badge images.
75 lines
2.0 KiB
JavaScript
75 lines
2.0 KiB
JavaScript
import Badge from "discourse/models/badge";
|
|
import I18n from "I18n";
|
|
import Route from "@ember/routing/route";
|
|
import { ajax } from "discourse/lib/ajax";
|
|
import bootbox from "bootbox";
|
|
import { get } from "@ember/object";
|
|
import showModal from "discourse/lib/show-modal";
|
|
|
|
export default Route.extend({
|
|
serialize(m) {
|
|
return { badge_id: get(m, "id") || "new" };
|
|
},
|
|
|
|
model(params) {
|
|
if (params.badge_id === "new") {
|
|
return Badge.create({
|
|
name: I18n.t("admin.badges.new_badge"),
|
|
});
|
|
}
|
|
return this.modelFor("adminBadges").findBy(
|
|
"id",
|
|
parseInt(params.badge_id, 10)
|
|
);
|
|
},
|
|
|
|
setupController(controller, model) {
|
|
this._super(...arguments);
|
|
if (model.image_url) {
|
|
controller.showImageUploader();
|
|
} else if (model.icon) {
|
|
controller.showIconSelector();
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
saveError(e) {
|
|
let msg = I18n.t("generic_error");
|
|
if (e.responseJSON && e.responseJSON.errors) {
|
|
msg = I18n.t("generic_error_with_reason", {
|
|
error: e.responseJSON.errors.join(". "),
|
|
});
|
|
}
|
|
bootbox.alert(msg);
|
|
},
|
|
|
|
editGroupings() {
|
|
const model = this.controllerFor("admin-badges").get("badgeGroupings");
|
|
showModal("admin-edit-badge-groupings", { model, admin: true });
|
|
},
|
|
|
|
preview(badge, explain) {
|
|
badge.set("preview_loading", true);
|
|
ajax("/admin/badges/preview.json", {
|
|
type: "POST",
|
|
data: {
|
|
sql: badge.get("query"),
|
|
target_posts: !!badge.get("target_posts"),
|
|
trigger: badge.get("trigger"),
|
|
explain,
|
|
},
|
|
})
|
|
.then(function (model) {
|
|
badge.set("preview_loading", false);
|
|
showModal("admin-badge-preview", { model, admin: true });
|
|
})
|
|
.catch(function (error) {
|
|
badge.set("preview_loading", false);
|
|
// eslint-disable-next-line no-console
|
|
console.error(error);
|
|
bootbox.alert("Network error");
|
|
});
|
|
},
|
|
},
|
|
});
|