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