Merge "Minor fixes to CRH language conversion."
[lhc/web/wiklou.git] / resources / src / mediawiki.page.gallery.js
1 /*!
2 * Enhance MediaWiki galleries (from the `<gallery>` parser tag).
3 *
4 * - Toggle gallery captions when focused.
5 * - Dynamically resize images to fill horizontal space.
6 */
7 ( function ( mw, $ ) {
8 var $galleries,
9 bound = false,
10 // Is there a better way to detect a touchscreen? Current check taken from stack overflow.
11 isTouchScreen = !!( window.ontouchstart !== undefined ||
12 window.DocumentTouch !== undefined && document instanceof window.DocumentTouch
13 );
14
15 /**
16 * Perform the layout justification.
17 *
18 * @ignore
19 * @context {HTMLElement} A `ul.mw-gallery-*` element
20 */
21 function justify() {
22 var lastTop,
23 $img,
24 imgWidth,
25 imgHeight,
26 captionWidth,
27 rows = [],
28 $gallery = $( this );
29
30 $gallery.children( 'li.gallerybox' ).each( function () {
31 // Math.floor to be paranoid if things are off by 0.00000000001
32 var top = Math.floor( $( this ).position().top ),
33 $this = $( this );
34
35 if ( top !== lastTop ) {
36 rows[ rows.length ] = [];
37 lastTop = top;
38 }
39
40 $img = $this.find( 'div.thumb a.image img' );
41 if ( $img.length && $img[ 0 ].height ) {
42 imgHeight = $img[ 0 ].height;
43 imgWidth = $img[ 0 ].width;
44 } else {
45 // If we don't have a real image, get the containing divs width/height.
46 // Note that if we do have a real image, using this method will generally
47 // give the same answer, but can be different in the case of a very
48 // narrow image where extra padding is added.
49 imgHeight = $this.children().children( 'div:first' ).height();
50 imgWidth = $this.children().children( 'div:first' ).width();
51 }
52
53 // Hack to make an edge case work ok
54 if ( imgHeight < 30 ) {
55 // Don't try and resize this item.
56 imgHeight = 0;
57 }
58
59 captionWidth = $this.children().children( 'div.gallerytextwrapper' ).width();
60 rows[ rows.length - 1 ][ rows[ rows.length - 1 ].length ] = {
61 $elm: $this,
62 width: $this.outerWidth(),
63 imgWidth: imgWidth,
64 // XXX: can divide by 0 ever happen?
65 aspect: imgWidth / imgHeight,
66 captionWidth: captionWidth,
67 height: imgHeight
68 };
69
70 // Save all boundaries so we can restore them on window resize
71 $this.data( 'imgWidth', imgWidth );
72 $this.data( 'imgHeight', imgHeight );
73 $this.data( 'width', $this.outerWidth() );
74 $this.data( 'captionWidth', captionWidth );
75 } );
76
77 ( function () {
78 var maxWidth,
79 combinedAspect,
80 combinedPadding,
81 curRow,
82 curRowHeight,
83 wantedWidth,
84 preferredHeight,
85 newWidth,
86 padding,
87 $outerDiv,
88 $innerDiv,
89 $imageDiv,
90 $imageElm,
91 imageElm,
92 $caption,
93 i,
94 j,
95 avgZoom,
96 totalZoom = 0;
97
98 for ( i = 0; i < rows.length; i++ ) {
99 maxWidth = $gallery.width();
100 combinedAspect = 0;
101 combinedPadding = 0;
102 curRow = rows[ i ];
103 curRowHeight = 0;
104
105 for ( j = 0; j < curRow.length; j++ ) {
106 if ( curRowHeight === 0 ) {
107 if ( isFinite( curRow[ j ].height ) ) {
108 // Get the height of this row, by taking the first
109 // non-out of bounds height
110 curRowHeight = curRow[ j ].height;
111 }
112 }
113
114 if ( curRow[ j ].aspect === 0 || !isFinite( curRow[ j ].aspect ) ) {
115 // One of the dimensions are 0. Probably should
116 // not try to resize.
117 combinedPadding += curRow[ j ].width;
118 } else {
119 combinedAspect += curRow[ j ].aspect;
120 combinedPadding += curRow[ j ].width - curRow[ j ].imgWidth;
121 }
122 }
123
124 // Add some padding for inter-element spacing.
125 combinedPadding += 5 * curRow.length;
126 wantedWidth = maxWidth - combinedPadding;
127 preferredHeight = wantedWidth / combinedAspect;
128
129 if ( preferredHeight > curRowHeight * 1.5 ) {
130 // Only expand at most 1.5 times current size
131 // As that's as high a resolution as we have.
132 // Also on the off chance there is a bug in this
133 // code, would prevent accidentally expanding to
134 // be 10 billion pixels wide.
135 if ( i === rows.length - 1 ) {
136 // If its the last row, and we can't fit it,
137 // don't make the entire row huge.
138 avgZoom = ( totalZoom / ( rows.length - 1 ) ) * curRowHeight;
139 if ( isFinite( avgZoom ) && avgZoom >= 1 && avgZoom <= 1.5 ) {
140 preferredHeight = avgZoom;
141 } else {
142 // Probably a single row gallery
143 preferredHeight = curRowHeight;
144 }
145 } else {
146 preferredHeight = 1.5 * curRowHeight;
147 }
148 }
149 if ( !isFinite( preferredHeight ) ) {
150 // This *definitely* should not happen.
151 // Skip this row.
152 continue;
153 }
154 if ( preferredHeight < 5 ) {
155 // Well something clearly went wrong...
156 // Skip this row.
157 continue;
158 }
159
160 if ( preferredHeight / curRowHeight > 1 ) {
161 totalZoom += preferredHeight / curRowHeight;
162 } else {
163 // If we shrink, still consider that a zoom of 1
164 totalZoom += 1;
165 }
166
167 for ( j = 0; j < curRow.length; j++ ) {
168 newWidth = preferredHeight * curRow[ j ].aspect;
169 padding = curRow[ j ].width - curRow[ j ].imgWidth;
170 $outerDiv = curRow[ j ].$elm;
171 $innerDiv = $outerDiv.children( 'div' ).first();
172 $imageDiv = $innerDiv.children( 'div.thumb' );
173 $imageElm = $imageDiv.find( 'img' ).first();
174 imageElm = $imageElm.length ? $imageElm[ 0 ] : null;
175 $caption = $outerDiv.find( 'div.gallerytextwrapper' );
176
177 // Since we are going to re-adjust the height, the vertical
178 // centering margins need to be reset.
179 $imageDiv.children( 'div' ).css( 'margin', '0px auto' );
180
181 if ( newWidth < 60 || !isFinite( newWidth ) ) {
182 // Making something skinnier than this will mess up captions,
183 if ( newWidth < 1 || !isFinite( newWidth ) ) {
184 $innerDiv.height( preferredHeight );
185 // Don't even try and touch the image size if it could mean
186 // making it disappear.
187 continue;
188 }
189 } else {
190 $outerDiv.width( newWidth + padding );
191 $innerDiv.width( newWidth + padding );
192 $imageDiv.width( newWidth );
193 $caption.width( curRow[ j ].captionWidth + ( newWidth - curRow[ j ].imgWidth ) );
194 }
195
196 if ( imageElm ) {
197 // We don't always have an img, e.g. in the case of an invalid file.
198 imageElm.width = newWidth;
199 imageElm.height = preferredHeight;
200 } else {
201 // Not a file box.
202 $imageDiv.height( preferredHeight );
203 }
204 }
205 }
206 }() );
207 }
208
209 function handleResizeStart() {
210 $galleries.children( 'li.gallerybox' ).each( function () {
211 var imgWidth = $( this ).data( 'imgWidth' ),
212 imgHeight = $( this ).data( 'imgHeight' ),
213 width = $( this ).data( 'width' ),
214 captionWidth = $( this ).data( 'captionWidth' ),
215 $innerDiv = $( this ).children( 'div' ).first(),
216 $imageDiv = $innerDiv.children( 'div.thumb' ),
217 $imageElm, imageElm;
218
219 // Restore original sizes so we can arrange the elements as on freshly loaded page
220 $( this ).width( width );
221 $innerDiv.width( width );
222 $imageDiv.width( imgWidth );
223 $( this ).find( 'div.gallerytextwrapper' ).width( captionWidth );
224
225 $imageElm = $( this ).find( 'img' ).first();
226 imageElm = $imageElm.length ? $imageElm[ 0 ] : null;
227 if ( imageElm ) {
228 imageElm.width = imgWidth;
229 imageElm.height = imgHeight;
230 } else {
231 $imageDiv.height( imgHeight );
232 }
233 } );
234 }
235
236 function handleResizeEnd() {
237 $galleries.each( justify );
238 }
239
240 mw.hook( 'wikipage.content' ).add( function ( $content ) {
241 if ( isTouchScreen ) {
242 // Always show the caption for a touch screen.
243 $content.find( 'ul.mw-gallery-packed-hover' )
244 .addClass( 'mw-gallery-packed-overlay' )
245 .removeClass( 'mw-gallery-packed-hover' );
246 } else {
247 // Note use of just `a`, not `a.image`, since we also want this to trigger if a link
248 // within the caption text receives focus.
249 // This is based on code from the 'jquery.mw-jump' module.
250 $content.find( 'ul.mw-gallery-packed-hover li.gallerybox' ).on( 'focus blur', 'a', function ( e ) {
251 // Confusingly jQuery leaves e.type as focusout for delegated blur events
252 var gettingFocus = e.type !== 'blur' && e.type !== 'focusout';
253 $( this ).closest( 'li.gallerybox' ).toggleClass( 'mw-gallery-focused', gettingFocus );
254 } );
255 }
256
257 $galleries = $content.find( 'ul.mw-gallery-packed-overlay, ul.mw-gallery-packed-hover, ul.mw-gallery-packed' );
258 // Call the justification asynchronous because live preview fires the hook with detached $content.
259 setTimeout( function () {
260 $galleries.each( justify );
261
262 // Bind here instead of in the top scope as the callbacks use $galleries.
263 if ( !bound ) {
264 bound = true;
265 $( window )
266 .resize( $.debounce( 300, true, handleResizeStart ) )
267 .resize( $.debounce( 300, handleResizeEnd ) );
268 }
269 } );
270 } );
271 }( mediaWiki, jQuery ) );