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 the text ("Show"/"Hide") within elements tagged with mw-collapsible-text
140 if ( options.toggleText ) {
141 collapseText = options.toggleText.collapseText;
142 expandText = options.toggleText.expandText;
143
144 $textContainer = $toggle.find( '.mw-collapsible-text' );
145 if ( $textContainer.length ) {
146 $textContainer.text( wasCollapsed ? collapseText : expandText );
147 }
148 }
149
150 // And finally toggle the element state itself
151 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
152 }
153
154 /**
155 * Enable collapsible-functionality on all elements in the collection.
156 *
157 * - Will prevent binding twice to the same element.
158 * - Initial state is expanded by default, this can be overridden by adding class
159 * "mw-collapsed" to the "mw-collapsible" element.
160 * - Elements made collapsible have jQuery data "mw-made-collapsible" set to true.
161 * - The inner content is wrapped in a "div.mw-collapsible-content" (except for tables and lists).
162 *
163 * @param {Object} [options]
164 * @param {string} [options.collapseText] Text used for the toggler, when clicking it would
165 * collapse the element. Default: the 'data-collapsetext' attribute of the
166 * collapsible element or the content of 'collapsible-collapse' message.
167 * @param {string} [options.expandText] Text used for the toggler, when clicking it would
168 * expand the element. Default: the 'data-expandtext' attribute of the
169 * collapsible element or the content of 'collapsible-expand' message.
170 * @param {boolean} [options.collapsed] Whether to collapse immediately. By default
171 * collapse only if the element has the 'mw-collapsed' class.
172 * @param {jQuery} [options.$customTogglers] Elements to be used as togglers
173 * for this collapsible element. By default, if the collapsible element
174 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
175 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
176 * @param {boolean} [options.plainMode=false] Whether to use a "plain mode" when making the
177 * element collapsible - that is, hide entire tables and lists (instead
178 * of hiding only all rows but first of tables, and hiding each list
179 * item separately for lists) and don't wrap other elements in
180 * div.mw-collapsible-content. May only be used with custom togglers.
181 * @return {jQuery}
182 * @chainable
183 */
184 $.fn.makeCollapsible = function ( options ) {
185 options = options || {};
186
187 this.each( function () {
188 var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler,
189 buildDefaultToggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
190
191 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
192 // is called on element(s) that don't have it yet.
193 $collapsible = $( this ).addClass( 'mw-collapsible' );
194
195 // Return if it has been enabled already.
196 if ( $collapsible.data( 'mw-made-collapsible' ) ) {
197 return;
198 } else {
199 // Let CSS know that it no longer needs to worry about flash of unstyled content.
200 // This will allow mediawiki.makeCollapsible.styles to disable temporary pseudo elements, that
201 // are needed to avoid a flash of unstyled content.
202 $collapsible.addClass( 'mw-made-collapsible' )
203 .data( 'mw-made-collapsible', true );
204 }
205
206 // Use custom text or default?
207 collapseText = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
208 expandText = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
209
210 // Default click/keypress handler and toggle link to use when none is present
211 actionHandler = function ( e, opts ) {
212 var defaultOpts = {
213 toggleClasses: true,
214 toggleText: { collapseText: collapseText, expandText: expandText }
215 };
216 opts = $.extend( defaultOpts, options, opts );
217 togglingHandler( $( this ), $collapsible, e, opts );
218 };
219
220 // Default toggle link. Only build it when needed to avoid jQuery memory leaks (event data).
221 buildDefaultToggleLink = function () {
222 return $( '<a>' )
223 .addClass( 'mw-collapsible-text' )
224 .text( collapseText )
225 .wrap( '<span class="mw-collapsible-toggle mw-collapsible-toggle-default"></span>' )
226 .parent()
227 .attr( {
228 role: 'button',
229 tabindex: 0
230 } );
231 };
232
233 // Check if this element has a custom position for the toggle link
234 // (ie. outside the container or deeper inside the tree)
235 if ( options.$customTogglers ) {
236 $customTogglers = $( options.$customTogglers );
237 } else {
238 collapsibleId = $collapsible.attr( 'id' ) || '';
239 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
240 $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) )
241 .addClass( 'mw-customtoggle' );
242 }
243 }
244
245 // Add event handlers to custom togglers or create our own ones
246 if ( $customTogglers && $customTogglers.length ) {
247 actionHandler = function ( e, opts ) {
248 var defaultOpts = {};
249 opts = $.extend( defaultOpts, options, opts );
250 togglingHandler( $( this ), $collapsible, e, opts );
251 };
252
253 $toggle = $customTogglers;
254
255 } else {
256 // If this is not a custom case, do the default: wrap the
257 // contents and add the toggle link. Different elements are
258 // treated differently.
259
260 if ( $collapsible.is( 'table' ) ) {
261
262 // If the table has a caption, collapse to the caption
263 // as opposed to the first row
264 $caption = $collapsible.find( '> caption' );
265 if ( $caption.length ) {
266 $toggle = $caption.find( '> .mw-collapsible-toggle' );
267
268 // If there is no toggle link, add it to the end of the caption
269 if ( !$toggle.length ) {
270 $toggle = buildDefaultToggleLink().appendTo( $caption );
271 }
272 } else {
273 // The toggle-link will be in one of the cells (td or th) of the first row
274 $firstItem = $collapsible.find( 'tr' ).first().find( 'th, td' );
275 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
276
277 // If theres no toggle link, add it to the last cell
278 if ( !$toggle.length ) {
279 $toggle = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
280 }
281 }
282
283 } else if ( $collapsible.parent().is( 'li' ) &&
284 $collapsible.parent().children( '.mw-collapsible' ).length === 1 &&
285 $collapsible.find( '> .mw-collapsible-toggle' ).length === 0
286 ) {
287 // special case of one collapsible in <li> tag
288 $toggle = buildDefaultToggleLink();
289 $collapsible.before( $toggle );
290 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
291 // The toggle-link will be in the first list-item
292 $firstItem = $collapsible.find( 'li' ).first();
293 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
294
295 // If theres no toggle link, add it
296 if ( !$toggle.length ) {
297 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
298 // to be "1". Except if the value-attribute is already used.
299 // If no value was set WebKit returns "", Mozilla returns '-1', others return 0, null or undefined.
300 firstval = $firstItem.prop( 'value' );
301 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
302 $firstItem.prop( 'value', '1' );
303 }
304 $toggle = buildDefaultToggleLink();
305 $toggle.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
306 }
307
308 } else { // <div>, <p> etc.
309
310 // The toggle-link will be the first child of the element
311 $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
312
313 // If a direct child .content-wrapper does not exists, create it
314 if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
315 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
316 }
317
318 // If theres no toggle link, add it
319 if ( !$toggle.length ) {
320 $toggle = buildDefaultToggleLink().prependTo( $collapsible );
321 }
322 }
323 }
324
325 // Attach event handlers to togglelink
326 $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
327 .prop( 'tabIndex', 0 );
328
329 $( this ).data( 'mw-collapsible', {
330 collapse: function () {
331 actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } );
332 },
333 expand: function () {
334 actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: true } );
335 },
336 toggle: function () {
337 actionHandler.call( $toggle.get( 0 ), null, null );
338 }
339 } );
340
341 // Initial state
342 // eslint-disable-next-line no-jquery/no-class-state
343 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
344 // One toggler can hook to multiple elements, and one element can have
345 // multiple togglers. This is the sanest way to handle that.
346 actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } );
347 }
348
349 } );
350
351 /**
352 * Fired after collapsible content has been initialized
353 *
354 * This gives an option to modify the collapsible behavior.
355 *
356 * @event wikipage_collapsibleContent
357 * @member mw.hook
358 * @param {jQuery} $content All the elements that have been made collapsible
359 */
360 mw.hook( 'wikipage.collapsibleContent' ).fire( this );
361
362 return this;
363 };
364
365 /**
366 * @class jQuery
367 * @mixins jQuery.plugin.makeCollapsible
368 */
369
370 }() );