2019-10-31 04:28:29 +08:00
|
|
|
import { alias } from "@ember/object/computed";
|
2019-10-30 01:31:44 +08:00
|
|
|
import { inject } from "@ember/controller";
|
2019-10-24 01:06:54 +08:00
|
|
|
import Controller from "@ember/controller";
|
2018-06-15 23:03:24 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2019-01-11 07:46:03 +08:00
|
|
|
import { bufferedProperty } from "discourse/mixins/buffered-content";
|
2018-06-15 23:03:24 +08:00
|
|
|
import { propertyNotEqual } from "discourse/lib/computed";
|
2019-04-26 18:16:21 +08:00
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend(bufferedProperty("model"), {
|
2019-10-30 01:13:31 +08:00
|
|
|
adminBadges: inject(),
|
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
|
|
|
|
2019-10-31 04:28:29 +08:00
|
|
|
badgeTypes: alias("adminBadges.badgeTypes"),
|
|
|
|
badgeGroupings: alias("adminBadges.badgeGroupings"),
|
|
|
|
badgeTriggers: alias("adminBadges.badgeTriggers"),
|
2019-10-31 07:18:29 +08:00
|
|
|
protectedSystemFields: alias("adminBadges.protectedSystemFields"),
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2019-10-31 04:28:29 +08:00
|
|
|
readOnly: alias("buffered.system"),
|
2018-06-15 23:03:24 +08:00
|
|
|
showDisplayName: propertyNotEqual("name", "displayName"),
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2019-04-26 18:16:21 +08:00
|
|
|
@computed("model.query", "buffered.query")
|
|
|
|
hasQuery(modelQuery, bufferedQuery) {
|
|
|
|
if (bufferedQuery) {
|
|
|
|
return bufferedQuery.trim().length > 0;
|
2015-08-26 04:30:09 +08:00
|
|
|
}
|
2019-04-26 18:16:21 +08:00
|
|
|
return modelQuery && modelQuery.trim().length > 0;
|
|
|
|
},
|
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() {
|
2019-05-27 16:15:39 +08:00
|
|
|
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"
|
|
|
|
];
|
2014-10-18 02:27:40 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (this.get("buffered.system")) {
|
2019-05-27 16:15:39 +08:00
|
|
|
var protectedFields = this.protectedSystemFields || [];
|
2018-11-27 23:01:02 +08:00
|
|
|
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 = {};
|
2019-05-27 16:15:39 +08:00
|
|
|
const buffered = this.buffered;
|
2018-06-15 23:03:24 +08:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
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"));
|
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");
|
2019-05-27 16:15:39 +08:00
|
|
|
const model = this.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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|