Browse Source

feat(JitsiLocalStorage): Implement localStorage wrapper

master
hristoterezov 8 years ago
parent
commit
7e2fe30472

+ 3
- 2
connection.js View File

1
 /* global APP, JitsiMeetJS, config */
1
 /* global APP, JitsiMeetJS, config */
2
 import AuthHandler from './modules/UI/authentication/AuthHandler';
2
 import AuthHandler from './modules/UI/authentication/AuthHandler';
3
+import jitsiLocalStorage from './modules/util/JitsiLocalStorage';
3
 
4
 
4
 const ConnectionEvents = JitsiMeetJS.events.connection;
5
 const ConnectionEvents = JitsiMeetJS.events.connection;
5
 const ConnectionErrors = JitsiMeetJS.errors.connection;
6
 const ConnectionErrors = JitsiMeetJS.errors.connection;
107
 export function openConnection({id, password, retry, roomName}) {
108
 export function openConnection({id, password, retry, roomName}) {
108
 
109
 
109
     let usernameOverride
110
     let usernameOverride
110
-        = window.localStorage.getItem("xmpp_username_override");
111
+        = jitsiLocalStorage.getItem("xmpp_username_override");
111
     let passwordOverride
112
     let passwordOverride
112
-        = window.localStorage.getItem("xmpp_password_override");
113
+        = jitsiLocalStorage.getItem("xmpp_password_override");
113
 
114
 
114
     if (usernameOverride && usernameOverride.length > 0) {
115
     if (usernameOverride && usernameOverride.length > 0) {
115
         id = usernameOverride;
116
         id = usernameOverride;

+ 4
- 3
modules/UI/UI.js View File

31
 var JitsiPopover = require("./util/JitsiPopover");
31
 var JitsiPopover = require("./util/JitsiPopover");
32
 var Feedback = require("./feedback/Feedback");
32
 var Feedback = require("./feedback/Feedback");
33
 import FollowMe from "../FollowMe";
33
 import FollowMe from "../FollowMe";
34
+import jitsiLocalStorage from '../util/JitsiLocalStorage';
34
 
35
 
35
 var eventEmitter = new EventEmitter();
36
 var eventEmitter = new EventEmitter();
36
 UI.eventEmitter = eventEmitter;
37
 UI.eventEmitter = eventEmitter;
1239
     }
1240
     }
1240
 
1241
 
1241
     if (showDoNotShowWarning) {
1242
     if (showDoNotShowWarning) {
1242
-        if (window.localStorage[localStoragePropName] === "true") {
1243
+        if (jitsiLocalStorage.getItem(localStoragePropName) === "true") {
1243
             return;
1244
             return;
1244
         }
1245
         }
1245
     }
1246
     }
1308
                 let input = form.find("#doNotShowWarningAgain");
1309
                 let input = form.find("#doNotShowWarningAgain");
1309
 
1310
 
1310
                 if (input.length) {
1311
                 if (input.length) {
1311
-                    window.localStorage[localStoragePropName] =
1312
-                        input.prop("checked");
1312
+                    jitsiLocalStorage.setItem(localStoragePropName,
1313
+                        input.prop("checked"));
1313
                 }
1314
                 }
1314
             }
1315
             }
1315
         },
1316
         },

+ 9
- 11
modules/UI/videolayout/RemoteVideo.js View File

6
 import UIUtils from "../util/UIUtil";
6
 import UIUtils from "../util/UIUtil";
7
 import UIEvents from '../../../service/UI/UIEvents';
7
 import UIEvents from '../../../service/UI/UIEvents';
8
 import JitsiPopover from "../util/JitsiPopover";
8
 import JitsiPopover from "../util/JitsiPopover";
9
+import jitsiLocalStorage from '../../util/JitsiLocalStorage';
9
 
10
 
