diff --git a/modules/jquery.codeEditor.js b/modules/jquery.codeEditor.js index 657decb9..d92574a4 100644 --- a/modules/jquery.codeEditor.js +++ b/modules/jquery.codeEditor.js @@ -38,12 +38,11 @@ textSelectionFn, hasErrorsOnSave = false, selectedLine = 0, - cookieEnabled, returnFalse = function () { return false; }, api = new mw.Api(); // Initialize state - cookieEnabled = parseInt( mw.cookie.get( 'codeEditor-' + context.instance + '-showInvisibleChars' ), 10 ); + var cookieEnabled = parseInt( mw.cookie.get( 'codeEditor-' + context.instance + '-showInvisibleChars' ), 10 ); context.showInvisibleChars = ( cookieEnabled === 1 ); cookieEnabled = parseInt( mw.cookie.get( 'codeEditor-' + context.instance + '-lineWrappingActive' ), 10 ); context.lineWrappingActive = ( cookieEnabled === 1 ); @@ -80,13 +79,10 @@ return true; }, codeEditorSave: function () { - var i, - annotations = context.codeEditor.getSession().getAnnotations(); - for ( i = 0; i < annotations.length; i++ ) { - if ( annotations[ i ].type === 'error' ) { - hasErrorsOnSave = true; - break; - } + if ( context.codeEditor.getSession().getAnnotations().some( function ( ann ) { + return ann.type === 'error'; + } ) ) { + hasErrorsOnSave = true; } }, codeEditorSync: function () { @@ -121,15 +117,13 @@ OO.ui.prompt( mw.msg( 'codeeditor-gotoline-prompt' ), { textInput: { placeholder: mw.msg( 'codeeditor-gotoline-placeholder' ) } } ).done( function ( result ) { - var matches, line, column; - if ( !result ) { return; } - matches = result.split( ':' ); - line = 0; - column = 0; + var matches = result.split( ':' ); + var line = 0; + var column = 0; if ( matches.length > 0 ) { line = +matches[ 0 ]; @@ -152,59 +146,53 @@ } ); }, setupCodeEditorToolbar: function () { - var toggleEditor, - toggleInvisibleChars, - toggleSearchReplace, - toggleLineWrapping, - indent, outdent, gotoLine; + var toggleEditor = function ( ctx ) { + ctx.codeEditorActive = !ctx.codeEditorActive; - toggleEditor = function ( context ) { - context.codeEditorActive = !context.codeEditorActive; + ctx.fn.setCodeEditorPreference( ctx.codeEditorActive ); + ctx.fn.updateCodeEditorToolbarButton(); - context.fn.setCodeEditorPreference( context.codeEditorActive ); - context.fn.updateCodeEditorToolbarButton(); - - if ( context.codeEditorActive ) { + if ( ctx.codeEditorActive ) { // set it back up! - context.fn.setupCodeEditor(); + ctx.fn.setupCodeEditor(); } else { - context.fn.disableCodeEditor(); + ctx.fn.disableCodeEditor(); } }; - toggleInvisibleChars = function ( context ) { - context.showInvisibleChars = !context.showInvisibleChars; + var toggleInvisibleChars = function ( ctx ) { + ctx.showInvisibleChars = !ctx.showInvisibleChars; - context.fn.changeCookieValue( 'showInvisibleChars', context.showInvisibleChars ? 1 : 0 ); - context.fn.updateInvisibleCharsButton(); + ctx.fn.changeCookieValue( 'showInvisibleChars', ctx.showInvisibleChars ? 1 : 0 ); + ctx.fn.updateInvisibleCharsButton(); - context.codeEditor.setShowInvisibles( context.showInvisibleChars ); + ctx.codeEditor.setShowInvisibles( ctx.showInvisibleChars ); }; - toggleSearchReplace = function ( context ) { - var searchBox = context.codeEditor.searchBox; + var toggleSearchReplace = function ( ctx ) { + var searchBox = ctx.codeEditor.searchBox; if ( searchBox && $( searchBox.element ).css( 'display' ) !== 'none' ) { searchBox.hide(); } else { - context.codeEditor.execCommand( - context.codeEditor.getReadOnly() ? 'find' : 'replace' + ctx.codeEditor.execCommand( + ctx.codeEditor.getReadOnly() ? 'find' : 'replace' ); } }; - toggleLineWrapping = function ( context ) { - context.lineWrappingActive = !context.lineWrappingActive; + var toggleLineWrapping = function ( ctx ) { + ctx.lineWrappingActive = !ctx.lineWrappingActive; - context.fn.changeCookieValue( 'lineWrappingActive', context.lineWrappingActive ? 1 : 0 ); - context.fn.updateLineWrappingButton(); + ctx.fn.changeCookieValue( 'lineWrappingActive', ctx.lineWrappingActive ? 1 : 0 ); + ctx.fn.updateLineWrappingButton(); - context.codeEditor.getSession().setUseWrapMode( context.lineWrappingActive ); + ctx.codeEditor.getSession().setUseWrapMode( ctx.lineWrappingActive ); }; - indent = function ( context ) { - context.codeEditor.execCommand( 'indent' ); + var indent = function ( ctx ) { + ctx.codeEditor.execCommand( 'indent' ); }; - outdent = function ( context ) { - context.codeEditor.execCommand( 'outdent' ); + var outdent = function ( ctx ) { + ctx.codeEditor.execCommand( 'outdent' ); }; - gotoLine = function ( context ) { - context.codeEditor.execCommand( 'gotolinecolumn' ); + var gotoLine = function ( ctx ) { + ctx.codeEditor.execCommand( 'gotolinecolumn' ); }; context.api.addToToolbar( context, { @@ -321,14 +309,12 @@ api.saveOption( 'usecodeeditor', prefValue ? 1 : 0 ) .fail( function ( code, result ) { - var message; - if ( code === 'http' && result.textStatus === 'abort' ) { // Request was aborted. Ignore error return; } - message = 'Failed to set code editor preference: ' + code; + var message = 'Failed to set code editor preference: ' + code; if ( result.error && result.error.info ) { message += '\n' + result.error.info; } @@ -339,11 +325,9 @@ * Sets up the iframe in place of the textarea to allow more advanced operations */ setupCodeEditor: function () { - var $box, lang, basePath, container, editdiv, session; - - $box = context.$textarea; - lang = mw.config.get( 'wgCodeEditorCurrentLanguage' ); - basePath = mw.config.get( 'wgExtensionAssetsPath', '' ); + var $box = context.$textarea; + var lang = mw.config.get( 'wgCodeEditorCurrentLanguage' ); + var basePath = mw.config.get( 'wgExtensionAssetsPath', '' ); if ( basePath.slice( 0, 2 ) === '//' ) { // ACE uses web workers, which have importScripts, which don't like relative links. // This is a problem only when the assets are on another server, so this rewrite should suffice @@ -357,8 +341,8 @@ // We'll stub this out to sit on top of it... // line-height is needed to compensate for oddity in WikiEditor extension, which zeroes the line-height on a parent container // eslint-disable-next-line no-jquery/no-parse-html-literal - container = context.$codeEditorContainer = $( '