Parcourir la source

ref(Overlay): introduce base class

master
paweldomas il y a 8 ans
Parent
révision
3c0c823a37

+ 37
- 22
modules/UI/gum_overlay/UserMediaPermissionsGuidanceOverlay.js Voir le fichier

@@ -1,27 +1,41 @@
1
-/* global $, APP */
1
+/* global */
2 2
 
3
-let $overlay;
3
+import Overlay from '../overlay/Overlay';
4 4
 
5 5
 /**
6
- * Internal function that constructs overlay with guidance how to proceed with
7
- * gUM prompt.
8
- * @param {string} browser - name of browser for which to construct the
9
- *      guidance overlay.
6
+ * An overlay with guidance how to proceed with gUM prompt.
10 7
  */
11
-function buildOverlayHtml(browser) {
12
-    $overlay = $(`
13
-        <div class='overlay_container'>
14
-            <div class='overlay_content'>
15
-                <span class="overlay_icon icon-microphone"></span>
16
-                <span class="overlay_icon icon-camera"></span>
17
-                <span data-i18n='[html]userMedia.${browser}GrantPermissions' 
18
-                      class='overlay_text_small'></span>
19
-            </div>
20
-        </div>`);
8
+class GUMOverlayImpl extends Overlay {
21 9
 
22
-    APP.translation.translateElement($overlay);
10
+    /**
11
+     * Constructs overlay with guidance how to proceed with gUM prompt.
12
+     * @param {string} browser - name of browser for which to construct the
13
+     *     guidance overlay.
14
+     * @override
15
+     */
16
+    constructor(browser) {
17
+        super();
18
+        this.browser = browser;
19
+    }
20
+
21
+    /**
22
+     * @inheritDoc
23
+     */
24
+    _buildOverlayContent() {
25
+        return `
26
+            <span class="overlay_icon icon-microphone"></span>
27
+            <span class="overlay_icon icon-camera"></span>
28
+            <span data-i18n='[html]userMedia.${this.browser}GrantPermissions' 
29
+                  class='overlay_text_small'></span>`;
30
+    }
23 31
 }
24 32
 
33
+/**
34
+ * Stores GUM overlay instance.
35
+ * @type {GUMOverlayImpl}
36
+ */
37
+let overlay;
38
+
25 39
 export default {
26 40
     /**
27 41
      * Checks whether the overlay is currently visible.
@@ -29,7 +43,7 @@ export default {
29 43
      * or <tt>false</tt> otherwise.
30 44
      */
31 45
     isVisible () {
32
-        return $overlay && $overlay.parents('body').length > 0;
46
+        return overlay && overlay.isVisible();
33 47
     },
34 48
     /**
35 49
      * Shows browser-specific overlay with guidance how to proceed with
@@ -38,9 +52,10 @@ export default {
38 52
      *      guidance overlay.
39 53
      */
40 54
     show(browser) {
41
-        !$overlay && buildOverlayHtml(browser);
42
-
43
-        !this.isVisible() && $overlay.appendTo('body');
55
+        if (!overlay) {
56
+            overlay = new GUMOverlayImpl(browser);
57
+        }
58
+        overlay.show();
44 59
     },
45 60
 
46 61
     /**
@@ -48,6 +63,6 @@ export default {
48 63
      * gUM prompt.
49 64
      */
50 65
     hide() {
51
-        $overlay && $overlay.detach();
66
+        overlay && overlay.hide();
52 67
     }
53 68
 };

+ 82
- 0
modules/UI/overlay/Overlay.js Voir le fichier

@@ -0,0 +1,82 @@
1
+/* global $, APP */
2
+
3
+/**
4
+ * Base class for overlay components - the components which are displayed on
5
+ * top of the application with semi-transparent background covering the whole
6
+ * screen.
7
+ */
8
+export default class Overlay{
9
+    /**
10
+     * Creates new <tt>Overlay</tt> instance.
11
+     */
12
+    constructor() {
13
+        /**
14
+         *
15
+         * @type {jQuery}
16
+         */
17
+        this.$overlay = null;
18
+    }
19
+    /**
20
+     * Template method which should be used by subclasses to provide the overlay
21
+     * content. The contents provided by this method are later subject to
22
+     * the translation using {@link APP.translation.translateElement}.
23
+     * @return {string} HTML representation of the overlay dialog contents.
24
+     * @private
25
+     */
26
+    _buildOverlayContent() {
27
+        return '';
28
+    }
29
+    /**
30
+     * Constructs the HTML body of the overlay dialog.
31
+     */
32
+    buildOverlayHtml() {
33
+
34
+        let overlayContent = this._buildOverlayContent();
35
+
36
+        this.$overlay = $(`
37
+            <div class='overlay_container'>
38
+                <div class='overlay_content'>
39
+                    ${overlayContent}
40
+                </div>
41
+            </div>`);
42
+
43
+        APP.translation.translateElement(this.$overlay);
44
+    }
45
+    /**
46
+     * Checks whether the page reload overlay has been displayed.
47
+     * @return {boolean} <tt>true</tt> if the page reload overlay is currently
48
+     * visible or <tt>false</tt> otherwise.
49
+     */
50
+    isVisible() {
51
+        return this.$overlay && this.$overlay.parents('body').length > 0;
52
+    }
53
+    /**
54
+     * Template method called just after the overlay is displayed for the first
55
+     * time.
56
+     * @private
57
+     */
58
+    _onShow() {
59
+        // To be overridden by subclasses.
60
+    }
61
+    /**
62
+     * Shows the overlay dialog adn attaches the underlying HTML representation
63
+     * to the DOM.
64
+     */
65
+    show() {
66
+
67
+        !this.$overlay && this.buildOverlayHtml();
68
+
69
+        if (!this.isVisible()) {
70
+            this.$overlay.appendTo('body');
71
+            this._onShow();
72
+        }
73
+    }
74
+
75
+    /**
76
+     * Hides the overlay dialog and detaches it's HTML representation from
77
+     * the DOM.
78
+     */
79
+    hide() {
80
+        this.$overlay && this.$overlay.detach();
81
+    }
82
+}

+ 84
- 74
modules/UI/reload_overlay/PageReloadOverlay.js Voir le fichier

@@ -1,89 +1,101 @@
1 1
 /* global $, APP, AJS */
2 2
 
3
-let $overlay;
3
+import Overlay from '../overlay/Overlay';
4 4
 
5 5
 /**
6
- * Conference reload counter in seconds.
7
- * @type {number}
6
+ * An overlay dialog which is shown before the conference is reloaded. Shows
7
+ * a warning message and counts down towards the reload.
8 8
  */
9
-let timeLeft;
10
-
11
-/**
12
- * Conference reload timeout in seconds.
13
- * @type {number}
14
- */
15
-let timeout;
16
-
17
-/**
18
- * Internal function that constructs overlay with the warning message and count
19
- * down towards the conference reload.
20
- */
21
-function buildReloadOverlayHtml() {
22
-    $overlay = $(`
23
-        <div class='overlay_container'>
24
-            <div class='overlay_content'>
25
-                <span data-i18n='dialog.serviceUnavailable' 
26
-                      class='overlay_text_small'></span>
27
-                <span data-i18n='dialog.conferenceReloadMsg' 
28
-                      class='overlay_text_small'></span>
29
-                <div>
30
-                    <div id='reloadProgressBar' class="aui-progress-indicator">
31
-                        <span class="aui-progress-indicator-value"></span>
32
-                    </div>
33
-                    <span id='reloadSecRemaining' class='overlay_text_small'>
34
-                    </span>
9
+class PageReloadOverlayImpl extends Overlay{
10
+    /**
11
+     * Creates new <tt>PageReloadOverlayImpl</tt>
12
+     * @param {number} timeoutSeconds how long the overlay dialog will be
13
+     * displayed, before the conference will be reloaded.
14
+     */
15
+    constructor(timeoutSeconds) {
16
+        super();
17
+        /**
18
+         * Conference reload counter in seconds.
19
+         * @type {number}
20
+         */
21
+        this.timeLeft = timeoutSeconds;
22
+        /**
23
+         * Conference reload timeout in seconds.
24
+         * @type {number}
25
+         */
26
+        this.timeout = timeoutSeconds;
27
+    }
28
+    /**
29
+     * Constructs overlay body with the warning message and count down towards
30
+     * the conference reload.
31
+     * @override
32
+     */
33
+    _buildOverlayContent() {
34
+        return `
35
+            <span data-i18n='dialog.serviceUnavailable' 
36
+                  class='overlay_text_small'></span>
37
+            <span data-i18n='dialog.conferenceReloadMsg' 
38
+                  class='overlay_text_small'></span>
39
+            <div>
40
+                <div id='reloadProgressBar' class="aui-progress-indicator">
41
+                    <span class="aui-progress-indicator-value"></span>
35 42
                 </div>
36
-            </div>
37
-        </div>`);
38
-
39
-    APP.translation.translateElement($overlay);
40
-}
41
-
42
-/**
43
- * Updates the progress indicator position and the label with the time left.
44
- */
45
-function updateDisplay() {
43
+                <span id='reloadSecRemaining' class='overlay_text_small'>
44
+                </span>
45
+            </div>`;
46
+    }
46 47
 
47
-    const timeLeftTxt
48
-        = APP.translation.translateString(
49
-            "dialog.conferenceReloadTimeLeft",
50
-            { seconds: timeLeft });
51
-    $("#reloadSecRemaining").text(timeLeftTxt);
48
+    /**
49
+     * Updates the progress indicator position and the label with the time left.
50
+     */
51
+    updateDisplay() {
52 52
 
53
-    const ratio = (timeout-timeLeft)/timeout;
54
-    AJS.progressBars.update("#reloadProgressBar", ratio);
55
-}
53
+        const timeLeftTxt
54
+            = APP.translation.translateString(
55
+                "dialog.conferenceReloadTimeLeft",
56
+                { seconds: this.timeLeft });
57
+        $("#reloadSecRemaining").text(timeLeftTxt);
56 58
 
57
-/**
58
- * Starts the reload countdown with the animation.
59
- * @param {number} timeoutSeconds how many seconds before the conference
60
- * reload will happen.
61
- */
62
-function start(timeoutSeconds) {
59
+        const ratio = (this.timeout - this.timeLeft) / this.timeout;
60
+        AJS.progressBars.update("#reloadProgressBar", ratio);
61
+    }
63 62
 
64
-    timeLeft = timeout = timeoutSeconds;
63
+    /**
64
+     * Starts the reload countdown with the animation.
65
+     * @override
66
+     */
67
+    _onShow() {
65 68
 
66
-    // Initialize displays
67
-    updateDisplay();
69
+        // Initialize displays
70
+        this.updateDisplay();
68 71
 
69
-    var intervalId = window.setInterval(function() {
72
+        var intervalId = window.setInterval(function() {
70 73
 
71
-        if (timeLeft >= 1) {
72
-            timeLeft -= 1;
73
-        }
74
+            if (this.timeLeft >= 1) {
75
+                this.timeLeft -= 1;
76
+            }
74 77
 
75
-        updateDisplay();
78
+            this.updateDisplay();
76 79
 
77
-        if (timeLeft === 0) {
78
-            window.clearInterval(intervalId);
79
-            APP.ConferenceUrl.reload();
80
-        }
81
-    }, 1000);
80
+            if (this.timeLeft === 0) {
81
+                window.clearInterval(intervalId);
82
+                APP.ConferenceUrl.reload();
83
+            }
84
+        }.bind(this), 1000);
82 85
 
83
-    console.info(
84
-        "The conference will be reloaded after " + timeLeft + " seconds.");
86
+        console.info(
87
+            "The conference will be reloaded after "
88
+                + this.timeLeft + " seconds.");
89
+    }
85 90
 }
86 91
 
92
+/**
93
+ * Holds the page reload overlay instance.
94
+ *
95
+ * {@type PageReloadOverlayImpl}
96
+ */
97
+let overlay;
98
+
87 99
 export default {
88 100
     /**
89 101
      * Checks whether the page reload overlay has been displayed.
@@ -91,7 +103,7 @@ export default {
91 103
      * visible or <tt>false</tt> otherwise.
92 104
      */
93 105
     isVisible() {
94
-        return $overlay && $overlay.parents('body').length > 0;
106
+        return overlay && overlay.isVisible();
95 107
     },
96 108
     /**
97 109
      * Shows the page reload overlay which will do the conference reload after
@@ -102,11 +114,9 @@ export default {
102 114
      */
103 115
     show(timeoutSeconds) {
104 116
 
105
-        !$overlay && buildReloadOverlayHtml();
106
-
107
-        if (!this.isVisible()) {
108
-            $overlay.appendTo('body');
109
-            start(timeoutSeconds);
117
+        if (!overlay) {
118
+            overlay = new PageReloadOverlayImpl(timeoutSeconds);
110 119
         }
120
+        overlay.show();
111 121
     }
112 122
 };

Chargement…
Annuler
Enregistrer