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