r110225 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110224‎ | r110225 | r110226 >
Date:00:32, 29 January 2012
Author:nad
Status:deferred
Tags:
Comment:
version 2.0.0 designed for MW1.18+. Also adds capability for an onload hook for each tree so JS can be added to manipulate them, and adds new #star magic word ready for a new menu type currently under development
Modified paths:
  • /trunk/extensions/TreeAndMenu/TreeAndMenu.i18n.magic.php (modified) (history)
  • /trunk/extensions/TreeAndMenu/TreeAndMenu.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TreeAndMenu/TreeAndMenu.i18n.magic.php
@@ -8,4 +8,5 @@
99 $magicWords['en'] = array(
1010 'menu' => array( 0, 'menu' ),
1111 'tree' => array( 0, 'tree' ),
 12+ 'star' => array( 0, 'star' ),
1213 );
Index: trunk/extensions/TreeAndMenu/TreeAndMenu.php
@@ -14,7 +14,7 @@
1515
1616 if( !defined( 'MEDIAWIKI' ) ) die( 'Not an entry point.' );
1717
18 -define( 'TREEANDMENU_VERSION','1.3.0, 2012-01-19' );
 18+define( 'TREEANDMENU_VERSION','2.0.0, 2012-01-28' );
1919
2020 // Set any unset images to default titles
2121 if( !isset( $wgTreeViewImages ) || !is_array( $wgTreeViewImages ) ) $wgTreeViewImages = array();
@@ -48,31 +48,29 @@
4949 var $images = ''; // internal JS to update dTree images
5050 var $useLines = true; // internal variable determining whether to render connector lines
5151 var $args = array(); // args for each tree
52 - var $js = 0;
5352
5453 /**
5554 * Constructor
5655 */
5756 function __construct() {
58 - global $wgOut, $wgHooks, $wgParser, $wgScriptPath, $wgJsMimeType,
59 - $wgTreeViewImages, $wgTreeViewShowLines, $wgTreeViewBaseDir, $wgTreeViewBaseUrl;
 57+ global $wgOut, $wgHooks, $wgParser, $wgJsMimeType, $wgExtensionAssetsPath, $wgResourceModules, $wgTreeViewImages, $wgTreeViewShowLines;
6058
6159 // Add hooks
62 - $wgParser->setFunctionHook( 'tree', array( $this,'expandTree' ) );
63 - $wgParser->setFunctionHook( 'menu', array( $this,'expandMenu' ) );
 60+ $wgParser->setFunctionHook( 'tree', array( $this, 'expandTree' ) );
 61+ $wgParser->setFunctionHook( 'menu', array( $this, 'expandMenu' ) );
 62+ $wgParser->setFunctionHook( 'star', array( $this, 'expandStar' ) );
6463 $wgHooks['ParserAfterTidy'][] = array( $this, 'renderTreeAndMenu' );
6564
6665 // Update general tree paths and properties
67 - $this->baseDir = isset( $wgTreeViewBaseDir ) ? $wgTreeViewBaseDir : dirname( __FILE__ );
68 - $this->baseUrl = isset( $wgTreeViewBaseUrl ) ? $wgTreeViewBaseUrl : preg_replace( '|^.+(?=/ext)|', $wgScriptPath, $this->baseDir );
 66+ $this->baseDir = dirname( __FILE__ );
 67+ $this->baseUrl = $wgExtensionAssetsPath . '/' . basename( dirname( __FILE__ ) );
6968 $this->useLines = $wgTreeViewShowLines ? 'true' : 'false';
7069 $this->uniq = uniqid( $this->uniqname );
7170
7271 // Convert image titles to file paths and store as JS to update dTree
7372 foreach( $wgTreeViewImages as $k => $v ) {
74 - $title = Title::newFromText( $v, NS_IMAGE );
75 - $image = wfFindFile( $title );
76 - $v = $image && $image->exists() ? $image->getURL() : $wgTreeViewImages[$k];
 73+ $image = wfLocalFile( $v );
 74+ $v = ( is_object( $image ) && $image->exists() ) ? $image->getURL() : $wgTreeViewImages[$k];
7775 $this->images .= "tree.icon['$k'] = '$v';";
7876 }
7977 }
@@ -94,6 +92,14 @@
9593 }
9694
9795 /**
 96+ * Expand #star parser-functions
 97+ */
 98+ public function expandStar() {
 99+ $args = func_get_args();
 100+ return 'star menus coming soon!';
 101+ }
 102+
 103+ /**
98104 * Expand either kind of parser-function (reformats tree rows for matching later) and store args
99105 */
100106 private function expandTreeAndMenu( $magic, $args ) {
@@ -212,6 +218,13 @@
213219 $class = isset( $args['class'] ) ? $args['class'] : "d$type";
214220 if( $type == 'tree' ) {
215221
 222+ // Load the dTree script if not loaded already
 223+ static $dtree = false;
 224+ if( !$dtree ) {
 225+ $wgOut->addScriptFile( $this->baseUrl . '/dtree.js' );
 226+ $dtree = true;
 227+ }
 228+
216229 // Finalise a tree
217230 $add = isset( $args['root'] ) ? "tree.add(0,-1,'".$args['root']."');" : '';
218231 $top = $bottom = $root = $opennodesjs = '';
@@ -222,41 +235,33 @@
223236 if( $top ) $top = "<p>&#160;$top</p>";
224237 if( $bottom ) $bottom = "<p>&#160;$bottom</p>";
225238
226 - // Add the dTRee script if not loaded yet
227 - $dTreeScript = $this->js++ ? "" : "<script type=\"$wgJsMimeType\" src=\"{$this->baseUrl}/dtree.js\"></script>";
228 -
229239 // Define the script to build this tree
230 - $script = "tree = new dTree('$objid');
231 - for (i in tree.icon) tree.icon[i] = '{$this->baseUrl}/'+tree.icon[i];{$this->images}
232 - tree.config.useLines = {$this->useLines};
233 - $add
234 - $objid = tree;
235 - $nodes
236 - document.getElementById('$id').innerHTML = $objid.toString();
237 - $opennodesjs";
238 -
239 - // Embed the script into the output
240 - $html = "$top<div class='$class' id='$id'>$dTreeScript<script type=\"$wgJsMimeType\">/*<![CDATA[*/
241 - // TreeAndMenu-{$this->version}
242 - $script
243 - /*]]>*/</script></div>$bottom";
 240+ $script = "// TreeAndMenu-{$this->version}\ntree = new dTree('$objid');
 241+ for (i in tree.icon) tree.icon[i] = '{$this->baseUrl}/'+tree.icon[i];{$this->images}
 242+ tree.config.useLines = {$this->useLines};
 243+ $add
 244+ $objid = tree;
 245+ $nodes
 246+ document.getElementById('$id').innerHTML = $objid.toString();
 247+ $opennodesjs
 248+ for(i in window.tamOnload_$objid) { window.tamOnload_{$objid}[i](); }";
 249+ $wgOut->addScript( "<script type=\"$wgJsMimeType\">$(function(){\n$script\n});</script>" );
 250+ $html = "$top<div class='$class' id='$id'></div>$bottom";
 251+ $html .= "<script type=\"$wgJsMimeType\">window.tamOnload_$objid=[]</script>";
244252 } else {
245253
246254 // Finalise a menu
247255 if( $depth > 0 ) $nodes .= str_repeat( '</ul></li>', $depth );
248256 $nodes = preg_replace( "/<(a.*? )title=\".+?\".*?>/", "<$1>", $nodes ); // IE has problems with title attribute in suckerfish menus
249 - $html = "
250 - <ul class='$class' id='$id'>\n$nodes</ul>
251 - <script type=\"$wgJsMimeType\">/*<![CDATA[*/
252 - if (window.attachEvent) {
 257+ $html = "<ul class='$class' id='$id'>\n$nodes</ul><div style=\"clear:both\"></div>";
 258+ $script = "if (window.attachEvent) {
253259 var sfEls = document.getElementById('$id').getElementsByTagName('li');
254260 for (var i=0; i<sfEls.length; i++) {
255261 sfEls[i].onmouseover=function() { this.className+=' sfhover'; }
256262 sfEls[i].onmouseout=function() { this.className=this.className.replace(new RegExp(' sfhover *'),''); }
257263 }
258 - }
259 - /*]]>*/</script>
260 - ";
 264+ }";
 265+ $wgOut->addScript( "<script type=\"$wgJsMimeType\">$(function(){\n$script\n});</script>" );
261266 }
262267
263268 $text = preg_replace( "/\x7f1$u\x7f$id\x7f.+?$/m", $html, $text, 1 ); // replace first occurence of this trees root-id

Status & tagging log