Simplify variables and clean up comments
Not supposed to have any functional changes Change-Id: Ibb16af9144f183d85b985bcb3133a4244e310cda
This commit is contained in:
committed by
Amir E. Aharoni
parent
3cce3dd599
commit
77383aa26a
@@ -27,39 +27,40 @@
|
|||||||
/**
|
/**
|
||||||
* Post to options API with correct token.
|
* Post to options API with correct token.
|
||||||
* If we have no token, get one and try to post.
|
* If we have no token, get one and try to post.
|
||||||
* If we have a cached token try using that, and if it fails, blank out the
|
* If we have a cached token try using that,
|
||||||
* cached token and start over.
|
* and if it fails, blank out the cached token and start over.
|
||||||
*
|
*
|
||||||
* @param params {Object} API parameters
|
* @param params {Object} API parameters
|
||||||
* @param ok {Function} callback for success
|
* @param ok {Function} callback for success
|
||||||
* @param err {Function} [optional] error callback
|
* @param err {Function} [optional] error callback
|
||||||
* @return {jqXHR}
|
* @return {jqXHR}
|
||||||
*/
|
*/
|
||||||
function saveOptionsWithToken ( params, ok, err ) {
|
function saveOptionsWithToken( params, ok, err ) {
|
||||||
var useTokenToPost, getTokenIfBad;
|
|
||||||
if ( cachedOptionsToken === null ) {
|
if ( cachedOptionsToken === null ) {
|
||||||
// We don't have a valid cached token, so get a fresh one and try posting.
|
// We don't have a valid cached token, so get a fresh one and try posting.
|
||||||
// We do not trap any 'badtoken' or 'notoken' errors, because we don't want
|
// We do not trap any 'badtoken' or 'notoken' errors, because we don't want
|
||||||
// an infinite loop. If this fresh token is bad, something else is very wrong.
|
// an infinite loop. If this fresh token is bad, something else is very wrong.
|
||||||
useTokenToPost = function ( token ) {
|
return getOptionsToken( function ( token ) {
|
||||||
params.token = token;
|
params.token = token;
|
||||||
new mw.Api().post( params, ok, err );
|
new mw.Api().post( params, ok, err );
|
||||||
};
|
}, err );
|
||||||
return getOptionToken( useTokenToPost, err );
|
|
||||||
} else {
|
} else {
|
||||||
// We do have a token, but it might be expired. So if it is 'bad' then
|
|
||||||
// start over with a new token.
|
|
||||||
params.token = cachedOptionsToken;
|
params.token = cachedOptionsToken;
|
||||||
getTokenIfBad = function ( code, result ) {
|
|
||||||
if ( code === 'badtoken' ) {
|
return new mw.Api().post( params, {
|
||||||
// force a new token, clear any old one
|
ok: ok,
|
||||||
cachedOptionsToken = null;
|
err: function ( code, result ) {
|
||||||
saveOptionsWithToken( params, ok, err );
|
// We do have a token, but it might be expired.
|
||||||
} else {
|
// So if it is 'bad', then start over with a new token.
|
||||||
err( code, result );
|
if ( code === 'badtoken' ) {
|
||||||
|
// force a new token, clear any old one
|
||||||
|
cachedOptionsToken = null;
|
||||||
|
saveOptionsWithToken( params, ok, err );
|
||||||
|
} else {
|
||||||
|
err( code, result );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
} );
|
||||||
return new mw.Api().post( params, { ok : ok, err : getTokenIfBad });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,13 +76,14 @@
|
|||||||
* @param err {Function} error callback
|
* @param err {Function} error callback
|
||||||
* @return {jqXHR}
|
* @return {jqXHR}
|
||||||
*/
|
*/
|
||||||
function getOptionToken ( tokenCallback, err ) {
|
function getOptionsToken( tokenCallback, err ) {
|
||||||
var parameters = {
|
return new mw.Api().get( {
|
||||||
action: 'tokens',
|
action: 'tokens',
|
||||||
type: 'options'
|
type: 'options'
|
||||||
},
|
}, {
|
||||||
ok = function ( data ) {
|
ok: function ( data ) {
|
||||||
var token;
|
var token;
|
||||||
|
|
||||||
// If token type is not available for this user,
|
// If token type is not available for this user,
|
||||||
// key 'translationreviewtoken' is missing or can contain Boolean false
|
// key 'translationreviewtoken' is missing or can contain Boolean false
|
||||||
if ( data.tokens && data.tokens.optionstoken ) {
|
if ( data.tokens && data.tokens.optionstoken ) {
|
||||||
@@ -92,16 +94,12 @@
|
|||||||
err( 'token-missing', data );
|
err( 'token-missing', data );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ajaxOptions = {
|
err: err,
|
||||||
ok: ok,
|
// Due to the API assuming we're logged out if we pass the callback-parameter,
|
||||||
err: err,
|
// we have to disable jQuery's callback system, and instead parse JSON string,
|
||||||
// Due to the API assuming we're logged out if we pass the callback-parameter,
|
// by setting 'jsonp' to false.
|
||||||
// we have to disable jQuery's callback system, and instead parse JSON string,
|
jsonp: false
|
||||||
// by setting 'jsonp' to false.
|
} );
|
||||||
jsonp: false
|
|
||||||
};
|
|
||||||
|
|
||||||
return new mw.Api().get( parameters, ajaxOptions );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ULSPreferences = function () {
|
ULSPreferences = function () {
|
||||||
@@ -155,11 +153,11 @@
|
|||||||
|
|
||||||
callback = callback || $.noop;
|
callback = callback || $.noop;
|
||||||
if ( this.isAnon ) {
|
if ( this.isAnon ) {
|
||||||
// Anonymous user- Save preferences in local storage
|
// Anonymous user. Save preferences in local storage
|
||||||
$.jStorage.set( this.preferenceName, this.preferences );
|
$.jStorage.set( this.preferenceName, this.preferences );
|
||||||
callback.call( this, true );
|
callback.call( this, true );
|
||||||
} else {
|
} else {
|
||||||
// Logged in user. Use MW apis to change preferences
|
// Logged in user. Use MW APIs to change preferences
|
||||||
saveOptionsWithToken( {
|
saveOptionsWithToken( {
|
||||||
action: 'options',
|
action: 'options',
|
||||||
optionname: ulsPreferences.preferenceName,
|
optionname: ulsPreferences.preferenceName,
|
||||||
|
|||||||
Reference in New Issue
Block a user