diff options
Diffstat (limited to 'AbuseFilter/modules/ext.abuseFilter.edit.js')
-rw-r--r-- | AbuseFilter/modules/ext.abuseFilter.edit.js | 173 |
1 files changed, 99 insertions, 74 deletions
diff --git a/AbuseFilter/modules/ext.abuseFilter.edit.js b/AbuseFilter/modules/ext.abuseFilter.edit.js index 93008139..908111b3 100644 --- a/AbuseFilter/modules/ext.abuseFilter.edit.js +++ b/AbuseFilter/modules/ext.abuseFilter.edit.js @@ -6,7 +6,7 @@ */ /* global ace */ -( function ( mw, $, OO ) { +( function () { 'use strict'; // @var {jQuery} Filter editor for JS and jQuery handling @@ -76,11 +76,11 @@ /** * Takes the data retrieved in doSyntaxCheck and processes it. * - * @param {Object} data Data returned from the AJAX request + * @param {Object} response Data returned from the AJAX request */ - function processSyntaxResult( data ) { - var position; - data = data.abusefilterchecksyntax; + function processSyntaxResult( response ) { + var position, + data = response.abusefilterchecksyntax; if ( data.status === 'ok' ) { // Successful @@ -105,7 +105,7 @@ filterEditor.scrollToRow( position.row ); } else { $plainTextBox - .focus() + .trigger( 'focus' ) .textSelection( 'setSelection', { start: data.character } ); } } @@ -118,9 +118,11 @@ * @param {Object} details Details about the error */ function processSyntaxResultFailure( error, details ) { - var msg = error === 'http' ? 'abusefilter-http-error' : 'unknown-error'; processSyntaxResultAlways( - mw.msg( msg, details && details.exception ), + mw.msg( + error === 'http' ? 'abusefilter-http-error' : 'unknown-error', + details && details.exception + ), 'mw-abusefilter-syntaxresult-error', false ); @@ -129,18 +131,17 @@ /** * Sends the current filter text to be checked for syntax issues. * - * @context HTMLElement + * @this HTMLElement * @param {jQuery.Event} e The event fired when the function is called */ function doSyntaxCheck() { - var filter = $plainTextBox.val(), - api = new mw.Api(); + var filter = $plainTextBox.val(); $( this ) .prop( 'disabled', true ) .injectSpinner( { id: 'abusefilter-syntaxcheck', size: 'large' } ); - api.post( { + new mw.Api().post( { action: 'abusefilterchecksyntax', filter: filter } ) @@ -174,12 +175,11 @@ /** * Fetches a filter from the API and inserts it into the filter box. * - * @context HTMLElement + * @this HTMLElement * @param {jQuery.Event} e The event fired when the function is called */ function fetchFilter() { - var filterId = $.trim( $( '#mw-abusefilter-load-filter input' ).val() ), - api; + var filterId = $( '#mw-abusefilter-load-filter input' ).val().trim(); if ( filterId === '' ) { return; @@ -188,8 +188,7 @@ $( this ).injectSpinner( { id: 'fetch-spinner', size: 'large' } ); // We just ignore errors or unexisting filters over here - api = new mw.Api(); - api.get( { + new mw.Api().get( { action: 'query', list: 'abusefilters', abfprop: 'pattern', @@ -197,10 +196,10 @@ abfendid: filterId, abflimit: 1 } ) - .always( function () { + .always( function removeSpinner() { $.removeSpinner( 'fetch-spinner' ); } ) - .done( function ( data ) { + .done( function insertFilter( data ) { if ( data.query.abusefilters[ 0 ] !== undefined ) { if ( useAce ) { filterEditor.setValue( data.query.abusefilters[ 0 ].pattern ); @@ -215,7 +214,7 @@ * that don't have checked boxes */ function hideDeselectedActions() { - $( '.mw-abusefilter-action-checkbox input' ).each( function () { + $( '.mw-abusefilter-action-checkbox input' ).each( function showHideParams() { // mw-abusefilter-action-checkbox-{$action} var action = this.parentNode.id.substr( 31 ), $params = $( '#mw-abusefilter-' + action + '-parameters' ); @@ -236,7 +235,7 @@ * @param {string} action The action the message refers to */ function previewMessage( action ) { - var api = new mw.Api(), + var api, args = [ '<nowiki>' + $( 'input[name=wpFilterDescription]' ).val() + '</nowiki>', $( '#mw-abusefilter-edit-id' ).children().last().text() @@ -246,30 +245,31 @@ $element = $( '#mw-abusefilter-' + action + '-preview' ), previewButton = action === 'warn' ? toggleWarnPreviewButton : toggleDisallowPreviewButton; - if ( !$element.is( ':visible' ) ) { + if ( $element.css( 'display' ) !== 'none' ) { + $element.hide(); + previewButton.setFlags( { destructive: false, progressive: true } ); + } else { + api = new mw.Api(); api.get( { action: 'query', meta: 'allmessages', ammessages: message, amargs: args.join( '|' ) } ) - .done( function ( data ) { + .done( function parseMessage( data ) { api.parse( data.query.allmessages[ 0 ][ '*' ], { disablelimitreport: '', preview: '', prop: 'text', title: 'MediaWiki:' + message } ) - .done( function ( html ) { + .done( function showMessage( html ) { $element.show().html( html ); previewButton.setFlags( { destructive: true, progressive: false } ); } ); } ); - } else { - $element.hide(); - previewButton.setFlags( { destructive: false, progressive: true } ); } } @@ -281,9 +281,10 @@ function editMessage( action ) { var message = getCurrentMessage( action ), defaultMsg = action === 'warn' ? 'warning' : 'disallowed', - url = mw.config.get( 'wgScript' ) + - '?title=MediaWiki:' + mw.util.wikiUrlencode( message ) + - '&action=edit&preload=MediaWiki:abusefilter-' + defaultMsg; + url = mw.util.getUrl( 'MediaWiki:' + message, { + action: 'edit', + preload: 'MediaWiki:abusefilter-' + defaultMsg + } ); window.open( url, '_blank' ); } @@ -291,7 +292,7 @@ /** * Called if the filter group (#mw-abusefilter-edit-group-input select) is changed. * - * @context HTMLELement + * @this HTMLELement * @param {jQuery.Event} e The event fired when the function is called */ function onFilterGroupChange() { @@ -348,48 +349,70 @@ /** * Called if the user presses a key in the load filter field. * - * @context HTMLELement + * @this HTMLELement * @param {jQuery.Event} e The event fired when the function is called */ function onFilterKeypress( e ) { if ( e.type === 'keypress' && e.which === 13 ) { e.preventDefault(); - $( '#mw-abusefilter-load' ).click(); + $( '#mw-abusefilter-load' ).trigger( 'click' ); } } /** + * Warn if the user changed anything and tries to leave the window + */ + function setWarnOnLeave() { + var warnOnLeave, + $form = $( '#mw-abusefilter-editing-form' ), + origValues = $form.serialize(); + + warnOnLeave = mw.confirmCloseWindow( { + test: function () { + return $form.serialize() !== origValues; + }, + message: mw.msg( 'abusefilter-edit-warn-leave' ) + } ); + + $form.on( 'submit', function () { + warnOnLeave.release(); + } ); + } + + /** * Builds a TagMultiselectWidget, to be used both for throttle groups and change tags * * @param {string} action Either 'throttle' or 'tag', will be used to build element IDs * @param {Array} config The array with configuration passed from PHP code */ function buildSelector( action, config ) { - var disabled = config.disabled.length !== 0, - // mw-abusefilter-throttle-parameters, mw-abusefilter-tag-parameters - $container = $( '#mw-abusefilter-' + action + '-parameters' ), + // mw-abusefilter-throttle-parameters, mw-abusefilter-tag-parameters + var $container = $( '#mw-abusefilter-' + action + '-parameters' ), // Character used to separate elements in the textarea. separator = action === 'throttle' ? '\n' : ',', - selector, field, hiddenField; - - selector = - new OO.ui.TagMultiselectWidget( { - inputPosition: 'outline', - allowArbitrary: true, - allowEditTags: true, - selected: config.values, - // abusefilter-edit-throttle-placeholder, abusefilter-edit-tag-placeholder - placeholder: OO.ui.msg( 'abusefilter-edit-' + action + '-placeholder' ), - disabled: disabled - } ); - field = - new OO.ui.FieldLayout( - selector, - { - label: $( $.parseHTML( config.label ) ), - align: 'top' - } - ); + selector, field, fieldOpts, hiddenField; + + selector = new OO.ui.TagMultiselectWidget( { + inputPosition: 'outline', + allowArbitrary: true, + allowEditTags: true, + selected: config.values, + // The following messages are used here: + // * abusefilter-edit-throttle-placeholder + // * abusefilter-edit-tag-placeholder + placeholder: OO.ui.msg( 'abusefilter-edit-' + action + '-placeholder' ), + disabled: config.disabled + } ); + + fieldOpts = { + label: $( $.parseHTML( config.label ) ), + align: 'top' + }; + if ( action === 'throttle' ) { + fieldOpts.help = new OO.ui.HtmlSnippet( config.help ); + } + + field = new OO.ui.FieldLayout( selector, fieldOpts ); // mw-abusefilter-hidden-throttle-field, mw-abusefilter-hidden-tag-field hiddenField = OO.ui.infuse( $( '#mw-abusefilter-hidden-' + action + '-field' ) ); @@ -403,12 +426,13 @@ } // On ready initialization - $( document ).ready( function () { + $( function () { var basePath, readOnly, $exportBox = $( '#mw-abusefilter-export' ), isFilterEditor = mw.config.get( 'isFilterEditor' ), tagConfig = mw.config.get( 'tagConfig' ), throttleConfig = mw.config.get( 'throttleConfig' ), + $switchEditorBtn = $( '#mw-abusefilter-switcheditor' ), cbEnabled, cbDeleted; if ( isFilterEditor ) { @@ -428,9 +452,10 @@ toggleDisallowPreviewButton = OO.ui.infuse( $( '#mw-abusefilter-disallow-preview-button' ) ); disallowMessageExisting = OO.ui.infuse( $( '#mw-abusefilter-disallow-message-existing' ) ); disallowMessageOther = OO.ui.infuse( $( '#mw-abusefilter-disallow-message-other' ) ); + setWarnOnLeave(); } - $plainTextBox = $( '#' + mw.config.get( 'abuseFilterBoxName' ) ); + $plainTextBox = $( '#wpFilterRules' ); if ( $( '#wpAceFilterEditor' ).length ) { // CodeEditor is installed. @@ -472,12 +497,13 @@ $plainTextBox.val( filterEditor.getSession().getValue() ); } ); - $( '#mw-abusefilter-switcheditor' ).click( switchEditor ); + $switchEditorBtn.on( 'click', switchEditor ); + $switchEditorBtn.show(); } ); } // Hide the syntax ok message when the text changes - $plainTextBox.change( function () { + $plainTextBox.on( 'change', function () { var $el = $( '#mw-abusefilter-syntaxresult' ); if ( $el.data( 'syntaxOk' ) ) { @@ -485,27 +511,27 @@ } } ); - $( '#mw-abusefilter-load' ).click( fetchFilter ); - $( '#mw-abusefilter-load-filter' ).keypress( onFilterKeypress ); + $( '#mw-abusefilter-load' ).on( 'click', fetchFilter ); + $( '#mw-abusefilter-load-filter' ).on( 'keypress', onFilterKeypress ); if ( isFilterEditor ) { // Add logic for flags and consequences - $( '#mw-abusefilter-warn-preview-button' ).click( + $( '#mw-abusefilter-warn-preview-button' ).on( 'click', function () { previewMessage( 'warn' ); } ); - $( '#mw-abusefilter-disallow-preview-button' ).click( + $( '#mw-abusefilter-disallow-preview-button' ).on( 'click', function () { previewMessage( 'disallow' ); } ); - $( '#mw-abusefilter-warn-edit-button' ).click( + $( '#mw-abusefilter-warn-edit-button' ).on( 'click', function () { editMessage( 'warn' ); } ); - $( '#mw-abusefilter-disallow-edit-button' ).click( + $( '#mw-abusefilter-disallow-edit-button' ).on( 'click', function () { editMessage( 'disallow' ); } ); - $( '.mw-abusefilter-action-checkbox input' ).click( hideDeselectedActions ); + $( '.mw-abusefilter-action-checkbox input' ).on( 'click', hideDeselectedActions ); hideDeselectedActions(); - $( '#wpFilterGlobal' ).change( toggleCustomMessages ); + $( '#wpFilterGlobal' ).on( 'change', toggleCustomMessages ); toggleCustomMessages(); cbEnabled = OO.ui.infuse( $( '#wpFilterEnabled' ) ); @@ -528,9 +554,9 @@ } ); - $( '#mw-abusefilter-edit-group-input select' ).change( onFilterGroupChange ); + $( '#mw-abusefilter-edit-group-input select' ).on( 'change', onFilterGroupChange ); - $( '#mw-abusefilter-export-link' ).click( + $( '#mw-abusefilter-export-link' ).on( 'click', function ( e ) { e.preventDefault(); $exportBox.toggle(); @@ -538,8 +564,7 @@ ); } - $( '#mw-abusefilter-syntaxcheck' ).click( doSyntaxCheck ); - $( '#wpFilterBuilder' ).change( addText ); - + $( '#mw-abusefilter-syntaxcheck' ).on( 'click', doSyntaxCheck ); + $( '#wpFilterBuilder' ).on( 'change', addText ); } ); -}( mediaWiki, jQuery, OO ) ); +}() ); |