From e21c221e2bc5a8ea0e19aff9e0ca466628571f0d Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Tue, 4 Feb 2014 16:44:20 +0530 Subject: [PATCH] Use localStorage, fallback to cookie, remove jquery.jStorage RL dependency As per comments in I22613d9f6846db5456beb532cec47396fccda8ae Change-Id: I09b2bd52972d9aef20dd469c556631e0299bc407 --- Resources.php | 1 - resources/js/ext.uls.preferences.js | 60 +++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Resources.php b/Resources.php index 80400c34..bf2a9984 100644 --- a/Resources.php +++ b/Resources.php @@ -140,7 +140,6 @@ $wgResourceModules['ext.uls.preferences'] = array( 'dependencies' => array( 'mediawiki.user', 'mediawiki.api', - 'jquery.jStorage', ), ) + $resourcePaths; diff --git a/resources/js/ext.uls.preferences.js b/resources/js/ext.uls.preferences.js index 11e11716..71be07c7 100644 --- a/resources/js/ext.uls.preferences.js +++ b/resources/js/ext.uls.preferences.js @@ -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 () { this.preferenceName = 'uls-preferences'; this.username = mw.user.getName(); @@ -115,10 +167,10 @@ */ init: function () { if ( this.isAnon ) { - this.preferences = $.jStorage.get( this.preferenceName ); + this.preferences = preferenceStore().get( this.preferenceName ); } else { var options = mw.user.options.get( this.preferenceName ); - this.preferences = $.parseJSON( options ); + this.preferences = JSON.parse( options ); } this.preferences = this.preferences || {}; }, @@ -153,7 +205,7 @@ callback = callback || $.noop; if ( this.isAnon ) { // Anonymous user. Save preferences in local storage - $.jStorage.set( this.preferenceName, this.preferences ); + preferenceStore().set( this.preferenceName, this.preferences ); callback.call( this, true ); } else { @@ -161,7 +213,7 @@ saveOptionsWithToken( { action: 'options', optionname: ulsPreferences.preferenceName, - optionvalue: $.toJSON( ulsPreferences.preferences ) + optionvalue: JSON.stringify( ulsPreferences.preferences ) }, function () { callback.call( this, true ); }, function () {