summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki/mediawiki.storage.js
blob: 39583926bbf32ed4205746090883db57bd96ee9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
( function ( mw ) {
	'use strict';

	/**
	 * 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.storage
	 * @singleton
	 */
	mw.storage = {

		localStorage: window.localStorage,

		/**
		 * Retrieve value from device storage.
		 *
		 * @param {string} key Key of item to retrieve
		 * @return {string|boolean} False when localStorage not available, otherwise string
		 */
		get: function ( key ) {
			try {
				return mw.storage.localStorage.getItem( key );
			} catch ( e ) {}
			return false;
		},

		/**
		  * Set a value in device storage.
		  *
		  * @param {string} key Key name to store under
		  * @param {string} value Value to be stored
		  * @returns {boolean} Whether the save succeeded or not
		  */
		set: function ( key, value ) {
			try {
				mw.storage.localStorage.setItem( key, value );
				return true;
			} catch ( e ) {}
			return false;
		},

		/**
		  * Remove a value from device storage.
		  *
		  * @param {string} key Key of item to remove
		  * @returns {boolean} Whether the save succeeded or not
		  */
		remove: function ( key ) {
			try {
				mw.storage.localStorage.removeItem( key );
				return true;
			} catch ( e ) {}
			return false;
		}
	};

}( mediaWiki ) );