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