Browse code

use location hash for room

devnewton authored on 08/01/2018 20:54:14
Showing 1 changed files
... ...
@@ -2,10 +2,19 @@ document.addEventListener('DOMContentLoaded', function () {
2 2
 
3 3
     var taab_backend2html = peg.generate(document.getElementById("taab-backend2html").textContent);
4 4
 
5
+    function hashToRoom() {
6
+        var result = /#(\w+)/.exec(window.location.hash);
7
+        if (result && result.length === 2) {
8
+            return result[1];
9
+        } else {
10
+            return '';
11
+        }
12
+    }
13
+
5 14
     new Vue({
6 15
         el: '#taab-coincoin',
7 16
         data: {
8
-            room: "",
17
+            room: hashToRoom(),
9 18
             message: "",
10 19
             posts: []
11 20
         },
... ...
@@ -74,7 +83,9 @@ document.addEventListener('DOMContentLoaded', function () {
74 83
                 if (result && result.length === 2) {
75 84
                     this.posts = [];
76 85
                     this.room = result[1] || '';
77
-                    this.get();
86
+                    if (this.room !== hashToRoom()) {
87
+                        window.location = this.room ? "#" + this.room : '';
88
+                    }
78 89
                     return true;
79 90
                 } else {
80 91
                     return false;
... ...
@@ -127,6 +138,11 @@ document.addEventListener('DOMContentLoaded', function () {
127 138
                         break;
128 139
                 }
129 140
             },
141
+            hashChanged: function (e) {
142
+                this.posts = [];
143
+                this.room = hashToRoom();
144
+                this.get();
145
+            },
130 146
             parseBackendResponse: function (xhr) {
131 147
                 var room = this.room || '';
132 148
                 var responseRoom = xhr.getResponseHeader("X-Room") || '';
... ...
@@ -159,6 +175,9 @@ document.addEventListener('DOMContentLoaded', function () {
159 175
         },
160 176
         mounted: function () {
161 177
             var self = this;
178
+            window.onhashchange = function (e) {
179
+                self.hashChanged(e);
180
+            };
162 181
             self.get();
163 182
             setInterval(function () {
164 183
                 self.get();