diff options
Diffstat (limited to 'MLEB/UniversalLanguageSelector/resources/js/ext.uls.preferences.js')
-rw-r--r-- | MLEB/UniversalLanguageSelector/resources/js/ext.uls.preferences.js | 69 |
1 files changed, 26 insertions, 43 deletions
diff --git a/MLEB/UniversalLanguageSelector/resources/js/ext.uls.preferences.js b/MLEB/UniversalLanguageSelector/resources/js/ext.uls.preferences.js index 36f96d2c..0569fab4 100644 --- a/MLEB/UniversalLanguageSelector/resources/js/ext.uls.preferences.js +++ b/MLEB/UniversalLanguageSelector/resources/js/ext.uls.preferences.js @@ -1,6 +1,6 @@ -/** +/*! * ULS preferences system for MediaWiki. - * Local storage for anonymous users, preferences for logged in users. + * Localstorage for anonymous users, preferences for logged in users. * * Copyright (C) 2012 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris, * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other @@ -17,7 +17,8 @@ * @licence GNU General Public Licence 2.0 or later * @licence MIT License */ -( function ( $, mw ) { + +( function () { 'use strict'; var ULSPreferences; @@ -25,6 +26,8 @@ /** * Wrapper for localStorage, falls back to cookie * when localStorage not supported by browser. + * + * @return {Object} */ function preferenceStore() { @@ -40,44 +43,22 @@ if ( typeof value === 'object' ) { value = JSON.stringify( value ); } - // Set the store + try { localStorage.setItem( key, value ); - } catch ( e ) { // Use cookie - $.cookie( key, value, { path: '/' } ); - } + } catch ( e ) {} }, /* * Returns the value of the given key * @param {string} key - * @retun {Object} value of the key + * @return {Object} value of the key */ get: function ( key ) { var data; - // No value supplied, return value - try { - data = localStorage.getItem( key ); - if ( !data ) { - // Try to restore the old preferences, if any, if possible. - try { - data = JSON.parse( localStorage.getItem( 'jStorage' ) )['uls-preferences']; - // And try to remove it. - localStorage.removeItem( 'jStorage' ); - } catch ( e ) { - // Don't bother about it. - } - } - } catch ( e ) { // Use cookie - data = $.cookie( key ); - } - - // Try to parse JSON try { - data = JSON.parse( data ); - } catch ( e ) { - data = data; - } + data = JSON.parse( localStorage.getItem( key ) ); + } catch ( e ) {} return data; } @@ -97,10 +78,12 @@ * Initialize */ init: function () { + var options; + if ( this.isAnon ) { this.preferences = preferenceStore().get( this.preferenceName ); } else { - var options = mw.user.options.get( this.preferenceName ); + options = mw.user.options.get( this.preferenceName ); if ( !options ) { options = '{}'; } @@ -118,26 +101,27 @@ /** * Set the preference * - * @param {String} key - * @param value + * @param {string} key + * @param {mixed} value */ set: function ( key, value ) { - this.preferences[key] = value; + this.preferences[ key ] = value; }, /** * Get a preference value for the given preference name * - * @param key + * @param {string} key + * @return {Mixed} */ get: function ( key ) { - return this.preferences[key]; + return this.preferences[ key ]; }, /** * Save the preferences * - * @param callback + * @param {Function} callback */ save: function ( callback ) { var ulsPreferences = this; @@ -149,11 +133,10 @@ callback.call( this, true ); } else { // Logged in user. Use MW APIs to change preferences - new mw.Api().postWithToken( 'options', { - action: 'options', - optionname: ulsPreferences.preferenceName, - optionvalue: JSON.stringify( ulsPreferences.preferences ) - } ).done( function () { + new mw.Api().saveOption( + ulsPreferences.preferenceName, + JSON.stringify( ulsPreferences.preferences ) + ).done( function () { callback.call( this, true ); } ).fail( function () { callback.call( this, false ); @@ -172,4 +155,4 @@ return data; }; -}( jQuery, mediaWiki ) ); +}() ); |