2020-11-20 12:53:08 +03:00
|
|
|
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;
|
2020-11-23 12:38:29 +03:00
|
|
|
this.validateUrl = args.validateUrl;
|
2021-03-09 11:22:41 +03:00
|
|
|
this.placeholder = args.placeholder;
|
2020-11-20 12:53:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setValue(event) {
|
|
|
|
this.value = event.target.value;
|
2020-11-23 12:38:29 +03:00
|
|
|
this.setResult(this.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
validateUrlInput() {
|
|
|
|
this.validateUrl(this.result);
|
2020-11-20 12:53:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get result() {
|
|
|
|
try {
|
|
|
|
return new URL(removeLeadingSlash(this.value), this.baseUrl);
|
|
|
|
} catch (err) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|