Переглянути джерело

feat(JitsiLocalStorage): Implement localStorage wrapper

master
hristoterezov 8 роки тому
джерело
коміт
7e2fe30472

+ 3
- 2
connection.js Переглянути файл

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

+ 4
- 3
modules/UI/UI.js Переглянути файл

@@ -31,6 +31,7 @@ var messageHandler = UI.messageHandler;
31 31
 var JitsiPopover = require("./util/JitsiPopover");
32 32
 var Feedback = require("./feedback/Feedback");
33 33
 import FollowMe from "../FollowMe";
34
+import jitsiLocalStorage from '../util/JitsiLocalStorage';
34 35
 
35 36
 var eventEmitter = new EventEmitter();
36 37
 UI.eventEmitter = eventEmitter;
@@ -1239,7 +1240,7 @@ UI.showDeviceErrorDialog = function (micError, cameraError) {
1239 1240
     }
1240 1241
 
1241 1242
     if (showDoNotShowWarning) {
1242
-        if (window.localStorage[localStoragePropName] === "true") {
1243
+        if (jitsiLocalStorage.getItem(localStoragePropName) === "true") {
1243 1244
             return;
1244 1245
         }
1245 1246
     }
@@ -1308,8 +1309,8 @@ UI.showDeviceErrorDialog = function (micError, cameraError) {
1308 1309
                 let input = form.find("#doNotShowWarningAgain");
1309 1310
 
1310 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 Переглянути файл

@@ -6,6 +6,7 @@ import SmallVideo from "./SmallVideo";
6 6
 import UIUtils from "../util/UIUtil";
7 7
 import UIEvents from '../../../service/UI/UIEvents';
8 8
 import JitsiPopover from "../util/JitsiPopover";
9
+import jitsiLocalStorage from '../../util/JitsiLocalStorage';
9 10
 
10 11
 const MUTED_DIALOG_BUTTON_VALUES = {
11 12
     cancel: 0,
@@ -654,8 +655,7 @@ RemoteVideo.createContainer = function (spanId) {
654 655
 RemoteVideo.showMuteParticipantDialog = function () {
655 656
     //FIXME: don't show again checkbox is implemented very dirty. we should add
656 657
     // this functionality to MessageHandler class.
657
-    if (window.localStorage
658
-        && window.localStorage.getItem(
658
+    if (jitsiLocalStorage.getItem(
659 659
             "dontShowMuteParticipantDialog") === "true") {
660 660
         return Promise.resolve(MUTED_DIALOG_BUTTON_VALUES.muted);
661 661
     }
@@ -672,15 +672,13 @@ RemoteVideo.showMuteParticipantDialog = function () {
672 672
             msgString,
673 673
             leftButtonKey: 'dialog.muteParticipantButton',
674 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 684
                 resolve(MUTED_DIALOG_BUTTON_VALUES.muted);

+ 46
- 66
modules/settings/Settings.js Переглянути файл

@@ -1,26 +1,6 @@
1 1
 /* global JitsiMeetJS */
2
-
3 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 5
 function generateUniqueId() {
26 6
     function _p8() {
@@ -29,45 +9,43 @@ function generateUniqueId() {
29 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 51
 export default {
@@ -82,7 +60,8 @@ export default {
82 60
         displayName = newDisplayName;
83 61
 
84 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,7 +81,7 @@ export default {
102 81
         email = newEmail;
103 82
 
104 83
         if (!disableLocalStore)
105
-            window.localStorage.email = UIUtil.escapeHtml(newEmail);
84
+            jitsiLocalStorage.setItem("email", UIUtil.escapeHtml(newEmail));
106 85
     },
107 86
 
108 87
     /**
@@ -142,7 +121,7 @@ export default {
142 121
     },
143 122
     setLanguage: function (lang) {
144 123
         language = lang;
145
-        window.localStorage.language = lang;
124
+        jitsiLocalStorage.setItem("language", lang);
146 125
     },
147 126
 
148 127
     /**
@@ -151,7 +130,7 @@ export default {
151 130
      */
152 131
     setLocalFlipX: function (val) {
153 132
         localFlipX = val;
154
-        window.localStorage.localFlipX = val;
133
+        jitsiLocalStorage.setItem("localFlipX", val);
155 134
     },
156 135
 
157 136
     /**
@@ -179,7 +158,7 @@ export default {
179 158
     setCameraDeviceId: function (newId, store) {
180 159
         cameraDeviceId = newId;
181 160
         if (store)
182
-            window.localStorage.cameraDeviceId = newId;
161
+            jitsiLocalStorage.setItem("cameraDeviceId", newId);
183 162
     },
184 163
 
185 164
     /**
@@ -199,7 +178,7 @@ export default {
199 178
     setMicDeviceId: function (newId, store) {
200 179
         micDeviceId = newId;
201 180
         if (store)
202
-            window.localStorage.micDeviceId = newId;
181
+            jitsiLocalStorage.setItem("micDeviceId", newId);
203 182
     },
204 183
 
205 184
     /**
@@ -218,7 +197,8 @@ export default {
218 197
      */
219 198
     setAudioOutputDeviceId: function (newId = 'default') {
220 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,6 +215,6 @@ export default {
235 215
      */
236 216
     setWelcomePageEnabled (enabled) {
237 217
         welcomePageDisabled = !enabled;
238
-        window.localStorage.welcomePageDisabled = welcomePageDisabled;
218
+        jitsiLocalStorage.setItem("welcomePageDisabled", welcomePageDisabled);
239 219
     }
240 220
 };

+ 64
- 0
modules/util/JitsiLocalStorage.js Переглянути файл

@@ -0,0 +1,64 @@
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);

Завантаження…
Відмінити
Зберегти