diff options
Diffstat (limited to 'MLEB/Translate/resources/js/ext.translate.special.aggregategroups.js')
-rw-r--r-- | MLEB/Translate/resources/js/ext.translate.special.aggregategroups.js | 135 |
1 files changed, 75 insertions, 60 deletions
diff --git a/MLEB/Translate/resources/js/ext.translate.special.aggregategroups.js b/MLEB/Translate/resources/js/ext.translate.special.aggregategroups.js index 11cd05a4..547fe264 100644 --- a/MLEB/Translate/resources/js/ext.translate.special.aggregategroups.js +++ b/MLEB/Translate/resources/js/ext.translate.special.aggregategroups.js @@ -1,20 +1,17 @@ -/*global alert:false */ - ( function ( $, mw ) { 'use strict'; function getApiParams( $target ) { return { action: 'aggregategroups', - token: $( '#token' ).val(), - aggregategroup: $target.parents( '.mw-tpa-group' ).data( 'groupid' ), - format: 'json' + aggregategroup: $target.parents( '.mw-tpa-group' ).data( 'groupid' ) }; } function dissociate( event ) { var params, - $target = $( event.target ); + $target = $( event.target ), + api = new mw.Api(); function successFunction() { $target.parent( 'li' ).remove(); @@ -24,9 +21,13 @@ 'do': 'dissociate', group: $target.data( 'groupid' ) } ); - new mw.Api().post( params ).done( successFunction ).fail( function ( code, data ) { - window.alert( data.error && data.error.info ); - } ); + + api.postWithToken( 'csrf', params ) + .done( successFunction ) + .fail( function ( code, data ) { + // eslint-disable-next-line no-alert + alert( data.error && data.error.info ); + } ); } function associate( event, resp ) { @@ -34,7 +35,8 @@ $target = $( event.target ), $parent = $target.parents( '.mw-tpa-group' ), parentId = $parent.data( 'id' ), - subgroupName = $parent.children( '.tp-group-input' ).val(); + subgroupName = $parent.children( '.tp-group-input' ).val(), + api = new mw.Api(); successFunction = function () { var aAttr, $a, spanAttr, $span, $ol; @@ -61,7 +63,7 @@ // Get the label for the value and make API request if valid subgroupId = ''; - $.each( resp, function( key, value ) { + $.each( resp, function ( key, value ) { if ( subgroupName === value.label ) { subgroupId = value.id; } @@ -72,16 +74,23 @@ 'do': 'associate', group: subgroupId } ); - new mw.Api().post( params ).done( successFunction ).fail( function ( code, data ) { - window.alert( data.error && data.error.info ); - } ); + + api.postWithToken( 'csrf', params ) + .done( successFunction ) + .fail( function ( code, data ) { + // eslint-disable-next-line no-alert + alert( data.error && data.error.info ); + } ); } else { + // eslint-disable-next-line no-alert alert( mw.msg( 'tpt-invalid-group' ) ); } } function removeGroup( event ) { - var params, $target = $( event.target ); + var params, + $target = $( event.target ), + api = new mw.Api(); function successFunction() { $( event.target ).parents( '.mw-tpa-group' ).remove(); @@ -89,20 +98,25 @@ // XXX: 'confirm' is nonstandard. if ( $.isFunction( window.confirm ) && + // eslint-disable-next-line no-alert window.confirm( mw.msg( 'tpt-aggregategroup-remove-confirm' ) ) ) { params = $.extend( getApiParams( $target ), { 'do': 'remove' } ); - new mw.Api().post( params ).done( successFunction ).fail( function ( code, data ) { - window.alert( data.error && data.error.info ); - } ); + + api.postWithToken( 'csrf', params ) + .done( successFunction ) + .fail( function ( code, data ) { + // eslint-disable-next-line no-alert + alert( data.error && data.error.info ); + } ); } } function editGroup( event ) { var $target = $( event.target ), $parent = $target.closest( '.mw-tpa-group' ), - aggregateGroupId = $parent.data( 'groupid' ), + aggregateGroupId = $parent.data( 'groupid' ), $displayGroup = $parent.children( '.tp-display-group' ), $editGroup = $parent.children( '.tp-edit-group' ), successFunction, @@ -115,7 +129,7 @@ successFunction = function () { // Replace the text by the new text without altering the other 2 span tags - $displayGroup.children( '.tp-name' ).contents().filter( function() { + $displayGroup.children( '.tp-name' ).contents().filter( function () { return this.nodeType === 3; } ).replaceWith( aggregateGroupName ); $displayGroup.children( '.tp-desc' ).text( aggregateGroupDesc ); @@ -126,16 +140,17 @@ params = { action: 'aggregategroups', 'do': 'update', - token: $( '#token' ).val(), groupname: aggregateGroupName, groupdescription: aggregateGroupDesc, - aggregategroup: aggregateGroupId, - format: 'json' + aggregategroup: aggregateGroupId }; - api.post( params ).done( successFunction ).fail( function ( code, data ) { - window.alert( data.error.info ); - } ); + api.postWithToken( 'csrf', params ) + .done( successFunction ) + .fail( function ( code, data ) { + // eslint-disable-next-line no-alert + alert( data.error.info ); + } ); } function cancelEditGroup( event ) { @@ -145,25 +160,25 @@ $parent.children( '.tp-edit-group' ).addClass( 'hidden' ); } - $( document ).ready( function () { - var excludeFunction, autocompleteFunction, + $( function () { + var excludeFunction, autocompleteFunction, resp, api = new mw.Api(), - exclude = [], resp, groups=[], + exclude = [], + groups = [], $input = $( '.tp-group-input' ); - excludeFunction = function( event ) { + excludeFunction = function ( event ) { exclude = []; if ( groups.length === 0 ) { // Get list of subgroups using API api.get( { action: 'query', - format: 'json', meta: 'messagegroups', mgformat: 'tree', mgroot: 'all', mgprop: 'label|id' - } ).done( function( result ) { + } ).done( function ( result ) { groups = result.query.messagegroups; } ); } @@ -174,18 +189,18 @@ // Need to trim to remove the trailing whitespace // Can't use innerText not supported by Firefox var groupName = $( data ).text(); - groupName = $.trim( groupName ); + groupName = groupName.trim(); exclude.push( groupName ); } ); }; - autocompleteFunction = function( request, response ) { - resp = []; - + autocompleteFunction = function ( request, response ) { // Allow case insensitive search var inp = new RegExp( request.term, 'i' ); + resp = []; + $.each( groups, function ( key, value ) { if ( value.label.match( inp ) && exclude.indexOf( value.label ) === -1 ) { resp.push( value ); @@ -203,7 +218,7 @@ $( this ).autocomplete( 'search', $( this ).val() ); } ); - $( '.tp-aggregate-add-button' ).click( function( event ) { + $( '.tp-aggregate-add-button' ).click( function ( event ) { associate( event, resp ); } ); $( '.tp-aggregate-remove-button' ).click( dissociate ); @@ -229,7 +244,8 @@ aggGroupNameInputName = $( 'input.tp-aggregategroup-add-name' ), aggGroupNameInputDesc = $( 'input.tp-aggregategroup-add-description' ), aggregateGroupName = aggGroupNameInputName.val(), - aggregateGroupDesc = aggGroupNameInputDesc.val(); + aggregateGroupDesc = aggGroupNameInputDesc.val(), + api = new mw.Api(); // Empty the fields. If they are not emptied, then when another group // is added, the values will appear again. @@ -253,20 +269,18 @@ .append( $displayHeader ) .append( $( '<p>' ).addClass( 'tp-desc' ).text( aggregateGroupDesc ) ); - $saveButton = ( $( '<input>' ) + $saveButton = $( '<input>' ) .attr( { - 'type': 'button', + type: 'button', 'class': 'tp-aggregategroup-update' } ) - .val( mw.msg( 'tpt-aggregategroup-update' ) ) - ); - $cancelButton = ( $( '<input>' ) + .val( mw.msg( 'tpt-aggregategroup-update' ) ); + $cancelButton = $( '<input>' ) .attr( { - 'type': 'button', + type: 'button', 'class': 'tp-aggregategroup-update-cancel' } ) - .val( mw.msg( 'tpt-aggregategroup-update-cancel' ) ) - ); + .val( mw.msg( 'tpt-aggregategroup-update-cancel' ) ); $divEdit = $( '<div>' ) .addClass( 'tp-edit-group hidden' ) .append( $( '<label>' ) @@ -274,7 +288,7 @@ .append( $( '<input>' ) .attr( { 'class': 'tp-aggregategroup-edit-name', - 'id': 'tp-agg-name' + id: 'tp-agg-name' } ) .val( aggregateGroupName ) ) @@ -283,7 +297,7 @@ .append( $( '<input>' ) .attr( { 'class': 'tp-aggregategroup-edit-description', - 'id': 'tp-agg-desc' + id: 'tp-agg-desc' } ) .val( aggregateGroupDesc ) ) @@ -296,9 +310,8 @@ $div.data( 'groupid', aggregateGroupId ); $div.data( 'id', aggregateGroupId ); - $groupSelector = $( '<input>' ).attr( { - 'type': 'text' , + type: 'text', 'class': 'tp-group-input' } ); $groupSelector.focus( excludeFunction ); @@ -311,13 +324,13 @@ } ); $addButton = $( '<input>' ) .attr( { - 'type': 'button' , + type: 'button', 'class': 'tp-aggregate-add-button', - 'id': aggregateGroupId + id: aggregateGroupId } ) .val( mw.msg( 'tpt-aggregategroup-add' ) ); $div.append( $groupSelector, $addButton ); - $addButton.click( function( event ) { + $addButton.click( function ( event ) { associate( event, resp ); } ); $editSpan.on( 'click', function ( event ) { @@ -336,14 +349,16 @@ params = { action: 'aggregategroups', 'do': 'add', - token: $( '#token' ).val(), groupname: aggregateGroupName, - groupdescription: aggregateGroupDesc, - format: 'json' + groupdescription: aggregateGroupDesc }; - new mw.Api().post( params ).done( successFunction ).fail( function ( code, data ) { - window.alert( data.error && data.error.info ); - } ); + + api.postWithToken( 'csrf', params ) + .done( successFunction ) + .fail( function ( code, data ) { + // eslint-disable-next-line no-alert + alert( data.error && data.error.info ); + } ); } ); } ); -} ( jQuery, mediaWiki ) ); +}( jQuery, mediaWiki ) ); |