Merge "Use ES6 features"

This commit is contained in:
jenkins-bot 2024-10-10 15:53:42 +00:00 committed by Gerrit Code Review
commit a6eb9c0e57
3 changed files with 22 additions and 26 deletions

View file

@ -1,10 +1,13 @@
{ {
"root": true, "root": true,
"extends": [ "extends": [
"wikimedia", "wikimedia/client-es6",
"wikimedia/mediawiki", "wikimedia/mediawiki",
"wikimedia/jquery" "wikimedia/jquery"
], ],
"rules": {
"no-var": "error"
},
"env": { "env": {
"browser": true, "browser": true,
"commonjs": true "commonjs": true

View file

@ -4,9 +4,9 @@
* @package CategoryTree * @package CategoryTree
*/ */
/* eslint-env node */ /* eslint-env node, es6 */
module.exports = function ( grunt ) { module.exports = function ( grunt ) {
var conf = grunt.file.readJSON( 'extension.json' ); const conf = grunt.file.readJSON( 'extension.json' );
grunt.loadNpmTasks( 'grunt-eslint' ); grunt.loadNpmTasks( 'grunt-eslint' );
grunt.loadNpmTasks( 'grunt-banana-checker' ); grunt.loadNpmTasks( 'grunt-banana-checker' );

View file

@ -23,8 +23,7 @@
*/ */
( function () { ( function () {
var loadChildren, const config = require( './data.json' );
config = require( './data.json' );
/** /**
* Expands a given node (loading it's children if not loaded) * Expands a given node (loading it's children if not loaded)
@ -33,7 +32,7 @@
*/ */
function expandNode( $link ) { function expandNode( $link ) {
// Show the children node // Show the children node
var $children = $link.parents( '.CategoryTreeItem' ) const $children = $link.parents( '.CategoryTreeItem' )
.siblings( '.CategoryTreeChildren' ) .siblings( '.CategoryTreeChildren' )
.css( 'display', '' ); .css( 'display', '' );
@ -67,13 +66,12 @@
/** /**
* Handles clicks on the expand buttons, and calls the appropriate function * Handles clicks on the expand buttons, and calls the appropriate function
* *
* @this {Element} CategoryTreeToggle
* @param {jQuery.Event} e * @param {jQuery.Event} e
*/ */
function handleNode( e ) { function handleNode( e ) {
e.preventDefault(); e.preventDefault();
var $link = $( this ); const $link = $( this );
if ( $link.attr( 'aria-expanded' ) === 'false' ) { if ( $link.attr( 'aria-expanded' ) === 'false' ) {
expandNode( $link ); expandNode( $link );
} else { } else {
@ -108,22 +106,18 @@
* @param {jQuery} $link * @param {jQuery} $link
* @param {jQuery} $children * @param {jQuery} $children
*/ */
loadChildren = function ( $link, $children ) { function loadChildren( $link, $children ) {
var $linkParentCTTag, ctTitle, ctMode, ctOptions;
/** /**
* Error callback * Error callback
*/ */
function error() { function error() {
var $retryLink; const $retryLink = $( '<a>' )
$retryLink = $( '<a>' )
.text( mw.msg( 'categorytree-retry' ) ) .text( mw.msg( 'categorytree-retry' ) )
.attr( { .attr( {
role: 'button', role: 'button',
tabindex: 0 tabindex: 0
} ) } )
.on( 'click keypress', function ( e ) { .on( 'click keypress', ( e ) => {
if ( if (
e.type === 'click' || e.type === 'click' ||
e.type === 'keypress' && e.which === 13 e.type === 'keypress' && e.which === 13
@ -145,14 +139,14 @@
.text( mw.msg( 'categorytree-loading' ) ) .text( mw.msg( 'categorytree-loading' ) )
); );
$linkParentCTTag = $link.parents( '.CategoryTreeTag' ); const $linkParentCTTag = $link.parents( '.CategoryTreeTag' );
// Element may not have a .CategoryTreeTag parent, fallback to defauls // Element may not have a .CategoryTreeTag parent, fallback to defauls
// Probably a CategoryPage (@todo: based on what?) // Probably a CategoryPage (@todo: based on what?)
ctTitle = $link.attr( 'data-ct-title' ); const ctTitle = $link.attr( 'data-ct-title' );
ctMode = $linkParentCTTag.data( 'ct-mode' ); const ctMode = $linkParentCTTag.data( 'ct-mode' );
ctMode = typeof ctMode === 'number' ? ctMode : undefined; const mode = typeof ctMode === 'number' ? ctMode : undefined;
ctOptions = $linkParentCTTag.attr( 'data-ct-options' ) || config.defaultCtOptions; const ctOptions = $linkParentCTTag.attr( 'data-ct-options' ) || config.defaultCtOptions;
// Mode and options have defaults or fallbacks, title does not. // Mode and options have defaults or fallbacks, title does not.
// Don't make a request if there is no title. // Don't make a request if there is no title.
@ -167,11 +161,10 @@
options: ctOptions, options: ctOptions,
uselang: mw.config.get( 'wgUserLanguage' ), uselang: mw.config.get( 'wgUserLanguage' ),
formatversion: 2 formatversion: 2
} ).done( function ( data ) { } ).done( ( data ) => {
var $data;
data = data.categorytree.html; data = data.categorytree.html;
let $data;
if ( data === '' ) { if ( data === '' ) {
$data = $( '<i>' ).addClass( 'CategoryTreeNotice' ) $data = $( '<i>' ).addClass( 'CategoryTreeNotice' )
// eslint-disable-next-line mediawiki/msg-doc // eslint-disable-next-line mediawiki/msg-doc
@ -179,7 +172,7 @@
0: 'categorytree-no-subcategories', 0: 'categorytree-no-subcategories',
10: 'categorytree-no-pages', 10: 'categorytree-no-pages',
100: 'categorytree-no-parent-categories' 100: 'categorytree-no-parent-categories'
}[ ctMode ] || 'categorytree-nothing-found' ) ); }[ mode ] || 'categorytree-nothing-found' ) );
} else { } else {
$data = $( $.parseHTML( data ) ); $data = $( $.parseHTML( data ) );
attachHandler( $data ); attachHandler( $data );
@ -187,7 +180,7 @@
$children.empty().append( $data ); $children.empty().append( $data );
} ).fail( error ); } ).fail( error );
}; }
// Register click events // Register click events
mw.hook( 'wikipage.content' ).add( attachHandler ); mw.hook( 'wikipage.content' ).add( attachHandler );
@ -196,7 +189,7 @@
// This is needed when wgCategoryTreeHijackPageCategories is enabled. // This is needed when wgCategoryTreeHijackPageCategories is enabled.
mw.hook( 'wikipage.categories' ).add( attachHandler ); mw.hook( 'wikipage.categories' ).add( attachHandler );
$( function () { $( () => {
// Attach click handler for sidebar // Attach click handler for sidebar
// eslint-disable-next-line no-jquery/no-global-selector // eslint-disable-next-line no-jquery/no-global-selector
attachHandler( $( '#p-categorytree-portlet' ) ); attachHandler( $( '#p-categorytree-portlet' ) );