Merge "Changing a message's lang must reset cached text."
[lhc/web/wiklou.git] / resources / src / jquery / jquery.arrowSteps.js
1 /*!
2 * jQuery arrowSteps plugin
3 * Copyright Neil Kandalgaonkar, 2010
4 *
5 * This work is licensed under the terms of the GNU General Public License,
6 * version 2 or later.
7 * (see http://www.fsf.org/licensing/licenses/gpl.html).
8 * Derivative works and later versions of the code must be free software
9 * licensed under the same or a compatible license.
10 */
11
12 /**
13 * @class jQuery.plugin.arrowSteps
14 */
15 ( function ( $ ) {
16 /**
17 * Show users their progress through a series of steps, via a row of items that fit
18 * together like arrows. One item can be highlighted at a time.
19 *
20 * <ul id="robin-hood-daffy">
21 * <li id="guard"><div>Guard!</div></li>
22 * <li id="turn"><div>Turn!</div></li>
23 * <li id="parry"><div>Parry!</div></li>
24 * <li id="dodge"><div>Dodge!</div></li>
25 * <li id="spin"><div>Spin!</div></li>
26 * <li id="ha"><div>Ha!</div></li>
27 * <li id="thrust"><div>Thrust!</div></li>
28 * </ul>
29 *
30 * <script>
31 * $( '#robin-hood-daffy' ).arrowSteps();
32 * </script>
33 *
34 * @return {jQuery}
35 * @chainable
36 */
37 $.fn.arrowSteps = function () {
38 var $steps, width, arrowWidth,
39 paddingSide = $( 'body' ).hasClass( 'rtl' ) ? 'padding-left' : 'padding-right';
40
41 this.addClass( 'arrowSteps' );
42 $steps = this.find( 'li' );
43
44 width = parseInt( 100 / $steps.length, 10 );
45 $steps.css( 'width', width + '%' );
46
47 // Every step except the last one has an arrow pointing forward:
48 // at the right hand side in LTR languages, and at the left hand side in RTL.
49 // Also add in the padding for the calculated arrow width.
50 arrowWidth = parseInt( this.outerHeight(), 10 );
51 $steps.filter( ':not(:last-child)' ).addClass( 'arrow' )
52 .find( 'div' ).css( paddingSide, arrowWidth.toString() + 'px' );
53
54 this.data( 'arrowSteps', $steps );
55 return this;
56 };
57
58 /**
59 * Highlights the element selected by the selector.
60 *
61 * $( '#robin-hood-daffy' ).arrowStepsHighlight( '#guard' );
62 * // 'Guard!' is highlighted.
63 *
64 * // ... user completes the 'guard' step ...
65 *
66 * $( '#robin-hood-daffy' ).arrowStepsHighlight( '#turn' );
67 * // 'Turn!' is highlighted.
68 *
69 * @param {string} selector
70 */
71 $.fn.arrowStepsHighlight = function ( selector ) {
72 var $previous,
73 $steps = this.data( 'arrowSteps' );
74 $.each( $steps, function ( i, step ) {
75 var $step = $( step );
76 if ( $step.is( selector ) ) {
77 if ($previous) {
78 $previous.addClass( 'tail' );
79 }
80 $step.addClass( 'head' );
81 } else {
82 $step.removeClass( 'head tail lasthead' );
83 }
84 $previous = $step;
85 } );
86 };
87
88 /**
89 * @class jQuery
90 * @mixins jQuery.plugin.arrowSteps
91 */
92 }( jQuery ) );