2022-05-01 04:17:40 +02:00
|
|
|
import { Translation, TypedTranslation } from "../../UI/i18n/Translation"
|
2021-07-03 14:35:44 +02:00
|
|
|
import { TagsFilter } from "../../Logic/Tags/TagsFilter"
|
2021-08-07 23:11:34 +02:00
|
|
|
import { DeleteConfigJson } from "./Json/DeleteConfigJson"
|
2021-07-03 14:35:44 +02:00
|
|
|
import Translations from "../../UI/i18n/Translations"
|
2021-08-07 23:11:34 +02:00
|
|
|
import { TagUtils } from "../../Logic/Tags/TagUtils"
|
2023-06-14 20:39:36 +02:00
|
|
|
import TagRenderingConfig from "./TagRenderingConfig"
|
|
|
|
import { QuestionableTagRenderingConfigJson } from "./Json/QuestionableTagRenderingConfigJson"
|
|
|
|
import { TagConfigJson } from "./Json/TagConfigJson"
|
2021-07-03 14:35:44 +02:00
|
|
|
|
|
|
|
export default class DeleteConfig {
|
2023-06-01 23:40:05 +02:00
|
|
|
public static readonly deleteReasonKey = "_delete_reason"
|
2022-12-06 00:49:34 +01:00
|
|
|
private static readonly defaultDeleteReasons: {
|
2022-05-01 04:17:40 +02:00
|
|
|
changesetMessage: string
|
|
|
|
explanation: Translation
|
|
|
|
}[] = [
|
|
|
|
{
|
|
|
|
changesetMessage: "testing point",
|
|
|
|
explanation: Translations.t.delete.reasons.test,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
changesetMessage: "disused",
|
|
|
|
explanation: Translations.t.delete.reasons.disused,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
changesetMessage: "not found",
|
|
|
|
explanation: Translations.t.delete.reasons.notFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
changesetMessage: "duplicate",
|
|
|
|
explanation: Translations.t.delete.reasons.duplicate,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2022-12-06 00:49:34 +01:00
|
|
|
public readonly deleteReasons?: {
|
|
|
|
explanation: TypedTranslation<object> | Translation
|
2021-07-03 14:35:44 +02:00
|
|
|
changesetMessage: string
|
|
|
|
}[]
|
|
|
|
|
2023-06-01 23:40:05 +02:00
|
|
|
private readonly nonDeleteMappings?: { if: TagConfigJson; then: Translation }[]
|
2021-07-03 14:35:44 +02:00
|
|
|
|
2021-08-07 23:11:34 +02:00
|
|
|
public readonly softDeletionTags?: TagsFilter
|
|
|
|
public readonly neededChangesets?: number
|
2021-07-03 14:35:44 +02:00
|
|
|
|
|
|
|
constructor(json: DeleteConfigJson, context: string) {
|
2022-12-06 00:49:34 +01:00
|
|
|
this.deleteReasons = (json.extraDeleteReasons ?? []).map((reason, i) => {
|
2021-07-03 14:35:44 +02:00
|
|
|
const ctx = `${context}.extraDeleteReasons[${i}]`
|
|
|
|
if ((reason.changesetMessage ?? "").length <= 5) {
|
|
|
|
throw `${ctx}.explanation is too short, needs at least 4 characters`
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
explanation: Translations.T(reason.explanation, ctx + ".explanation"),
|
|
|
|
changesetMessage: reason.changesetMessage,
|
|
|
|
}
|
|
|
|
})
|
2022-12-06 00:49:34 +01:00
|
|
|
|
2022-12-16 13:45:07 +01:00
|
|
|
if (!json.omitDefaultDeleteReasons) {
|
2022-12-06 00:49:34 +01:00
|
|
|
for (const defaultDeleteReason of DeleteConfig.defaultDeleteReasons) {
|
|
|
|
this.deleteReasons.push({
|
|
|
|
changesetMessage: defaultDeleteReason.changesetMessage,
|
2022-12-16 13:45:07 +01:00
|
|
|
explanation:
|
|
|
|
defaultDeleteReason.explanation.Clone(/*Must clone, hides translation otherwise*/),
|
2022-12-06 00:49:34 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 04:17:40 +02:00
|
|
|
this.nonDeleteMappings = (json.nonDeleteMappings ?? []).map((nonDelete, i) => {
|
2021-07-03 14:35:44 +02:00
|
|
|
const ctx = `${context}.extraDeleteReasons[${i}]`
|
2023-06-01 23:40:05 +02:00
|
|
|
TagUtils.Tag(nonDelete.if, ctx + ".if") // for validation only
|
2021-07-03 14:35:44 +02:00
|
|
|
return {
|
2023-06-01 23:40:05 +02:00
|
|
|
if: nonDelete.if,
|
2021-07-03 14:35:44 +02:00
|
|
|
then: Translations.T(nonDelete.then, ctx + ".then"),
|
|
|
|
}
|
|
|
|
})
|
2021-08-07 23:11:34 +02:00
|
|
|
|
2022-12-16 13:45:07 +01:00
|
|
|
if (this.nonDeleteMappings.length + this.deleteReasons.length == 0) {
|
|
|
|
throw (
|
|
|
|
"At " +
|
|
|
|
context +
|
|
|
|
": a deleteconfig should have some reasons to delete: either the default delete reasons or a nonDeleteMapping or extraDeletereason should be given"
|
|
|
|
)
|
2022-12-06 00:49:34 +01:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:41:16 +02:00
|
|
|
this.softDeletionTags = undefined
|
2021-08-07 23:11:34 +02:00
|
|
|
if (json.softDeletionTags !== undefined) {
|
|
|
|
this.softDeletionTags = TagUtils.Tag(
|
|
|
|
json.softDeletionTags,
|
|
|
|
`${context}.softDeletionTags`
|
|
|
|
)
|
2021-07-03 14:35:44 +02:00
|
|
|
}
|
2021-08-07 23:11:34 +02:00
|
|
|
|
|
|
|
if (json["hardDeletionTags"] !== undefined) {
|
2021-08-05 16:20:25 +02:00
|
|
|
throw `You probably meant 'softDeletionTags' instead of 'hardDeletionTags' (at ${context})`
|
|
|
|
}
|
2021-07-03 14:35:44 +02:00
|
|
|
this.neededChangesets = json.neededChangesets
|
|
|
|
}
|
2023-06-01 23:40:05 +02:00
|
|
|
|
|
|
|
public constructTagRendering(): TagRenderingConfig {
|
|
|
|
const t = Translations.t.delete
|
|
|
|
|
2023-06-14 20:39:36 +02:00
|
|
|
const mappings: { if: TagConfigJson; then: Record<string, string> }[] = []
|
2023-06-01 23:40:05 +02:00
|
|
|
for (const nonDeleteMapping of this.nonDeleteMappings) {
|
|
|
|
mappings.push({
|
|
|
|
if: nonDeleteMapping.if,
|
2023-06-14 20:39:36 +02:00
|
|
|
then: nonDeleteMapping.then.translations,
|
2023-06-01 23:40:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const deleteReason of this.deleteReasons) {
|
|
|
|
mappings.push({
|
2023-06-14 20:39:36 +02:00
|
|
|
if: DeleteConfig.deleteReasonKey + "=" + deleteReason.changesetMessage,
|
|
|
|
then: deleteReason.explanation.translations,
|
2023-06-01 23:40:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const config: QuestionableTagRenderingConfigJson = {
|
|
|
|
question: t.whyDelete.translations,
|
2023-06-14 20:39:36 +02:00
|
|
|
mappings,
|
2023-06-01 23:40:05 +02:00
|
|
|
}
|
|
|
|
return new TagRenderingConfig(config)
|
|
|
|
}
|
2021-07-03 14:35:44 +02:00
|
|
|
}
|