Index: trunk/extensions/CategoryTree/CategoryTreePage.php |
— | — | @@ -26,6 +26,7 @@ |
27 | 27 | * Constructor |
28 | 28 | */ |
29 | 29 | function CategoryTree() { |
| 30 | + global $wgOut; |
30 | 31 | SpecialPage::SpecialPage( 'CategoryTree', 'categorytree' ); |
31 | 32 | |
32 | 33 | #inject messages |
— | — | @@ -60,7 +61,7 @@ |
61 | 62 | $wgOut->addHtml( $this->makeInputForm() ); |
62 | 63 | |
63 | 64 | if( $this->target !== '' && $this->target !== NULL ) { |
64 | | - efCategoryTreeHeader(); |
| 65 | + $wgOut->addScript( efCategoryTreeGetJsMessages() ); #TODO: move it... |
65 | 66 | |
66 | 67 | $title = efCategoryTreeMakeTitle( $this->target ); |
67 | 68 | |
— | — | @@ -77,7 +78,7 @@ |
78 | 79 | $html .= wfCloseElement( 'div' ); |
79 | 80 | |
80 | 81 | $html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeResult' ) ); |
81 | | - $html .= efCategoryTreeRenderNode( $title, $this->mode, true ); |
| 82 | + $html .= efCategoryTreeRenderNode( $title, $this->mode, true, false ); |
82 | 83 | $html .= wfCloseElement( 'div' ); |
83 | 84 | $wgOut->addHtml( $html ); |
84 | 85 | } |
Index: trunk/extensions/CategoryTree/CategoryTree.php |
— | — | @@ -59,6 +59,7 @@ |
60 | 60 | $wgAutoloadClasses['CategoryTree'] = dirname( __FILE__ ) . '/CategoryTreePage.php'; |
61 | 61 | $wgSpecialPages['CategoryTree'] = 'CategoryTree'; |
62 | 62 | $wgHooks['SkinTemplateTabs'][] = 'efCategoryTreeInstallTabs'; |
| 63 | +#$wgHooks['OutputPageBeforeHTML'][] = 'efCategoryTreeHeadHook'; |
63 | 64 | |
64 | 65 | /** |
65 | 66 | * register Ajax function |
— | — | @@ -66,18 +67,21 @@ |
67 | 68 | $wgAjaxExportList[] = 'efCategoryTreeAjaxWrapper'; |
68 | 69 | |
69 | 70 | /** |
70 | | - * Internal state |
71 | | - */ |
72 | | -$wgCategoryTreeHeaderDone = false; #set to true by efCategoryTreeHeader after registering the code |
73 | | -$wgCategoryTreeMessagesDone = false; #set to true by efInjectCategoryTreeMessages after registering the messages |
74 | | - |
75 | | -/** |
76 | 71 | * Hook it up |
77 | 72 | */ |
78 | 73 | function efCategoryTree() { |
79 | | - global $wgParser, $wgCategoryTreeAllowTag; |
| 74 | + global $wgParser, $wgOut, $wgCategoryTreeAllowTag; |
| 75 | + global $wgJsMimeType, $wgScriptPath; |
80 | 76 | |
81 | 77 | if ( $wgCategoryTreeAllowTag ) $wgParser->setHook( 'categorytree' , 'efCategoryTreeParserHook' ); |
| 78 | + |
| 79 | + #TODO: injecting scripts should be done on demand, by "somehow" using the ParserOutput |
| 80 | + |
| 81 | + #register css file for CategoryTree |
| 82 | + $wgOut->addLink( array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => $wgScriptPath . '/extensions/CategoryTree/CategoryTree.css' ) ); |
| 83 | + |
| 84 | + #register main js file for CategoryTree |
| 85 | + $wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/CategoryTree/CategoryTree.js\"></script>\n" ); |
82 | 86 | } |
83 | 87 | |
84 | 88 | /** |
— | — | @@ -87,6 +91,8 @@ |
88 | 92 | function efCategoryTreeAjaxWrapper( $category, $mode = CT_MODE_CATEGORIES ) { |
89 | 93 | require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' ); |
90 | 94 | |
| 95 | + efInjectCategoryTreeMessages(); |
| 96 | + |
91 | 97 | return efCategoryTreeAjax( $category, $mode ); |
92 | 98 | } |
93 | 99 | |
— | — | @@ -95,6 +101,9 @@ |
96 | 102 | * This loads CategoryTreeFunctions.php and calls efCategoryTreeTag() |
97 | 103 | */ |
98 | 104 | function efCategoryTreeParserHook( $cat, $argv ) { |
| 105 | + #global $wgParser; |
| 106 | + #$wgParser->mOutput->mCategoryTreeTag = true; #HACK: flag for use by efCategoryTreeHeadHook |
| 107 | + |
99 | 108 | require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' ); |
100 | 109 | |
101 | 110 | $style= @$argv[ 'style' ]; |
— | — | @@ -118,8 +127,24 @@ |
119 | 128 | if ( $hideroot === '1' || $hideroot === 'yes' || $hideroot === 'on' || $hideroot === 'true' ) $hideroot = true; |
120 | 129 | else if ( $hideroot === '0' || $hideroot === 'no' || $hideroot === 'off' || $hideroot === 'false' ) $hideroot = false; |
121 | 130 | } |
| 131 | + |
| 132 | + efInjectCategoryTreeMessages(); |
| 133 | + #efCategoryTreeHeader(); |
122 | 134 | |
123 | | - return efCategoryTreeTag( $cat, $mode, $hideroot, $style ); |
| 135 | + #HACK for inlining JS messages "on demand". Putting them into the head would be nicer, |
| 136 | + # but that would require some changes to ParserOutput to deal with the parser cache |
| 137 | + static $messages = true; |
| 138 | + |
| 139 | + if ( $messages ) { |
| 140 | + efInjectCategoryTreeMessages(); |
| 141 | + $m = efCategoryTreeGetJsMessages(); |
| 142 | + $messages = false; |
| 143 | + } |
| 144 | + else { |
| 145 | + $m = ''; |
| 146 | + } |
| 147 | + |
| 148 | + return $m . efCategoryTreeTag( $cat, $mode, $hideroot, $style ); |
124 | 149 | } |
125 | 150 | |
126 | 151 | /** |
— | — | @@ -142,14 +167,28 @@ |
143 | 168 | } |
144 | 169 | |
145 | 170 | /** |
| 171 | +* Hook callback that injects messages and things into the <head> tag |
| 172 | +* Does nothing if $parserOutput->mCategoryTreeTag is not set |
| 173 | +*/ |
| 174 | +/* function efCategoryTreeHeadHook( &$parserOutput, &$text ) { |
| 175 | + if ( ! @$parserOutput->mCategoryTreeTag ) return; |
| 176 | + |
| 177 | + require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' ); |
| 178 | + |
| 179 | + efInjectCategoryTreeMessages(); |
| 180 | + efCategoryTreeHeader(); |
| 181 | +} */ |
| 182 | + |
| 183 | +/** |
146 | 184 | * inject messages used by CategoryTree into the message cache |
147 | 185 | */ |
148 | 186 | function efInjectCategoryTreeMessages() { |
149 | | - global $wgMessageCache, $wgCategoryTreeMessagesDone; |
| 187 | + global $wgMessageCache; |
150 | 188 | |
151 | | - if ( $wgCategoryTreeMessagesDone ) return; |
152 | | - else $wgCategoryTreeMessagesDone = true; |
153 | | - |
| 189 | + static $done = false; |
| 190 | + if ( $done ) return; |
| 191 | + else $done = true; |
| 192 | + |
154 | 193 | $msg = efLoadCategoryTreeMessages(); |
155 | 194 | $wgMessageCache->addMessages( $msg ); |
156 | 195 | } |
— | — | @@ -158,14 +197,14 @@ |
159 | 198 | * load the CategoryTree internationalization file |
160 | 199 | */ |
161 | 200 | function efLoadCategoryTreeMessages() { |
162 | | - global $wgLanguageCode, $wgContLang; |
| 201 | + global $wgLang; |
163 | 202 | |
164 | 203 | $messages= array(); |
165 | 204 | |
166 | 205 | $f= dirname( __FILE__ ) . '/CategoryTree.i18n.php'; |
167 | 206 | include( $f ); |
168 | 207 | |
169 | | - $f= dirname( __FILE__ ) . '/CategoryTree.i18n.' . $wgContLang->getCode() . '.php'; |
| 208 | + $f= dirname( __FILE__ ) . '/CategoryTree.i18n.' . $wgLang->getCode() . '.php'; |
170 | 209 | if ( file_exists( $f ) ) include( $f ); |
171 | 210 | |
172 | 211 | return $messages; |
Index: trunk/extensions/CategoryTree/CategoryTree.js |
— | — | @@ -42,15 +42,18 @@ |
43 | 43 | } |
44 | 44 | |
45 | 45 | function categoryTreeLoadNode(cat, mode, lnk, div) { |
46 | | - var page_request = false; |
47 | | - |
48 | | - div.innerHTML= '<i class="CategoryTreeNotice">' + categoryTreeLoadingMsg + '</i>'; |
49 | 46 | div.style.display= 'block'; |
50 | 47 | lnk.className= 'CategoryTreeLoaded'; |
51 | 48 | lnk.innerHTML= '–'; |
52 | 49 | lnk.title= categoryTreeCollapseMsg; |
53 | 50 | lnk.onclick= function() { categoryTreeCollapseNode(cat, mode, lnk) } |
54 | | - |
| 51 | + |
| 52 | + categoryTreeLoadChildren(cat, mode, div) |
| 53 | + } |
| 54 | + |
| 55 | + function categoryTreeLoadChildren(cat, mode, div) { |
| 56 | + div.innerHTML= '<i class="CategoryTreeNotice">' + categoryTreeLoadingMsg + '</i>'; |
| 57 | + |
55 | 58 | function f( result ) { |
56 | 59 | if ( result == '' ) result= '<i class="CategorTreeNotice">' + categoryTreeNothingFoundMsg + '</i>'; |
57 | 60 | div.innerHTML= result; |
Index: trunk/extensions/CategoryTree/CategoryTreeFunctions.php |
— | — | @@ -19,12 +19,14 @@ |
20 | 20 | /** |
21 | 21 | * Inserts code into the HTML head. This mainly links CategoryTree.js and CategoryTree.css |
22 | 22 | */ |
23 | | -function efCategoryTreeHeader() { |
24 | | - global $wgOut, $wgCategoryTreeHeaderDone; |
| 23 | +/* function efCategoryTreeHeader() { |
| 24 | + global $wgOut; |
25 | 25 | global $wgJsMimeType, $wgScriptPath; |
| 26 | + |
| 27 | + static $done = false; |
26 | 28 | |
27 | | - if ( $wgCategoryTreeHeaderDone ) return; |
28 | | - else $wgCategoryTreeHeaderDone = true; |
| 29 | + if ( $done ) return; |
| 30 | + else $done = true; |
29 | 31 | |
30 | 32 | #register css file for CategoryTree |
31 | 33 | $wgOut->addLink( array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => $wgScriptPath . '/extensions/CategoryTree/CategoryTree.css' ) ); |
— | — | @@ -39,6 +41,21 @@ |
40 | 42 | categoryTreeLoadingMsg = \"".Xml::escapeJsString(wfMsg('categorytree-loading'))."\"; |
41 | 43 | categoryTreeNothingFoundMsg = \"".Xml::escapeJsString(wfMsg('categorytree-nothing-found'))."\"; |
42 | 44 | </script>\n" ); |
| 45 | +} */ |
| 46 | + |
| 47 | +/* |
| 48 | +* Returns a JS script that sets up messages as global JS variables. |
| 49 | +*/ |
| 50 | +function efCategoryTreeGetJsMessages() { |
| 51 | + global $wgJsMimeType; |
| 52 | + |
| 53 | + return |
| 54 | +" <script type=\"{$wgJsMimeType}\"> |
| 55 | + categoryTreeCollapseMsg = \"".Xml::escapeJsString(wfMsg('categorytree-collapse'))."\"; |
| 56 | + categoryTreeExpandMsg = \"".Xml::escapeJsString(wfMsg('categorytree-expand'))."\"; |
| 57 | + categoryTreeLoadingMsg = \"".Xml::escapeJsString(wfMsg('categorytree-loading'))."\"; |
| 58 | + categoryTreeNothingFoundMsg = \"".Xml::escapeJsString(wfMsg('categorytree-nothing-found'))."\"; |
| 59 | + </script>\n"; |
43 | 60 | } |
44 | 61 | |
45 | 62 | /** |
— | — | @@ -46,8 +63,6 @@ |
47 | 64 | * load CategoryTreeFunctions.php on demand. |
48 | 65 | */ |
49 | 66 | function efCategoryTreeAjax( $category, $mode ) { |
50 | | - efInjectCategoryTreeMessages(); |
51 | | - |
52 | 67 | $title = efCategoryTreeMakeTitle( $category ); |
53 | 68 | |
54 | 69 | return efCategoryTreeRenderChildren( $title, $mode ); |
— | — | @@ -59,10 +74,8 @@ |
60 | 75 | */ |
61 | 76 | function efCategoryTreeTag( $category, $mode, $hideroot = false, $style = '' ) { |
62 | 77 | global $wgOut, $wgParser, $wgCategoryTreeDisableCache, $wgCategoryTreeDynamicTag; |
| 78 | + static $uniq = 0; |
63 | 79 | |
64 | | - efInjectCategoryTreeMessages(); |
65 | | - efCategoryTreeHeader(); |
66 | | - |
67 | 80 | if ( $wgCategoryTreeDisableCache && !$wgCategoryTreeDynamicTag ) $wgParser->disableCache(); |
68 | 81 | |
69 | 82 | $title = efCategoryTreeMakeTitle( $category ); |
— | — | @@ -76,11 +89,20 @@ |
77 | 90 | $html .= wfCloseElement( 'span' ); |
78 | 91 | } |
79 | 92 | else { |
80 | | - if ( !$hideroot ) $html .= efCategoryTreeRenderNode( $title, $mode, true ); |
81 | | - else $html .= efCategoryTreeRenderChildren( $title, $mode ); |
| 93 | + if ( !$hideroot ) $html .= efCategoryTreeRenderNode( $title, $mode, true, $wgCategoryTreeDynamicTag ); |
| 94 | + else if ( !$wgCategoryTreeDynamicTag ) $html .= efCategoryTreeRenderChildren( $title, $mode ); |
| 95 | + else { |
| 96 | + $uniq += 1; |
| 97 | + $load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 ); |
| 98 | + |
| 99 | + $html .= wfOpenElement( 'script', array( 'type' => 'text/javascript', 'id' => $load ) ); |
| 100 | + $html .= 'categoryTreeLoadChildren("' . Xml::escapeJsString( $title->getDBKey() ) . '", "' . $mode . '", document.getElementById("' . $load . '").parentNode );'; |
| 101 | + $html .= wfCloseElement( 'script' ); |
| 102 | + } |
82 | 103 | } |
83 | 104 | |
84 | 105 | $html .= wfCloseElement( 'div' ); |
| 106 | + $html .= "\n\t\t"; |
85 | 107 | |
86 | 108 | return $html; |
87 | 109 | } |
— | — | @@ -196,13 +218,12 @@ |
197 | 219 | * Returns a string with a HTML represenation of the given page. |
198 | 220 | * $title must be a Title object |
199 | 221 | */ |
200 | | -function efCategoryTreeRenderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false ) { |
201 | | - global $wgCategoryTreeDynamicTag; |
| 222 | +function efCategoryTreeRenderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false ) { |
202 | 223 | static $uniq = 0; |
203 | 224 | |
204 | 225 | $load = false; |
205 | 226 | |
206 | | - if ( $children && $wgCategoryTreeDynamicTag ) { |
| 227 | + if ( $loadchildren ) { |
207 | 228 | $uniq += 1; |
208 | 229 | |
209 | 230 | $load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 ); |