2018-06-15 23:03:24 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import BufferedContent from "discourse/mixins/buffered-content";
|
|
|
|
import { propertyNotEqual } from "discourse/lib/computed";
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2015-08-12 00:27:07 +08:00
|
|
|
export default Ember.Controller.extend(BufferedContent, {
|
2016-10-21 01:26:41 +08:00
|
|
|
adminBadges: Ember.inject.controller(),
|
2014-10-18 02:27:40 +08:00
|
|
|
saving: false,
|
2018-06-15 23:03:24 +08:00
|
|
|
savingStatus: "",
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
badgeTypes: Ember.computed.alias("adminBadges.badgeTypes"),
|
|
|
|
badgeGroupings: Ember.computed.alias("adminBadges.badgeGroupings"),
|
|
|
|
badgeTriggers: Ember.computed.alias("adminBadges.badgeTriggers"),
|
|
|
|
protectedSystemFields: Ember.computed.alias(
|
|
|
|
"adminBadges.protectedSystemFields"
|
|
|
|
),
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
readOnly: Ember.computed.alias("buffered.system"),
|
|
|
|
showDisplayName: propertyNotEqual("name", "displayName"),
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2015-08-26 04:30:09 +08:00
|
|
|
hasQuery: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
const bQuery = this.get("buffered.query");
|
2015-08-26 04:30:09 +08:00
|
|
|
if (bQuery) {
|
|
|
|
return bQuery.trim().length > 0;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
const mQuery = this.get("model.query");
|
2015-08-26 04:30:09 +08:00
|
|
|
return mQuery && mQuery.trim().length > 0;
|
2018-06-15 23:03:24 +08:00
|
|
|
}.property("model.query", "buffered.query"),
|
2015-08-26 04:30:09 +08:00
|
|
|
|
2014-10-18 02:27:40 +08:00
|
|
|
_resetSaving: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("saving", false);
|
|
|
|
this.set("savingStatus", "");
|
|
|
|
}.observes("model.id"),
|
2014-10-18 02:27:40 +08:00
|
|
|
|
|
|
|
actions: {
|
2016-10-21 01:26:41 +08:00
|
|
|
save() {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (!this.get("saving")) {
|
|
|
|
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"
|
|
|
|
];
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (this.get("buffered.system")) {
|
2018-11-19 19:08:54 +08:00
|
|
|
var protectedFields = this.get("protectedSystemFields") || [];
|
|
|
|
fields = _.filter(fields, f => protectedFields.includes(f));
|
2014-10-18 02:27:40 +08:00
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("saving", true);
|
|
|
|
this.set("savingStatus", I18n.t("saving"));
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const boolFields = [
|
|
|
|
"allow_title",
|
|
|
|
"multiple_grant",
|
|
|
|
"listable",
|
|
|
|
"auto_revoke",
|
|
|
|
"enabled",
|
|
|
|
"show_posts",
|
|
|
|
"target_posts"
|
|
|
|
];
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2016-10-21 01:26:41 +08:00
|
|
|
const data = {};
|
2018-06-15 23:03:24 +08:00
|
|
|
const buffered = this.get("buffered");
|
|
|
|
fields.forEach(function(field) {
|
2014-10-18 02:27:40 +08:00
|
|
|
var d = buffered.get(field);
|
2018-11-19 19:08:54 +08:00
|
|
|
if (boolFields.includes(field)) {
|
2018-06-15 23:03:24 +08:00
|
|
|
d = !!d;
|
|
|
|
}
|
2014-10-18 02:27:40 +08:00
|
|
|
data[field] = d;
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const newBadge = !this.get("id");
|
|
|
|
const model = this.get("model");
|
|
|
|
this.get("model")
|
|
|
|
.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"));
|
2016-10-21 01:26:41 +08:00
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => {
|
|
|
|
this.set("saving", false);
|
|
|
|
this.set("savingStatus", "");
|
|
|
|
});
|
2014-10-18 02:27:40 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-21 01:26:41 +08:00
|
|
|
destroy() {
|
2018-06-15 23:03:24 +08:00
|
|
|
const adminBadges = this.get("adminBadges.model");
|
|
|
|
const model = this.get("model");
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (!model.get("id")) {
|
|
|
|
this.transitionToRoute("adminBadges.index");
|
2014-10-18 02:27:40 +08:00
|
|
|
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"));
|
|
|
|
});
|
|
|
|
}
|
2014-10-18 02:27:40 +08:00
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2014-10-18 02:27:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|