Difference between revisions of "Extension:Treeview3.php"
(JS must be added unconditionally currently) |
m ({{legacy|the TreeAndMenu extension}}) |
||
(12 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{legacy|the [[MW:Extension:TreeAndMenu|TreeAndMenu extension]]}} | |
− | + | <php><?php | |
+ | # MediaWiki Treeview Extension | ||
# - See http://www.mediawiki.org/wiki/Extension:Tree_view for installation and usage details | # - See http://www.mediawiki.org/wiki/Extension:Tree_view for installation and usage details | ||
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | # - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | ||
# - Author: http://www.organicdesign.co.nz/nad | # - Author: http://www.organicdesign.co.nz/nad | ||
− | + | ||
if (!defined('MEDIAWIKI')) die('Not an entry point.'); | if (!defined('MEDIAWIKI')) die('Not an entry point.'); | ||
− | define('TREEVIEW_VERSION','3. | + | define('TREEVIEW_VERSION','3.6.2, 2007-09-02'); |
− | + | ||
− | if (!is_array($wgTreeViewImages)) $wgTreeViewImages = array( | + | # Set any unset images to default titles |
− | + | if (!is_array($wgTreeViewImages)) $wgTreeViewImages = array(); | |
− | + | if (!isset($wgTreeViewImages['plus'])) $wgTreeViewImages['plus'] = 'Plus.gif'; | |
− | + | if (!isset($wgTreeViewImages['minus'])) $wgTreeViewImages['minus'] = 'Minus.gif'; | |
− | + | if (!isset($wgTreeViewImages['opened'])) $wgTreeViewImages['opened'] = 'Folder_opn_sml_yel.gif'; | |
− | + | if (!isset($wgTreeViewImages['closed'])) $wgTreeViewImages['closed'] = 'Folder_sml_yel.gif'; | |
− | + | if (!isset($wgTreeViewImages['doc'])) $wgTreeViewImages['doc'] = 'Doc-icon.gif'; | |
− | + | if (!isset($wgTreeViewImages['spacer'])) $wgTreeViewImages['spacer'] = 'Blank.gif'; | |
− | + | ||
# Keep track of JavaScript added to page to avoid doubleups | # Keep track of JavaScript added to page to avoid doubleups | ||
if (!isset($wgJS)) $wgJS = array(); | if (!isset($wgJS)) $wgJS = array(); | ||
− | + | ||
$wgTreeViewMagic = "tree"; # the parser-function name for trees | $wgTreeViewMagic = "tree"; # the parser-function name for trees | ||
$wgExtensionFunctions[] = 'wfSetupTreeView'; | $wgExtensionFunctions[] = 'wfSetupTreeView'; | ||
$wgHooks['LanguageGetMagic'][] = 'wfTreeViewLanguageGetMagic'; | $wgHooks['LanguageGetMagic'][] = 'wfTreeViewLanguageGetMagic'; | ||
− | + | ||
$wgExtensionCredits['parserhook'][] = array( | $wgExtensionCredits['parserhook'][] = array( | ||
'name' => 'Treeview', | 'name' => 'Treeview', | ||
Line 32: | Line 33: | ||
'version' => TREEVIEW_VERSION | 'version' => TREEVIEW_VERSION | ||
); | ); | ||
− | + | ||
class TreeView { | class TreeView { | ||
− | + | ||
var $version = TREEVIEW_VERSION; | var $version = TREEVIEW_VERSION; | ||
+ | var $width = 16; | ||
var $token = '@@@'; | var $token = '@@@'; | ||
− | var $id = | + | var $id = 1; |
var $js = 0; | var $js = 0; | ||
var $info; | var $info; | ||
var $args; | var $args; | ||
− | + | ||
# Constructor | # Constructor | ||
function TreeView($magic) { | function TreeView($magic) { | ||
− | global $wgParser,$wgHooks; | + | global $wgParser,$wgHooks,$wgTreeViewImages; |
$this->info = array(); | $this->info = array(); | ||
$this->args = array(); | $this->args = array(); | ||
+ | |||
+ | # Set hooks | ||
$wgParser->setFunctionHook($magic,array($this,'Tree')); | $wgParser->setFunctionHook($magic,array($this,'Tree')); | ||
$wgParser->setHook('TreeViewRowStart',array($this,'RowStart')); | $wgParser->setHook('TreeViewRowStart',array($this,'RowStart')); | ||
$wgParser->setHook('TreeViewRowEnd',array($this,'RowEnd')); | $wgParser->setHook('TreeViewRowEnd',array($this,'RowEnd')); | ||
+ | |||
+ | # Convert image titles to file paths and obtain pixel width of items | ||
+ | foreach ($wgTreeViewImages as $k => $v) { | ||
+ | $title = Title::newFromText($v,NS_IMAGE); | ||
+ | $image = Image::newFromTitle($title); | ||
+ | if ($image && $image->exists()) { | ||
+ | $wgTreeViewImages[$k] = $image->getURL(); | ||
+ | if ($k == 'plus') $this->width = $image->getWidth(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # Obtain the width of tree items (use 16px as default if unobtainable) | ||
+ | |||
} | } | ||
− | + | ||
# Restructure recursive trees to single root trees surrounded by $magic tags | # Restructure recursive trees to single root trees surrounded by $magic tags | ||
function Tree(&$parser) { | function Tree(&$parser) { | ||
− | |||
$args = array(); | $args = array(); | ||
foreach (func_get_args() as $arg) if (!is_object($arg)) { | foreach (func_get_args() as $arg) if (!is_object($arg)) { | ||
Line 63: | Line 79: | ||
$token = $this->token; | $token = $this->token; | ||
$ver = $this->version; | $ver = $this->version; | ||
− | $id = | + | $id = uniqid(); |
if (count($parser->mTemplatePath)) $tree = "*O$token\n$text\n*C$token"; | if (count($parser->mTemplatePath)) $tree = "*O$token\n$text\n*C$token"; | ||
elseif (preg_match_all("/^(\\*+)(.$token)? *(.*?) *$/m",$text,$match)) { | elseif (preg_match_all("/^(\\*+)(.$token)? *(.*?) *$/m",$text,$match)) { | ||
Line 79: | Line 95: | ||
$ld = $indent; | $ld = $indent; | ||
} | } | ||
− | $tree = "<table class='tree-view' id=' | + | $tree = "<table class='tree-view' id='tv$id' title='Extension:Treeview (ver $ver)'>\n$tree</table>"; |
} | } | ||
return $tree; | return $tree; | ||
} | } | ||
− | + | ||
# Convert each row into HTML | # Convert each row into HTML | ||
function RowStart($text,$argv,&$parser) { | function RowStart($text,$argv,&$parser) { | ||
global $wgTreeViewImages; | global $wgTreeViewImages; | ||
list($id,$row) = split(',',$text); | list($id,$row) = split(',',$text); | ||
− | + | ||
$args = $this->args[$id]; | $args = $this->args[$id]; | ||
$info = $this->info[$id]; | $info = $this->info[$id]; | ||
− | $ | + | $width = $this->width; |
$depth = $info[$row]; | $depth = $info[$row]; | ||
$next = isset($info[$row+1]) ? $info[$row+1] : 0; | $next = isset($info[$row+1]) ? $info[$row+1] : 0; | ||
Line 101: | Line 117: | ||
$doc = isset($args['doc']) ? $args['doc'] : $wgTreeViewImages['doc']; | $doc = isset($args['doc']) ? $args['doc'] : $wgTreeViewImages['doc']; | ||
$default = isset($args['openlevels']) ? $args['openlevels'] : 0; | $default = isset($args['openlevels']) ? $args['openlevels'] : 0; | ||
− | + | ||
$show = $depth > $default ? ' style="display:none"' : ''; | $show = $depth > $default ? ' style="display:none"' : ''; | ||
if ($depth >= $default) { $open = $plus; $icon = $closed; } else { $open = $minus; $icon = $opened; } | if ($depth >= $default) { $open = $plus; $icon = $closed; } else { $open = $minus; $icon = $opened; } | ||
− | if ($depth >= $next) { $open = ''; $icon = $doc; } | + | if ($depth >= $next) { $open = "<img src='$spacer' width='$width'/>"; $icon = $doc; } |
− | else $open = "<a href='javascript:toggleTreeviewItem($ | + | else $open = "<a href='javascript:toggleTreeviewItem(\"$id\",$row)'><img id='tvi$id$row' src='$open'/></a>"; |
− | + | ||
− | return "<tr$show class='tree-row' depth='$depth' id='$ | + | return "<tr$show class='tree-row' depth='$depth' id='tvr$id$row'>" |
− | . "<td valign='bottom'><img src='$spacer' width='".($depth* | + | . "<td valign='bottom'><img src='$spacer' width='".($depth*$width)."' height='1'/>$open" |
− | . " <img id=' | + | . " <img id='tvf$id$row' src='$icon'/> "; |
} | } | ||
− | + | ||
function RowEnd($text,$argv,&$parser) { | function RowEnd($text,$argv,&$parser) { | ||
return "</td></tr>\n"; | return "</td></tr>\n"; | ||
} | } | ||
− | + | ||
# Add the javascript to the output object if not added yet and there is at least one tree | # Add the javascript to the output object if not added yet and there is at least one tree | ||
function addJS() { | function addJS() { | ||
Line 128: | Line 144: | ||
$doc = $wgTreeViewImages['doc']; | $doc = $wgTreeViewImages['doc']; | ||
$wgOut->addScript('<script type="'.$wgJsMimeType.'"> | $wgOut->addScript('<script type="'.$wgJsMimeType.'"> | ||
− | function toggleTreeviewItem(id) { | + | function toggleTreeviewItem(id,row) { |
var plus = "'.$plus.'"; | var plus = "'.$plus.'"; | ||
var minus = "'.$minus.'"; | var minus = "'.$minus.'"; | ||
Line 134: | Line 150: | ||
var closed = "'.$closed.'"; | var closed = "'.$closed.'"; | ||
var doc = "'.$doc.'"; | var doc = "'.$doc.'"; | ||
− | var item = document.getElementById(id); | + | var item = document.getElementById("tvr"+id+row); |
− | var next = document.getElementById(id+1); | + | var next = document.getElementById("tvr"+id+(row+1)); |
var depth = 0+item.getAttribute("depth"); | var depth = 0+item.getAttribute("depth"); | ||
var close = next.style.display != "none"; | var close = next.style.display != "none"; | ||
− | var img = document.getElementById(" | + | var img = document.getElementById("tvi"+id+row); |
− | var fld = document.getElementById(" | + | var fld = document.getElementById("tvf"+id+row); |
fld.setAttribute("src",close ? closed : opened); | fld.setAttribute("src",close ? closed : opened); | ||
img.setAttribute("src",close ? plus : minus); | img.setAttribute("src",close ? plus : minus); | ||
− | while ((item = document.getElementById(++ | + | while ((item = document.getElementById("tvr"+id+(++row))) && (0+item.getAttribute("depth") > depth)) { |
if (close) item.style.display = "none"; | if (close) item.style.display = "none"; | ||
else if (depth == item.getAttribute("depth")-1) { | else if (depth == item.getAttribute("depth")-1) { | ||
item.style.display = ""; | item.style.display = ""; | ||
− | if (img = document.getElementById(" | + | if (img = document.getElementById("tvi"+id+row)) img.setAttribute("src",plus); |
− | if (fld = document.getElementById(" | + | if (fld = document.getElementById("tvf"+id+row)) |
if (fld.getAttribute("src") == opened) fld.setAttribute("src",closed); | if (fld.getAttribute("src") == opened) fld.setAttribute("src",closed); | ||
} | } | ||
Line 154: | Line 170: | ||
} | } | ||
} | } | ||
− | + | ||
# Called from $wgExtensionFunctions array when initialising extensions | # Called from $wgExtensionFunctions array when initialising extensions | ||
function wfSetupTreeView() { | function wfSetupTreeView() { | ||
global $wgTreeView,$wgTreeViewMagic,$wgVersion; | global $wgTreeView,$wgTreeViewMagic,$wgVersion; | ||
$wgTreeView = new TreeView($wgTreeViewMagic); | $wgTreeView = new TreeView($wgTreeViewMagic); | ||
− | $wgTreeView->addJS(); # | + | $wgTreeView->addJS(); # Make code unconditional for now due to parser caching |
} | } | ||
− | + | ||
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly | # Needed in MediaWiki >1.8.0 for magic word hooks to work properly | ||
function wfTreeViewLanguageGetMagic(&$magicWords,$langCode = 0) { | function wfTreeViewLanguageGetMagic(&$magicWords,$langCode = 0) { | ||
Line 168: | Line 184: | ||
return true; | return true; | ||
} | } | ||
− | ?> | + | ?></php> |
+ | [[Category:Legacy Extensions|Treeview]] |
Latest revision as of 02:26, 8 June 2008
<php><?php
- MediaWiki Treeview Extension
- - See http://www.mediawiki.org/wiki/Extension:Tree_view for installation and usage details
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: http://www.organicdesign.co.nz/nad
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('TREEVIEW_VERSION','3.6.2, 2007-09-02');
- Set any unset images to default titles
if (!is_array($wgTreeViewImages)) $wgTreeViewImages = array(); if (!isset($wgTreeViewImages['plus'])) $wgTreeViewImages['plus'] = 'Plus.gif'; if (!isset($wgTreeViewImages['minus'])) $wgTreeViewImages['minus'] = 'Minus.gif'; if (!isset($wgTreeViewImages['opened'])) $wgTreeViewImages['opened'] = 'Folder_opn_sml_yel.gif'; if (!isset($wgTreeViewImages['closed'])) $wgTreeViewImages['closed'] = 'Folder_sml_yel.gif'; if (!isset($wgTreeViewImages['doc'])) $wgTreeViewImages['doc'] = 'Doc-icon.gif'; if (!isset($wgTreeViewImages['spacer'])) $wgTreeViewImages['spacer'] = 'Blank.gif';
- Keep track of JavaScript added to page to avoid doubleups
if (!isset($wgJS)) $wgJS = array();
$wgTreeViewMagic = "tree"; # the parser-function name for trees $wgExtensionFunctions[] = 'wfSetupTreeView'; $wgHooks['LanguageGetMagic'][] = 'wfTreeViewLanguageGetMagic';
$wgExtensionCredits['parserhook'][] = array( 'name' => 'Treeview', 'author' => 'User:Nad', 'url' => 'http://www.mediawiki.org/wiki/Extension:Treeview', 'description' => 'Allows dynamic tree-views to be made with bullet-list syntax', 'version' => TREEVIEW_VERSION );
class TreeView {
var $version = TREEVIEW_VERSION; var $width = 16; var $token = '@@@'; var $id = 1; var $js = 0; var $info; var $args;
# Constructor function TreeView($magic) { global $wgParser,$wgHooks,$wgTreeViewImages; $this->info = array(); $this->args = array();
# Set hooks $wgParser->setFunctionHook($magic,array($this,'Tree')); $wgParser->setHook('TreeViewRowStart',array($this,'RowStart')); $wgParser->setHook('TreeViewRowEnd',array($this,'RowEnd'));
# Convert image titles to file paths and obtain pixel width of items foreach ($wgTreeViewImages as $k => $v) { $title = Title::newFromText($v,NS_IMAGE); $image = Image::newFromTitle($title); if ($image && $image->exists()) { $wgTreeViewImages[$k] = $image->getURL(); if ($k == 'plus') $this->width = $image->getWidth(); } }
# Obtain the width of tree items (use 16px as default if unobtainable)
}
# Restructure recursive trees to single root trees surrounded by $magic tags function Tree(&$parser) { $args = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)=(.+)$/',$arg,$match)) $args[$match[1]] = $match[2]; else $args[] = $arg; } $text = $args[0]; $tree = ; $token = $this->token; $ver = $this->version; $id = uniqid(); if (count($parser->mTemplatePath)) $tree = "*O$token\n$text\n*C$token"; elseif (preg_match_all("/^(\\*+)(.$token)? *(.*?) *$/m",$text,$match)) { $this->args[$id] = $args; $this->info[$id] = array(); $nest = array(); $ld = ; $row = -1; foreach ($match[1] as $i => $indent) { if ($sub = $match[2][$i]) $sub == "O$token" ? $nest[] = substr($ld,0,-1) : array_pop($nest); elseif ($item = $match[3][$i]) { $this->info[$id][++$row] = strlen($indent.join(,$nest))-1; $tree .= "<TreeViewRowStart>$id,$row</TreeViewRowStart>$item<TreeViewRowEnd/>\n"; } $ld = $indent; }
$tree = "
\n$tree";
} return $tree; }
# Convert each row into HTML function RowStart($text,$argv,&$parser) { global $wgTreeViewImages; list($id,$row) = split(',',$text);
$args = $this->args[$id]; $info = $this->info[$id]; $width = $this->width; $depth = $info[$row]; $next = isset($info[$row+1]) ? $info[$row+1] : 0; $plus = isset($args['plus']) ? $args['plus'] : $wgTreeViewImages['plus']; $minus = isset($args['minus']) ? $args['minus'] : $wgTreeViewImages['minus']; $opened = isset($args['opened']) ? $args['opened'] : $wgTreeViewImages['opened']; $closed = isset($args['closed']) ? $args['closed'] : $wgTreeViewImages['closed']; $spacer = isset($args['spacer']) ? $args['spacer'] : $wgTreeViewImages['spacer']; $doc = isset($args['doc']) ? $args['doc'] : $wgTreeViewImages['doc']; $default = isset($args['openlevels']) ? $args['openlevels'] : 0;
$show = $depth > $default ? ' style="display:none"' : ; if ($depth >= $default) { $open = $plus; $icon = $closed; } else { $open = $minus; $icon = $opened; } if ($depth >= $next) { $open = "<img src='$spacer' width='$width'/>"; $icon = $doc; } else $open = "<a href='javascript:toggleTreeviewItem(\"$id\",$row)'><img id='tvi$id$row' src='$open'/></a>";
return "<tr$show class='tree-row' depth='$depth' id='tvr$id$row'>"
. "<img src='$spacer' width='".($depth*$width)."' height='1'/>$open"
. " <img id='tvf$id$row' src='$icon'/> "; }
function RowEnd($text,$argv,&$parser) {
return "\n"; } # Add the javascript to the output object if not added yet and there is at least one tree function addJS() { global $wgOut,$wgTreeViewImages,$wgJS,$wgJsMimeType; if (isset($wgJS['TreeView'])) return; $wgJS['TreeView'] = true; $plus = $wgTreeViewImages['plus']; $minus = $wgTreeViewImages['minus']; $opened = $wgTreeViewImages['opened']; $closed = $wgTreeViewImages['closed']; $spacer = $wgTreeViewImages['spacer']; $doc = $wgTreeViewImages['doc']; $wgOut->addScript('<script type="'.$wgJsMimeType.'"> function toggleTreeviewItem(id,row) { var plus = "'.$plus.'"; var minus = "'.$minus.'"; var opened = "'.$opened.'"; var closed = "'.$closed.'"; var doc = "'.$doc.'"; var item = document.getElementById("tvr"+id+row); var next = document.getElementById("tvr"+id+(row+1)); var depth = 0+item.getAttribute("depth"); var close = next.style.display != "none"; var img = document.getElementById("tvi"+id+row); var fld = document.getElementById("tvf"+id+row); fld.setAttribute("src",close ? closed : opened); img.setAttribute("src",close ? plus : minus); while ((item = document.getElementById("tvr"+id+(++row))) && (0+item.getAttribute("depth") > depth)) { if (close) item.style.display = "none"; else if (depth == item.getAttribute("depth")-1) { item.style.display = ""; if (img = document.getElementById("tvi"+id+row)) img.setAttribute("src",plus); if (fld = document.getElementById("tvf"+id+row)) if (fld.getAttribute("src") == opened) fld.setAttribute("src",closed); } } }</script>'); } }
- Called from $wgExtensionFunctions array when initialising extensions
function wfSetupTreeView() { global $wgTreeView,$wgTreeViewMagic,$wgVersion; $wgTreeView = new TreeView($wgTreeViewMagic); $wgTreeView->addJS(); # Make code unconditional for now due to parser caching }
- Needed in MediaWiki >1.8.0 for magic word hooks to work properly
function wfTreeViewLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgTreeViewMagic; $magicWords[$wgTreeViewMagic] = array(0,$wgTreeViewMagic); return true; } ?></php>