2023-10-26 16:01:15 +02:00
|
|
|
import { isFormInputElement, matchInputElement } from '@coldwired/utils';
|
2023-01-17 13:38:08 +01:00
|
|
|
|
2022-10-27 17:07:18 +02:00
|
|
|
import { ApplicationController } from './application_controller';
|
2023-01-11 21:45:41 +01:00
|
|
|
|
|
|
|
const AUTOSUBMIT_DATE_DEBOUNCE_DELAY = 5000;
|
2023-01-17 13:38:08 +01:00
|
|
|
const AUTOSUBMIT_EVENTS = ['input', 'change', 'blur'];
|
2022-10-27 17:07:18 +02:00
|
|
|
|
|
|
|
export class AutosubmitController extends ApplicationController {
|
2023-12-11 17:30:06 +01:00
|
|
|
static targets = ['submitter', 'input'];
|
2024-01-31 11:30:06 +01:00
|
|
|
static values = {
|
|
|
|
debounceDelay: { type: Number, default: 500 }
|
|
|
|
};
|
2022-10-27 17:07:18 +02:00
|
|
|
|
2023-01-11 21:45:41 +01:00
|
|
|
declare readonly submitterTarget: HTMLButtonElement | HTMLInputElement;
|
|
|
|
declare readonly hasSubmitterTarget: boolean;
|
2023-12-11 17:30:06 +01:00
|
|
|
declare readonly inputTarget: HTMLInputElement;
|
2023-12-13 16:14:37 +01:00
|
|
|
declare readonly hasInputTarget: boolean;
|
2024-01-31 11:30:06 +01:00
|
|
|
declare readonly debounceDelayValue: number;
|
2022-10-27 17:07:18 +02:00
|
|
|
|
2023-01-17 13:38:08 +01:00
|
|
|
#dateTimeChangedInputs = new WeakSet<HTMLElement>();
|
2022-11-04 12:22:39 +01:00
|
|
|
|
2023-01-11 21:45:41 +01:00
|
|
|
connect() {
|
|
|
|
this.on('input', (event) => this.onInput(event));
|
|
|
|
this.on('change', (event) => this.onChange(event));
|
|
|
|
this.on('blur', (event) => this.onBlur(event));
|
2022-11-04 12:22:39 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 21:45:41 +01:00
|
|
|
private onChange(event: Event) {
|
2023-01-17 13:38:08 +01:00
|
|
|
const target = this.findTargetElement(event);
|
2023-01-11 21:45:41 +01:00
|
|
|
|
2023-10-26 16:01:15 +02:00
|
|
|
matchInputElement(target, {
|
|
|
|
date: (target) => {
|
2023-01-11 21:45:41 +01:00
|
|
|
if (target.value.trim() == '' || !isNaN(Date.parse(target.value))) {
|
|
|
|
this.#dateTimeChangedInputs.add(target);
|
|
|
|
this.debounce(this.submit, AUTOSUBMIT_DATE_DEBOUNCE_DELAY);
|
|
|
|
} else {
|
|
|
|
this.#dateTimeChangedInputs.delete(target);
|
|
|
|
this.cancelDebounce(this.submit);
|
|
|
|
}
|
2023-10-26 16:01:15 +02:00
|
|
|
},
|
|
|
|
text: () => this.submitNow(),
|
2023-12-08 10:38:51 +01:00
|
|
|
changeable: () => this.submitNow(),
|
|
|
|
hidden: () => this.submitNow()
|
2023-10-26 16:01:15 +02:00
|
|
|
});
|
2023-01-11 21:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private onInput(event: Event) {
|
2023-01-17 13:38:08 +01:00
|
|
|
const target = this.findTargetElement(event);
|
2023-01-11 21:45:41 +01:00
|
|
|
|
2023-10-26 16:01:15 +02:00
|
|
|
matchInputElement(target, {
|
|
|
|
date: () => {},
|
2024-01-31 11:30:06 +01:00
|
|
|
inputable: () => this.debounce(this.submit, this.debounceDelayValue),
|
|
|
|
hidden: () => this.debounce(this.submit, this.debounceDelayValue)
|
2023-10-26 16:01:15 +02:00
|
|
|
});
|
2023-01-11 21:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private onBlur(event: Event) {
|
2023-01-17 13:38:08 +01:00
|
|
|
const target = this.findTargetElement(event);
|
|
|
|
if (!target) return;
|
2023-01-11 21:45:41 +01:00
|
|
|
|
2023-10-26 16:01:15 +02:00
|
|
|
matchInputElement(target, {
|
|
|
|
date: () => {
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
if (this.#dateTimeChangedInputs.has(target)) {
|
|
|
|
this.submitNow();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-01-11 21:45:41 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 13:38:08 +01:00
|
|
|
private findTargetElement(event: Event) {
|
|
|
|
const target = event.target;
|
2023-12-11 17:30:06 +01:00
|
|
|
|
2023-01-17 13:38:08 +01:00
|
|
|
if (
|
|
|
|
!isFormInputElement(target) ||
|
|
|
|
this.preventAutosubmit(target, event.type)
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
private preventAutosubmit(
|
2023-12-08 10:38:51 +01:00
|
|
|
target: HTMLElement & { disabled?: boolean } & { value?: string },
|
2023-01-17 13:38:08 +01:00
|
|
|
type: string
|
|
|
|
) {
|
|
|
|
if (target.disabled) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-12-11 17:30:06 +01:00
|
|
|
if (this.hasInputTarget && this.inputTarget != target) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-12-08 10:38:51 +01:00
|
|
|
if (
|
|
|
|
Boolean(target.getAttribute('data-no-autosubmit-on-empty')) &&
|
|
|
|
target.value == ''
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-17 13:38:08 +01:00
|
|
|
const noAutosubmit = this.parseNoAutosubmit(
|
|
|
|
target.getAttribute('data-no-autosubmit')
|
|
|
|
);
|
|
|
|
if (Array.isArray(noAutosubmit)) {
|
|
|
|
return noAutosubmit.includes(type);
|
|
|
|
}
|
|
|
|
return noAutosubmit;
|
|
|
|
}
|
|
|
|
|
|
|
|
private parseNoAutosubmit(value?: string | null): boolean | string[] {
|
|
|
|
if (value == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const eventTypes = value
|
|
|
|
.split(' ')
|
|
|
|
.map((token) => token.trim())
|
|
|
|
.filter((eventType) => AUTOSUBMIT_EVENTS.includes(eventType));
|
|
|
|
return eventTypes.length == 0 ? true : eventTypes;
|
|
|
|
}
|
|
|
|
|
2023-10-26 16:01:15 +02:00
|
|
|
private submitNow() {
|
|
|
|
this.cancelDebounce(this.submit);
|
|
|
|
this.submit();
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:45:41 +01:00
|
|
|
private submit() {
|
|
|
|
const submitter = this.hasSubmitterTarget ? this.submitterTarget : null;
|
|
|
|
const form =
|
|
|
|
submitter?.form ?? this.element.closest<HTMLFormElement>('form');
|
2023-01-12 16:15:27 +01:00
|
|
|
|
|
|
|
// Safari does not support "formaction" attribute on submitter passed to requestSubmit :(
|
|
|
|
if (submitter && navigator.userAgent.indexOf('Safari') > -1) {
|
|
|
|
submitter.click();
|
|
|
|
} else {
|
|
|
|
form?.requestSubmit(submitter);
|
|
|
|
}
|
2022-10-27 17:07:18 +02:00
|
|
|
}
|
|
|
|
}
|