summaryrefslogtreecommitdiff
path: root/WebInterface/NodeJSServer/src/js/modules/ui/components/router.js
blob: c01c21b5e5fe54e61cf6a19a7db2044d3746a918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * Class for routing between pages
 */
export default class Router {
  /**
   * @param {Interface} iface Interface for comm. with other objects
   */
  constructor(iface) {
    iface.addObject(this, 'serverListing', ['routePlay']);
    this.iface = iface;
  }

  /**
   * Routes to the play page
   * @param {HubConnection} connection Connection to the server
   */
  routePlay(connection) {
    window.history.pushState('object or string', 'Game Page',
        'play#game=' + this.serverName);
    fetch('play').then((response) => {
      response.text().then((htmlString) => {
        // Replace all references, since we're starting one level farther up
        htmlString = htmlString.replace(/\.\.\//g, './');
        htmlString = /<body>((.)|(\n))*<\/body>/g.exec(htmlString)[0];
        htmlString = htmlString.replace(/<script src=".*"><\/script>/, '');
        htmlString = htmlString.replace(
            /<remove_if_redirected>((.)|\n)*?<\/remove_if_redirected>/g, '');
        document.body.innerHTML = htmlString;

        let stylesheet = document.createElement('link');
        stylesheet.rel = 'stylesheet';
        stylesheet.type = 'text/css';
        stylesheet.href = './style/play.css';
        document.head.appendChild(stylesheet);


        this.iface.callMethod('uiMananger', 'initPlay');
        for (let ui of this.pageUI) {
          ui.refresh();
        }
      });
    });
  }
}