Index: trunk/phase3/resources/jquery/jquery.makeCollapsible.js |
— | — | @@ -20,62 +20,78 @@ |
21 | 21 | $.fn.makeCollapsible = function() { |
22 | 22 | |
23 | 23 | return this.each(function() { |
| 24 | + |
24 | 25 | var $that = $(this).addClass( 'kr-collapsible' ), // in case $( '#myAJAXelement' ).makeCollapsible() was called |
25 | 26 | that = this, |
26 | 27 | collapsetext = $(this).attr( 'data-collapsetext' ), |
27 | | - expandtext = $(this).attr( 'data-expandtext' ); |
| 28 | + expandtext = $(this).attr( 'data-expandtext' ), |
| 29 | + toggleFunction = function( that ) { |
| 30 | + var $that = $(that), |
| 31 | + $collapsible = $that.closest( '.kr-collapsible.kr-made-collapsible' ).toggleClass( 'kr-collapsed' ); |
| 32 | + |
| 33 | + // It's expanded right now |
| 34 | + if ( $that.hasClass( 'kr-collapsible-toggle-expanded' ) ) { |
| 35 | + // Change link to "Show" |
| 36 | + $that.removeClass( 'kr-collapsible-toggle-expanded' ).addClass( 'kr-collapsible-toggle-collapsed' ); |
| 37 | + if ( $that.find( '> a' ).size() ) { |
| 38 | + $that.find( '> a' ).text( expandtext ); |
| 39 | + } else { |
| 40 | + $that.text( expandtext ); |
| 41 | + } |
| 42 | + // Hide the collapsible element |
| 43 | + if ( $collapsible.is( 'table' ) ) { |
| 44 | + // Hide all direct childing table rows of this table, except the row containing the link |
| 45 | + // Slide doens't work, but fade works fine as of jQuery 1.1.3 |
| 46 | + // http://stackoverflow.com/questions/467336/jquery-how-to-use-slidedown-or-collapsed-function-on-a-table-row#920480 |
| 47 | + // Stop to prevent animations from stacking up |
| 48 | + $collapsible.find( '> tbody > tr' ).not( $that.parent().parent() ).stop( true, true ).fadeOut(); |
| 49 | + |
| 50 | + } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) { |
| 51 | + $collapsible.find( '> li' ).not( $that.parent() ).stop( true, true ).slideUp(); |
| 52 | + |
| 53 | + } else { // <div>, <p> etc. |
| 54 | + $collapsible.find( '> .kr-collapsible-content' ).slideUp(); |
| 55 | + } |
| 56 | + |
| 57 | + // It's collapsed right now |
| 58 | + } else { |
| 59 | + // Change link to "Hide" |
| 60 | + $that.removeClass( 'kr-collapsible-toggle-collapsed' ).addClass( 'kr-collapsible-toggle-expanded' ); |
| 61 | + if ( $that.find( '> a' ).size() ) { |
| 62 | + $that.find( '> a' ).text( collapsetext ); |
| 63 | + } else { |
| 64 | + $that.text( collapsetext ); |
| 65 | + } |
| 66 | + // Show the collapsible element |
| 67 | + if ( $collapsible.is( 'table' ) ) { |
| 68 | + $collapsible.find( '> tbody > tr' ).not( $that.parent().parent() ).stop( true, true ).fadeIn(); |
| 69 | + |
| 70 | + } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) { |
| 71 | + $collapsible.find( '> li' ).not( $that.parent() ).stop( true, true ).slideDown(); |
| 72 | + |
| 73 | + } else { // <div>, <p> etc. |
| 74 | + $collapsible.find( '> .kr-collapsible-content' ).slideDown(); |
| 75 | + } |
| 76 | + } |
| 77 | + return; |
| 78 | + }; |
| 79 | + |
28 | 80 | // Use custom text or default ? |
29 | 81 | if( !collapsetext || collapsetext == '' ){ |
30 | | - collapsetext = mw.msg( 'hide' ); |
| 82 | + collapsetext = mw.msg( 'collapsible-collapse' ); |
31 | 83 | } |
32 | 84 | if ( !expandtext || expandtext == '' ){ |
33 | | - expandtext = mw.msg( 'show' ); |
| 85 | + expandtext = mw.msg( 'collapsible-expand' ); |
34 | 86 | } |
35 | | - // Create toggle link with a space around the brackets ( ) |
36 | | - $toggleLink = $( '<span class="kr-collapsible-toggle kr-collapsible-toggle-hide"> [<a href="#">' + collapsetext + '</a>] </span>' ).click( function(){ |
37 | | - var $that = $(this), |
38 | | - $collapsible = $that.closest( '.kr-collapsible.kr-made-collapsible' ).toggleClass( 'kr-collapsed' ); |
39 | | - if ( $that.hasClass( 'kr-collapsible-toggle-hide' ) ) { |
40 | | - // Change link to "Show" |
41 | | - $that |
42 | | - .removeClass( 'kr-collapsible-toggle-hide' ).addClass( 'kr-collapsible-toggle-show' ) |
43 | | - .find( '> a' ).html( expandtext ); |
44 | | - // Hide the collapsible element |
45 | | - if ( $collapsible.is( 'table' ) ) { |
46 | | - // Hide all direct childing table rows of this table, except the row containing the link |
47 | | - // Slide doens't work, but fade works fine as of jQuery 1.1.3 |
48 | | - // http://stackoverflow.com/questions/467336/jquery-how-to-use-slidedown-or-show-function-on-a-table-row#920480 |
49 | | - // Stop to prevent animaties from stacking up |
50 | | - $collapsible.find( '> tbody > tr' ).not( $that.parent().parent() ).stop(true, true).fadeOut(); |
51 | 87 | |
52 | | - } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) { |
53 | | - $collapsible.find( '> li' ).not($that.parent()).stop(true, true).slideUp(); |
54 | | - |
55 | | - } else { // <div>, <p> etc. |
56 | | - $collapsible.find( '> .kr-collapsible-content' ).slideUp(); |
57 | | - } |
58 | | - |
59 | | - } else { |
60 | | - // Change link to "Hide" |
61 | | - $that |
62 | | - .removeClass( 'kr-collapsible-toggle-show' ).addClass( 'kr-collapsible-toggle-hide' ) |
63 | | - .find( '> a' ).html( collapsetext ); |
64 | | - // Show the collapsible element |
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 | | - |
71 | | - } else { // <div>, <p> etc. |
72 | | - $collapsible.find( '> .kr-collapsible-content' ).slideDown(); |
73 | | - } |
74 | | - } |
75 | | - return false; |
| 88 | + // Create toggle link with a space around the brackets ( [text] ) |
| 89 | + var $toggleLink = $( '<a href="#">' ).text( collapsetext ).wrap( '<span class="kr-collapsible-toggle kr-collapsible-toggle-expanded">' ).parent().prepend( ' [' ).append( '] ' ).click( function(e){ |
| 90 | + e.preventDefault(); |
| 91 | + toggleFunction( this ); |
76 | 92 | } ); |
77 | 93 | |
78 | 94 | // Skip if it has been enabled already. |
79 | | - if ($that.hasClass( 'kr-made-collapsible' )) { |
| 95 | + if ( $that.hasClass( 'kr-made-collapsible' ) ) { |
80 | 96 | return; |
81 | 97 | } else { |
82 | 98 | $that.addClass( 'kr-made-collapsible' ); |
— | — | @@ -84,19 +100,36 @@ |
85 | 101 | // Elements are treated differently |
86 | 102 | if ( $that.is( 'table' ) ) { |
87 | 103 | // The toggle-link will be in the last cell (td or th) of the first row |
88 | | - var $lastCell = $( 'tr:first th, tr:first td', that ).eq(-1); |
89 | | - if ( !$lastCell.find( '> .kr-collapsible-toggle' ).size() ) { |
| 104 | + var $lastCell = $( 'tr:first th, tr:first td', that ).eq(-1), |
| 105 | + $toggle = $lastCell.find( '> .kr-collapsible-toggle' ); |
| 106 | + |
| 107 | + if ( !$toggle.size() ) { |
90 | 108 | $lastCell.prepend( $toggleLink ); |
| 109 | + } else { |
| 110 | + $toggleLink = $toggle.unbind( 'click' ).click( function( e ){ |
| 111 | + e.preventDefault(); |
| 112 | + toggleFunction( this ); |
| 113 | + } ); |
91 | 114 | } |
92 | 115 | |
93 | | - } else if ($that.is( 'ul' ) || $that.is( 'ol' )) { |
94 | | - if ( !$( 'li:first', $that).find( '> .kr-collapsible-toggle' ).size() ) { |
95 | | - // Make sure the numeral count doesn't get messed up, reset to 1 unless value-attribute is already used |
96 | | - if ( $( 'li:first', $that ).attr( 'value' ) == '' ) { |
97 | | - $( 'li:first', $that ).attr( 'value', '1' ); |
| 116 | + } else if ( $that.is( 'ul' ) || $that.is( 'ol' ) ) { |
| 117 | + // The toggle-link will be in the first list-item |
| 118 | + var $firstItem = $( 'li:first', $that), |
| 119 | + $toggle = $firstItem.find( '> .kr-collapsible-toggle' ); |
| 120 | + |
| 121 | + if ( !$toggle.size() ) { |
| 122 | + // Make sure the numeral order doesn't get messed up, reset to 1 unless value-attribute is already used |
| 123 | + if ( $firstItem.attr( 'value' ) == '' ) { |
| 124 | + $firstItem.attr( 'value', '1' ); |
98 | 125 | } |
99 | 126 | $that.prepend( $toggleLink.wrap( '<li class="kr-collapsibile-toggle-li">' ).parent() ); |
100 | | - } |
| 127 | + } else { |
| 128 | + $toggleLink = $toggle.unbind( 'click' ).click( function( e ){ |
| 129 | + e.preventDefault(); |
| 130 | + toggleFunction( this ); |
| 131 | + } ); |
| 132 | + } |
| 133 | + |
101 | 134 | } else { // <div>, <p> etc. |
102 | 135 | // Is there an content-wrapper already made ? |
103 | 136 | // If a direct child with the class does not exists, create the wrap. |
— | — | @@ -104,11 +137,20 @@ |
105 | 138 | $that.wrapInner( '<div class="kr-collapsible-content">' ); |
106 | 139 | } |
107 | 140 | |
108 | | - // Add toggle-link if not present |
109 | | - if ( !$that.find( '> .kr-collapsible-toggle' ).size() ) { |
| 141 | + // The toggle-link will be the first child of the element |
| 142 | + var $toggle = $that.find( '> .kr-collapsible-toggle' ); |
| 143 | + |
| 144 | + if ( !$toggle.size() ) { |
110 | 145 | $that.prepend( $toggleLink ); |
| 146 | + } else { |
| 147 | + $toggleLink = $toggle.unbind( 'click' ).click( function( e ){ |
| 148 | + e.preventDefault(); |
| 149 | + toggleFunction( this ); |
| 150 | + } ); |
111 | 151 | } |
| 152 | + |
112 | 153 | } |
| 154 | + console.log( $toggleLink.get(0) ); |
113 | 155 | // Initial state |
114 | 156 | if ( $that.hasClass( 'kr-collapsed' ) ) { |
115 | 157 | $toggleLink.click(); |
Index: trunk/phase3/resources/jquery/jquery.makeCollapsible.css |
— | — | @@ -2,12 +2,12 @@ |
3 | 3 | .kr-collapsible-toggle { |
4 | 4 | float:right; |
5 | 5 | } |
6 | | -.kr-collapsible-toggle-hide, |
7 | | -.kr-collapsible-toggle-hide a { |
| 6 | +.kr-collapsible-toggle-expanded, |
| 7 | +.kr-collapsible-toggle-expanded a { |
8 | 8 | cursor:n-resize; |
9 | 9 | } |
10 | | -.kr-collapsible-toggle-show, |
11 | | -.kr-collapsible-toggle-show a { |
| 10 | +.kr-collapsible-toggle-collapsed, |
| 11 | +.kr-collapsible-toggle-collapsed a { |
12 | 12 | cursor:s-resize; |
13 | 13 | } |
14 | 14 | /* list-items go as wide as their parent element, don't float them inside list items */ |
Index: trunk/phase3/resources/Resources.php |
— | — | @@ -81,6 +81,7 @@ |
82 | 82 | 'jquery.makeCollapsible' => array( |
83 | 83 | 'scripts' => 'resources/jquery/jquery.makeCollapsible.js', |
84 | 84 | 'styles' => 'resources/jquery/jquery.makeCollapsible.css', |
| 85 | + 'messages' => array( 'collapsible-expand', 'collapsible-collapse' ), |
85 | 86 | ), |
86 | 87 | 'jquery.suggestions' => array( |
87 | 88 | 'scripts' => 'resources/jquery/jquery.suggestions.js', |