Skip to content

Commit

Permalink
feat: ConfirmNavigationController - Support for Hotwire/Turbo and Tur…
Browse files Browse the repository at this point in the history
…bolinks navigation events
  • Loading branch information
Sub-Xaero committed Mar 2, 2021
1 parent f9be6e8 commit f698920
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 18 deletions.
10 changes: 7 additions & 3 deletions dist/confirm_navigation_controller.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { BaseController } from "./base_controller";
export declare class ConfirmNavigationController extends BaseController {
static values: {
message: StringConstructor;
_message: StringConstructor;
};
readonly messageValue: string;
readonly _messageValue: string;
readonly hasMessageValue: boolean;
get _message(): string;
initialize(): void;
connect(): void;
handlePopstate(event: PopStateEvent): boolean;
handlePopstate(_event: PopStateEvent): boolean;
handleTurboNavigation(event: Event): void;
}
//# sourceMappingURL=confirm_navigation_controller.d.ts.map
2 changes: 1 addition & 1 deletion dist/confirm_navigation_controller.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/stimulus-library.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.modern.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.modern.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.module.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.umd.js.map

Large diffs are not rendered by default.

28 changes: 22 additions & 6 deletions src/confirm_navigation_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@ import {BaseController} from "./base_controller";

export class ConfirmNavigationController extends BaseController {

static values = {message: String};
static values = {_message: String};

declare readonly messageValue: string;
declare readonly _messageValue: string;
declare readonly hasMessageValue: boolean;

get _message(): string {
return this.hasMessageValue ? this._messageValue : "Do you want to leave this page? Changes you made may not be saved";
}

initialize() {
this.handlePopstate = this.handlePopstate.bind(this);
this.handleTurboNavigation = this.handleTurboNavigation.bind(this);
}

connect() {
let confirmMessage = this.messageValue;
window.onbeforeunload = () => (confirmMessage == null ? true : confirmMessage);
window.onbeforeunload = () => this._message;
window.addEventListener("popstate", this.handlePopstate);
window.addEventListener("submit", () => {
window.removeEventListener("popstate", this.handlePopstate);
window.onbeforeunload = null;
});
// TODO: Turbo navigation events
window.addEventListener("turbolinks:before-visit", this.handleTurboNavigation);
window.addEventListener("turbo:before-visit", this.handleTurboNavigation);
}

handlePopstate(event: PopStateEvent) {
handlePopstate(_event: PopStateEvent) {
return false;
}

handleTurboNavigation(event: Event) {
if (!confirm(this._message)) {
event.preventDefault();
}
}

}

0 comments on commit f698920

Please sign in to comment.