Ghost/ghost/admin/app/components/gh-url-input.js
Rishabh Garg 487724e0fe Added support for external urls for redirect (#1778)
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>
2020-11-23 09:38:29 +00:00

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;
}
}
}