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
|
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 );
|
2012-03-01 23:04:44 +00:00
|
|
|
if ( $link.data( 'ct-state' ) === 'collapsed' ) {
|
2011-10-02 19:25:17 +00:00
|
|
|
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' ) )
|
2012-03-01 23:04:44 +00:00
|
|
|
.data( 'ct-state', 'expanded' );
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2012-03-01 23:04:44 +00:00
|
|
|
if ( !$link.data( 'ct-loaded' ) ) {
|
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' ) )
|
2012-03-01 23:04:44 +00:00
|
|
|
.data( 'ct-state', '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 ) {
|
2012-03-01 23:04:44 +00:00
|
|
|
var $linkParentCTTag, ctTitle, ctMode, ctOptions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error callback
|
|
|
|
*/
|
|
|
|
function error() {
|
|
|
|
var $retryLink;
|
|
|
|
|
|
|
|
$retryLink = $( '<a>' )
|
|
|
|
.text( mw.msg( 'categorytree-retry' ) )
|
|
|
|
.attr( 'href', '#' )
|
2012-03-02 00:31:33 +00:00
|
|
|
.click( function ( e ) {
|
|
|
|
e.preventDefault();
|
2012-03-01 23:04:44 +00:00
|
|
|
categoryTree.loadChildren( $link, $children );
|
|
|
|
} );
|
|
|
|
|
|
|
|
$children
|
2012-03-02 00:31:33 +00:00
|
|
|
.text( mw.msg( 'categorytree-error' ) + ' ' )
|
2012-03-01 23:04:44 +00:00
|
|
|
.append( $retryLink );
|
|
|
|
}
|
|
|
|
|
|
|
|
$link.data( 'ct-loaded', true );
|
|
|
|
|
2011-09-30 21:08:28 +00:00
|
|
|
$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
|
|
|
);
|
|
|
|
|
2012-03-01 23:04:44 +00:00
|
|
|
$linkParentCTTag = $link.parents( '.CategoryTreeTag' );
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2012-03-01 23:04:44 +00:00
|
|
|
// Element may not have a .CategoryTreeTag parent, fallback to defauls
|
|
|
|
// Probably a CategoryPage (@todo: based on what?)
|
|
|
|
ctTitle = $link.data( 'ct-title' );
|
2012-03-01 23:43:41 +00:00
|
|
|
ctMode = $linkParentCTTag.data( 'ct-mode' );
|
|
|
|
ctMode = typeof ctMode === 'number' ? ctMode : undefined;
|
2012-03-01 23:04:44 +00:00
|
|
|
ctOptions = $linkParentCTTag.data( 'ct-options' ) || mw.config.get( 'wgCategoryTreePageCategoryOptions' );
|
|
|
|
|
|
|
|
// 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;
|
2010-04-20 22:00:34 +00:00
|
|
|
}
|
|
|
|
|
2011-09-30 21:08:28 +00:00
|
|
|
$.get(
|
|
|
|
mw.util.wikiScript(), {
|
|
|
|
action: 'ajax',
|
|
|
|
rs: 'efCategoryTreeAjaxWrapper',
|
2012-03-01 23:04:44 +00:00
|
|
|
rsargs: [ctTitle, ctOptions, 'json'] // becomes &rsargs[]=arg1&rsargs[]=arg2...
|
2011-09-30 21:08:28 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.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 === '' ) {
|
2012-03-01 23:04:44 +00:00
|
|
|
switch ( ctMode ) {
|
2012-03-01 23:43:41 +00:00
|
|
|
// CT_MODE_CATEGORIES = 0
|
2011-09-30 21:08:28 +00:00
|
|
|
case 0:
|
|
|
|
data = mw.msg( 'categorytree-no-subcategories' );
|
|
|
|
break;
|
2012-03-01 23:43:41 +00:00
|
|
|
// CT_MODE_PAGES = 10
|
2011-09-30 21:08:28 +00:00
|
|
|
case 10:
|
|
|
|
data = mw.msg( 'categorytree-no-pages' );
|
|
|
|
break;
|
2012-03-01 23:43:41 +00:00
|
|
|
// CT_MODE_PARENTS = 100
|
2011-09-30 21:08:28 +00:00
|
|
|
case 100:
|
|
|
|
data = mw.msg( 'categorytree-no-parent-categories' );
|
|
|
|
break;
|
2012-03-01 23:43:41 +00:00
|
|
|
// CT_MODE_ALL = 20
|
2011-09-30 21:08:28 +00:00
|
|
|
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 );
|
2012-03-01 23:43:41 +00:00
|
|
|
|
2011-10-02 19:25:17 +00:00
|
|
|
categoryTree.showToggles();
|
2011-09-30 21:08:28 +00:00
|
|
|
} )
|
2012-03-01 23:04:44 +00:00
|
|
|
.error( error );
|
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
|
|
|
|
2012-01-24 11:34:12 +00:00
|
|
|
} )( jQuery, mediaWiki );
|