Use localStorage, fallback to cookie, remove jquery.jStorage RL dependency

As per comments in I22613d9f6846db5456beb532cec47396fccda8ae

Change-Id: I09b2bd52972d9aef20dd469c556631e0299bc407
This commit is contained in:
Santhosh Thottingal
2014-02-04 16:44:20 +05:30
committed by Santhosh
parent 27771fdeaf
commit e21c221e2b
2 changed files with 56 additions and 5 deletions

View File

@@ -140,7 +140,6 @@ $wgResourceModules['ext.uls.preferences'] = array(
'dependencies' => array( 'dependencies' => array(
'mediawiki.user', 'mediawiki.user',
'mediawiki.api', 'mediawiki.api',
'jquery.jStorage',
), ),
) + $resourcePaths; ) + $resourcePaths;

View File

@@ -101,6 +101,58 @@
} ); } );
} }
/**
* Wrapper for localStorage, falls back to cookie
* when localStorage not supported by browser.
*/
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 );
}
// Set the store
try {
localStorage.setItem( key, value );
} catch ( e ) { // Use cookie
$.cookie( key, value, { path: '/' } );
}
},
/*
* Returns the value of the given key
* @param {string} key
* @retun {Object} value of the key
*/
get: function ( key ) {
var data;
// No value supplied, return value
try {
data = localStorage.getItem( key );
} catch ( e ) { // Use cookie
data = $.cookie( key );
}
// Try to parse JSON
try {
data = JSON.parse( data );
} catch ( e ) {
data = data;
}
return data;
}
};
}
ULSPreferences = function () { ULSPreferences = function () {
this.preferenceName = 'uls-preferences'; this.preferenceName = 'uls-preferences';
this.username = mw.user.getName(); this.username = mw.user.getName();
@@ -115,10 +167,10 @@
*/ */
init: function () { init: function () {
if ( this.isAnon ) { if ( this.isAnon ) {
this.preferences = $.jStorage.get( this.preferenceName ); this.preferences = preferenceStore().get( this.preferenceName );
} else { } else {
var options = mw.user.options.get( this.preferenceName ); var options = mw.user.options.get( this.preferenceName );
this.preferences = $.parseJSON( options ); this.preferences = JSON.parse( options );
} }
this.preferences = this.preferences || {}; this.preferences = this.preferences || {};
}, },
@@ -153,7 +205,7 @@
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 ); preferenceStore().set( this.preferenceName, this.preferences );
callback.call( this, true ); callback.call( this, true );
} else { } else {
@@ -161,7 +213,7 @@
saveOptionsWithToken( { saveOptionsWithToken( {
action: 'options', action: 'options',
optionname: ulsPreferences.preferenceName, optionname: ulsPreferences.preferenceName,
optionvalue: $.toJSON( ulsPreferences.preferences ) optionvalue: JSON.stringify( ulsPreferences.preferences )
}, function () { }, function () {
callback.call( this, true ); callback.call( this, true );
}, function () { }, function () {