diff --git a/modules/insertlink/TitleInputField.js b/modules/insertlink/TitleInputField.js index 149291ef..899eb697 100644 --- a/modules/insertlink/TitleInputField.js +++ b/modules/insertlink/TitleInputField.js @@ -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; diff --git a/modules/insertlink/TitleInputWidget.js b/modules/insertlink/TitleInputWidget.js index 01222233..64274aa9 100644 --- a/modules/insertlink/TitleInputWidget.js +++ b/modules/insertlink/TitleInputWidget.js @@ -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; diff --git a/modules/insertlink/TitleOptionWidget.js b/modules/insertlink/TitleOptionWidget.js index 0b1d6e4c..787e0232 100644 --- a/modules/insertlink/TitleOptionWidget.js +++ b/modules/insertlink/TitleOptionWidget.js @@ -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; diff --git a/modules/jquery.wikiEditor.dialogs.config.js b/modules/jquery.wikiEditor.dialogs.config.js index 4d7626ab..1c4688af 100644 --- a/modules/jquery.wikiEditor.dialogs.config.js +++ b/modules/jquery.wikiEditor.dialogs.config.js @@ -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 ); } } diff --git a/modules/jquery.wikiEditor.js b/modules/jquery.wikiEditor.js index c3e72f92..1965bbd5 100644 --- a/modules/jquery.wikiEditor.js +++ b/modules/jquery.wikiEditor.js @@ -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( - $( '