Merge "Add tests for API action=edit&section=new"
[lhc/web/wiklou.git] / resources / mediawiki.page / mediawiki.page.gallery.js
1 /**
2 * Show gallery captions when focused. Copied directly from jquery.mw-jump.js.
3 * Also Dynamically resize images to justify them.
4 */
5 ( function ( $, mw ) {
6 $( function () {
7 var isTouchScreen,
8 gettingFocus,
9 galleries = 'ul.mw-gallery-packed-overlay, ul.mw-gallery-packed-hover, ul.mw-gallery-packed';
10
11 // Is there a better way to detect a touchscreen? Current check taken from stack overflow.
12 isTouchScreen = !!( window.ontouchstart !== undefined || window.DocumentTouch !== undefined && document instanceof window.DocumentTouch );
13
14 if ( isTouchScreen ) {
15 // Always show the caption for a touch screen.
16 $( 'ul.mw-gallery-packed-hover' )
17 .addClass( 'mw-gallery-packed-overlay' )
18 .removeClass( 'mw-gallery-packed-hover' );
19 } else {
20 // Note use of just "a", not a.image, since we want this to trigger if a link in
21 // the caption receives focus
22 $( 'ul.mw-gallery-packed-hover li.gallerybox' ).on( 'focus blur', 'a', function ( e ) {
23 // Confusingly jQuery leaves e.type as focusout for delegated blur events
24 gettingFocus = e.type !== 'blur' && e.type !== 'focusout';
25 $( this ).closest( 'li.gallerybox' ).toggleClass( 'mw-gallery-focused', gettingFocus );
26 } );
27 }
28
29 // Now on to justification.
30 // We may still get ragged edges if someone resizes their window. Could bind to
31 // that event, otoh do we really want to constantly be resizing galleries?
32 $( galleries ).each( function() {
33 var lastTop,
34 $img,
35 imgWidth,
36 imgHeight,
37 rows = [],
38 $gallery = $( this );
39
40 $gallery.children( 'li' ).each( function() {
41 // Math.floor to be paranoid if things are off by 0.00000000001
42 var top = Math.floor( $(this ).position().top ),
43 $this = $( this );
44
45 if ( top !== lastTop ) {
46 rows[rows.length] = [];
47 lastTop = top;
48 }
49
50 $img = $this.find( 'div.thumb a.image img' );
51 if ( $img.length && $img[0].height ) {
52 imgHeight = $img[0].height;
53 imgWidth = $img[0].width;
54 } else {
55 // If we don't have a real image, get the containing divs width/height.
56 // Note that if we do have a real image, using this method will generally
57 // give the same answer, but can be different in the case of a very
58 // narrow image where extra padding is added.
59 imgHeight = $this.children().children( 'div:first' ).height();
60 imgWidth = $this.children().children( 'div:first' ).width();
61 }
62
63 // Hack to make an edge case work ok
64 if ( imgHeight < 30 ) {
65 // Don't try and resize this item.
66 imgHeight = 0;
67 }
68
69 rows[rows.length-1][rows[rows.length-1].length] = {
70 $elm: $this,
71 width: $this.outerWidth(),
72 imgWidth: imgWidth,
73 aspect: imgWidth / imgHeight, // XXX: can divide by 0 ever happen?
74 captionWidth: $this.children().children( 'div.gallerytextwrapper' ).width(),
75 height: imgHeight
76 };
77 });
78
79 (function () {
80 var maxWidth,
81 combinedAspect,
82 combinedPadding,
83 curRow,
84 curRowHeight,
85 wantedWidth,
86 preferredHeight,
87 newWidth,
88 padding,
89 $outerDiv,
90 $innerDiv,
91 $imageDiv,
92 $imageElm,
93 imageElm,
94 $caption,
95 hookInfo,
96 i,
97 j;
98
99 for ( i = 0; i < rows.length; i++ ) {
100 maxWidth = $gallery.width();
101 combinedAspect = 0;
102 combinedPadding = 0;
103 curRow = rows[i];
104 curRowHeight = 0;
105
106 for ( j = 0; j < curRow.length; j++ ) {
107 if ( curRowHeight === 0 ) {
108 if ( isFinite( curRow[j].height ) ) {
109 // Get the height of this row, by taking the first
110 // non-out of bounds height
111 curRowHeight = curRow[j].height;
112 }
113 }
114
115 if ( curRow[j].aspect === 0 || !isFinite( curRow[j].aspect ) ) {
116 mw.log( 'Skipping item ' + j + ' due to aspect: ' + curRow[j].aspect );
117 // One of the dimensions are 0. Probably should
118 // not try to resize.
119 combinedPadding += curRow[j].width;
120 } else {
121 combinedAspect += curRow[j].aspect;
122 combinedPadding += curRow[j].width - curRow[j].imgWidth;
123 }
124 }
125
126 // Add some padding for inter-element spacing.
127 combinedPadding += 5 * curRow.length;
128 wantedWidth = maxWidth - combinedPadding;
129 preferredHeight = wantedWidth / combinedAspect;
130
131 if ( preferredHeight > curRowHeight * 1.5 ) {
132 // Only expand at most 1.5 times current size
133 // As that's as high a resolution as we have.
134 // Also on the off chance there is a bug in this
135 // code, would prevent accidentally expanding to
136 // be 10 billion pixels wide.
137 mw.log( 'mw.page.gallery: Cannot fit row, aspect is ' + preferredHeight/curRowHeight );
138 preferredHeight = 1.5 * curRowHeight;
139 }
140 if ( !isFinite( preferredHeight ) ) {
141 // This *definitely* should not happen.
142 mw.log( 'mw.page.gallery: Trying to resize row ' + i + ' to ' + preferredHeight + '?!' );
143 // Skip this row.
144 continue;
145 }
146 if ( preferredHeight < 5 ) {
147 // Well something clearly went wrong...
148 mw.log( {maxWidth: maxWidth, combinedPadding: combinedPadding, combinedAspect: combinedAspect, wantedWidth: wantedWidth } );
149 mw.log( 'mw.page.gallery: [BUG!] Fitting row ' + i + ' to too small a size: ' + preferredHeight );
150 // Skip this row.
151 continue;
152 }
153 for ( j = 0; j < curRow.length; j++ ) {
154 newWidth = preferredHeight * curRow[j].aspect;
155 padding = curRow[j].width - curRow[j].imgWidth;
156 $outerDiv = curRow[j].$elm;
157 $innerDiv = $outerDiv.children( 'div' ).first();
158 $imageDiv = $innerDiv.children( 'div.thumb' );
159 $imageElm = $imageDiv.find( 'img' ).first();
160 imageElm = $imageElm.length ? $imageElm[0] : null;
161 $caption = $outerDiv.find( 'div.gallerytextwrapper' );
162
163
164 // Since we are going to re-adjust the height, the vertical
165 // centering margins need to be reset.
166 $imageDiv.children( 'div' ).css( 'margin', '0px auto' );
167
168 if ( newWidth < 60 || !isFinite( newWidth ) ) {
169 // Making something skinnier than this will mess up captions,
170 mw.log( 'mw.page.gallery: Tried to make image ' + newWidth + 'px wide but too narrow.' );
171 if ( newWidth < 1 || !isFinite( newWidth ) ) {
172 $innerDiv.height( preferredHeight );
173 // Don't even try and touch the image size if it could mean
174 // making it disappear.
175 continue;
176 }
177 } else {
178 $outerDiv.width( newWidth + padding );
179 $innerDiv.width( newWidth + padding );
180 $imageDiv.width( newWidth );
181 $caption.width( curRow[j].captionWidth + (newWidth - curRow[j].imgWidth ) );
182 }
183
184 hookInfo = {
185 fullWidth: newWidth + padding,
186 imgWidth: newWidth,
187 imgHeight: preferredHeight,
188 $innerDiv: $innerDiv,
189 $imageDiv: $imageDiv,
190 $outerDiv: $outerDiv,
191 resolved: false /* Did the hook take action */
192 };
193 // Allow other media handlers to hook in.
194 // If your hook resizes an image, it is expected it will
195 // set resolved to true. Additionally you should load
196 // your module in position top to ensure it is registered
197 // before this runs (FIXME: there must be a better way?)
198 // See TimedMediaHandler for an example.
199 mw.hook( 'mediawiki.page.gallery.resize' ).fire( hookInfo );
200
201 if ( !hookInfo.resolved ) {
202 if ( imageElm ) {
203 // We don't always have an img, e.g. in the case of an invalid file.
204 imageElm.width = newWidth;
205 imageElm.height = preferredHeight;
206 } else {
207 // Not a file box.
208 $imageDiv.height( preferredHeight );
209 }
210 }
211 }
212 }
213 } )();
214 } );
215 } );
216 } )( jQuery, mediaWiki );