X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki.storage.js;h=3405ba7747bb8a39127e544a327865a7529602af;hb=a2c5e9eadfd356bfc753e48f5be83ebcd7ce4209;hp=84e146a73f87045917ddaa39c9b5200d696d5e46;hpb=b37772a99128afe1699a60718ebe2a6f8469aa8f;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki.storage.js b/resources/src/mediawiki.storage.js index 84e146a73f..3405ba7747 100644 --- a/resources/src/mediawiki.storage.js +++ b/resources/src/mediawiki.storage.js @@ -1,4 +1,4 @@ -( function ( mw ) { +( function () { 'use strict'; // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode @@ -33,7 +33,7 @@ * * @param {string} key Key of item to retrieve * @return {string|null|boolean} String value, null if no value exists, or false - * if localStorage is not available. + * if storage is not available. */ SafeStorage.prototype.get = function ( key ) { try { @@ -47,7 +47,7 @@ * * @param {string} key Key name to store under * @param {string} value Value to be stored - * @return {boolean} Whether the save succeeded or not + * @return {boolean} The value was set */ SafeStorage.prototype.set = function ( key, value ) { try { @@ -61,7 +61,7 @@ * Remove a value from device storage. * * @param {string} key Key of item to remove - * @return {boolean} Whether the save succeeded or not + * @return {boolean} Whether the key was removed */ SafeStorage.prototype.remove = function ( key ) { try { @@ -71,6 +71,43 @@ return false; }; + /** + * Retrieve JSON object from device storage. + * + * @param {string} key Key of item to retrieve + * @return {Object|null|boolean} Object, null if no value exists or value + * is not JSON-parseable, or false if storage is not available. + */ + SafeStorage.prototype.getObject = function ( key ) { + var json = this.get( key ); + + if ( json === false ) { + return false; + } + + try { + return JSON.parse( json ); + } catch ( e ) {} + + return null; + }; + + /** + * Set an object value in device storage by JSON encoding + * + * @param {string} key Key name to store under + * @param {Object} value Object value to be stored + * @return {boolean} The value was set + */ + SafeStorage.prototype.setObject = function ( key, value ) { + var json; + try { + json = JSON.stringify( value ); + return this.set( key, json ); + } catch ( e ) {} + return false; + }; + /** * A wrapper for the HTML5 `localStorage` interface * that is safe to call on all browsers. @@ -91,4 +128,4 @@ */ mw.storage.session = new SafeStorage( sessionStorage ); -}( mediaWiki ) ); +}() );