Simplify ext.uls.preferences module
Related to T246370 Change-Id: Iedef79c75ef0ab9c1baff5a0e6f1953c3c56ac91
This commit is contained in:
committed by
jenkins-bot
parent
31f23edd14
commit
61f1a9863a
@@ -323,8 +323,9 @@
|
|||||||
"ext.uls.preferences": {
|
"ext.uls.preferences": {
|
||||||
"scripts": "js/ext.uls.preferences.js",
|
"scripts": "js/ext.uls.preferences.js",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
|
"mediawiki.api",
|
||||||
"mediawiki.user",
|
"mediawiki.user",
|
||||||
"mediawiki.api"
|
"mediawiki.storage"
|
||||||
],
|
],
|
||||||
"localBasePath": "resources",
|
"localBasePath": "resources",
|
||||||
"remoteExtPath": "UniversalLanguageSelector/resources"
|
"remoteExtPath": "UniversalLanguageSelector/resources"
|
||||||
|
|||||||
@@ -21,51 +21,11 @@
|
|||||||
( function () {
|
( function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ULSPreferences;
|
var ULSPreferences, instance;
|
||||||
|
|
||||||
/**
|
|
||||||
* Wrapper for localStorage, falls back to cookie
|
|
||||||
* when localStorage not supported by browser.
|
|
||||||
*
|
|
||||||
* @return {Object}
|
|
||||||
*/
|
|
||||||
function preferenceStore() {
|
|
||||||
|
|
||||||
// If value is detected, set new or modify store
|
|
||||||
return {
|
|
||||||
/*
|
|
||||||
* Set the value to the given key
|
|
||||||
* @param {string} key
|
|
||||||
* @param {Object} value value to be set
|
|
||||||
*/
|
|
||||||
set: function ( key, value ) {
|
|
||||||
// Convert object values to JSON
|
|
||||||
if ( typeof value === 'object' ) {
|
|
||||||
value = JSON.stringify( value );
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
localStorage.setItem( key, value );
|
|
||||||
} catch ( e ) {}
|
|
||||||
},
|
|
||||||
/*
|
|
||||||
* Returns the value of the given key
|
|
||||||
* @param {string} key
|
|
||||||
* @return {Object} value of the key
|
|
||||||
*/
|
|
||||||
get: function ( key ) {
|
|
||||||
var data;
|
|
||||||
|
|
||||||
try {
|
|
||||||
data = JSON.parse( localStorage.getItem( key ) );
|
|
||||||
} catch ( e ) {}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
ULSPreferences = function () {
|
ULSPreferences = function () {
|
||||||
|
// This violates coding conventions for localstorage:
|
||||||
|
// https://www.mediawiki.org/wiki/Manual:Coding_conventions/JavaScript#Keys
|
||||||
this.preferenceName = 'uls-preferences';
|
this.preferenceName = 'uls-preferences';
|
||||||
this.username = mw.user.getName();
|
this.username = mw.user.getName();
|
||||||
this.isAnon = mw.user.isAnon();
|
this.isAnon = mw.user.isAnon();
|
||||||
@@ -74,28 +34,19 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
ULSPreferences.prototype = {
|
ULSPreferences.prototype = {
|
||||||
/**
|
|
||||||
* Initialize
|
|
||||||
*/
|
|
||||||
init: function () {
|
init: function () {
|
||||||
var options;
|
|
||||||
|
|
||||||
if ( this.isAnon ) {
|
if ( this.isAnon ) {
|
||||||
this.preferences = preferenceStore().get( this.preferenceName );
|
this.preferences = mw.storage.getObject( this.preferenceName );
|
||||||
} else {
|
} else {
|
||||||
options = mw.user.options.get( this.preferenceName );
|
|
||||||
if ( !options ) {
|
|
||||||
options = '{}';
|
|
||||||
}
|
|
||||||
// Try to parse JSON
|
|
||||||
try {
|
try {
|
||||||
this.preferences = JSON.parse( options );
|
this.preferences = JSON.parse( mw.user.options.get( this.preferenceName ) );
|
||||||
} catch ( e ) {
|
} catch ( e ) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !$.isPlainObject( this.preferences ) ) {
|
||||||
this.preferences = {};
|
this.preferences = {};
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
this.preferences = this.preferences || {};
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -124,22 +75,22 @@
|
|||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
save: function ( callback ) {
|
save: function ( callback ) {
|
||||||
var ulsPreferences = this;
|
var self = this;
|
||||||
|
|
||||||
callback = callback || function () {};
|
callback = callback || function () {};
|
||||||
if ( this.isAnon ) {
|
if ( this.isAnon ) {
|
||||||
// Anonymous user. Save preferences in local storage
|
// Anonymous user. Save preferences in local storage
|
||||||
preferenceStore().set( this.preferenceName, this.preferences );
|
mw.storage.setObject( 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
|
||||||
new mw.Api().saveOption(
|
new mw.Api().saveOption(
|
||||||
ulsPreferences.preferenceName,
|
this.preferenceName,
|
||||||
JSON.stringify( ulsPreferences.preferences )
|
JSON.stringify( this.preferences )
|
||||||
).done( function () {
|
).done( function () {
|
||||||
callback.call( this, true );
|
callback.call( self, true );
|
||||||
} ).fail( function () {
|
} ).fail( function () {
|
||||||
callback.call( this, false );
|
callback.call( self, false );
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,12 +98,8 @@
|
|||||||
|
|
||||||
mw.uls = mw.uls || {};
|
mw.uls = mw.uls || {};
|
||||||
mw.uls.preferences = function () {
|
mw.uls.preferences = function () {
|
||||||
var data = $( document.body ).data( 'preferences' );
|
instance = instance || new ULSPreferences();
|
||||||
|
return instance;
|
||||||
if ( !data ) {
|
|
||||||
$( document.body ).data( 'preferences', ( data = new ULSPreferences() ) );
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}() );
|
}() );
|
||||||
|
|||||||
Reference in New Issue
Block a user