2022-05-03 19:43:49 +02:00
|
|
|
import { httpRequest } from '@utils';
|
|
|
|
|
|
|
|
import { ApplicationController } from './application_controller';
|
|
|
|
|
|
|
|
export class TurboInputController extends ApplicationController {
|
|
|
|
static values = {
|
2023-01-11 15:07:16 +01:00
|
|
|
url: String,
|
|
|
|
loadOnConnect: { type: Boolean, default: false }
|
2022-05-03 19:43:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
declare readonly urlValue: string;
|
2023-01-11 15:07:16 +01:00
|
|
|
declare readonly loadOnConnectValue: boolean;
|
2022-05-03 19:43:49 +02:00
|
|
|
|
|
|
|
connect(): void {
|
|
|
|
this.on('input', () => this.debounce(this.load, 200));
|
2023-01-11 15:07:16 +01:00
|
|
|
if (this.loadOnConnectValue) {
|
|
|
|
this.load();
|
|
|
|
}
|
2022-05-03 19:43:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private load(): void {
|
|
|
|
const target = this.element as HTMLInputElement;
|
|
|
|
const url = new URL(this.urlValue, document.baseURI);
|
|
|
|
url.searchParams.append(target.name, target.value);
|
|
|
|
httpRequest(url.toString()).turbo();
|
|
|
|
}
|
|
|
|
}
|