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