]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - resources/src/mediawiki/mediawiki.storage.js
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.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          * @param {Object|undefined} store The Storage instance to wrap around
26          */
27         function SafeStorage( store ) {
28                 this.store = store;
29         }
30
31         /**
32          * Retrieve value from device storage.
33          *
34          * @param {string} key Key of item to retrieve
35          * @return {string|null|boolean} String value, null if no value exists, or false
36          *  if localStorage is not available.
37          */
38         SafeStorage.prototype.get = function ( key ) {
39                 try {
40                         return this.store.getItem( key );
41                 } catch ( e ) {}
42                 return false;
43         };
44
45         /**
46          * Set a value in device storage.
47          *
48          * @param {string} key Key name to store under
49          * @param {string} value Value to be stored
50          * @return {boolean} Whether the save succeeded or not
51          */
52         SafeStorage.prototype.set = function ( key, value ) {
53                 try {
54                         this.store.setItem( key, value );
55                         return true;
56                 } catch ( e ) {}
57                 return false;
58         };
59
60         /**
61          * Remove a value from device storage.
62          *
63          * @param {string} key Key of item to remove
64          * @return {boolean} Whether the save succeeded or not
65          */
66         SafeStorage.prototype.remove = function ( key ) {
67                 try {
68                         this.store.removeItem( key );
69                         return true;
70                 } catch ( e ) {}
71                 return false;
72         };
73
74         /**
75          * A wrapper for the HTML5 `localStorage` interface
76          * that is safe to call on all browsers.
77          *
78          * @class
79          * @singleton
80          * @extends mw.SafeStorage
81          */
82         mw.storage = new SafeStorage( localStorage );
83
84         /**
85          * A wrapper for the HTML5 `sessionStorage` interface
86          * that is safe to call on all browsers.
87          *
88          * @class
89          * @singleton
90          * @extends mw.SafeStorage
91          */
92         mw.storage.session = new SafeStorage( sessionStorage );
93
94 }( mediaWiki ) );