Skip to content

Commit

Permalink
Add types for mediawiki.confirmCloseWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien LESÉNÉCHAL committed Mar 8, 2024
1 parent 773af71 commit dffd1ac
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
69 changes: 69 additions & 0 deletions mw/confirmCloseWindow.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
interface Options {
/**
* Optional jQuery event namespace, to allow loosely coupled
* external code to release your trigger. For example, the VisualEditor extension can use this
* remove the trigger registered by mediawiki.action.edit, without strong runtime coupling.
*/
namespace?: string;

/**
* @returns {boolean} Whether to show the dialog to the user.
*/
test?(): boolean;
}

interface ConfirmCloseWindow {
/**
* Remove the event listener and don't show an alert anymore, if the user wants to leave
* the page.
*/
release(): void;

/**
* Trigger the module's function manually.
*
* Check, if options.test() returns true and show an alert to the user if he/she want
* to leave this page. Returns false, if options.test() returns false or the user
* cancelled the alert window (~don't leave the page), true otherwise.
*
* @returns {boolean}
*/
trigger(): boolean;
}

declare global {
namespace mw {
/**
* Prevent the closing of a window with a confirm message (the onbeforeunload event seems to
* work in most browsers.)
*
* This supersedes any previous onbeforeunload handler. If there was a handler before, it is
* restored when you execute the returned release() function.
*
* ```js
* var allowCloseWindow = mw.confirmCloseWindow();
* // ... do stuff that can't be interrupted ...
* allowCloseWindow.release();
* ```
*
* The second function returned is a trigger function to trigger the check and an alert
* window manually, e.g.:
*
* ```js
* var allowCloseWindow = mw.confirmCloseWindow();
* // ... do stuff that can't be interrupted ...
* if ( allowCloseWindow.trigger() ) {
* // don't do anything (e.g. destroy the input field)
* } else {
* // do whatever you wanted to do
* }
* ```
*
* @param {Options} [options]
* @returns {ConfirmCloseWindow} An object of functions to work with this module
*/
function confirmCloseWindow(options?: Options): ConfirmCloseWindow;
}
}

export {};
1 change: 1 addition & 0 deletions mw/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./Api";
import "./config";
import "./confirmCloseWindow";
import "./cookie";
import "./ForeignApi";
import "./ForeignRest";
Expand Down

0 comments on commit dffd1ac

Please sign in to comment.