2006-07-26 17:12:30 +00:00
|
|
|
/*
|
2008-02-04 09:22:12 +00:00
|
|
|
* JavaScript functions for the CategoryTree extension, an AJAX based gadget
|
2006-07-26 17:12:30 +00:00
|
|
|
* to display the category structure of a wiki
|
|
|
|
*
|
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
|
2008-02-27 10:37:52 +00:00
|
|
|
*
|
2010-04-20 22:00:34 +00:00
|
|
|
* NOTE: if you change this, increment $wgCategoryTreeVersion
|
2008-02-27 10:37:52 +00:00
|
|
|
* in CategoryTree.php to avoid users getting stale copies from cache.
|
2010-04-20 22:00:34 +00:00
|
|
|
*/
|
2007-08-25 15:23:20 +00:00
|
|
|
|
2011-10-02 19:25:17 +00:00
|
|
|
(function( $, mw ) {
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2011-10-02 19:25:17 +00:00
|
|
|
var categoryTree = {
|
2011-09-30 21:08:28 +00:00
|
|
|
/**
|
|
|
|
* Sets display inline to tree toggle
|
|
|
|
*/
|
2011-10-02 19:25:17 +00:00
|
|
|
showToggles: function() {
|
2011-09-30 21:08:28 +00:00
|
|
|
$( 'span.CategoryTreeToggle' ).css( 'display', 'inline' );
|
2011-10-02 19:25:17 +00:00
|
|
|
},
|
|
|
|
|
2011-09-30 21:08:28 +00:00
|
|
|
/**
|
|
|
|
* Handles clicks on the expand buttons, and calls the appropriate function
|
2011-10-02 19:25:17 +00:00
|
|
|
*
|
|
|
|
* @context {Element} CategoryTreeToggle
|
|
|
|
* @param e {jQuery.Event}
|
2011-09-30 21:08:28 +00:00
|
|
|
*/
|
2011-10-02 19:25:17 +00:00
|
|
|
handleNode: function( e ) {
|
2011-09-30 22:13:51 +00:00
|
|
|
var $link = $( this );
|
2011-10-02 19:25:17 +00:00
|
|
|
if ( $link.data( 'ctState' ) === 'collapsed' ) {
|
|
|
|
categoryTree.expandNode( $link );
|
2011-09-30 21:08:28 +00:00
|
|
|
} else {
|
2011-10-02 19:25:17 +00:00
|
|
|
categoryTree.collapseNode( $link );
|
2011-09-30 21:08:28 +00:00
|
|
|
}
|
2011-10-02 19:25:17 +00:00
|
|
|
},
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2011-09-30 21:08:28 +00:00
|
|
|
/**
|
|
|
|
* Expands a given node (loading it's children if not loaded)
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
|
|
|
*/
|
2011-10-02 19:25:17 +00:00
|
|
|
expandNode: function( $link ) {
|
2011-09-30 21:08:28 +00:00
|
|
|
// Show the children node
|
2011-10-02 19:25:17 +00:00
|
|
|
var $children = $link.parents( '.CategoryTreeItem' )
|
2011-09-30 21:08:28 +00:00
|
|
|
.siblings( '.CategoryTreeChildren' );
|
|
|
|
$children.show();
|
|
|
|
|
|
|
|
$link
|
|
|
|
.html( mw.msg( 'categorytree-collapse-bullet' ) )
|
|
|
|
.attr( 'title', mw.msg( 'categorytree-collapse' ) )
|
|
|
|
.data( 'ctState', 'expanded' );
|
|
|
|
|
|
|
|
if ( !$link.data( 'ctLoaded' ) ) {
|
2011-10-02 19:25:17 +00:00
|
|
|
categoryTree.loadChildren( $link, $children );
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2011-10-02 19:25:17 +00:00
|
|
|
},
|
2011-09-30 21:08:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Collapses a node
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
|
|
|
*/
|
2011-10-02 19:25:17 +00:00
|
|
|
collapseNode: function( $link ) {
|
2011-09-30 21:08:28 +00:00
|
|
|
// Hide the children node
|
|
|
|
$link.parents( '.CategoryTreeItem' )
|
|
|
|
.siblings( '.CategoryTreeChildren' ).hide();
|
|
|
|
|
|
|
|
$link
|
|
|
|
.html( mw.msg( 'categorytree-expand-bullet' ) )
|
|
|
|
.attr( 'title', mw.msg( 'categorytree-expand' ) )
|
|
|
|
.data( 'ctState', 'collapsed' );
|
2011-10-02 19:25:17 +00:00
|
|
|
},
|
2011-09-30 21:08:28 +00:00
|
|
|
|
|
|
|
/**
|
2011-10-02 19:25:17 +00:00
|
|
|
* Loads children for a node via an HTTP call
|
2011-09-30 21:08:28 +00:00
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
|
|
|
* @param {jQuery} $children
|
|
|
|
*/
|
2011-10-02 19:25:17 +00:00
|
|
|
loadChildren: function( $link, $children ) {
|
2011-09-30 21:08:28 +00:00
|
|
|
$link.data( 'ctLoaded', true );
|
|
|
|
$children.html(
|
2011-10-02 19:25:17 +00:00
|
|
|
$( '<i class="CategoryTreeNotice"></i>' )
|
|
|
|
.text( mw.msg( 'categorytree-loading' ) )
|
2011-09-30 21:08:28 +00:00
|
|
|
);
|
|
|
|
|
2011-09-30 22:13:51 +00:00
|
|
|
var $parentTag = $link.parents( '.CategoryTreeTag' );
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2011-10-02 19:25:17 +00:00
|
|
|
if ( $parentTag.length === 0 ) {
|
2011-09-30 21:08:28 +00:00
|
|
|
// Probably a CategoryPage
|
|
|
|
$parentTag = $( '<div />' )
|
|
|
|
.hide()
|
2011-10-02 19:25:17 +00:00
|
|
|
.data( 'ctOptions', mw.config.get( 'wgCategoryTreePageCategoryOptions' ) );
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
|
|
|
|
2011-09-30 21:08:28 +00:00
|
|
|
$.get(
|
|
|
|
mw.util.wikiScript(), {
|
|
|
|
action: 'ajax',
|
|
|
|
rs: 'efCategoryTreeAjaxWrapper',
|
|
|
|
rsargs: [$link.data( 'ctTitle' ), $parentTag.data( 'ctOptions' ), 'json'] // becomes &rsargs[]=arg1&rsargs[]=arg2...
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.success( function ( data ) {
|
|
|
|
data = data.replace(/^\s+|\s+$/, '');
|
|
|
|
data = data.replace(/##LOAD##/g, mw.msg( 'categorytree-expand' ) );
|
|
|
|
|
2011-10-02 19:25:17 +00:00
|
|
|
if ( data === '' ) {
|
2011-09-30 21:08:28 +00:00
|
|
|
switch ( $parentTag.data( 'ctMode' ) ) {
|
|
|
|
case 0:
|
|
|
|
data = mw.msg( 'categorytree-no-subcategories' );
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
data = mw.msg( 'categorytree-no-pages' );
|
|
|
|
break;
|
|
|
|
case 100:
|
|
|
|
data = mw.msg( 'categorytree-no-parent-categories' );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
data = mw.msg( 'categorytree-nothing-found' );
|
|
|
|
}
|
|
|
|
|
2011-10-02 19:25:17 +00:00
|
|
|
data = $( '<i class="CategoryTreeNotice"></i>' ).text( data );
|
2011-09-30 21:08:28 +00:00
|
|
|
}
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2011-09-30 21:08:28 +00:00
|
|
|
$children
|
|
|
|
.html( data )
|
|
|
|
.find( '.CategoryTreeToggle' )
|
2011-10-02 19:25:17 +00:00
|
|
|
.click( categoryTree.handleNode );
|
|
|
|
categoryTree.showToggles();
|
2011-09-30 21:08:28 +00:00
|
|
|
} )
|
|
|
|
.error( function() {
|
2011-09-30 22:13:51 +00:00
|
|
|
var $retryLink = $( '<a />' )
|
2011-09-30 21:08:28 +00:00
|
|
|
.text( mw.msg( 'categorytree-retry' ) )
|
|
|
|
.attr( 'href', '#' )
|
2011-10-02 19:25:17 +00:00
|
|
|
.click( function() { categoryTree.loadChildren( $link, $children ); } );
|
2011-09-30 21:08:28 +00:00
|
|
|
$children
|
|
|
|
.text( mw.msg( 'categorytree-error' ) )
|
|
|
|
.append( $retryLink );
|
|
|
|
} );
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
2011-10-02 19:25:17 +00:00
|
|
|
};
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2011-10-02 19:25:17 +00:00
|
|
|
// Register click events and show toggle buttons
|
|
|
|
$( function( $ ) {
|
|
|
|
$( '.CategoryTreeToggle' ).click( categoryTree.handleNode );
|
|
|
|
categoryTree.showToggles();
|
|
|
|
} );
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2011-09-30 21:08:28 +00:00
|
|
|
} )( jQuery, mediaWiki );
|