Index: trunk/phase3/resources/jquery/jquery.makeCollapsible.js |
— | — | @@ -1,5 +1,5 @@ |
2 | 2 | /** |
3 | | - * make lists, divs, tables etc. toggleable |
| 3 | + * jQuery makeCollapsible |
4 | 4 | * |
5 | 5 | * This will enable collapsible-functionality on all passed elements. |
6 | 6 | * Will prevent binding twice to the same element. |
— | — | @@ -8,102 +8,113 @@ |
9 | 9 | * Elements made collapsible have class "kr-made-collapsible". |
10 | 10 | * Except for tables and lists, the inner content is wrapped in "kr-collapsible-content". |
11 | 11 | * |
12 | | - * Copyright 2008-2009 Krinkle <krinklemail@gmail.com> |
| 12 | + * @author Krinkle <krinklemail@gmail.com> |
13 | 13 | * |
14 | | - * @license CC-BY 2.0 NL <http://creativecommons.org/licenses/by/2.0/nl/deed.en> |
| 14 | + * @TODO: Remove old "kr-" prefix |
| 15 | + * |
| 16 | + * Dual license: |
| 17 | + * @license CC-BY 3.0 <http://creativecommons.org/licenses/by/3.0> |
| 18 | + * @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html> |
15 | 19 | */ |
16 | 20 | |
17 | 21 | $.fn.makeCollapsible = function() { |
18 | 22 | |
19 | 23 | return this.each(function() { |
20 | | - var $that = $(this).addClass('kr-collapsible'), // in case $('#myAJAXelement').makeCollapsible() was called |
| 24 | + var $that = $(this).addClass( 'kr-collapsible' ), // in case $( '#myAJAXelement' ).makeCollapsible() was called |
21 | 25 | that = this, |
22 | | - collapsetext = $(this).attr('data-collapsetext'), |
23 | | - expandtext = $(this).attr('data-expandtext'); |
| 26 | + collapsetext = $(this).attr( 'data-collapsetext' ), |
| 27 | + expandtext = $(this).attr( 'data-expandtext' ); |
24 | 28 | // Use custom text or default ? |
25 | | - if( !collapsetext || collapsetext == ''){ |
26 | | - collapsetext = 'Collapse'; |
| 29 | + if( !collapsetext || collapsetext == '' ){ |
| 30 | + collapsetext = mw.msg( 'hide' ); |
27 | 31 | } |
28 | | - if ( !expandtext || expandtext == ''){ |
29 | | - expandtext = 'Expand'; |
| 32 | + if ( !expandtext || expandtext == '' ){ |
| 33 | + expandtext = mw.msg( 'show' ); |
30 | 34 | } |
31 | 35 | // Create toggle link with a space around the brackets ( ) |
32 | | - $toggleLink = $('<span class="kr-collapsible-toggle kr-collapsible-toggle-hide"> [<a href="#">' + collapsetext + '</a>] </span>').click(function(){ |
| 36 | + $toggleLink = $( '<span class="kr-collapsible-toggle kr-collapsible-toggle-hide"> [<a href="#">' + collapsetext + '</a>] </span>' ).click( function(){ |
33 | 37 | var $that = $(this), |
34 | | - $collapsible = $that.closest('.kr-collapsible.kr-made-collapsible').toggleClass('kr-collapsed'); |
35 | | - if ($that.hasClass('kr-collapsible-toggle-hide')) { |
| 38 | + $collapsible = $that.closest( '.kr-collapsible.kr-made-collapsible' ).toggleClass( 'kr-collapsed' ); |
| 39 | + if ( $that.hasClass( 'kr-collapsible-toggle-hide' ) ) { |
36 | 40 | // Change link to "Show" |
37 | 41 | $that |
38 | | - .removeClass('kr-collapsible-toggle-hide').addClass('kr-collapsible-toggle-show') |
39 | | - .find('> a').html(expandtext); |
| 42 | + .removeClass( 'kr-collapsible-toggle-hide' ).addClass( 'kr-collapsible-toggle-show' ) |
| 43 | + .find( '> a' ).html( expandtext ); |
40 | 44 | // Hide the collapsible element |
41 | | - if ($collapsible.is('table')) { |
| 45 | + if ( $collapsible.is( 'table' ) ) { |
42 | 46 | // Hide all direct childing table rows of this table, except the row containing the link |
43 | 47 | // Slide doens't work, but fade works fine as of jQuery 1.1.3 |
44 | 48 | // http://stackoverflow.com/questions/467336/jquery-how-to-use-slidedown-or-show-function-on-a-table-row#920480 |
45 | 49 | // Stop to prevent animaties from stacking up |
46 | | - $collapsible.find('> tbody > tr').not($that.parent().parent()).stop(true, true).fadeOut(); |
47 | | - } else if ($collapsible.is('ul') || $collapsible.is('ol')) { |
48 | | - $collapsible.find('> li').not($that.parent()).stop(true, true).slideUp(); |
| 50 | + $collapsible.find( '> tbody > tr' ).not( $that.parent().parent() ).stop(true, true).fadeOut(); |
| 51 | + |
| 52 | + } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) { |
| 53 | + $collapsible.find( '> li' ).not($that.parent()).stop(true, true).slideUp(); |
| 54 | + |
49 | 55 | } else { // <div>, <p> etc. |
50 | | - $collapsible.find('> .kr-collapsible-content').slideUp(); |
| 56 | + $collapsible.find( '> .kr-collapsible-content' ).slideUp(); |
51 | 57 | } |
| 58 | + |
52 | 59 | } else { |
53 | 60 | // Change link to "Hide" |
54 | 61 | $that |
55 | | - .removeClass('kr-collapsible-toggle-show').addClass('kr-collapsible-toggle-hide') |
56 | | - .find('> a').html(collapsetext); |
| 62 | + .removeClass( 'kr-collapsible-toggle-show' ).addClass( 'kr-collapsible-toggle-hide' ) |
| 63 | + .find( '> a' ).html( collapsetext ); |
57 | 64 | // Show the collapsible element |
58 | | - if ($collapsible.is('table')) { |
59 | | - $collapsible.find('> tbody > tr').not($that.parent().parent()).stop(true, true).fadeIn(); |
60 | | - } else if ($collapsible.is('ul') || $collapsible.is('ol')) { |
61 | | - $collapsible.find('> li').not($that.parent()).stop(true, true).slideDown(); |
| 65 | + if ( $collapsible.is( 'table' ) ) { |
| 66 | + $collapsible.find( '> tbody > tr' ).not( $that.parent().parent() ).stop(true, true).fadeIn(); |
| 67 | + |
| 68 | + } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) { |
| 69 | + $collapsible.find( '> li' ).not( $that.parent() ).stop(true, true).slideDown(); |
| 70 | + |
62 | 71 | } else { // <div>, <p> etc. |
63 | | - $collapsible.find('> .kr-collapsible-content').slideDown(); |
| 72 | + $collapsible.find( '> .kr-collapsible-content' ).slideDown(); |
64 | 73 | } |
65 | 74 | } |
66 | 75 | return false; |
67 | | - }); |
| 76 | + } ); |
| 77 | + |
68 | 78 | // Skip if it has been enabled already. |
69 | | - if ($that.hasClass('kr-made-collapsible')) { |
| 79 | + if ($that.hasClass( 'kr-made-collapsible' )) { |
70 | 80 | return; |
71 | 81 | } else { |
72 | | - $that.addClass('kr-made-collapsible'); |
| 82 | + $that.addClass( 'kr-made-collapsible' ); |
73 | 83 | } |
| 84 | + |
74 | 85 | // Elements are treated differently |
75 | | - if ($that.is('table')) { |
| 86 | + if ( $that.is( 'table' ) ) { |
76 | 87 | // The toggle-link will be in the last cell (td or th) of the first row |
77 | | - var $lastCell = $('tr:first th, tr:first td', that).eq(-1); |
78 | | - if (!$lastCell.find('> .kr-collapsible-toggle').size()) { |
79 | | - $lastCell.prepend($toggleLink); |
| 88 | + var $lastCell = $( 'tr:first th, tr:first td', that ).eq(-1); |
| 89 | + if ( !$lastCell.find( '> .kr-collapsible-toggle' ).size() ) { |
| 90 | + $lastCell.prepend( $toggleLink ); |
80 | 91 | } |
81 | 92 | |
82 | | - } else if ($that.is('ul') || $that.is('ol')) { |
83 | | - if (!$('li:first', $that).find('> .kr-collapsible-toggle').size()) { |
| 93 | + } else if ($that.is( 'ul' ) || $that.is( 'ol' )) { |
| 94 | + if ( !$( 'li:first', $that).find( '> .kr-collapsible-toggle' ).size() ) { |
84 | 95 | // Make sure the numeral count doesn't get messed up, reset to 1 unless value-attribute is already used |
85 | | - if ( $('li:first', $that).attr('value') == '' ) { |
86 | | - $('li:first', $that).attr('value', '1'); |
| 96 | + if ( $( 'li:first', $that ).attr( 'value' ) == '' ) { |
| 97 | + $( 'li:first', $that ).attr( 'value', '1' ); |
87 | 98 | } |
88 | | - $that.prepend($toggleLink.wrap('<li class="kr-collapsibile-toggle-li">').parent()); |
| 99 | + $that.prepend( $toggleLink.wrap( '<li class="kr-collapsibile-toggle-li">' ).parent() ); |
89 | 100 | } |
90 | 101 | } else { // <div>, <p> etc. |
91 | 102 | // Is there an content-wrapper already made ? |
92 | 103 | // If a direct child with the class does not exists, create the wrap. |
93 | | - if (!$that.find('g> .kr-collapsible-content').size()) { |
94 | | - $that.wrapInner('<div class="kr-collapsible-content">'); |
| 104 | + if ( !$that.find( '> .kr-collapsible-content' ).size() ) { |
| 105 | + $that.wrapInner( '<div class="kr-collapsible-content">' ); |
95 | 106 | } |
96 | 107 | |
97 | 108 | // Add toggle-link if not present |
98 | | - if (!$that.find('> .kr-collapsible-toggle').size()) { |
99 | | - $that.prepend($toggleLink); |
| 109 | + if ( !$that.find( '> .kr-collapsible-toggle' ).size() ) { |
| 110 | + $that.prepend( $toggleLink ); |
100 | 111 | } |
101 | 112 | } |
102 | 113 | // Initial state |
103 | | - if ($that.hasClass('kr-collapsed')) { |
| 114 | + if ( $that.hasClass( 'kr-collapsed' ) ) { |
104 | 115 | $toggleLink.click(); |
105 | 116 | } |
106 | | - }); |
| 117 | + } ); |
107 | 118 | }; |
108 | | -$(function(){ |
109 | | - $('.kr-collapsible').makeCollapsible(); |
110 | | -}); |
\ No newline at end of file |
| 119 | +$( function(){ |
| 120 | + $( '.kr-collapsible' ).makeCollapsible(); |
| 121 | +} ); |
\ No newline at end of file |