Merge "search: Add result ranking in MySQL"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.makeCollapsible.js
1 /**
2 * jQuery makeCollapsible
3 * Note: To avoid performance issues such as reflows, several styles are
4 * shipped in mediawiki.makeCollapsible.styles to reserve space for the toggle control. Please
5 * familiarise yourself with that CSS before making any changes to this code.
6 *
7 * Dual licensed:
8 * - CC BY 3.0 <http://creativecommons.org/licenses/by/3.0>
9 * - GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
10 *
11 * @class jQuery.plugin.makeCollapsible
12 */
13 ( function ( $, mw ) {
14 /**
15 * Handler for a click on a collapsible toggler.
16 *
17 * @private
18 * @param {jQuery} $collapsible
19 * @param {string} action The action this function will take ('expand' or 'collapse').
20 * @param {jQuery|null} [$defaultToggle]
21 * @param {Object|undefined} [options]
22 */
23 function toggleElement( $collapsible, action, $defaultToggle, options ) {
24 var $collapsibleContent, $containers, hookCallback;
25 options = options || {};
26
27 // Validate parameters
28
29 // $collapsible must be an instance of jQuery
30 if ( !$collapsible.jquery ) {
31 return;
32 }
33 if ( action !== 'expand' && action !== 'collapse' ) {
34 // action must be string with 'expand' or 'collapse'
35 return;
36 }
37 if ( $defaultToggle === undefined ) {
38 $defaultToggle = null;
39 }
40
41 // Trigger a custom event to allow callers to hook to the collapsing/expanding,
42 // allowing the module to be testable, and making it possible to
43 // e.g. implement persistence via cookies
44 $collapsible.trigger( action === 'expand' ? 'beforeExpand.mw-collapsible' : 'beforeCollapse.mw-collapsible' );
45 hookCallback = function () {
46 $collapsible.trigger( action === 'expand' ? 'afterExpand.mw-collapsible' : 'afterCollapse.mw-collapsible' );
47 };
48
49 // Handle different kinds of elements
50
51 if ( !options.plainMode && $collapsible.is( 'table' ) ) {
52 // Tables
53 // If there is a caption, hide all rows; otherwise, only hide body rows
54 if ( $collapsible.find( '> caption' ).length ) {
55 $containers = $collapsible.find( '> * > tr' );
56 } else {
57 $containers = $collapsible.find( '> tbody > tr' );
58 }
59 if ( $defaultToggle ) {
60 // Exclude table row containing togglelink
61 $containers = $containers.not( $defaultToggle.closest( 'tr' ) );
62 }
63
64 if ( action === 'collapse' ) {
65 // Hide all table rows of this table
66 // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
67 // http://stackoverflow.com/questions/467336#920480
68 if ( options.instantHide ) {
69 $containers.hide();
70 hookCallback();
71 } else {
72 $containers.stop( true, true ).fadeOut().promise().done( hookCallback );
73 }
74 } else {
75 $containers.stop( true, true ).fadeIn().promise().done( hookCallback );
76 }
77
78 } else if ( !options.plainMode && ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) ) {
79 // Lists
80 $containers = $collapsible.find( '> li' );
81 if ( $defaultToggle ) {
82 // Exclude list-item containing togglelink
83 $containers = $containers.not( $defaultToggle.parent() );
84 }
85
86 if ( action === 'collapse' ) {
87 if ( options.instantHide ) {
88 $containers.hide();
89 hookCallback();
90 } else {
91 $containers.stop( true, true ).slideUp().promise().done( hookCallback );
92 }
93 } else {
94 $containers.stop( true, true ).slideDown().promise().done( hookCallback );
95 }
96
97 } else {
98 // Everything else: <div>, <p> etc.
99 $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
100
101 // If a collapsible-content is defined, act on it
102 if ( !options.plainMode && $collapsibleContent.length ) {
103 if ( action === 'collapse' ) {
104 if ( options.instantHide ) {
105 $collapsibleContent.hide();
106 hookCallback();
107 } else {
108 $collapsibleContent.slideUp().promise().done( hookCallback );
109 }
110 } else {
111 $collapsibleContent.slideDown().promise().done( hookCallback );
112 }
113
114 // Otherwise assume this is a customcollapse with a remote toggle
115 // .. and there is no collapsible-content because the entire element should be toggled
116 } else {
117 if ( action === 'collapse' ) {
118 if ( options.instantHide ) {
119 $collapsible.hide();
120 hookCallback();
121 } else {
122 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
123 $collapsible.fadeOut().promise().done( hookCallback );
124 } else {
125 $collapsible.slideUp().promise().done( hookCallback );
126 }
127 }
128 } else {
129 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
130 $collapsible.fadeIn().promise().done( hookCallback );
131 } else {
132 $collapsible.slideDown().promise().done( hookCallback );
133 }
134 }
135 }
136 }
137 }
138
139 /**
140 * Handle clicking/keypressing on the collapsible element toggle and other
141 * situations where a collapsible element is toggled (e.g. the initial
142 * toggle for collapsed ones).
143 *
144 * @private
145 * @param {jQuery} $toggle the clickable toggle itself
146 * @param {jQuery} $collapsible the collapsible element
147 * @param {jQuery.Event|null} e either the event or null if unavailable
148 * @param {Object|undefined} options
149 */
150 function togglingHandler( $toggle, $collapsible, e, options ) {
151 var wasCollapsed, $textContainer, collapseText, expandText;
152 options = options || {};
153
154 if ( e ) {
155 if (
156 e.type === 'click' &&
157 e.target.nodeName.toLowerCase() === 'a' &&
158 $( e.target ).attr( 'href' )
159 ) {
160 // Don't fire if a link was clicked (for premade togglers)
161 return;
162 } else if ( e.type === 'keypress' && e.which !== 13 && e.which !== 32 ) {
163 // Only handle keypresses on the "Enter" or "Space" keys
164 return;
165 } else {
166 e.preventDefault();
167 e.stopPropagation();
168 }
169 }
170
171 // This allows the element to be hidden on initial toggle without fiddling with the class
172 if ( options.wasCollapsed !== undefined ) {
173 wasCollapsed = options.wasCollapsed;
174 } else {
175 wasCollapsed = $collapsible.hasClass( 'mw-collapsed' );
176 }
177
178 // Toggle the state of the collapsible element (that is, expand or collapse)
179 $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed );
180
181 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
182 if ( options.toggleClasses ) {
183 $toggle
184 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed )
185 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed );
186 }
187
188 // Toggle the text ("Show"/"Hide") within elements tagged with mw-collapsible-text
189 if ( options.toggleText ) {
190 collapseText = options.toggleText.collapseText;
191 expandText = options.toggleText.expandText;
192
193 $textContainer = $collapsible.find( '.mw-collapsible-text' );
194 if ( $textContainer.length ) {
195 $textContainer.text( wasCollapsed ? collapseText : expandText );
196 }
197 }
198
199 // And finally toggle the element state itself
200 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
201 }
202
203 /**
204 * Enable collapsible-functionality on all elements in the collection.
205 *
206 * - Will prevent binding twice to the same element.
207 * - Initial state is expanded by default, this can be overridden by adding class
208 * "mw-collapsed" to the "mw-collapsible" element.
209 * - Elements made collapsible have jQuery data "mw-made-collapsible" set to true.
210 * - The inner content is wrapped in a "div.mw-collapsible-content" (except for tables and lists).
211 *
212 * @param {Object} [options]
213 * @param {string} [options.collapseText] Text used for the toggler, when clicking it would
214 * collapse the element. Default: the 'data-collapsetext' attribute of the
215 * collapsible element or the content of 'collapsible-collapse' message.
216 * @param {string} [options.expandText] Text used for the toggler, when clicking it would
217 * expand the element. Default: the 'data-expandtext' attribute of the
218 * collapsible element or the content of 'collapsible-expand' message.
219 * @param {boolean} [options.collapsed] Whether to collapse immediately. By default
220 * collapse only if the element has the 'mw-collapsed' class.
221 * @param {jQuery} [options.$customTogglers] Elements to be used as togglers
222 * for this collapsible element. By default, if the collapsible element
223 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
224 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
225 * @param {boolean} [options.plainMode=false] Whether to use a "plain mode" when making the
226 * element collapsible - that is, hide entire tables and lists (instead
227 * of hiding only all rows but first of tables, and hiding each list
228 * item separately for lists) and don't wrap other elements in
229 * div.mw-collapsible-content. May only be used with custom togglers.
230 * @return {jQuery}
231 * @chainable
232 */
233 $.fn.makeCollapsible = function ( options ) {
234 options = options || {};
235
236 this.each( function () {
237 var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler, buildDefaultToggleLink,
238 $toggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
239
240 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
241 // is called on element(s) that don't have it yet.
242 $collapsible = $( this ).addClass( 'mw-collapsible' );
243
244 // Return if it has been enabled already.
245 if ( $collapsible.data( 'mw-made-collapsible' ) ) {
246 return;
247 } else {
248 // Let CSS know that it no longer needs to worry about flash of unstyled content.
249 // This will allow mediawiki.makeCollapsible.styles to disable temporary pseudo elements, that
250 // are needed to avoid a flash of unstyled content.
251 $collapsible.addClass( 'mw-made-collapsible' )
252 .data( 'mw-made-collapsible', true );
253 }
254
255 // Use custom text or default?
256 collapseText = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
257 expandText = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
258
259 // Default click/keypress handler and toggle link to use when none is present
260 actionHandler = function ( e, opts ) {
261 var defaultOpts = {
262 toggleClasses: true,
263 toggleText: { collapseText: collapseText, expandText: expandText }
264 };
265 opts = $.extend( defaultOpts, options, opts );
266 togglingHandler( $( this ), $collapsible, e, opts );
267 };
268
269 // Default toggle link. Only build it when needed to avoid jQuery memory leaks (event data).
270 buildDefaultToggleLink = function () {
271 return $( '<a class="mw-collapsible-text"></a>' )
272 .text( collapseText )
273 .wrap( '<span class="mw-collapsible-toggle mw-collapsible-toggle-default"></span>' )
274 .parent()
275 .attr( {
276 role: 'button',
277 tabindex: 0
278 } )
279 .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler );
280 };
281
282 // Check if this element has a custom position for the toggle link
283 // (ie. outside the container or deeper inside the tree)
284 if ( options.$customTogglers ) {
285 $customTogglers = $( options.$customTogglers );
286 } else {
287 collapsibleId = $collapsible.attr( 'id' ) || '';
288 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
289 $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) )
290 .addClass( 'mw-customtoggle' );
291 }
292 }
293
294 // Add event handlers to custom togglers or create our own ones
295 if ( $customTogglers && $customTogglers.length ) {
296 actionHandler = function ( e, opts ) {
297 var defaultOpts = {};
298 opts = $.extend( defaultOpts, options, opts );
299 togglingHandler( $( this ), $collapsible, e, opts );
300 };
301
302 $toggleLink = $customTogglers
303 .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
304 .prop( 'tabIndex', 0 );
305
306 } else {
307 // If this is not a custom case, do the default: wrap the
308 // contents and add the toggle link. Different elements are
309 // treated differently.
310
311 if ( $collapsible.is( 'table' ) ) {
312
313 // If the table has a caption, collapse to the caption
314 // as opposed to the first row
315 $caption = $collapsible.find( '> caption' );
316 if ( $caption.length ) {
317 $toggle = $caption.find( '> .mw-collapsible-toggle' );
318
319 // If there is no toggle link, add it to the end of the caption
320 if ( !$toggle.length ) {
321 $toggleLink = buildDefaultToggleLink().appendTo( $caption );
322 } else {
323 $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
324 .prop( 'tabIndex', 0 );
325 }
326 } else {
327 // The toggle-link will be in one of the cells (td or th) of the first row
328 $firstItem = $collapsible.find( 'tr:first th, tr:first td' );
329 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
330
331 // If theres no toggle link, add it to the last cell
332 if ( !$toggle.length ) {
333 $toggleLink = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
334 } else {
335 $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
336 .prop( 'tabIndex', 0 );
337 }
338 }
339
340 } else if ( $collapsible.parent().is( 'li' ) &&
341 $collapsible.parent().children( '.mw-collapsible' ).length === 1 &&
342 $collapsible.find( '> .mw-collapsible-toggle' ).length === 0
343 ) {
344 // special case of one collapsible in <li> tag
345 $toggleLink = buildDefaultToggleLink();
346 $collapsible.before( $toggleLink );
347 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
348 // The toggle-link will be in the first list-item
349 $firstItem = $collapsible.find( 'li:first' );
350 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
351
352 // If theres no toggle link, add it
353 if ( !$toggle.length ) {
354 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
355 // to be "1". Except if the value-attribute is already used.
356 // If no value was set WebKit returns "", Mozilla returns '-1', others return 0, null or undefined.
357 firstval = $firstItem.prop( 'value' );
358 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
359 $firstItem.prop( 'value', '1' );
360 }
361 $toggleLink = buildDefaultToggleLink();
362 $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
363 } else {
364 $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
365 .prop( 'tabIndex', 0 );
366 }
367
368 } else { // <div>, <p> etc.
369
370 // The toggle-link will be the first child of the element
371 $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
372
373 // If a direct child .content-wrapper does not exists, create it
374 if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
375 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
376 }
377
378 // If theres no toggle link, add it
379 if ( !$toggle.length ) {
380 $toggleLink = buildDefaultToggleLink().prependTo( $collapsible );
381 } else {
382 $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
383 .prop( 'tabIndex', 0 );
384 }
385 }
386 }
387
388 $( this ).data( 'mw-collapsible', {
389 collapse: function () {
390 actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: false } );
391 },
392 expand: function () {
393 actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: true } );
394 },
395 toggle: function () {
396 actionHandler.call( $toggleLink.get( 0 ), null, null );
397 }
398 } );
399
400 // Initial state
401 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
402 // One toggler can hook to multiple elements, and one element can have
403 // multiple togglers. This is the sanest way to handle that.
404 actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: false } );
405 }
406
407 } );
408
409 /**
410 * Fired after collapsible content has been initialized
411 *
412 * This gives an option to modify the collapsible behavior.
413 *
414 * @event wikipage_collapsibleContent
415 * @member mw.hook
416 * @param {jQuery} $content All the elements that have been made collapsible
417 */
418 mw.hook( 'wikipage.collapsibleContent' ).fire( this );
419
420 return this;
421 };
422
423 /**
424 * @class jQuery
425 * @mixins jQuery.plugin.makeCollapsible
426 */
427
428 }( jQuery, mediaWiki ) );