Difference between revisions of "Javascript extension"
From FollowTheScore
Line 7: | Line 7: | ||
# install the attached small piece of code as an extension (call it JavaScript) and invoke it from your LocalSettings.php | # install the attached small piece of code as an extension (call it JavaScript) and invoke it from your LocalSettings.php | ||
# make suitable entries in your [[Mediawiki:Common.css]] article; define sortablewikitable or something similar there | # make suitable entries in your [[Mediawiki:Common.css]] article; define sortablewikitable or something similar there | ||
− | # call the script using <nowiki><code><script src="sorttable | + | # call the script using <nowiki><code><script src="sorttable"/></nowiki></code> -- or create a suitable template, e.g. [[Template:Sortable Tables]] and include it from the page where you have your DPL definition using <code><nowiki>{{Sortable Tables}}</nowiki></code> |
− | # use the proper DPL syntax in combination with mode=userformat; use class= | + | # use the proper DPL syntax in combination with mode=userformat; use class=sortablewikitable and do not forget to define an "id" for each table |
---- | ---- |
Revision as of 15:23, 15 February 2007
There is a very useful piece of java script code which can be used to create sortable tables.
The following steps are needed:
- Create a directory "javascript" under the home directory of your wiki (i.e. on the same level as extensions, includes, ..).
- put the script from kryogenix there : script from kryogenix there
- install the attached small piece of code as an extension (call it JavaScript) and invoke it from your LocalSettings.php
- make suitable entries in your Mediawiki:Common.css article; define sortablewikitable or something similar there
- call the script using <code><script src="sorttable"/></code> -- or create a suitable template, e.g. Template:Sortable Tables and include it from the page where you have your DPL definition using
{{Sortable Tables}}
- use the proper DPL syntax in combination with mode=userformat; use class=sortablewikitable and do not forget to define an "id" for each table
And here is the code for the javascript extension:
<?php if ( !defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, it is not a valid entry point' ); } /* inserts a reference to a pre-defined javascript file <javascript src="file"/> file must be a plain name (path & extension are stripped) which can be found under /javascript. */ /** * Register the extension with MediaWiki */ $wgExtensionFunctions[] = "wfJavaScript"; $wgExtensionCredits['parserhook'][] = array( 'name' => 'JavaScript', 'author' => 'Gero Scholz', 'description' => 'allows to insert a reference to a predefined javascript file', 'url' => 'http://www.mediawiki.org/wiki/JavaScript' ); $wgJavaScriptMessages = array(); $wgJavaScriptMessages['en'] = array( 'no_path_' . '1' => "ERROR: No path allowed" ); function wfJavaScript() { global $wgParser, $wgMessageCache, $wgJavaScriptMessages; foreach( $wgJavaScriptMessages as $sLang => $aMsgs ) { $wgMessageCache->addMessages( $aMsgs, $sLang ); } $wgParser->setHook( "javascript", "JavaScript" ); } function JavaScript( $input, $params, &$parser ) { error_reporting(E_ALL); global $wgUser, $wgContLang, $wgScriptPath; // INVALIDATE CACHE $parser->disableCache(); $text=''; $file=''; foreach($params as $param => $value) { if ($param == 'src') { $file = ereg_replace("^[^/]*/+",'',$value); $file = ereg_replace("[.].*",'',$file); } } if ($file == '') return ''; return '<script type="text/javascript" src="'.$wgScriptPath.'/javascript/'.$file.'.js"></script>'; } ?>