discourse/app/assets/javascripts/admin/controllers/admin-badges-show.js

Failed to ignore revisions in .git-blame-ignore-revs.

167 lines
4.5 KiB
JavaScript
Raw Normal View History

import discourseComputed, { observes } from "discourse-common/utils/decorators";
import { reads } from "@ember/object/computed";
import Controller, { inject as controller } from "@ember/controller";
2018-06-15 23:03:24 +08:00
import { popupAjaxError } from "discourse/lib/ajax-error";
import { bufferedProperty } from "discourse/mixins/buffered-content";
2018-06-15 23:03:24 +08:00
import { propertyNotEqual } from "discourse/lib/computed";
import { run } from "@ember/runloop";
export default Controller.extend(bufferedProperty("model"), {
adminBadges: controller(),
saving: false,
2018-06-15 23:03:24 +08:00
savingStatus: "",
badgeTypes: reads("adminBadges.badgeTypes"),
badgeGroupings: reads("adminBadges.badgeGroupings"),
badgeTriggers: reads("adminBadges.badgeTriggers"),
protectedSystemFields: reads("adminBadges.protectedSystemFields"),
readOnly: reads("buffered.system"),
showDisplayName: propertyNotEqual("name", "displayName"),
init() {
this._super(...arguments);
// this is needed because the model doesnt have default values
// and as we are using a bufferedProperty it's not accessible
// in any other way
run.next(() => {
if (this.model) {
if (!this.model.badge_type_id) {
this.model.set(
"badge_type_id",
this.get("badgeTypes.firstObject.id")
);
}
if (!this.model.badge_grouping_id) {
this.model.set(
"badge_grouping_id",
this.get("badgeGroupings.firstObject.id")
);
}
if (!this.model.trigger) {
this.model.set("trigger", this.get("badgeTriggers.firstObject.id"));
}
}
});
},
@discourseComputed("model.query", "buffered.query")
hasQuery(modelQuery, bufferedQuery) {
if (bufferedQuery) {
return bufferedQuery.trim().length > 0;
}
return modelQuery && modelQuery.trim().length > 0;
},
@observes("model.id")
_resetSaving: function() {
2018-06-15 23:03:24 +08:00
this.set("saving", false);
this.set("savingStatus", "");
},
actions: {
save() {
if (!this.saving) {
2018-06-15 23:03:24 +08:00
let fields = [
"allow_title",
"multiple_grant",
"listable",
"auto_revoke",
"enabled",
"show_posts",
"target_posts",
"name",
"description",
"long_description",
"icon",
"image",
"query",
"badge_grouping_id",
"trigger",
"badge_type_id"
];
2018-06-15 23:03:24 +08:00
if (this.get("buffered.system")) {
var protectedFields = this.protectedSystemFields || [];
fields = _.filter(fields, f => !protectedFields.includes(f));
}
2018-06-15 23:03:24 +08:00
this.set("saving", true);
this.set("savingStatus", I18n.t("saving"));
2018-06-15 23:03:24 +08:00
const boolFields = [
"allow_title",
"multiple_grant",
"listable",
"auto_revoke",
"enabled",
"show_posts",
"target_posts"
];
const data = {};
const buffered = this.buffered;
2018-06-15 23:03:24 +08:00
fields.forEach(function(field) {
var d = buffered.get(field);
if (boolFields.includes(field)) {
2018-06-15 23:03:24 +08:00
d = !!d;
}
data[field] = d;
});
const newBadge = !this.id;
const model = this.model;
this.model
2018-06-15 23:03:24 +08:00
.save(data)
.then(() => {
if (newBadge) {
const adminBadges = this.get("adminBadges.model");
if (!adminBadges.includes(model)) {
adminBadges.pushObject(model);
}
this.transitionToRoute("adminBadges.show", model.get("id"));
} else {
this.commitBuffer();
this.set("savingStatus", I18n.t("saved"));
}
2018-06-15 23:03:24 +08:00
})
.catch(popupAjaxError)
.finally(() => {
this.set("saving", false);
this.set("savingStatus", "");
});
}
},
destroy() {
2018-06-15 23:03:24 +08:00
const adminBadges = this.get("adminBadges.model");
const model = this.model;
2018-06-15 23:03:24 +08:00
if (!model.get("id")) {
this.transitionToRoute("adminBadges.index");
return;
}
2018-06-15 23:03:24 +08:00
return bootbox.confirm(
I18n.t("admin.badges.delete_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
if (result) {
model
.destroy()
.then(() => {
adminBadges.removeObject(model);
this.transitionToRoute("adminBadges.index");
})
.catch(() => {
bootbox.alert(I18n.t("generic_error"));
});
}
}
2018-06-15 23:03:24 +08:00
);
}
}
});