Merge "Remove Revision::getRevisionText from migrateArchiveText"
[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 <https://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 () {
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 if ( !options.plainMode && $collapsible.is( 'table' ) ) {
51 // Tables
52 // If there is a caption, hide all rows; otherwise, only hide body rows
53 if ( $collapsible.find( '> caption' ).length ) {
54 $containers = $collapsible.find( '> * > tr' );
55 } else {
56 $containers = $collapsible.find( '> tbody > tr' );
57 }
58 if ( $defaultToggle ) {
59 // Exclude table row containing togglelink
60 $containers = $containers.not( $defaultToggle.closest( 'tr' ) );
61 }
62
63 } else if ( !options.plainMode && ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) ) {
64 // Lists
65 $containers = $collapsible.find( '> li' );
66 if ( $defaultToggle ) {
67 // Exclude list-item containing togglelink
68 $containers = $containers.not( $defaultToggle.parent() );
69 }
70 } else {
71 // Everything else: <div>, <p> etc.
72 $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
73
74 // If a collapsible-content is defined, act on it
75 if ( !options.plainMode && $collapsibleContent.length ) {
76 $containers = $collapsibleContent;
77
78 // Otherwise assume this is a customcollapse with a remote toggle
79 // .. and there is no collapsible-content because the entire element should be toggled
80 } else {
81 $containers = $collapsible;
82 }
83 }
84
85 $containers.toggle( action === 'expand' );
86 hookCallback();
87 }
88
89 /**
90 * Handle clicking/keypressing on the collapsible element toggle and other
91 * situations where a collapsible element is toggled (e.g. the initial
92 * toggle for collapsed ones).
93 *
94 * @private
95 * @param {jQuery} $toggle the clickable toggle itself
96 * @param {jQuery} $collapsible the collapsible element
97 * @param {jQuery.Event|null} e either the event or null if unavailable
98 * @param {Object|undefined} options
99 */
100 function togglingHandler( $toggle, $collapsible, e, options ) {
101 var wasCollapsed, $textContainer, collapseText, expandText;
102 options = options || {};
103
104 if ( e ) {
105 if (
106 e.type === 'click' &&
107 e.target.nodeName.toLowerCase() === 'a' &&
108 $( e.target ).attr( 'href' )
109 ) {
110 // Don't fire if a link was clicked (for premade togglers)
111 return;
112 } else if ( e.type === 'keypress' && e.which !== 13 && e.which !== 32 ) {
113 // Only handle keypresses on the "Enter" or "Space" keys
114 return;
115 } else {
116 e.preventDefault();
117 e.stopPropagation();
118 }
119 }
120
121 // This allows the element to be hidden on initial toggle without fiddling with the class
122 if ( options.wasCollapsed !== undefined ) {
123 wasCollapsed = options.wasCollapsed;
124 } else {
125 // eslint-disable-next-line no-jquery/no-class-state
126 wasCollapsed = $collapsible.hasClass( 'mw-collapsed' );
127 }
128
129 // Toggle the state of the collapsible element (that is, expand or collapse)
130 $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed );
131
132 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
133 if ( options.toggleClasses ) {
134 $toggle
135 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed )
136 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed );
137 }
138
139 // Toggle `aria-expanded` attribute, if requested (for default and premade togglers by default).
140 if ( options.toggleARIA ) {
141 $toggle.attr( 'aria-expanded', wasCollapsed ? 'true' : 'false' );
142 }
143
144 // Toggle the text ("Show"/"Hide") within elements tagged with mw-collapsible-text
145 if ( options.toggleText ) {
146 collapseText = options.toggleText.collapseText;
147 expandText = options.toggleText.expandText;
148
149 $textContainer = $toggle.find( '.mw-collapsible-text' );
150 if ( $textContainer.length ) {
151 $textContainer.text( wasCollapsed ? collapseText : expandText );
152 }
153 }
154
155 // And finally toggle the element state itself
156 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
157 }
158
159 /**
160 * Enable collapsible-functionality on all elements in the collection.
161 *
162 * - Will prevent binding twice to the same element.
163 * - Initial state is expanded by default, this can be overridden by adding class
164 * "mw-collapsed" to the "mw-collapsible" element.
165 * - Elements made collapsible have jQuery data "mw-made-collapsible" set to true.
166 * - The inner content is wrapped in a "div.mw-collapsible-content" (except for tables and lists).
167 *
168 * @param {Object} [options]
169 * @param {string} [options.collapseText] Text used for the toggler, when clicking it would
170 * collapse the element. Default: the 'data-collapsetext' attribute of the
171 * collapsible element or the content of 'collapsible-collapse' message.
172 * @param {string} [options.expandText] Text used for the toggler, when clicking it would
173 * expand the element. Default: the 'data-expandtext' attribute of the
174 * collapsible element or the content of 'collapsible-expand' message.
175 * @param {boolean} [options.collapsed] Whether to collapse immediately. By default
176 * collapse only if the element has the 'mw-collapsed' class.
177 * @param {jQuery} [options.$customTogglers] Elements to be used as togglers
178 * for this collapsible element. By default, if the collapsible element
179 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
180 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
181 * @param {boolean} [options.plainMode=false] Whether to use a "plain mode" when making the
182 * element collapsible - that is, hide entire tables and lists (instead
183 * of hiding only all rows but first of tables, and hiding each list
184 * item separately for lists) and don't wrap other elements in
185 * div.mw-collapsible-content. May only be used with custom togglers.
186 * @return {jQuery}
187 * @chainable
188 */
189 $.fn.makeCollapsible = function ( options ) {
190 options = options || {};
191
192 this.each( function () {
193 var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler,
194 buildDefaultToggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
195
196 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
197 // is called on element(s) that don't have it yet.
198 $collapsible = $( this ).addClass( 'mw-collapsible' );
199
200 // Return if it has been enabled already.
201 if ( $collapsible.data( 'mw-made-collapsible' ) ) {
202 return;
203 } else {
204 // Let CSS know that it no longer needs to worry about flash of unstyled content.
205 // This will allow mediawiki.makeCollapsible.styles to disable temporary pseudo elements, that
206 // are needed to avoid a flash of unstyled content.
207 $collapsible.addClass( 'mw-made-collapsible' )
208 .data( 'mw-made-collapsible', true );
209 }
210
211 // Use custom text or default?
212 collapseText = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
213 expandText = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
214
215 // Default click/keypress handler and toggle link to use when none is present
216 actionHandler = function ( e, opts ) {
217 var defaultOpts = {
218 toggleClasses: true,
219 toggleARIA: true,
220 toggleText: { collapseText: collapseText, expandText: expandText }
221 };
222 opts = $.extend( defaultOpts, options, opts );
223 togglingHandler( $( this ), $collapsible, e, opts );
224 };
225
226 // Default toggle link. Only build it when needed to avoid jQuery memory leaks (event data).
227 buildDefaultToggleLink = function () {
228 return $( '<a>' )
229 .addClass( 'mw-collapsible-text' )
230 .text( collapseText )
231 .wrap( '<span class="mw-collapsible-toggle mw-collapsible-toggle-default"></span>' )
232 .parent()
233 .attr( {
234 role: 'button',
235 tabindex: 0
236 } );
237 };
238
239 // Check if this element has a custom position for the toggle link
240 // (ie. outside the container or deeper inside the tree)
241 if ( options.$customTogglers ) {
242 $customTogglers = $( options.$customTogglers );
243 } else {
244 collapsibleId = $collapsible.attr( 'id' ) || '';
245 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
246 $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) )
247 .addClass( 'mw-customtoggle' );
248 }
249 }
250
251 // Add event handlers to custom togglers or create our own ones
252 if ( $customTogglers && $customTogglers.length ) {
253 actionHandler = function ( e, opts ) {
254 var defaultOpts = {};
255 opts = $.extend( defaultOpts, options, opts );
256 togglingHandler( $( this ), $collapsible, e, opts );
257 };
258
259 $toggle = $customTogglers;
260
261 } else {
262 // If this is not a custom case, do the default: wrap the
263 // contents and add the toggle link. Different elements are
264 // treated differently.
265
266 if ( $collapsible.is( 'table' ) ) {
267
268 // If the table has a caption, collapse to the caption
269 // as opposed to the first row
270 $caption = $collapsible.find( '> caption' );
271 if ( $caption.length ) {
272 $toggle = $caption.find( '> .mw-collapsible-toggle' );
273
274 // If there is no toggle link, add it to the end of the caption
275 if ( !$toggle.length ) {
276 $toggle = buildDefaultToggleLink().appendTo( $caption );
277 }
278 } else {
279 // The toggle-link will be in one of the cells (td or th) of the first row
280 $firstItem = $collapsible.find( 'tr' ).first().find( 'th, td' );
281 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
282
283 // If theres no toggle link, add it to the last cell
284 if ( !$toggle.length ) {
285 $toggle = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
286 }
287 }
288
289 } else if ( $collapsible.parent().is( 'li' ) &&
290 $collapsible.parent().children( '.mw-collapsible' ).length === 1 &&
291 $collapsible.find( '> .mw-collapsible-toggle' ).length === 0
292 ) {
293 // special case of one collapsible in <li> tag
294 $toggle = buildDefaultToggleLink();
295 $collapsible.before( $toggle );
296 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
297 // The toggle-link will be in the first list-item
298 $firstItem = $collapsible.find( 'li' ).first();
299 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
300
301 // If theres no toggle link, add it
302 if ( !$toggle.length ) {
303 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
304 // to be "1". Except if the value-attribute is already used.
305 // If no value was set WebKit returns "", Mozilla returns '-1', others return 0, null or undefined.
306 firstval = $firstItem.prop( 'value' );
307 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
308 $firstItem.prop( 'value', '1' );
309 }
310 $toggle = buildDefaultToggleLink();
311 $toggle.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
312 }
313
314 } else { // <div>, <p> etc.
315
316 // The toggle-link will be the first child of the element
317 $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
318
319 // If a direct child .content-wrapper does not exists, create it
320 if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
321 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
322 }
323
324 // If theres no toggle link, add it
325 if ( !$toggle.length ) {
326 $toggle = buildDefaultToggleLink().prependTo( $collapsible );
327 }
328 }
329 }
330
331 // Attach event handlers to togglelink
332 $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
333 .attr( 'aria-expanded', 'true' )
334 .prop( 'tabIndex', 0 );
335
336 $( this ).data( 'mw-collapsible', {
337 collapse: function () {
338 actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } );
339 },
340 expand: function () {
341 actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: true } );
342 },
343 toggle: function () {
344 actionHandler.call( $toggle.get( 0 ), null, null );
345 }
346 } );
347
348 // Initial state
349 // eslint-disable-next-line no-jquery/no-class-state
350 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
351 // One toggler can hook to multiple elements, and one element can have
352 // multiple togglers. This is the sanest way to handle that.
353 actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } );
354 }
355
356 } );
357
358 /**
359 * Fired after collapsible content has been initialized
360 *
361 * This gives an option to modify the collapsible behavior.
362 *
363 * @event wikipage_collapsibleContent
364 * @member mw.hook
365 * @param {jQuery} $content All the elements that have been made collapsible
366 */
367 mw.hook( 'wikipage.collapsibleContent' ).fire( this );
368
369 return this;
370 };
371
372 /**
373 * @class jQuery
374 * @mixins jQuery.plugin.makeCollapsible
375 */
376
377 }() );