2024-05-29 12:39:58 +08:00
|
|
|
import Component from "@glimmer/component";
|
2024-06-05 11:27:06 +08:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
import { action } from "@ember/object";
|
2024-07-25 21:09:06 +08:00
|
|
|
import { service } from "@ember/service";
|
2024-06-05 11:27:06 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2024-08-06 08:50:12 +08:00
|
|
|
import { SYSTEM_FLAG_IDS } from "discourse/lib/constants";
|
2024-05-29 12:39:58 +08:00
|
|
|
import i18n from "discourse-common/helpers/i18n";
|
2024-06-05 11:27:06 +08:00
|
|
|
import { bind } from "discourse-common/utils/decorators";
|
2024-07-03 06:45:37 +08:00
|
|
|
import AdminConfigHeader from "admin/components/admin-config-header";
|
2024-05-29 12:39:58 +08:00
|
|
|
import AdminFlagItem from "admin/components/admin-flag-item";
|
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
export default class AdminConfigAreasFlags extends Component {
|
2024-05-29 12:39:58 +08:00
|
|
|
@service site;
|
2024-08-06 08:50:12 +08:00
|
|
|
@service siteSettings;
|
2024-06-05 11:27:06 +08:00
|
|
|
@tracked flags = this.site.flagTypes;
|
|
|
|
|
2024-08-06 08:50:12 +08:00
|
|
|
get addFlagButtonDisabled() {
|
|
|
|
return (
|
|
|
|
this.flags.filter(
|
|
|
|
(flag) => !Object.values(SYSTEM_FLAG_IDS).includes(flag.id)
|
|
|
|
).length >= this.siteSettings.custom_flags_limit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-05 11:27:06 +08:00
|
|
|
@bind
|
|
|
|
isFirstFlag(flag) {
|
|
|
|
return this.flags.indexOf(flag) === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@bind
|
|
|
|
isLastFlag(flag) {
|
|
|
|
return this.flags.indexOf(flag) === this.flags.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
moveFlagCallback(flag, direction) {
|
|
|
|
const fallbackFlags = [...this.flags];
|
|
|
|
|
|
|
|
const flags = this.flags;
|
|
|
|
|
|
|
|
const flagIndex = flags.indexOf(flag);
|
|
|
|
const targetFlagIndex = direction === "up" ? flagIndex - 1 : flagIndex + 1;
|
|
|
|
|
|
|
|
const targetFlag = flags[targetFlagIndex];
|
|
|
|
|
|
|
|
flags[flagIndex] = targetFlag;
|
|
|
|
flags[targetFlagIndex] = flag;
|
|
|
|
|
|
|
|
this.flags = flags;
|
|
|
|
|
|
|
|
return ajax(`/admin/config/flags/${flag.id}/reorder/${direction}`, {
|
|
|
|
type: "PUT",
|
|
|
|
}).catch((error) => {
|
|
|
|
this.flags = fallbackFlags;
|
|
|
|
return popupAjaxError(error);
|
|
|
|
});
|
|
|
|
}
|
2024-05-29 12:39:58 +08:00
|
|
|
|
2024-07-03 06:45:37 +08:00
|
|
|
@action
|
|
|
|
deleteFlagCallback(flag) {
|
|
|
|
return ajax(`/admin/config/flags/${flag.id}`, {
|
|
|
|
type: "DELETE",
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.flags.removeObject(flag);
|
|
|
|
})
|
|
|
|
.catch((error) => popupAjaxError(error));
|
|
|
|
}
|
|
|
|
|
2024-05-29 12:39:58 +08:00
|
|
|
<template>
|
|
|
|
<div class="container admin-flags">
|
2024-07-03 06:45:37 +08:00
|
|
|
<AdminConfigHeader
|
|
|
|
@name="flags"
|
|
|
|
@heading="admin.config_areas.flags.header"
|
|
|
|
@subheading="admin.config_areas.flags.subheader"
|
|
|
|
@primaryActionRoute="adminConfig.flags.new"
|
|
|
|
@primaryActionCssClass="admin-flags__header-add-flag"
|
|
|
|
@primaryActionIcon="plus"
|
|
|
|
@primaryActionLabel="admin.config_areas.flags.add"
|
2024-08-06 08:50:12 +08:00
|
|
|
@primaryActionDisabled={{this.addFlagButtonDisabled}}
|
2024-07-03 06:45:37 +08:00
|
|
|
/>
|
|
|
|
<table class="admin-flags__items grid">
|
2024-05-29 12:39:58 +08:00
|
|
|
<thead>
|
2024-07-03 06:45:37 +08:00
|
|
|
<th>{{i18n "admin.config_areas.flags.description"}}</th>
|
|
|
|
<th>{{i18n "admin.config_areas.flags.enabled"}}</th>
|
2024-05-29 12:39:58 +08:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{{#each this.flags as |flag|}}
|
2024-06-05 11:27:06 +08:00
|
|
|
<AdminFlagItem
|
|
|
|
@flag={{flag}}
|
|
|
|
@moveFlagCallback={{this.moveFlagCallback}}
|
2024-07-03 06:45:37 +08:00
|
|
|
@deleteFlagCallback={{this.deleteFlagCallback}}
|
2024-06-05 11:27:06 +08:00
|
|
|
@isFirstFlag={{this.isFirstFlag flag}}
|
|
|
|
@isLastFlag={{this.isLastFlag flag}}
|
|
|
|
/>
|
2024-05-29 12:39:58 +08:00
|
|
|
{{/each}}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
}
|