10
 const MUTED_DIALOG_BUTTON_VALUES = {
11
 const MUTED_DIALOG_BUTTON_VALUES = {
11
     cancel: 0,
12
     cancel: 0,
654
 RemoteVideo.showMuteParticipantDialog = function () {
655
 RemoteVideo.showMuteParticipantDialog = function () {
655
     //FIXME: don't show again checkbox is implemented very dirty. we should add
656
     //FIXME: don't show again checkbox is implemented very dirty. we should add
656
     // this functionality to MessageHandler class.
657
     // this functionality to MessageHandler class.
657
-    if (window.localStorage
658
-        && window.localStorage.getItem(
658
+    if (jitsiLocalStorage.getItem(
659
             "dontShowMuteParticipantDialog") === "true") {
659
             "dontShowMuteParticipantDialog") === "true") {
660
         return Promise.resolve(MUTED_DIALOG_BUTTON_VALUES.muted);
660
         return Promise.resolve(MUTED_DIALOG_BUTTON_VALUES.muted);
661
     }
661
     }
672
             msgString,
672
             msgString,
673
             leftButtonKey: 'dialog.muteParticipantButton',
673
             leftButtonKey: 'dialog.muteParticipantButton',
674
             submitFunction: () => {
674
             submitFunction: () => {
675
-                if(window.localStorage) {
676
-                    let form  = $.prompt.getPrompt();
677
-                    if (form) {
678
-                        let input = form.find("#doNotShowMessageAgain");
679
-                        if (input.length) {
680
-                            window.localStorage.setItem(
681
-                                "dontShowMuteParticipantDialog",
682
-                                input.prop("checked"));
683
-                        }
675
+                let form  = $.prompt.getPrompt();
676
+                if (form) {
677
+                    let input = form.find("#doNotShowMessageAgain");
678
+                    if (input.length) {
679
+                        jitsiLocalStorage.setItem(
680
+                            "dontShowMuteParticipantDialog",
681
+                            input.prop("checked"));
684
                     }
682
                     }
685
                 }
683
                 }
686
                 resolve(MUTED_DIALOG_BUTTON_VALUES.muted);
684
                 resolve(MUTED_DIALOG_BUTTON_VALUES.muted);

+ 46
- 66
modules/settings/Settings.js View File

1
 /* global JitsiMeetJS */
1
 /* global JitsiMeetJS */
2
-
3
 import UIUtil from '../UI/util/UIUtil';
2
 import UIUtil from '../UI/util/UIUtil';
4
-
5
-let email = '';
6
-let avatarId = '';
7
-let displayName = '';
8
-let language = null;
9
-let cameraDeviceId = '';
10
-let micDeviceId = '';
11
-let welcomePageDisabled = false;
12
-let localFlipX = null;
13
-let avatarUrl = '';
14
-
15
-function supportsLocalStorage() {
16
-    try {
17
-        return 'localStorage' in window && window.localStorage !== null;
18
-    } catch (e) {
19
-        console.log("localstorage is not supported");
20
-        return false;
21
-    }
22
-}
23
-
3
+import jitsiLocalStorage from '../util/JitsiLocalStorage';
24
 
4
 
25
 function generateUniqueId() {
5
 function generateUniqueId() {
26
     function _p8() {
6
     function _p8() {
29
     return _p8() + _p8() + _p8() + _p8();
9
     return _p8() + _p8() + _p8() + _p8();
30
 }
10
 }
31
 
11
 
32
-if (supportsLocalStorage()) {
33
-    if (!window.localStorage.jitsiMeetId) {
34
-        window.localStorage.jitsiMeetId = generateUniqueId();
35
-        console.log("generated id", window.localStorage.jitsiMeetId);
36
-    }
12
+if (!jitsiLocalStorage.getItem("jitsiMeetId")) {
13
+    jitsiLocalStorage.setItem("jitsiMeetId",generateUniqueId());
14
+    console.log("generated id", jitsiLocalStorage.getItem("jitsiMeetId"));
15
+}
37
 
16
 
38
-    email = UIUtil.unescapeHtml(window.localStorage.email || '');
39
-    avatarId = UIUtil.unescapeHtml(window.localStorage.avatarId || '');
40
-    if (!avatarId) {
41
-        // if there is no avatar id, we generate a unique one and use it forever
42
-        avatarId = generateUniqueId();
43
-        window.localStorage.avatarId = avatarId;
44
-    }
17
+let avatarUrl = '';
45
 
18
 
46
-    localFlipX = JSON.parse(window.localStorage.localFlipX || true);
47
-    displayName = UIUtil.unescapeHtml(window.localStorage.displayname || '');
48
-    language = window.localStorage.language;
49
-    cameraDeviceId = window.localStorage.cameraDeviceId || '';
50
-    micDeviceId = window.localStorage.micDeviceId || '';
51
-    welcomePageDisabled = JSON.parse(
52
-        window.localStorage.welcomePageDisabled || false
53
-    );
54
-
55
-    // Currently audio output device change is supported only in Chrome and
56
-    // default output always has 'default' device ID
57
-    var audioOutputDeviceId = window.localStorage.audioOutputDeviceId
58
-        || 'default';
59
-
60
-    if (audioOutputDeviceId !==
61
-        JitsiMeetJS.mediaDevices.getAudioOutputDevice()) {
62
-        JitsiMeetJS.mediaDevices.setAudioOutputDevice(audioOutputDeviceId)
63
-            .catch((ex) => {
64
-                console.warn('Failed to set audio output device from local ' +
65
-                    'storage. Default audio output device will be used' +
66
-                    'instead.', ex);
67
-            });
68
-    }
69
-} else {
70
-    console.log("local storage is not supported");
19
+let email = UIUtil.unescapeHtml(jitsiLocalStorage.getItem("email") || '');
20
+let avatarId = UIUtil.unescapeHtml(jitsiLocalStorage.getItem("avatarId") || '');
21
+if (!avatarId) {
22
+    // if there is no avatar id, we generate a unique one and use it forever
23
+    avatarId = generateUniqueId();
24
+    jitsiLocalStorage.setItem("avatarId", avatarId);
25
+}
26
+
27
+let localFlipX = JSON.parse(jitsiLocalStorage.getItem("localFlipX") || true);
28
+let displayName = UIUtil.unescapeHtml(
29
+    jitsiLocalStorage.getItem("displayname") || '');
30
+let language = jitsiLocalStorage.getItem("language");
31
+let cameraDeviceId = jitsiLocalStorage.getItem("cameraDeviceId") || '';
32
+let micDeviceId = jitsiLocalStorage.getItem("micDeviceId") || '';
33
+let welcomePageDisabled = JSON.parse(
34
+    jitsiLocalStorage.getItem("welcomePageDisabled") || false);
35
+
36
+// Currently audio output device change is supported only in Chrome and
37
+// default output always has 'default' device ID
38
+let audioOutputDeviceId = jitsiLocalStorage.getItem("audioOutputDeviceId")
39
+    || 'default';
40
+
41
+if (audioOutputDeviceId !==
42
+    JitsiMeetJS.mediaDevices.getAudioOutputDevice()) {
43
+    JitsiMeetJS.mediaDevices.setAudioOutputDevice(audioOutputDeviceId)
44
+        .catch((ex) => {
45
+            console.warn('Failed to set audio output device from local ' +
46
+                'storage. Default audio output device will be used' +
47
+                'instead.', ex);
48
+        });
71
 }
49
 }
72
 
50
 
73
 export default {
51
 export default {
82
         displayName = newDisplayName;
60
         displayName = newDisplayName;
83
 
61
 
84
         if (!disableLocalStore)
62
         if (!disableLocalStore)
85
-            window.localStorage.displayname = UIUtil.escapeHtml(displayName);
63
+            jitsiLocalStorage.setItem("displayname",
64
+                UIUtil.escapeHtml(displayName));
86
     },
65
     },
87
 
66
 
88
     /**
67
     /**
102
         email = newEmail;
81
         email = newEmail;
103
 
82
 
104
         if (!disableLocalStore)
83
         if (!disableLocalStore)
105
-            window.localStorage.email = UIUtil.escapeHtml(newEmail);
84
+            jitsiLocalStorage.setItem("email", UIUtil.escapeHtml(newEmail));
106
     },
85
     },
107
 
86
 
108
     /**
87
     /**
142
     },
121
     },
143
     setLanguage: function (lang) {
122
     setLanguage: function (lang) {
144
         language = lang;
123
         language = lang;
145
-        window.localStorage.language = lang;
124
+        jitsiLocalStorage.setItem("language", lang);
146
     },
125
     },
147
 
126
 
148
     /**
127
     /**
151
      */
130
      */
152
     setLocalFlipX: function (val) {
131
     setLocalFlipX: function (val) {
153
         localFlipX = val;
132
         localFlipX = val;
154
-        window.localStorage.localFlipX = val;
133
+        jitsiLocalStorage.setItem("localFlipX", val);
155
     },
134
     },
156
 
135
 
157
     /**
136
     /**
179
     setCameraDeviceId: function (newId, store) {
158
     setCameraDeviceId: function (newId, store) {
180
         cameraDeviceId = newId;
159
         cameraDeviceId = newId;
181
         if (store)
160
         if (store)
182
-            window.localStorage.cameraDeviceId = newId;
161
+            jitsiLocalStorage.setItem("cameraDeviceId", newId);
183
     },
162
     },
184
 
163
 
185
     /**
164
     /**
199
     setMicDeviceId: function (newId, store) {
178
     setMicDeviceId: function (newId, store) {
200
         micDeviceId = newId;
179
         micDeviceId = newId;
201
         if (store)
180
         if (store)
202
-            window.localStorage.micDeviceId = newId;
181
+            jitsiLocalStorage.setItem("micDeviceId", newId);
203
     },
182
     },
204
 
183
 
205
     /**
184
     /**
218
      */
197
      */
219
     setAudioOutputDeviceId: function (newId = 'default') {
198
     setAudioOutputDeviceId: function (newId = 'default') {
220
         return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
199
         return JitsiMeetJS.mediaDevices.setAudioOutputDevice(newId)
221
-            .then(() => window.localStorage.audioOutputDeviceId = newId);
200
+            .then(() =>
201
+                jitsiLocalStorage.setItem("audioOutputDeviceId", newId));
222
     },
202
     },
223
 
203
 
224
     /**
204
     /**
235
      */
215
      */
236
     setWelcomePageEnabled (enabled) {
216
     setWelcomePageEnabled (enabled) {
237
         welcomePageDisabled = !enabled;
217
         welcomePageDisabled = !enabled;
238
-        window.localStorage.welcomePageDisabled = welcomePageDisabled;
218
+        jitsiLocalStorage.setItem("welcomePageDisabled", welcomePageDisabled);
239
     }
219
     }
240
 };
220
 };

+ 64
- 0
modules/util/JitsiLocalStorage.js View File

1
+/**
2
+ * Dummy implementation of Storage interface with empty methods.
3
+ */
4
+class DummyLocalStorage {
5
+    /**
6
+     * Empty function
7
+     */
8
+    getItem() { }
9
+
10
+    /**
11
+     * Empty function
12
+     */
13
+    setItem() { }
14
+
15
+    /**
16
+     * Empty function
17
+     */
18
+    removeItem() { }
19
+}
20
+
21
+/**
22
+ * Wrapper class for browser's local storage object.
23
+ */
24
+class JitsiLocalStorage extends DummyLocalStorage {
25
+    /**
26
+     * @constructor
27
+     * @param {Storage} storage browser's local storage object.
28
+     */
29
+    constructor(storage) {
30
+        super();
31
+        this.storage = storage || new DummyLocalStorage();
32
+    }
33
+
34
+    /**
35
+     * Returns that passed key's value.
36
+     * @param {string} keyName the name of the key you want to retrieve
37
+     * the value of.
38
+     * @returns {String|null} the value of the key. If the key does not exist,
39
+     * null is returned.
40
+     */
41
+    getItem(keyName) {
42
+        return this.storage.getItem(keyName);
43
+    }
44
+
45
+    /**
46
+     * Adds a key to the storage, or update key's value if it already exists.
47
+     * @param {string} keyName the name of the key you want to create/update.
48
+     * @param {string} keyValue the value you want to give the key you are
49
+     * creating/updating.
50
+     */
51
+    setItem(keyName, keyValue) {
52
+        return this.storage.setItem(keyName, keyValue);
53
+    }
54
+
55
+    /**
56
+     * Remove a key from the storage.
57
+     * @param {string} keyName the name of the key you want to remove.
58
+     */
59
+    removeItem(keyName) {
60
+        return this.storage.removeItem(keyName);
61
+    }
62
+}
63
+
64
+export default new JitsiLocalStorage(window.localStorage);

Loading…
Cancel
Save