summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki.action/mediawiki.action.edit.editWarning.js
blob: 56dba703f451a5412a90631ba49bf27aa71a0810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * Javascript for module editWarning
 */
( function ( mw, $ ) {
	'use strict';

	$( function () {
		var allowCloseWindow,
			$textBox = $( '#wpTextbox1' ),
			$summary = $( '#wpSummary' ),
			$both = $textBox.add( $summary );

		// Check if EditWarning is enabled and if we need it
		if ( !mw.user.options.get( 'useeditwarning' ) ) {
			return true;
		}

		// Save the original value of the text fields
		$both.each( function ( index, element ) {
			var $element = $( element );
			$element.data( 'origtext', $element.textSelection( 'getContents' ) );
		} );

		allowCloseWindow = mw.confirmCloseWindow( {
			test: function () {
				// We use .textSelection, because editors might not have updated the form yet.
				return mw.config.get( 'wgAction' ) === 'submit' ||
					$textBox.data( 'origtext' ) !== $textBox.textSelection( 'getContents' ) ||
					$summary.data( 'origtext' ) !== $summary.textSelection( 'getContents' );
			},

			message: mw.msg( 'editwarning-warning' ),
			namespace: 'editwarning'
		} );

		// Add form submission handler
		$( '#editform' ).submit( function () {
			allowCloseWindow.release();
		} );
	} );

}( mediaWiki, jQuery ) );