aboutsummaryrefslogtreecommitdiffstats
path: root/resources/src/mediawiki.page.media.js
blob: bb1beac26e8a89c088c2d3086e8c885ff4533299 (plain) (blame)
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
/*!
 * Add magnify links to thumbs and resize broken media spans, where needed
 */
( function () {
	mw.hook( 'wikipage.content' ).add( ( $content ) => {
		$content.find(
			'figure[typeof~="mw:File/Thumb"] > :not(figcaption) .mw-file-element'
		).each( function () {
			const inner = this.parentNode;
			const wrapper = inner.parentNode;

			if ( this.classList.contains( 'mw-broken-media' ) ) {
				// Resize broken media spans, where needed
				const isDefault = wrapper.classList.contains( 'mw-default-size' );
				if ( isDefault ) {
					return;
				}
				if ( this.hasAttribute( 'data-width' ) ) {
					this.style.width = this.getAttribute( 'data-width' ) + 'px';
				}
				if ( this.hasAttribute( 'data-height' ) ) {
					this.style.height = this.getAttribute( 'data-height' ) + 'px';
				}
			} else {
				// Add magnify links to thumbs, where needed
				const resource = this.getAttribute( 'resource' );
				if ( !resource ) {
					return;
				}
				if ( inner.classList.contains( 'mw-file-description' ) ) {
					return;
				}
				const desc = this.ownerDocument.createElement( 'a' );
				desc.setAttribute( 'href', resource );
				// Using a different class than mw-file-description to avoid the
				// expectation that the media will be found inside it
				desc.classList.add( 'mw-file-magnify' );
				wrapper.insertBefore( desc, inner.nextSibling );
			}
		} );
	} );
}() );