Merge "Tiny clean up of Parser::doQuotes()"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.storage.js
1 ( function ( mw ) {
2 'use strict';
3 var storage;
4
5 /**
6 * Library for storing device specific information. It should be used for storing simple
7 * strings and is not suitable for storing large chunks of data.
8 * @class mw.storage
9 * @singleton
10 */
11 storage = {
12 isLocalStorageSupported: false,
13 /**
14 * Retrieve value from device storage.
15 *
16 * @param {String} key of item to retrieve
17 * @returns {String|Boolean} false when localStorage not available, otherwise string
18 */
19 get: function ( key ) {
20 if ( this.isLocalStorageSupported ) {
21 return localStorage.getItem( key );
22 } else {
23 return false;
24 }
25 },
26
27 /**
28 * Set a value in device storage.
29 *
30 * @param {String} key key name to store under.
31 * @param {String} value to be stored.
32 * @returns {Boolean} whether the save succeeded or not.
33 */
34 set: function ( key, value ) {
35 try {
36 localStorage.setItem( key, value );
37 return true;
38 } catch ( e ) {
39 return false;
40 }
41 },
42
43 /**
44 * Remove a value from device storage.
45 *
46 * @param {String} key of item to remove.
47 * @returns {Boolean} whether the save succeeded or not.
48 */
49 remove: function ( key ) {
50 if ( this.isLocalStorageSupported ) {
51 localStorage.removeItem( key );
52 return true;
53 } else {
54 return false;
55 }
56 }
57 };
58
59 mw.storage = storage;
60 // See if local storage is supported
61 try {
62 localStorage.setItem( 'localStorageTest', 'localStorageTest' );
63 localStorage.removeItem( 'localStorageTest' );
64 storage.isLocalStorageSupported = true;
65 } catch ( e ) {
66 // Already set. No body needed.
67 }
68
69 }( mediaWiki ) );