2020-05-13 16:23:41 -04:00
|
|
|
import I18n from "I18n";
|
2020-01-16 12:56:53 -05:00
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
2020-02-03 08:22:14 -05:00
|
|
|
import { reads } from "@ember/object/computed";
|
2020-03-06 11:41:41 -05:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
2015-05-21 16:05:48 -04:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2019-01-10 18:46:03 -05:00
|
|
|
import { bufferedProperty } from "discourse/mixins/buffered-content";
|
2015-08-07 15:08:27 -04:00
|
|
|
import { propertyNotEqual } from "discourse/lib/computed";
|
2020-02-05 11:14:42 -05:00
|
|
|
import { run } from "@ember/runloop";
|
2020-08-26 12:57:13 -04:00
|
|
|
import bootbox from "bootbox";
|
2014-10-17 14:27:40 -04:00
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend(bufferedProperty("model"), {
|
2020-03-06 11:41:41 -05:00
|
|
|
adminBadges: controller(),
|
2014-10-17 14:27:40 -04:00
|
|
|
saving: false,
|
|
|
|
savingStatus: "",
|
2020-02-03 08:22:14 -05: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 08:22:14 -05:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2014-10-17 14:27:40 -04:00
|
|
|
|
2020-02-03 08:22:14 -05: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-05 11:14:42 -05:00
|
|
|
run.next(() => {
|
2020-02-04 09:34:19 -05:00
|
|
|
if (this.model) {
|
|
|
|
if (!this.model.badge_type_id) {
|
|
|
|
this.model.set(
|
|
|
|
"badge_type_id",
|
|
|
|
this.get("badgeTypes.firstObject.id")
|
|
|
|
);
|
|
|
|
}
|
2020-02-03 08:22:14 -05:00
|
|
|
|
2020-02-04 09:34:19 -05:00
|
|
|
if (!this.model.badge_grouping_id) {
|
|
|
|
this.model.set(
|
|
|
|
"badge_grouping_id",
|
|
|
|
this.get("badgeGroupings.firstObject.id")
|
|
|
|
);
|
|
|
|
}
|
2020-02-03 08:22:14 -05:00
|
|
|
|
2020-02-04 09:34:19 -05:00
|
|
|
if (!this.model.trigger) {
|
|
|
|
this.model.set("trigger", this.get("badgeTriggers.firstObject.id"));
|
|
|
|
}
|
2020-02-03 08:22:14 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2014-10-17 14:27:40 -04:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("model.query", "buffered.query")
|
2019-04-26 06:16:21 -04:00
|
|
|
hasQuery(modelQuery, bufferedQuery) {
|
|
|
|
if (bufferedQuery) {
|
|
|
|
return bufferedQuery.trim().length > 0;
|
2015-08-25 16:30:09 -04:00
|
|
|
}
|
2019-04-26 06:16:21 -04:00
|
|
|
return modelQuery && modelQuery.trim().length > 0;
|
|
|
|
},
|
2015-08-25 16:30:09 -04:00
|
|
|
|
2020-01-16 12:56:53 -05:00
|
|
|
@observes("model.id")
|
2014-10-17 14:27:40 -04:00
|
|
|
_resetSaving: function () {
|
|
|
|
this.set("saving", false);
|
|
|
|
this.set("savingStatus", "");
|
2020-01-16 12:56:53 -05:00
|
|
|
},
|
2014-10-17 14:27:40 -04:00
|
|
|
|
|
|
|
actions: {
|
2016-10-20 13:26:41 -04:00
|
|
|
save() {
|
2019-05-27 04:15:39 -04:00
|
|
|
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")) {
|
2020-09-01 13:51:42 -04:00
|
|
|
let protectedFields = this.protectedSystemFields || [];
|
|
|
|
fields = fields.filter((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 = {};
|
2019-05-27 04:15:39 -04:00
|
|
|
const buffered = this.buffered;
|
2014-10-17 14:27:40 -04:00
|
|
|
fields.forEach(function (field) {
|
|
|
|
var d = buffered.get(field);
|
2018-11-19 06:08:54 -05:00
|
|
|
if (boolFields.includes(field)) {
|
2014-10-17 14:27:40 -04:00
|
|
|
d = !!d;
|
|
|
|
}
|
|
|
|
data[field] = d;
|
|
|
|
});
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
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 11:03:24 -04:00
|
|
|
}
|
2016-10-20 13:26:41 -04:00
|
|
|
this.transitionToRoute("adminBadges.show", model.get("id"));
|
2018-06-15 11:03:24 -04: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");
|
2019-05-27 04:15:39 -04:00
|
|
|
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 11:03:24 -04:00
|
|
|
}
|
2014-10-17 14:27:40 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|