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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

168 lines
4.6 KiB
JavaScript
Raw Normal View History

import I18n from "I18n";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
2020-02-03 14:22:14 +01:00
import { reads } from "@ember/object/computed";
2020-03-06 17:41:41 +01:00
import Controller, { inject as controller } from "@ember/controller";
2015-05-21 16:05:48 -04:00
import { popupAjaxError } from "discourse/lib/ajax-error";
import { bufferedProperty } from "discourse/mixins/buffered-content";
2015-08-07 15:08:27 -04:00
import { propertyNotEqual } from "discourse/lib/computed";
import { run } from "@ember/runloop";
2014-10-17 14:27:40 -04:00
export default Controller.extend(bufferedProperty("model"), {
2020-03-06 17:41:41 +01:00
adminBadges: controller(),
2014-10-17 14:27:40 -04:00
saving: false,
savingStatus: "",
2020-02-03 14:22:14 +01:00
badgeTypes: reads("adminBadges.badgeTypes"),
badgeGroupings: reads("adminBadges.badgeGroupings"),
badgeTriggers: reads("adminBadges.badgeTriggers"),
protectedSystemFields: reads("adminBadges.protectedSystemFields"),
readOnly: reads("buffered.system"),
showDisplayName: propertyNotEqual("name", "displayName"),
2014-10-17 14:27:40 -04:00
2020-02-03 14:22:14 +01:00
init() {
this._super(...arguments);
2014-10-17 14:27:40 -04:00
2020-02-03 14:22:14 +01:00
// 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")
);
}
2020-02-03 14:22:14 +01:00
if (!this.model.badge_grouping_id) {
this.model.set(
"badge_grouping_id",
this.get("badgeGroupings.firstObject.id")
);
}
2020-02-03 14:22:14 +01:00
if (!this.model.trigger) {
this.model.set("trigger", this.get("badgeTriggers.firstObject.id"));
}
2020-02-03 14:22:14 +01:00
}
});
},
2014-10-17 14:27:40 -04:00
@discourseComputed("model.query", "buffered.query")
2019-04-26 12:16:21 +02:00
hasQuery(modelQuery, bufferedQuery) {
if (bufferedQuery) {
return bufferedQuery.trim().length > 0;
}
2019-04-26 12:16:21 +02:00
return modelQuery && modelQuery.trim().length > 0;
},
@observes("model.id")
2014-10-17 14:27:40 -04:00
_resetSaving: function() {
this.set("saving", false);
this.set("savingStatus", "");
},
2014-10-17 14:27:40 -04:00
actions: {
2016-10-20 13:26:41 -04:00
save() {
if (!this.saving) {
2016-10-20 13:26:41 -04: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-17 14:27:40 -04:00
if (this.get("buffered.system")) {
var protectedFields = this.protectedSystemFields || [];
fields = _.filter(fields, f => !protectedFields.includes(f));
2014-10-17 14:27:40 -04:00
}
this.set("saving", true);
this.set("savingStatus", I18n.t("saving"));
2016-10-20 13:26:41 -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 = {};
const buffered = this.buffered;
2014-10-17 14:27:40 -04:00
fields.forEach(function(field) {
var d = buffered.get(field);
if (boolFields.includes(field)) {
2014-10-17 14:27:40 -04:00
d = !!d;
}
data[field] = d;
});
const newBadge = !this.id;
const model = this.model;
this.model
2016-10-20 13:26:41 -04:00
.save(data)
.then(() => {
2014-10-17 14:27:40 -04:00
if (newBadge) {
2016-10-20 13:26:41 -04:00
const adminBadges = this.get("adminBadges.model");
2016-12-19 11:19:10 -05:00
if (!adminBadges.includes(model)) {
2016-10-20 13:26:41 -04:00
adminBadges.pushObject(model);
2018-06-15 17:03:24 +02:00
}
2016-10-20 13:26:41 -04:00
this.transitionToRoute("adminBadges.show", model.get("id"));
2018-06-15 17:03:24 +02:00
} else {
2016-10-20 13:26:41 -04:00
this.commitBuffer();
this.set("savingStatus", I18n.t("saved"));
}
})
.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() {
const adminBadges = this.get("adminBadges.model");
const model = this.model;
2014-10-17 14:27:40 -04:00
if (!model.get("id")) {
2016-10-20 13:26:41 -04:00
this.transitionToRoute("adminBadges.index");
2014-10-17 14:27:40 -04:00
return;
}
2016-10-20 13:26:41 -04:00
return bootbox.confirm(
I18n.t("admin.badges.delete_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
2014-10-17 14:27:40 -04:00
if (result) {
2016-10-20 13:26:41 -04:00
model
.destroy()
.then(() => {
adminBadges.removeObject(model);
this.transitionToRoute("adminBadges.index");
})
.catch(() => {
2014-10-17 14:27:40 -04:00
bootbox.alert(I18n.t("generic_error"));
});
2018-06-15 17:03:24 +02:00
}
2014-10-17 14:27:40 -04:00
}
);
}
}
});