@stimulus-library/controllers
Version:
A library of useful controllers for Stimulus
31 lines (30 loc) • 1.02 kB
JavaScript
import { BaseController, isTurboFrame } from "@stimulus-library/utilities";
import { useTimeout } from "@stimulus-library/mixins";
export class TurboFrameRefreshController extends BaseController {
get _poll() {
return this.hasPollValue ? this.pollValue : false;
}
connect() {
const element = this.el;
if (isTurboFrame(element)) {
if (element.src) {
throw new Error("The provided <turbo-frame> element has no `src` attribute.");
}
}
else {
throw new Error("Expected controller to be mounted on a <turbo-frame> element.");
}
if (this._poll) {
requestAnimationFrame(() => useTimeout(this, this.refresh, this.intervalValue));
}
}
refresh(event) {
event === null || event === void 0 ? void 0 : event.preventDefault();
const element = this.el;
element.reload();
}
}
TurboFrameRefreshController.values = {
interval: Number,
poll: Boolean,
};