2013-10-09 22:40:35 +00:00
|
|
|
/**
|
|
|
|
* JavaScript for the CategoryTree extension.
|
2006-07-26 17:12:30 +00:00
|
|
|
*
|
2010-06-06 15:12:22 +00:00
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
2007-03-13 11:39:12 +00:00
|
|
|
* @author Daniel Kinzler, brightbyte.de
|
2006-07-26 17:12:30 +00:00
|
|
|
* @copyright © 2006 Daniel Kinzler
|
|
|
|
* @licence GNU General Public Licence 2.0 or later
|
2010-04-20 22:00:34 +00:00
|
|
|
*/
|
2007-08-25 15:23:20 +00:00
|
|
|
|
2013-10-09 22:40:35 +00:00
|
|
|
( function ( $, mw ) {
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
mw.hook( 'wikipage.content' ).add( function ( $content ) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets display inline to tree toggle
|
|
|
|
*/
|
|
|
|
function showToggles() {
|
|
|
|
$content.find( 'span.CategoryTreeToggle' ).css( 'display', 'inline' );
|
2011-09-30 21:08:28 +00:00
|
|
|
}
|
2014-03-29 10:04:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles clicks on the expand buttons, and calls the appropriate function
|
|
|
|
*
|
|
|
|
* @context {Element} CategoryTreeToggle
|
|
|
|
* @param e {jQuery.Event}
|
|
|
|
*/
|
2015-03-15 08:41:56 +00:00
|
|
|
function handleNode( /*e*/ ) {
|
2014-03-29 10:04:21 +00:00
|
|
|
var $link = $( this );
|
|
|
|
if ( $link.data( 'ct-state' ) === 'collapsed' ) {
|
|
|
|
expandNode( $link );
|
|
|
|
} else {
|
|
|
|
collapseNode( $link );
|
|
|
|
}
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2012-03-01 23:04:44 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-29 10:04:21 +00:00
|
|
|
* Expands a given node (loading it's children if not loaded)
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
2012-03-01 23:04:44 +00:00
|
|
|
*/
|
2014-03-29 10:04:21 +00:00
|
|
|
function expandNode( $link ) {
|
|
|
|
// Show the children node
|
|
|
|
var $children = $link.parents( '.CategoryTreeItem' )
|
|
|
|
.siblings( '.CategoryTreeChildren' );
|
|
|
|
$children.show();
|
|
|
|
|
|
|
|
$link
|
|
|
|
.text( mw.msg( 'categorytree-collapse-bullet' ) )
|
|
|
|
.attr( 'title', mw.msg( 'categorytree-collapse' ) )
|
|
|
|
.data( 'ct-state', 'expanded' );
|
|
|
|
|
|
|
|
if ( !$link.data( 'ct-loaded' ) ) {
|
|
|
|
loadChildren( $link, $children );
|
|
|
|
}
|
2012-03-01 23:04:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
/**
|
|
|
|
* Collapses a node
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
|
|
|
*/
|
|
|
|
function collapseNode( $link ) {
|
|
|
|
// Hide the children node
|
|
|
|
$link.parents( '.CategoryTreeItem' )
|
|
|
|
.siblings( '.CategoryTreeChildren' ).hide();
|
|
|
|
|
|
|
|
$link
|
|
|
|
.text( mw.msg( 'categorytree-expand-bullet' ) )
|
|
|
|
.attr( 'title', mw.msg( 'categorytree-expand' ) )
|
|
|
|
.data( 'ct-state', 'collapsed' );
|
|
|
|
}
|
2012-03-01 23:04:44 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
/**
|
|
|
|
* Loads children for a node via an HTTP call
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
|
|
|
* @param {jQuery} $children
|
|
|
|
*/
|
|
|
|
function loadChildren( $link, $children ) {
|
|
|
|
var $linkParentCTTag, ctTitle, ctMode, ctOptions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error callback
|
|
|
|
*/
|
|
|
|
function error() {
|
|
|
|
var $retryLink;
|
|
|
|
|
|
|
|
$retryLink = $( '<a>' )
|
|
|
|
.text( mw.msg( 'categorytree-retry' ) )
|
|
|
|
.attr( 'href', '#' )
|
|
|
|
.click( function ( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
loadChildren( $link, $children );
|
|
|
|
} );
|
|
|
|
|
|
|
|
$children
|
|
|
|
.text( mw.msg( 'categorytree-error' ) + ' ' )
|
|
|
|
.append( $retryLink );
|
|
|
|
}
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
$link.data( 'ct-loaded', true );
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
$children.append(
|
|
|
|
$( '<i class="CategoryTreeNotice"></i>' )
|
|
|
|
.text( mw.msg( 'categorytree-loading' ) )
|
|
|
|
);
|
2012-03-01 23:04:44 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
$linkParentCTTag = $link.parents( '.CategoryTreeTag' );
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
// Element may not have a .CategoryTreeTag parent, fallback to defauls
|
|
|
|
// Probably a CategoryPage (@todo: based on what?)
|
|
|
|
ctTitle = $link.data( 'ct-title' );
|
|
|
|
ctMode = $linkParentCTTag.data( 'ct-mode' );
|
|
|
|
ctMode = typeof ctMode === 'number' ? ctMode : undefined;
|
2015-05-20 17:35:20 +00:00
|
|
|
ctOptions = JSON.stringify( $linkParentCTTag.data( 'ct-options' ) );
|
|
|
|
if ( !ctOptions ) {
|
|
|
|
ctOptions = mw.config.get( 'wgCategoryTreePageCategoryOptions' );
|
|
|
|
}
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
// Mode and options have defaults or fallbacks, title does not.
|
|
|
|
// Don't make a request if there is no title.
|
|
|
|
if ( typeof ctTitle !== 'string' ) {
|
|
|
|
error();
|
|
|
|
return;
|
2014-02-21 22:33:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 17:35:20 +00:00
|
|
|
new mw.Api().get( {
|
|
|
|
action: 'categorytree',
|
|
|
|
category: ctTitle,
|
|
|
|
options: ctOptions,
|
|
|
|
uselang: mw.config.get( 'wgUserLanguage' ),
|
|
|
|
formatversion: 2
|
|
|
|
} ).done( function ( data ) {
|
2016-01-31 20:44:58 +00:00
|
|
|
data = data.categorytree.html;
|
2014-03-29 10:04:21 +00:00
|
|
|
|
|
|
|
if ( data === '' ) {
|
|
|
|
switch ( ctMode ) {
|
2016-02-05 06:45:46 +00:00
|
|
|
// CategoryTreeMode::CATEGORIES = 0
|
2014-03-29 10:04:21 +00:00
|
|
|
case 0:
|
|
|
|
data = mw.msg( 'categorytree-no-subcategories' );
|
|
|
|
break;
|
2016-02-05 06:45:46 +00:00
|
|
|
// CategoryTreeMode::PAGES = 10
|
2014-03-29 10:04:21 +00:00
|
|
|
case 10:
|
|
|
|
data = mw.msg( 'categorytree-no-pages' );
|
|
|
|
break;
|
2016-02-05 06:45:46 +00:00
|
|
|
// CategoryTreeMode::PARENTS = 100
|
2014-03-29 10:04:21 +00:00
|
|
|
case 100:
|
|
|
|
data = mw.msg( 'categorytree-no-parent-categories' );
|
|
|
|
break;
|
2016-02-05 06:45:46 +00:00
|
|
|
// CategoryTreeMode::ALL = 20
|
2014-03-29 10:04:21 +00:00
|
|
|
default:
|
|
|
|
data = mw.msg( 'categorytree-nothing-found' );
|
|
|
|
}
|
|
|
|
|
|
|
|
data = $( '<i class="CategoryTreeNotice"></i>' ).text( data );
|
|
|
|
}
|
2012-03-01 23:43:41 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
$children
|
|
|
|
.html( data )
|
|
|
|
.find( '.CategoryTreeToggle' )
|
|
|
|
.click( handleNode );
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
showToggles();
|
|
|
|
} )
|
|
|
|
.fail( error );
|
|
|
|
}
|
|
|
|
|
2014-07-30 16:34:31 +00:00
|
|
|
// Register click events and show toggle buttons
|
2014-03-29 10:04:21 +00:00
|
|
|
$content.find( '.CategoryTreeToggle' ).click( handleNode );
|
2014-02-21 22:33:04 +00:00
|
|
|
showToggles();
|
|
|
|
} );
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2013-10-09 22:40:35 +00:00
|
|
|
}( jQuery, mediaWiki ) );
|