summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki/mediawiki.filewarning.js
blob: 882affe17fe1ff7bb63fbf77dc1c2a735763eca9 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*!
 * mediawiki.filewarning
 *
 * @author Mark Holmquist, 2015
 * @since 1.25
 */
/*global OO*/
( function ( mw, $, oo ) {
	var warningConfig = mw.config.get( 'wgFileWarning' ),
		warningMessages = warningConfig.messages,
		warningLink = warningConfig.link,
		$origMimetype = $( '.fullMedia .fileInfo .mime-type' ),
		$mimetype = $origMimetype.clone(),
		$header = $( '<h3>' )
			.addClass( 'mediawiki-filewarning-header empty' ),
		$main = $( '<p>' )
			.addClass( 'mediawiki-filewarning-main empty' ),
		$info = $( '<a>' )
			.addClass( 'mediawiki-filewarning-info empty' ),
		$footer = $( '<p>' )
			.addClass( 'mediawiki-filewarning-footer empty' ),
		dialog = new oo.ui.PopupButtonWidget( {
			classes: [ 'mediawiki-filewarning-anchor' ],
			label: $mimetype,
			flags: [ 'warning' ],
			icon: 'alert',
			framed: false,
			popup: {
				classes: [ 'mediawiki-filewarning' ],
				padded: true,
				width: 400,
				$content: $header.add( $main ).add( $info ).add( $footer )
			}
		} );

	function loadMessage( $target, message ) {
		if ( message ) {
			$target.removeClass( 'empty' )
				.text( mw.message( message ).text() );
		}
	}

	// The main message must be populated for the dialog to show.
	if ( warningConfig && warningConfig.messages && warningConfig.messages.main ) {
		$mimetype.addClass( 'has-warning' );

		$origMimetype.replaceWith( dialog.$element );

		if ( warningMessages ) {
			loadMessage( $main, warningMessages.main );
			loadMessage( $header, warningMessages.header );
			loadMessage( $footer, warningMessages.footer );

			if ( warningLink ) {
				loadMessage( $info, warningMessages.info );
				$info.attr( 'href', warningLink );
			}
		}

		// Make OOUI open the dialog, it won't appear until the user
		// hovers over the warning.
		dialog.getPopup().toggle( true );

		// Override toggle handler because we don't need it for this popup
		// object at all. Sort of nasty, but it gets the job done.
		dialog.getPopup().toggle = $.noop;
	}
}( mediaWiki, jQuery, OO ) );