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