User:Sophivorus/DebateTree.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * DebateTree is a MediaWiki implementation of the dialectic algorithm
 *
 * By Sophivorus
 *
 * Documentation at https://commons.wikimedia.org/wiki/Help:DebateTree
 *
 * DebateTree is available under the GNU General Public License (http://www.gnu.org/licenses/gpl.html)
 */
var DebateTree = {

	/**
	 * Get all the arguments in the page
	 */
	getArguments: function () {
		return $( '.debatetree' ).closest( 'li' ).add( $( '.debatetree' ).closest( 'li' ).find( 'li' ) );
	},

	/**
	 * Get the objections to the given argument
	 */
	getObjections: function ( argument ) {
		return $( argument ).children( 'ul' ).children( 'li' );
	},

	/**
	 * Return true if the given argument is sustained
	 */
	isSustained: function ( index, argument ) {
		return DebateTree.getObjections( argument ).filter( DebateTree.isSustained ).length ? false : true;
	},

	/**
	 * Add the status to the arguments
	 */
	addStatus: function ( index, argument ) {
		var status = DebateTree.isSustained( index, argument ) ? 'sustained' : 'refuted',
			div = $( '<div>' ).addClass( 'debatetree-argument debatetree-' + status );
		$( argument ).contents().filter( function () {
			return this.tagName !== 'UL';
		} ).wrapAll( div );
	},

	/**
	 * Prepend the arrows to the arguments
	 */
	addArrow: function ( index, argument ) {
		if ( DebateTree.getObjections( argument ).length ) {
			var arrow = $( '<span>' ).text( '►' ).addClass( 'debatetree-arrow' ).click( DebateTree.toggleObjections ).click();
			$( argument ).css( 'list-style', 'none' ).children( '.debatetree-argument' ).prepend( arrow );
		}
	},

	/**
	 * Toggle the objections of the clicked argument
	 */
	toggleObjections: function ( event ) {
		var arrow = $( this ),
			argument = arrow.closest( 'li' );
			objections = DebateTree.getObjections( argument );
		objections.toggle();
		if ( objections.is( ':visible' ) ) {
			arrow.text( '▼' );
		} else {
			arrow.text( '►' );
		}
	},

	/**
	 * Initialization script
	 */
	init: function () {
		DebateTree.getArguments().each( DebateTree.addStatus );
		//args.each( DebateTree.addArrow );
		//$( '.debatetree-arrow', args ).click();
	}
};

$( DebateTree.init );