Revert "Simplify ext.uls.preferences module"

This reverts commit 61f1a9863a.

Reason: mw.storage.getObject was introduced in MW 1.34. Need to
support MW 1.33 for MLEB 2020.04 release.

Change-Id: If9cd8d1f9e1ab544eba280c8fa7b36db91b64573
This commit is contained in:
Abijeet Patro
2020-04-16 12:26:10 +00:00
committed by jenkins-bot
parent 19baf4fa22
commit 56c38a5515
2 changed files with 70 additions and 18 deletions

View File

@@ -323,9 +323,8 @@
"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.storage" "mediawiki.api"
], ],
"localBasePath": "resources", "localBasePath": "resources",
"remoteExtPath": "UniversalLanguageSelector/resources" "remoteExtPath": "UniversalLanguageSelector/resources"

View File

@@ -21,11 +21,51 @@
( function () { ( function () {
'use strict'; 'use strict';
var ULSPreferences, instance; var ULSPreferences;
/**
* 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();
@@ -34,19 +74,28 @@
}; };
ULSPreferences.prototype = { ULSPreferences.prototype = {
/**
* Initialize
*/
init: function () { init: function () {
var options;
if ( this.isAnon ) { if ( this.isAnon ) {
this.preferences = mw.storage.getObject( this.preferenceName ); this.preferences = preferenceStore().get( this.preferenceName );
} else { } else {
options = mw.user.options.get( this.preferenceName );
if ( !options ) {
options = '{}';
}
// Try to parse JSON
try { try {
this.preferences = JSON.parse( mw.user.options.get( this.preferenceName ) ); this.preferences = JSON.parse( options );
} catch ( e ) { } catch ( e ) {
this.preferences = {};
} }
} }
if ( !$.isPlainObject( this.preferences ) ) { this.preferences = this.preferences || {};
this.preferences = {};
}
}, },
/** /**
@@ -75,22 +124,22 @@
* @param {Function} callback * @param {Function} callback
*/ */
save: function ( callback ) { save: function ( callback ) {
var self = this; var ulsPreferences = 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
mw.storage.setObject( this.preferenceName, this.preferences ); preferenceStore().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
new mw.Api().saveOption( new mw.Api().saveOption(
this.preferenceName, ulsPreferences.preferenceName,
JSON.stringify( this.preferences ) JSON.stringify( ulsPreferences.preferences )
).done( function () { ).done( function () {
callback.call( self, true ); callback.call( this, true );
} ).fail( function () { } ).fail( function () {
callback.call( self, false ); callback.call( this, false );
} ); } );
} }
} }
@@ -98,8 +147,12 @@
mw.uls = mw.uls || {}; mw.uls = mw.uls || {};
mw.uls.preferences = function () { mw.uls.preferences = function () {
instance = instance || new ULSPreferences(); var data = $( document.body ).data( 'preferences' );
return instance;
if ( !data ) {
$( document.body ).data( 'preferences', ( data = new ULSPreferences() ) );
}
return data;
}; };
}() ); }() );