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 discourseComputed, { observes } from "discourse-common/utils/decorators";
|
|
|
|
import I18n from "I18n";
|
|
|
|
import bootbox from "bootbox";
|
2019-01-10 18:46:03 -05:00
|
|
|
import { bufferedProperty } from "discourse/mixins/buffered-content";
|
2015-08-07 15:08:27 -04:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import { propertyNotEqual } from "discourse/lib/computed";
|
2021-03-17 01:55:23 -04:00
|
|
|
import { equal, reads } from "@ember/object/computed";
|
2022-06-17 12:19:14 -04:00
|
|
|
import { next } from "@ember/runloop";
|
2021-03-17 01:55:23 -04:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import getURL from "discourse-common/lib/get-url";
|
|
|
|
|
|
|
|
const IMAGE = "image";
|
|
|
|
const ICON = "icon";
|
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: "",
|
2021-03-17 01:55:23 -04:00
|
|
|
selectedGraphicType: null,
|
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"),
|
2021-03-17 01:55:23 -04:00
|
|
|
iconSelectorSelected: equal("selectedGraphicType", ICON),
|
|
|
|
imageUploaderSelected: equal("selectedGraphicType", IMAGE),
|
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
|
2022-06-17 12:19:14 -04:00
|
|
|
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-12-08 14:55:49 -05:00
|
|
|
@discourseComputed("model.i18n_name")
|
|
|
|
textCustomizationPrefix(i18n_name) {
|
|
|
|
return `badges.${i18n_name}.`;
|
|
|
|
},
|
|
|
|
|
2020-01-16 12:56:53 -05:00
|
|
|
@observes("model.id")
|
2021-11-13 08:01:55 -05:00
|
|
|
_resetSaving() {
|
2014-10-17 14:27:40 -04:00
|
|
|
this.set("saving", false);
|
|
|
|
this.set("savingStatus", "");
|
2020-01-16 12:56:53 -05:00
|
|
|
},
|
2014-10-17 14:27:40 -04:00
|
|
|
|
2021-03-17 01:55:23 -04:00
|
|
|
showIconSelector() {
|
|
|
|
this.set("selectedGraphicType", ICON);
|
|
|
|
},
|
|
|
|
|
|
|
|
showImageUploader() {
|
|
|
|
this.set("selectedGraphicType", IMAGE);
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
changeGraphicType(newType) {
|
|
|
|
if (newType === IMAGE) {
|
|
|
|
this.showImageUploader();
|
|
|
|
} else if (newType === ICON) {
|
|
|
|
this.showIconSelector();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown badge graphic type "${newType}"`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
setImage(upload) {
|
|
|
|
this.buffered.setProperties({
|
|
|
|
image_upload_id: upload.id,
|
|
|
|
image_url: getURL(upload.url),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
removeImage() {
|
|
|
|
this.buffered.setProperties({
|
|
|
|
image_upload_id: null,
|
|
|
|
image_url: null,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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",
|
2021-03-17 01:55:23 -04:00
|
|
|
"image_upload_id",
|
2016-10-20 13:26:41 -04:00
|
|
|
"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) {
|
2021-01-27 06:39:20 -05:00
|
|
|
let 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
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|