Merge "Replace Linker::link() with LinkRenderer in includes directory"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.storage.js
1 ( function ( mw ) {
2 'use strict';
3
4 // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode
5 // which throws when accessing the localStorage property itself, as opposed
6 // to the standard behaviour of throwing on getItem/setItem. (T148998)
7 var
8 localStorage = ( function () {
9 try {
10 return window.localStorage;
11 } catch ( e ) {}
12 }() ),
13 sessionStorage = ( function () {
14 try {
15 return window.sessionStorage;
16 } catch ( e ) {}
17 }() );
18
19 /**
20 * A wrapper for an HTML5 Storage interface (`localStorage` or `sessionStorage`)
21 * that is safe to call on all browsers.
22 *
23 * @class mw.SafeStorage
24 * @private
25 */
26
27 /**
28 * @ignore
29 * @param {Object|undefined} store The Storage instance to wrap around
30 */
31 function SafeStorage( store ) {
32 this.store = store;
33 }
34
35 /**
36 * Retrieve value from device storage.
37 *
38 * @param {string} key Key of item to retrieve
39 * @return {string|boolean} False when localStorage not available, otherwise string
40 */
41 SafeStorage.prototype.get = function ( key ) {
42 try {
43 return this.store.getItem( key );
44 } catch ( e ) {}
45 return false;
46 };
47
48 /**
49 * Set a value in device storage.
50 *
51 * @param {string} key Key name to store under
52 * @param {string} value Value to be stored
53 * @return {boolean} Whether the save succeeded or not
54 */
55 SafeStorage.prototype.set = function ( key, value ) {
56 try {
57 this.store.setItem( key, value );
58 return true;
59 } catch ( e ) {}
60 return false;
61 };
62
63 /**
64 * Remove a value from device storage.
65 *
66 * @param {string} key Key of item to remove
67 * @return {boolean} Whether the save succeeded or not
68 */
69 SafeStorage.prototype.remove = function ( key ) {
70 try {
71 this.store.removeItem( key );
72 return true;
73 } catch ( e ) {}
74 return false;
75 };
76
77 /**
78 * @class
79 * @singleton
80 * @extends mw.SafeStorage
81 */
82 mw.storage = new SafeStorage( localStorage );
83
84 /**
85 * @class
86 * @singleton
87 * @extends mw.SafeStorage
88 */
89 mw.storage.session = new SafeStorage( sessionStorage );
90
91 }( mediaWiki ) );