Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.templateEditor.js |
— | — | @@ -7,7 +7,7 @@ |
8 | 8 | evt: { |
9 | 9 | mark: function( context, event ) { |
10 | 10 | // This is shared by both the closure findOutermostTemplates and the calling code - is this a good idea? |
11 | | - var i = 0; |
| 11 | + var tokenIterator = 0; |
12 | 12 | /** |
13 | 13 | * Finds the left and right character positions of the outer-most template declaration, playing nicely with |
14 | 14 | * nested template calls of any depth. This function acts as an iterator, which is why the i var is shared - but |
— | — | @@ -17,13 +17,13 @@ |
18 | 18 | */ |
19 | 19 | function findOutermostTemplates( tokenStack ) { |
20 | 20 | var templateBeginFound = false; |
21 | | - for ( ; i < tokenStack.length; i++ ) { |
22 | | - if ( tokenStack[i].label == 'TEMPLATE_BEGIN' ) { |
| 21 | + for ( ; tokenIterator < tokenStack.length; tokenIterator++ ) { |
| 22 | + if ( tokenStack[tokenIterator].label == 'TEMPLATE_BEGIN' ) { |
23 | 23 | templateBeginFound = true; |
24 | 24 | break; |
25 | 25 | } |
26 | 26 | } |
27 | | - var j = i++; |
| 27 | + var j = tokenIterator++; |
28 | 28 | if ( !templateBeginFound ) { |
29 | 29 | return false; |
30 | 30 | } else { |
— | — | @@ -36,9 +36,9 @@ |
37 | 37 | } |
38 | 38 | if ( nestedBegins == 0 ) { |
39 | 39 | // Outer template begins at tokenStack[i].offset and ends at tokenStack[j].offset + 2 |
40 | | - var leftMarker = i -1; |
| 40 | + var leftMarker = tokenIterator -1; |
41 | 41 | var rightMarker = j; |
42 | | - i = j; |
| 42 | + tokenIterator = j; |
43 | 43 | return [leftMarker, rightMarker]; |
44 | 44 | } else { |
45 | 45 | return false; |
— | — | @@ -90,6 +90,69 @@ |
91 | 91 | model: function( wikitext ) { |
92 | 92 | |
93 | 93 | /* Private Functions */ |
| 94 | + }, |
| 95 | + |
| 96 | + |
| 97 | + stylize: function( context ) { |
| 98 | + var $templates = context.$content.find( ".wiki-template" ); |
| 99 | + $templates.each( function(){ |
| 100 | + if( typeof $( this ).data( 'model' ) != 'undefined' ){ |
| 101 | + //we have a model, so all this init stuff has already happened |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + //hide this |
| 106 | + $(this).addClass('wikieditor-nodisplay'); |
| 107 | + |
| 108 | + //build a model for this |
| 109 | + $( this ).data( 'model' , new model( $( this ).text() ) ); |
| 110 | + var model = $( this ).data( 'model' ); |
| 111 | + |
| 112 | + |
| 113 | + //expand |
| 114 | + function expandTemplate($displayDiv){ |
| 115 | + //housekeeping |
| 116 | + $displayDiv.removeClass('wiki-collapsed-template'); |
| 117 | + $displayDiv.addClass('wiki-expanded-template'); |
| 118 | + $displayDiv.data('mode') = "expanded"; |
| 119 | + |
| 120 | + $displayDiv.text( model.getText() ); |
| 121 | + |
| 122 | + }; |
| 123 | + |
| 124 | + //collapse |
| 125 | + function collapseTemplate($displayDiv){ |
| 126 | + //housekeeping |
| 127 | + $displayDiv.addClass('wiki-collapsed-template'); |
| 128 | + $displayDiv.removeClass('wiki-expanded-template'); |
| 129 | + $displayDiv.data('mode') = "collapsed"; |
| 130 | + |
| 131 | + $displayDiv.text( model.getName() ); |
| 132 | + |
| 133 | + }; |
| 134 | + |
| 135 | + //build the collapsed version of this template |
| 136 | + var $visibleDiv = $( "<div></div>" ).addClass( 'wikieditor-noinclude' ); |
| 137 | + |
| 138 | + //let these two know about eachother |
| 139 | + $(this).data( 'display' , $visibleDiv ); |
| 140 | + $visibleDiv.data('wikitext-node', $(this)); |
| 141 | + $(this).after( $visibleDiv ); |
| 142 | + |
| 143 | + //onClick |
| 144 | + $visibleDiv.click( function(){ |
| 145 | + //is collapsed, switch to expand |
| 146 | + if( $(this).data('mode') == 'collapsed' ){ |
| 147 | + expandTemplate( $(this) ); |
| 148 | + } |
| 149 | + else{ |
| 150 | + collapseTemplate( $(this) ); |
| 151 | + } |
| 152 | + });//click |
| 153 | + |
| 154 | + collapseTemplate($visibleDiv); |
| 155 | + }); |
| 156 | + }, |
94 | 157 | |
95 | 158 | /** |
96 | 159 | * Builds a Param object. |