From 3ac9853220fcc0921788fb5dc656041b163d987b Mon Sep 17 00:00:00 2001 From: Nikerabbit Date: Tue, 19 Jan 2021 07:59:03 +0000 Subject: [PATCH] Simplify ext.uls.preferences module This is updated version of 61f1a9863a1f47cc953e9ca34a43562d5de1afc2 which was reverted due to compatability issues which are no longer present. Change-Id: I05333ddc288171b6d867dce8a1d3efc1e1450e0a --- extension.json | 3 +- resources/js/ext.uls.preferences.js | 86 ++++++----------------------- 2 files changed, 18 insertions(+), 71 deletions(-) diff --git a/extension.json b/extension.json index 21d8e171..0df287bb 100644 --- a/extension.json +++ b/extension.json @@ -295,8 +295,9 @@ ], "scripts": "js/ext.uls.preferences.js", "dependencies": [ + "mediawiki.api", "mediawiki.user", - "mediawiki.api" + "mediawiki.storage" ], "localBasePath": "resources", "remoteExtPath": "UniversalLanguageSelector/resources" diff --git a/resources/js/ext.uls.preferences.js b/resources/js/ext.uls.preferences.js index 136e5657..41f08108 100644 --- a/resources/js/ext.uls.preferences.js +++ b/resources/js/ext.uls.preferences.js @@ -21,51 +21,11 @@ ( function () { 'use strict'; - 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; - } - }; - } + var ULSPreferences, instance; ULSPreferences = function () { + // This violates coding conventions for localstorage: + // https://www.mediawiki.org/wiki/Manual:Coding_conventions/JavaScript#Keys this.preferenceName = 'uls-preferences'; this.username = mw.user.getName(); this.isAnon = mw.user.isAnon(); @@ -74,28 +34,19 @@ }; ULSPreferences.prototype = { - /** - * Initialize - */ init: function () { - var options; - if ( this.isAnon ) { - this.preferences = preferenceStore().get( this.preferenceName ); + this.preferences = mw.storage.getObject( this.preferenceName ); } else { - options = mw.user.options.get( this.preferenceName ); - if ( !options ) { - options = '{}'; - } - // Try to parse JSON try { - this.preferences = JSON.parse( options ); + this.preferences = JSON.parse( mw.user.options.get( this.preferenceName ) ); } catch ( e ) { - this.preferences = {}; } } - this.preferences = this.preferences || {}; + if ( !$.isPlainObject( this.preferences ) ) { + this.preferences = {}; + } }, /** @@ -124,34 +75,29 @@ * @param {Function} callback */ save: function ( callback ) { - var ulsPreferences = this; + var self = this; callback = callback || function () {}; if ( this.isAnon ) { // Anonymous user. Save preferences in local storage - preferenceStore().set( this.preferenceName, this.preferences ); + mw.storage.setObject( this.preferenceName, this.preferences ); callback.call( this, true ); } else { // Logged in user. Use MW APIs to change preferences new mw.Api().saveOption( - ulsPreferences.preferenceName, - JSON.stringify( ulsPreferences.preferences ) + this.preferenceName, + JSON.stringify( this.preferences ) ).done( function () { - callback.call( this, true ); + callback.call( self, true ); } ).fail( function () { - callback.call( this, false ); + callback.call( self, false ); } ); } } }; module.exports = function () { - var data = $( document.body ).data( 'preferences' ); - - if ( !data ) { - $( document.body ).data( 'preferences', ( data = new ULSPreferences() ) ); - } - return data; + instance = instance || new ULSPreferences(); + return instance; }; - }() );