mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-29 07:09:48 +03:00
487724e0fe
refs https://github.com/TryGhost/Ghost/issues/12366 Adds support for saving external URL's for custom redirects and improves error handling to validate on blur/before saving Co-authored-by: Fabien O'Carroll <fabien@allou.is>
40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
import Component from '@glimmer/component';
|
|
import {action} from '@ember/object';
|
|
|
|
function ensureEndsWith(string, endsWith) {
|
|
return string.endsWith(endsWith) ? string : string + endsWith;
|
|
}
|
|
|
|
function removeLeadingSlash(string) {
|
|
return string.replace(/^\//, '');
|
|
}
|
|
|
|
export default class GhUrlInput extends Component {
|
|
constructor(owner, args) {
|
|
super(owner, args);
|
|
this.baseUrl = ensureEndsWith(args.baseUrl, '/');
|
|
this.value = args.value && args.value !== '/' ? (new URL(removeLeadingSlash(args.value), this.baseUrl)).href : '';
|
|
this.setResult = args.setResult;
|
|
this.validateUrl = args.validateUrl;
|
|
}
|
|
|
|
@action
|
|
setValue(event) {
|
|
this.value = event.target.value;
|
|
this.setResult(this.result);
|
|
}
|
|
|
|
@action
|
|
validateUrlInput() {
|
|
this.validateUrl(this.result);
|
|
}
|
|
|
|
get result() {
|
|
try {
|
|
return new URL(removeLeadingSlash(this.value), this.baseUrl);
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|