Jdlrobson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/230835

Change subject: Add simplified user settings API
......................................................................

Add simplified user settings API

Provide a standard mechanism for accessing localStorage.

See:
Id5c32bb7a662dda8d153490f7c47e972cabc1efd
I3fd44b0ae6633a7053aee247bc3c4704ba987bc8

Bug: T96155
Change-Id: Idb37352acecd745beb53aa8d77ea050851448e0d
---
M resources/Resources.php
A resources/src/mediawiki/mediawiki.settings.js
M tests/qunit/QUnitTestResources.php
A tests/qunit/suites/resources/mediawiki/mediawiki.settings.test.js
4 files changed, 130 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/35/230835/1

diff --git a/resources/Resources.php b/resources/Resources.php
index 2396128..a22ed00 100644
--- a/resources/Resources.php
+++ b/resources/Resources.php
@@ -1092,6 +1092,10 @@
                'styles' => 
'resources/src/mediawiki/mediawiki.sectionAnchor.css',
                'targets' => array( 'desktop', 'mobile' ),
        ),
+       'mediawiki.settings' => array(
+               'scripts' => 'resources/src/mediawiki/mediawiki.settings.js',
+               'targets' => array( 'desktop', 'mobile' ),
+       ),
        'mediawiki.Title' => array(
                'scripts' => 'resources/src/mediawiki/mediawiki.Title.js',
                'dependencies' => array(
diff --git a/resources/src/mediawiki/mediawiki.settings.js 
b/resources/src/mediawiki/mediawiki.settings.js
new file mode 100644
index 0000000..36066aa
--- /dev/null
+++ b/resources/src/mediawiki/mediawiki.settings.js
@@ -0,0 +1,63 @@
+( function ( mw ) {
+       'use strict';
+       var settings,
+               prefix = 'mw-setting-';
+       /**
+        * Library for storing device specific information. It should be used 
for storing simple
+        * strings and is not suitable for storing large chunks of data.
+        * @class mw.settings
+        * @singleton
+        */
+       settings = {
+               isLocalStorageSupported: false,
+               /**
+                * Retrieve value from device storage.
+                *
+                * @param {String} key of item to retrieve
+                * @returns {String|Boolean} false when localStorage not 
available, otherwise string
+                */
+               get: function ( key ) {
+                       if ( this.isLocalStorageSupported ) {
+                               return localStorage.getItem( prefix + key );
+                       } else {
+                               return false;
+                       }
+               },
+
+               /**
+                * Set a value in device storage.
+                *
+                * @param {String} key key name to store under.
+                * @param {String} value to be stored.
+                * @throws {Exception} when localStorage is not available or 
has no room.
+                */
+               set: function ( key, value ) {
+                       localStorage.setItem( prefix + key, value );
+               },
+
+               /**
+                * Remove a value from device storage.
+                *
+                * @param {String} key of item to remove.
+                * @throws {Exception} when localStorage is not available.
+                */
+               remove: function ( key ) {
+                       if ( this.isLocalStorageSupported ) {
+                               localStorage.removeItem( prefix + key );
+                       } else {
+                               throw 'LocalStorage not supported';
+                       }
+               }
+       };
+
+       mw.settings = settings;
+       // See if local storage is supported
+       try {
+               localStorage.setItem( 'localStorageTest', 'localStorageTest' );
+               localStorage.removeItem( 'localStorageTest' );
+               settings.isLocalStorageSupported = true;
+       } catch ( e ) {
+               // Already set. No body needed.
+       }
+
+}( mediaWiki ) );
diff --git a/tests/qunit/QUnitTestResources.php 
b/tests/qunit/QUnitTestResources.php
index 3608a53..39a0aa1 100644
--- a/tests/qunit/QUnitTestResources.php
+++ b/tests/qunit/QUnitTestResources.php
@@ -68,6 +68,7 @@
                        
'tests/qunit/suites/resources/mediawiki/mediawiki.jscompat.test.js',
                        
'tests/qunit/suites/resources/mediawiki/mediawiki.messagePoster.factory.test.js',
                        
'tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js',
+                       
'tests/qunit/suites/resources/mediawiki/mediawiki.settings.test.js',
                        
'tests/qunit/suites/resources/mediawiki/mediawiki.template.test.js',
                        
'tests/qunit/suites/resources/mediawiki/mediawiki.test.js',
                        
'tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js',
@@ -113,6 +114,7 @@
                        'mediawiki.jqueryMsg',
                        'mediawiki.messagePoster',
                        'mediawiki.RegExp',
+                       'mediawiki.settings',
                        'mediawiki.Title',
                        'mediawiki.toc',
                        'mediawiki.Uri',
diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.settings.test.js 
b/tests/qunit/suites/resources/mediawiki/mediawiki.settings.test.js
new file mode 100644
index 0000000..c43b51a
--- /dev/null
+++ b/tests/qunit/suites/resources/mediawiki/mediawiki.settings.test.js
@@ -0,0 +1,61 @@
+( function ( mw ) {
+       QUnit.module( 'mediawiki.settings: normal case.', {
+               setup: function () {
+                       this.sandbox.stub( mw.settings, 
'isLocalStorageSupported', true );
+                       this.spy = this.sandbox.spy( localStorage, 'setItem' );
+                       this.sandbox.stub( localStorage, 'getItem' )
+                               .withArgs( 'mw-setting-foo' ).returns( 'test' );
+               }
+       } );
+
+       QUnit.test( 'set/get with localStorage', 4, function ( assert ) {
+               mw.settings.set( 'foo', 'test' );
+               assert.strictEqual( this.spy.calledOnce, true, 'Check 
localStorage called.' );
+               assert.strictEqual( this.spy.calledWith( 'mw-setting-foo', 
'test' ), true,
+                       'Check prefixed.' );
+               assert.strictEqual( mw.settings.get( 'foo' ), 'test', 'Check 
value gets stored.' );
+               assert.strictEqual( mw.settings.get( 'bar' ), undefined, 'Unset 
values are undefined.' );
+       } );
+
+       QUnit.module( 'mediawiki.settings: localStorage does not exist', {
+               setup: function () {
+                       this.sandbox.stub( mw.settings, 
'isLocalStorageSupported', false );
+                       this.sandbox.stub( localStorage, 'setItem' ).throws();
+               }
+       } );
+
+       QUnit.test( 'set/get without localStorage', 3, function ( assert ) {
+               assert.throws(
+                       function () {
+                               mw.settings.set( 'foo', 'test' );
+                       },
+                       'When localStorage not available throw an exception.'
+               );
+               assert.throws(
+                       function () {
+                               mw.settings.remove( 'foo', 'test' );
+                       },
+                       'When localStorage not available throw an exception.'
+               );
+               assert.strictEqual( mw.settings.get( 'foo' ), false );
+       } );
+
+       QUnit.module( 'mediawiki.settings: localStorage exhausted', {
+               setup: function () {
+                       this.sandbox.stub( mw.settings, 
'isLocalStorageSupported', true );
+                       this.sandbox.stub( localStorage, 'setItem' ).throws();
+                       this.sandbox.stub( localStorage, 'getItem' );
+               }
+       } );
+
+       QUnit.test( 'set/get without localStorage', 2, function ( assert ) {
+               assert.throws(
+                       function () {
+                               mw.settings.set( 'foo', 'test' );
+                       },
+                       'When localStorage not available throw an exception.'
+               );
+               assert.strictEqual( mw.settings.get( 'foo' ), undefined, 'No 
value registered.' );
+       } );
+
+}( mediaWiki ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/230835
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idb37352acecd745beb53aa8d77ea050851448e0d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to