2013-10-09 22:40:35 +00:00
|
|
|
/**
|
|
|
|
* JavaScript for the CategoryTree extension.
|
2006-07-26 17:12:30 +00:00
|
|
|
*
|
2018-04-16 06:50:28 +00:00
|
|
|
* © 2006 Daniel Kinzler
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
2010-06-06 15:12:22 +00:00
|
|
|
* @file
|
2007-03-13 11:39:12 +00:00
|
|
|
* @author Daniel Kinzler, brightbyte.de
|
2010-04-20 22:00:34 +00:00
|
|
|
*/
|
2007-08-25 15:23:20 +00:00
|
|
|
|
2018-12-06 18:55:37 +00:00
|
|
|
( function () {
|
2019-04-04 19:45:27 +00:00
|
|
|
var loadChildren,
|
2021-08-21 15:21:15 +00:00
|
|
|
config = require( './data.json' );
|
2017-09-05 09:20:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expands a given node (loading it's children if not loaded)
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
|
|
|
*/
|
|
|
|
function expandNode( $link ) {
|
|
|
|
// Show the children node
|
|
|
|
var $children = $link.parents( '.CategoryTreeItem' )
|
2018-02-14 17:21:18 +00:00
|
|
|
.siblings( '.CategoryTreeChildren' );
|
2017-09-05 09:20:18 +00:00
|
|
|
$children.show();
|
|
|
|
|
|
|
|
$link
|
|
|
|
.attr( 'title', mw.msg( 'categorytree-collapse' ) )
|
2019-02-06 11:00:16 +00:00
|
|
|
.attr( 'data-ct-state', 'expanded' );
|
2017-09-05 09:20:18 +00:00
|
|
|
|
|
|
|
if ( !$link.data( 'ct-loaded' ) ) {
|
|
|
|
loadChildren( $link, $children );
|
2012-03-01 23:04:44 +00:00
|
|
|
}
|
2017-09-05 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collapses a node
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
|
|
|
*/
|
|
|
|
function collapseNode( $link ) {
|
|
|
|
// Hide the children node
|
|
|
|
$link.parents( '.CategoryTreeItem' )
|
|
|
|
.siblings( '.CategoryTreeChildren' ).hide();
|
|
|
|
|
|
|
|
$link
|
|
|
|
.attr( 'title', mw.msg( 'categorytree-expand' ) )
|
2019-02-06 11:00:16 +00:00
|
|
|
.attr( 'data-ct-state', 'collapsed' );
|
2017-09-05 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:31:23 +00:00
|
|
|
/**
|
|
|
|
* Handles clicks on the expand buttons, and calls the appropriate function
|
|
|
|
*
|
2021-08-21 15:21:15 +00:00
|
|
|
* @this {Element} CategoryTreeToggle
|
2018-04-17 00:31:23 +00:00
|
|
|
*/
|
|
|
|
function handleNode() {
|
|
|
|
var $link = $( this );
|
2019-02-06 11:00:16 +00:00
|
|
|
if ( $link.attr( 'data-ct-state' ) === 'collapsed' ) {
|
2018-04-17 00:31:23 +00:00
|
|
|
expandNode( $link );
|
|
|
|
} else {
|
|
|
|
collapseNode( $link );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach click handler to buttons
|
|
|
|
*
|
|
|
|
* @param {jQuery} $content
|
|
|
|
*/
|
|
|
|
function attachHandler( $content ) {
|
|
|
|
$content.find( '.CategoryTreeToggle' )
|
2020-10-29 06:41:31 +00:00
|
|
|
.on( 'click', handleNode )
|
2018-04-17 00:31:23 +00:00
|
|
|
.attr( 'title', function () {
|
|
|
|
return mw.msg(
|
2019-02-06 11:00:16 +00:00
|
|
|
$( this ).attr( 'data-ct-state' ) === 'collapsed' ?
|
2018-04-17 00:31:23 +00:00
|
|
|
'categorytree-expand' :
|
|
|
|
'categorytree-collapse'
|
|
|
|
);
|
|
|
|
} )
|
|
|
|
.addClass( 'CategoryTreeToggleHandlerAttached' );
|
|
|
|
}
|
|
|
|
|
2017-09-05 09:20:18 +00:00
|
|
|
/**
|
|
|
|
* Loads children for a node via an HTTP call
|
|
|
|
*
|
|
|
|
* @param {jQuery} $link
|
|
|
|
* @param {jQuery} $children
|
|
|
|
*/
|
2018-04-17 00:31:23 +00:00
|
|
|
loadChildren = function ( $link, $children ) {
|
2017-09-05 09:20:18 +00:00
|
|
|
var $linkParentCTTag, ctTitle, ctMode, ctOptions;
|
2012-03-01 23:04:44 +00:00
|
|
|
|
2014-03-29 10:04:21 +00:00
|
|
|
/**
|
2017-09-05 09:20:18 +00:00
|
|
|
* Error callback
|
2014-03-29 10:04:21 +00:00
|
|
|
*/
|
2017-09-05 09:20:18 +00:00
|
|
|
function error() {
|
|
|
|
var $retryLink;
|
|
|
|
|
|
|
|
$retryLink = $( '<a>' )
|
|
|
|
.text( mw.msg( 'categorytree-retry' ) )
|
2017-08-26 12:36:29 +00:00
|
|
|
.attr( {
|
|
|
|
role: 'button',
|
|
|
|
tabindex: 0
|
|
|
|
} )
|
|
|
|
.on( 'click keypress', function ( e ) {
|
|
|
|
if (
|
|
|
|
e.type === 'click' ||
|
|
|
|
e.type === 'keypress' && e.which === 13
|
|
|
|
) {
|
|
|
|
loadChildren( $link, $children );
|
|
|
|
}
|
2017-09-05 09:20:18 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
$children
|
|
|
|
.text( mw.msg( 'categorytree-error' ) + ' ' )
|
|
|
|
.append( $retryLink );
|
2014-03-29 10:04:21 +00:00
|
|
|
}
|
2012-03-01 23:04:44 +00:00
|
|
|
|
2017-09-05 09:20:18 +00:00
|
|
|
$link.data( 'ct-loaded', true );
|
2011-09-30 21:08:28 +00:00
|
|
|
|
2016-02-01 19:09:56 +00:00
|
|
|
$children.empty().append(
|
2020-10-29 06:41:31 +00:00
|
|
|
$( '<i>' )
|
|
|
|
.addClass( 'CategoryTreeNotice' )
|
2017-09-05 09:20:18 +00:00
|
|
|
.text( mw.msg( 'categorytree-loading' ) )
|
|
|
|
);
|
2012-03-01 23:04:44 +00:00
|
|
|
|
2017-09-05 09:20:18 +00:00
|
|
|
$linkParentCTTag = $link.parents( '.CategoryTreeTag' );
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2017-09-05 09:20:18 +00:00
|
|
|
// Element may not have a .CategoryTreeTag parent, fallback to defauls
|
|
|
|
// Probably a CategoryPage (@todo: based on what?)
|
2018-04-29 03:14:41 +00:00
|
|
|
ctTitle = $link.attr( 'data-ct-title' );
|
2017-09-05 09:20:18 +00:00
|
|
|
ctMode = $linkParentCTTag.data( 'ct-mode' );
|
|
|
|
ctMode = typeof ctMode === 'number' ? ctMode : undefined;
|
2021-08-21 15:21:15 +00:00
|
|
|
ctOptions = $linkParentCTTag.attr( 'data-ct-options' ) || config.defaultCtOptions;
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2017-09-05 09:20:18 +00:00
|
|
|
// Mode and options have defaults or fallbacks, title does not.
|
|
|
|
// Don't make a request if there is no title.
|
2018-04-29 03:14:41 +00:00
|
|
|
if ( !ctTitle ) {
|
2017-09-05 09:20:18 +00:00
|
|
|
error();
|
|
|
|
return;
|
|
|
|
}
|
2014-02-21 22:33:04 +00:00
|
|
|
|
2017-09-05 09:20:18 +00:00
|
|
|
new mw.Api().get( {
|
|
|
|
action: 'categorytree',
|
|
|
|
category: ctTitle,
|
|
|
|
options: ctOptions,
|
|
|
|
uselang: mw.config.get( 'wgUserLanguage' ),
|
|
|
|
formatversion: 2
|
|
|
|
} ).done( function ( data ) {
|
2018-05-24 08:33:36 +00:00
|
|
|
var $data;
|
|
|
|
|
2017-09-05 09:20:18 +00:00
|
|
|
data = data.categorytree.html;
|
|
|
|
|
|
|
|
if ( data === '' ) {
|
|
|
|
switch ( ctMode ) {
|
|
|
|
// CategoryTreeMode::CATEGORIES = 0
|
|
|
|
case 0:
|
|
|
|
data = mw.msg( 'categorytree-no-subcategories' );
|
|
|
|
break;
|
|
|
|
// CategoryTreeMode::PAGES = 10
|
|
|
|
case 10:
|
|
|
|
data = mw.msg( 'categorytree-no-pages' );
|
|
|
|
break;
|
|
|
|
// CategoryTreeMode::PARENTS = 100
|
|
|
|
case 100:
|
|
|
|
data = mw.msg( 'categorytree-no-parent-categories' );
|
|
|
|
break;
|
|
|
|
// CategoryTreeMode::ALL = 20
|
|
|
|
default:
|
|
|
|
data = mw.msg( 'categorytree-nothing-found' );
|
2014-03-29 10:04:21 +00:00
|
|
|
}
|
2012-03-01 23:43:41 +00:00
|
|
|
|
2020-10-29 06:41:31 +00:00
|
|
|
$data = $( '<i>' ).addClass( 'CategoryTreeNotice' ).text( data );
|
2018-05-24 08:33:36 +00:00
|
|
|
} else {
|
|
|
|
$data = $( $.parseHTML( data ) );
|
|
|
|
attachHandler( $data );
|
2017-09-05 09:20:18 +00:00
|
|
|
}
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2018-05-24 08:33:36 +00:00
|
|
|
$children.empty().append( $data );
|
2017-09-05 09:20:18 +00:00
|
|
|
} )
|
2018-02-14 17:21:18 +00:00
|
|
|
.fail( error );
|
2018-04-17 00:31:23 +00:00
|
|
|
};
|
2017-09-05 09:20:18 +00:00
|
|
|
|
|
|
|
// Register click events
|
|
|
|
mw.hook( 'wikipage.content' ).add( attachHandler );
|
2010-04-20 22:00:34 +00:00
|
|
|
|
2019-01-04 20:38:41 +00:00
|
|
|
// Attach click handler for categories.
|
|
|
|
// This is needed when wgCategoryTreeHijackPageCategories is enabled.
|
|
|
|
mw.hook( 'wikipage.categories' ).add( attachHandler );
|
|
|
|
|
2017-09-25 19:07:56 +00:00
|
|
|
$( function () {
|
|
|
|
// Attach click handler for sidebar
|
2020-10-29 06:41:31 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2017-09-25 19:07:56 +00:00
|
|
|
attachHandler( $( '#p-categorytree-portlet' ) );
|
|
|
|
} );
|
|
|
|
|
2018-12-06 18:55:37 +00:00
|
|
|
}() );
|