2020-02-18 08:57:00 +01:00
|
|
|
import { Component, Mixins, Vue } from "vue-property-decorator";
|
|
|
|
import { Person } from "@/types/actor";
|
2019-11-18 17:37:38 +01:00
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
// TODO: Refactor into js/src/utils/username.ts
|
|
|
|
@Component
|
2019-11-18 17:37:38 +01:00
|
|
|
export default class IdentityEditionMixin extends Mixins(Vue) {
|
2019-12-17 19:32:48 +01:00
|
|
|
identity: Person = new Person();
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2019-11-18 17:37:38 +01:00
|
|
|
oldDisplayName: string | null = null;
|
|
|
|
|
2020-11-17 10:57:04 +01:00
|
|
|
autoUpdateUsername(newDisplayName: string | null): void {
|
2020-11-30 10:24:11 +01:00
|
|
|
const oldUsername = IdentityEditionMixin.convertToUsername(
|
|
|
|
this.oldDisplayName
|
|
|
|
);
|
2019-11-18 17:37:38 +01:00
|
|
|
|
|
|
|
if (this.identity.preferredUsername === oldUsername) {
|
2021-05-17 19:01:08 +02:00
|
|
|
this.identity.preferredUsername =
|
|
|
|
IdentityEditionMixin.convertToUsername(newDisplayName);
|
2019-11-18 17:37:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.oldDisplayName = newDisplayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static convertToUsername(value: string | null) {
|
2020-02-18 08:57:00 +01:00
|
|
|
if (!value) return "";
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/37511463
|
|
|
|
return value
|
|
|
|
.toLocaleLowerCase()
|
|
|
|
.normalize("NFD")
|
|
|
|
.replace(/[\u0300-\u036f]/g, "")
|
2021-11-09 09:01:40 +01:00
|
|
|
.replace(/\s{2,}/, " ")
|
2020-02-18 08:57:00 +01:00
|
|
|
.replace(/ /g, "_")
|
2021-11-09 09:01:40 +01:00
|
|
|
.replace(/[^a-z0-9_]/g, "")
|
|
|
|
.replace(/_{2,}/, "");
|
2019-11-18 17:37:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 10:57:04 +01:00
|
|
|
validateUsername(): boolean {
|
2020-02-18 08:57:00 +01:00
|
|
|
return (
|
|
|
|
this.identity.preferredUsername ===
|
|
|
|
IdentityEditionMixin.convertToUsername(this.identity.preferredUsername)
|
|
|
|
);
|
2019-11-18 17:37:38 +01:00
|
|
|
}
|
|
|
|
}
|