Merge "Streamline smaller pieces of JavaScript code"

This commit is contained in:
jenkins-bot 2022-03-16 20:52:38 +00:00 committed by Gerrit Code Review
commit a65aaa0c6d
7 changed files with 98 additions and 144 deletions

View file

@ -49,7 +49,7 @@ TitleInputField.prototype.reset = function () {
* Set the URL mode and disable automatic detection of external URLs.
*
* @public
* @param {string} urlMode One of the `TitleInputField.urlModes.*` values.
* @param {string} urlMode One of the `LinkTypeField.static.LINK_MODE_*` values.
*/
TitleInputField.prototype.setUrlMode = function ( urlMode ) {
this.urlMode = urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL ?
@ -90,8 +90,9 @@ TitleInputField.prototype.makeMessage = function ( kind, text ) {
TitleInputField.prototype.setMessage = function ( icon, message, type ) {
this.setNotices( [ message ] );
// Note that setNotices() must be called before this.message is available.
this.message.setType( type || 'notice' );
this.message.setIcon( icon );
this.message
.setIcon( icon )
.setType( type );
};
/**
@ -130,29 +131,24 @@ TitleInputField.prototype.validate = function ( value ) {
* @param {TitleOptionWidget} item
*/
TitleInputField.prototype.onSelect = function ( item ) {
var icon, msg;
if ( this.urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL ||
( !this.urlModeManual && this.urlMode === LinkTypeField.static.LINK_MODE_INTERNAL && item.isExternal() )
) {
this.setMessage(
'linkExternal',
mw.message( 'wikieditor-toolbar-tool-link-int-target-status-external' ).parse()
);
icon = 'linkExternal';
msg = 'wikieditor-toolbar-tool-link-int-target-status-external';
} else if ( item.isDisambiguation() ) {
this.setMessage(
'articleDisambiguation',
mw.message( 'wikieditor-toolbar-tool-link-int-target-status-disambig' ).parse()
);
icon = 'articleDisambiguation';
msg = 'wikieditor-toolbar-tool-link-int-target-status-disambig';
} else if ( !item.isMissing() && !item.isExternal() ) {
this.setMessage(
'article',
mw.message( 'wikieditor-toolbar-tool-link-int-target-status-exists' ).parse()
);
icon = 'article';
msg = 'wikieditor-toolbar-tool-link-int-target-status-exists';
} else {
this.setMessage(
'articleNotFound',
mw.message( 'wikieditor-toolbar-tool-link-int-target-status-notexists' ).parse()
);
icon = 'articleNotFound';
msg = 'wikieditor-toolbar-tool-link-int-target-status-notexists';
}
// eslint-disable-next-line mediawiki/msg-doc
this.setMessage( icon, mw.message( msg ).parse() );
};
module.exports = TitleInputField;

View file

@ -133,11 +133,10 @@ TitleInputWidget.prototype.onLookupMenuChoose = function ( item ) {
*/
TitleInputWidget.prototype.getOverlay = function () {
// Overlay z-index must be greater than the jQuery UI dialog's of 1002.
var $overlay = OO.ui.getDefaultOverlay()
return OO.ui.getDefaultOverlay()
.clone()
.css( 'z-index', '1010' );
$( document.body ).append( $overlay );
return $overlay;
.css( 'z-index', '1010' )
.appendTo( document.body );
};
/**
@ -149,8 +148,7 @@ TitleInputWidget.prototype.getOverlay = function () {
* @return {boolean}
*/
TitleInputWidget.prototype.looksLikeExternalLink = function ( urlString ) {
var matches = urlString.match( this.constructor.static.urlRegex );
return matches !== null && matches.length > 0;
this.constructor.static.urlRegex.test( urlString );
};
module.exports = TitleInputWidget;

View file

@ -17,7 +17,7 @@ function InsertLinkTitleOptionWidget( config ) {
config.data = config.url;
// Prepend http:// if there is no protocol (i.e. if it starts with "www.").
// @TODO This is repeated when the link is inserted (in jquery.wikiEditor.dialogs.config.js).
if ( !config.url.match( /^[a-z]+:\/\/./ ) ) {
if ( !/^[a-z]+:\/\/./.test( config.url ) ) {
config.url = 'http://' + config.url;
}
config.missing = false;

View file

@ -189,7 +189,7 @@
} else {
target = target.trim();
// Prepend http:// if there is no protocol
if ( !target.match( /^[a-z]+:\/\/./ ) ) {
if ( !/^[a-z]+:\/\/./.test( target ) ) {
target = 'http://' + target;
}
@ -247,8 +247,9 @@
// Blank form
insertLinkTitleInputField.reset();
insertLinkLinkTextField.getField().setValue( '' );
insertLinkLinkTypeField.getField().selectItem( null );
insertLinkLinkTextField.getField()
.setValue( '' )
.selectItem( null );
}
},
'wikieditor-toolbar-tool-link-cancel': function () {
@ -498,9 +499,6 @@
if ( !match ) {
return false;
}
var result = {};
result.pre = match[ 1 ];
result.post = match[ 3 ];
// Escape pipes inside links and templates,
// then split the parameters at the remaining pipes
var params = match[ 2 ].replace( /\[\[[^[\]]*\]\]|\{\{[^{}]\}\}/g, function ( link ) {
@ -510,7 +508,11 @@
if ( !file || file.getNamespaceId() !== 6 ) {
return false;
}
result.fileName = file.getMainText();
var result = {
pre: match[ 1 ],
post: match[ 3 ],
fileName: file.getMainText()
};
for ( var i = 1; i < params.length; i++ ) {
var paramOrig = params[ i ];
var param = paramOrig.toLowerCase();
@ -811,11 +813,7 @@
var match = false;
var offset, textRemainder;
if ( mode !== 'replaceAll' ) {
if ( mode === 'replace' ) {
offset = $( this ).data( 'matchIndex' );
} else {
offset = $( this ).data( 'offset' );
}
offset = $( this ).data( mode === 'replace' ? 'matchIndex' : 'offset' );
textRemainder = text.slice( offset );
match = textRemainder.match( regex );
}
@ -865,28 +863,20 @@
offset = offset + match.index + actualReplacement.length;
textRemainder = text.slice( offset );
match = textRemainder.match( regex );
if ( match ) {
start = offset + match.index;
end = start + match[ 0 ].length;
} else {
if ( !match ) {
// If no new string was found, try searching from the beginning.
// TODO: Add a "Wrap around" option.
offset = 0;
textRemainder = text;
match = textRemainder.match( regex );
if ( match ) {
start = match.index;
end = start + match[ 0 ].length;
} else {
// Give up
start = 0;
end = 0;
}
}
} else {
start = offset + match.index;
end = start + match[ 0 ].length;
if ( !match ) {
// Give up
match = { index: 0, 0: { length: 0 } };
}
}
start = offset + match.index;
end = start + match[ 0 ].length;
$( this ).data( 'matchIndex', start );
@ -922,8 +912,7 @@
},
open: function () {
var that = this;
$( this ).data( 'offset', 0 );
$( this ).data( 'matchIndex', 0 );
$( this ).data( { offset: 0, matchIndex: 0 } );
$( '#wikieditor-toolbar-replace-search' ).trigger( 'focus' );
$( '#wikieditor-toolbar-replace-nomatch, #wikieditor-toolbar-replace-success, #wikieditor-toolbar-replace-emptysearch, #wikieditor-toolbar-replace-invalidregex' ).hide();
@ -945,10 +934,7 @@
}
var $dialog = $( this ).closest( '.ui-dialog' );
that = this;
var context = $( this ).data( 'context' );
var $textbox = context.$textarea;
$textbox
$( this ).data( 'context' ).$textarea
.on( 'keypress.srdialog', function ( e ) {
if ( e.which === 13 ) {
// Enter
@ -961,9 +947,8 @@
} );
},
close: function () {
var context = $( this ).data( 'context' ),
$textbox = context.$textarea;
$textbox.off( 'keypress.srdialog' );
$( this ).data( 'context' ).$textarea
.off( 'keypress.srdialog' );
$( this ).closest( '.ui-dialog' ).data( 'dialogaction', false );
}
}

View file

@ -222,7 +222,7 @@
// where we left off
var context = $( this ).data( 'wikiEditor-context' );
// On first call, we need to set things up, but on all following calls we can skip right to the API handling
if ( !context || typeof context === 'undefined' ) {
if ( !context ) {
// Star filling the context with useful data - any jQuery selections, as usual should be named with a preceding $
context = {
@ -315,13 +315,9 @@
*/
trigger: function ( name, event ) {
// Event is an optional argument, but from here on out, at least the type field should be dependable
if ( typeof event === 'undefined' ) {
event = { type: 'custom' };
}
event = event || { type: 'custom' };
// Ensure there's a place for extra information to live
if ( typeof event.data === 'undefined' ) {
event.data = {};
}
event.data = event.data || {};
// Allow filtering to occur
if ( name in context.evt ) {
@ -329,7 +325,7 @@
return false;
}
}
var returnFromModules = null; // they return null by default
var returnFromModules = true;
// Pass the event around to all modules activated on this context
for ( var module in context.modules ) {
@ -341,19 +337,11 @@
var ret = $.wikiEditor.modules[ module ].evt[ name ]( context, event );
if ( ret !== null ) {
// if 1 returns false, the end result is false
if ( returnFromModules === null ) {
returnFromModules = ret;
} else {
returnFromModules = returnFromModules && ret;
}
returnFromModules = returnFromModules && ret;
}
}
}
if ( returnFromModules !== null ) {
return returnFromModules;
} else {
return true;
}
return returnFromModules;
},
/**
@ -484,13 +472,13 @@
context.$ui = context.$textarea.parent().parent().parent().parent().parent();
context.$wikitext = context.$textarea.parent().parent().parent().parent();
// Add in tab and button containers
context.$wikitext
.before(
$( '<div>' ).addClass( 'wikiEditor-ui-controls' )
.append( $( '<div>' ).addClass( 'wikiEditor-ui-tabs' ).hide() )
.append( $( '<div>' ).addClass( 'wikiEditor-ui-buttons' ) )
)
.before( $( '<div>' ).addClass( 'wikiEditor-ui-clear' ) );
context.$wikitext.before(
$( '<div>' ).addClass( 'wikiEditor-ui-controls' ).append(
$( '<div>' ).addClass( 'wikiEditor-ui-tabs' ).hide(),
$( '<div>' ).addClass( 'wikiEditor-ui-buttons' )
),
$( '<div>' ).addClass( 'wikiEditor-ui-clear' )
);
// Get references to some of the newly created containers
context.$controls = context.$ui.find( '.wikiEditor-ui-buttons' ).hide();
context.$buttons = context.$ui.find( '.wikiEditor-ui-buttons' );
@ -498,8 +486,10 @@
// Clear all floating after the UI
context.$ui.after( $( '<div>' ).addClass( 'wikiEditor-ui-clear' ) );
// Attach a right container
context.$wikitext.append( $( '<div>' ).addClass( 'wikiEditor-ui-right' ) );
context.$wikitext.append( $( '<div>' ).addClass( 'wikiEditor-ui-clear' ) );
context.$wikitext.append(
$( '<div>' ).addClass( 'wikiEditor-ui-right' ),
$( '<div>' ).addClass( 'wikiEditor-ui-clear' )
);
// Attach a top container to the left pane
context.$wikitext.find( '.wikiEditor-ui-left' ).prepend( $( '<div>' ).addClass( 'wikiEditor-ui-top' ) );
// Setup the initial view
@ -544,7 +534,7 @@
// Handle API calls
var callArg = args.shift();
if ( callArg in context.api ) {
context.api[ callArg ]( context, typeof args[ 0 ] === 'undefined' ? {} : args[ 0 ] );
context.api[ callArg ]( context, args[ 0 ] || {} );
}
}

View file

@ -221,7 +221,7 @@
var replace = action.type === 'replace';
if ( 'regex' in action.options && 'regexReplace' in action.options ) {
var selection = context.$textarea.textSelection( 'getSelection' );
if ( selection !== '' && selection.match( action.options.regex ) ) {
if ( selection !== '' && action.options.regex.test( selection ) ) {
parts.peri = selection.replace( action.options.regex,
action.options.regexReplace );
parts.pre = parts.post = '';
@ -249,10 +249,7 @@
var $group = $( '<div>' ).attr( { class: 'group group-' + id, rel: id } ),
label = $.wikiEditor.autoMsg( group, 'label' );
if ( label ) {
var $label = $( '<span>' )
.addClass( 'label' )
.text( label );
$group.append( $label );
$( '<span>' ).addClass( 'label' ).text( label ).appendTo( $group );
}
var empty = true;
if ( 'tools' in group ) {
@ -297,9 +294,9 @@
} else if ( tool.type === 'toggle' ) {
oouiButton = new OO.ui.ToggleButtonWidget( config );
}
$button = oouiButton.$element;
$button.attr( 'rel', id );
$button.data( 'ooui', oouiButton );
$button = oouiButton.$element
.attr( 'rel', id )
.data( 'ooui', oouiButton );
} else {
$button = $( '<a>' )
.attr( {
@ -430,16 +427,17 @@
return $select;
case 'element':
// A raw 'element' type can be {htmlString|Element|Text|Array|jQuery|OO.ui.HTMLSnippet|function}.
var $element = $( '<div>' )
.attr( { rel: id, class: 'tool tool-element' } );
var $element;
if ( tool.element instanceof OO.ui.HtmlSnippet ) {
$element.append( tool.element.toString() );
$element = tool.element.toString();
} else if ( typeof tool.element === 'function' ) {
$element.append( tool.element( context ) );
$element = tool.element( context );
} else {
$element.append( tool.element );
$element = tool.element;
}
return $element;
return $( '<div>' )
.attr( { rel: id, class: 'tool tool-element' } )
.append( $element );
default:
return null;
}
@ -461,8 +459,9 @@
.on( 'click', function ( event ) {
$( this ).parent().parent().find( '.page' ).hide();
$( this ).parent().parent().find( '.page-' + $( this ).attr( 'rel' ) ).show().trigger( 'loadPage' );
$( this ).siblings().removeClass( 'current' );
$( this ).addClass( 'current' );
$( this )
.addClass( 'current' )
.siblings().removeClass( 'current' );
var section = $( this ).parent().parent().attr( 'rel' );
$.cookie(
'wikiEditor-' + $( this ).data( 'context' ).instance + '-booklet-' + section + '-page',
@ -493,8 +492,7 @@
switch ( page.layout ) {
case 'table':
$page.addClass( 'page-table' );
var html =
'<table class="table-' + id + '">';
var html = '';
if ( 'headings' in page ) {
html += toolbarModule.fn.buildHeading( context, page.headings );
}
@ -503,7 +501,7 @@
html += toolbarModule.fn.buildRow( context, page.rows[ i ] );
}
}
$page.html( html + '</table>' );
$page.html( '<table class="table-' + id + '">' + html + '</table>' );
break;
case 'characters':
$page.addClass( 'page-characters' );
@ -549,21 +547,21 @@
}
},
buildHeading: function ( context, headings ) {
var html = '<tr>';
var html = '';
for ( var i = 0; i < headings.length; i++ ) {
html += '<th>' + $.wikiEditor.autoSafeMsg( headings[ i ], [ 'html', 'text' ] ) + '</th>';
}
return html + '</tr>';
return '<tr>' + html + '</tr>';
},
buildRow: function ( context, row ) {
var html = '<tr>';
var html = '';
for ( var cell in row ) {
// FIXME: This currently needs to use the "unsafe" .text() message because it embeds raw HTML
// in the messages (as used exclusively by the 'help' toolbar panel).
html += '<td class="cell cell-' + cell + '"><span>' +
$.wikiEditor.autoMsg( row[ cell ], [ 'html', 'text' ] ) + '</span></td>';
}
return html + '</tr>';
return '<tr>' + html + '</tr>';
},
buildCharacter: function ( character, actions ) {
if ( typeof character === 'string' ) {
@ -593,23 +591,13 @@
}
if ( character && 'action' in character && 'label' in character ) {
actions[ character.label ] = character.action;
if ( character.titleMsg !== undefined || character.title !== undefined ) {
var title;
if ( character.titleMsg !== undefined ) {
// eslint-disable-next-line mediawiki/msg-doc
title = mw.msg( character.titleMsg );
} else {
title = character.title;
}
return mw.html.element(
'span',
{ rel: character.label, title: title },
character.label
);
} else {
return mw.html.element( 'span', { rel: character.label }, character.label );
}
// eslint-disable-next-line mediawiki/msg-doc
var title = character.titleMsg ? mw.msg( character.titleMsg ) : character.title;
return mw.html.element(
'span',
{ rel: character.label, title: title || false },
character.label
);
}
mw.log( 'A character for the toolbar was undefined. This is not supposed to happen. Double check the config.' );
// bug 31673; also an additional fix for bug 24208...
@ -663,8 +651,9 @@
.removeClass( 'section-visible' )
.addClass( 'section-hidden' );
$( this ).attr( 'aria-expanded', 'false' );
$( this ).parent().parent().find( 'a' ).removeClass( 'current' );
$( this )
.attr( 'aria-expanded', 'false' )
.parent().parent().find( 'a' ).removeClass( 'current' );
if ( show ) {
$section
.removeClass( 'section-hidden' )
@ -705,13 +694,9 @@
// Show or hide section
if ( id !== 'main' && id !== 'secondary' ) {
$section.attr( 'aria-expanded', show ? 'true' : 'false' );
if ( show ) {
$section.addClass( 'section-visible' );
} else {
$section.addClass( 'section-hidden' );
}
$section
.attr( 'aria-expanded', show.toString() )
.addClass( show ? 'section-visible' : 'section-hidden' );
}
return $section;
},
@ -759,7 +744,7 @@
);
}
}
$section.append( $index ).append( $pages );
$section.append( $index, $pages );
toolbarModule.fn.updateBookletSelection( context, id, $pages, $index );
break;
}

View file

@ -1,10 +1,10 @@
( function () {
QUnit.module( 'ext.wikiEditor.toolbar', QUnit.newMwEnvironment( {
setup: function () {
var $fixture = $( '#qunit-fixture' ),
$target = $( '<textarea>' ).attr( 'id', 'wpTextBox1' );
var $target = $( '<textarea>' )
.attr( 'id', 'wpTextBox1' )
.appendTo( '#qunit-fixture' );
this.$target = $target;
$fixture.append( $target );
$target.wikiEditor( 'addModule', 'toolbar' );
this.$ui = $target.data( 'wikiEditor-context' ).$ui;
}