Extension:ToggleDisplay

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
ToggleDisplay

Release status: beta

Implementation Tag, Page action
Description show and hide regions on a page with a javascript click
Author(s) RV1971 Talk
Version 0.121 (2007-09-08)
MediaWiki >=1.10.0
Download #Source
Extension:ToggleDisplay/ChangeLog
Parameters ExtToggleDisplay::$mDefaultArgs
Hooks used

BeforePageDisplay

Contents

[edit] Abstract

This extension allows to show and hide regions on a page by clicking on a link.

[edit] Installation

Copy the Source into a file extensions/ToggleDisplay/ToggleDisplay.php and add the following line to your LocalSettings.php:

require_once( 'extensions/ToggleDisplay/ToggleDisplay.php' );

[edit] Usage

The simplest usage is:

<toggledisplay>
hidable text
</toggledisplay>

What you will see is a link "[show details]" in small font and nothing else. When you click on the link, the hidable text will be shown and the link text changes to "[hide details]". When you click again, you return to the initial state.

The following optional parameters can be provided in the <toggledisplay> tag:

Parameter Default Explanation
status hide Initial state when the page is first displayed. May be hide or show.
showtext [show details] Link text when hidable area is hidden.
hidetext [hide details] Link text when hidable area is shown.
linkstyle font-size:smaller CSS style specification for the link.

To change the defaults, you can redefine ExtToggleDisplay::$mDefaultArgs in your LocalSettings.php.

[edit] Source

<?php
 
$wgExtensionCredits['parserhook'][] = array(
  'name' => 'ToggleDisplay',
  'version' => ExtToggleDisplay::VERSION,
  'author' => '[http://www.mediawiki.org/wiki/User:RV1971 RV1971]',
  'url' => 'http://www.mediawiki.org/wiki/Extension:ToggleDisplay',
  'description' => 'show and hide regions on a page with a javascript click'
);
 
// instance of this extension class
$wgExtToggleDisplay = new ExtToggleDisplay();
 
// register the extension
$wgExtensionFunctions[] = array( &$wgExtToggleDisplay, 'setup' );
 
$wgHooks['BeforePageDisplay'][] = array( &$wgExtToggleDisplay, 
                                        'onBeforePageDisplay' );
 
class ExtToggleDisplay
{
  const VERSION = '0.121';
 
  public static $mDefaultArgs = 
  array( 'status' => 'hide',
         'showtext' => '[show details]',
         'hidetext' => '[hide details]',
         'linkstyle' => 'font-size:smaller' );
 
  private static $mCount = 0;
 
  public function setup() 
  {       
    global $wgParser;
 
    // XML-style extension
    $wgParser->setHook( 'toggledisplay', array( &$this, 'toggleDisplay' ) );
  }
 
  function onBeforePageDisplay( &$outarray )
  {
    global $wgStylePath;
 
    $outarray->addScript( '<script type="text/javascript"> 
function toggleDisplay( id, hidetext, showtext )
{
  link = document.getElementById( id + "l" ).childNodes[0];
 
  with( document.getElementById( id ).style )
    {
      if( display == "none" )
        {
          display = "inline";
          link.nodeValue = hidetext;
        }
      else
        {
          display = "none";
          link.nodeValue = showtext;
        }
    }
}
</script>' );
 
    return true;
  }
 
  function toggleDisplay( $input, $args, &$parser )
  {
    self::$mCount++;
 
    $id = 'toggledisplay' . self::$mCount;
    $linkid = $id . 'l';
 
    extract( array_merge( self::$mDefaultArgs, $args ) );
    $hidetext = htmlspecialchars( $hidetext );
    $showtext = htmlspecialchars( $showtext );
 
    if( $status == 'hide' )
      {
        $display = 'none';
        $linktext = $showtext;
      }
    else
      {
        $display = 'inline';
        $linktext = $hidetext;
      }
 
    $result = <<<EOD
<a id='$linkid' href='javascript:toggleDisplay( "$id", "$hidetext", "$showtext" )' style='$linkstyle'>$linktext</a><div id='$id' style='display:$display;'>
EOD;
 
    $result .= $parser->recursiveTagParse( $input )
      . '</div>';
 
    return $result;
  }
}
?>

[edit] See also

The following extensions seem to provide similar functionality:

The following feature requests ask for similar functionality:

Other:

Personal tools