xsnippet logotype

xsnippet

#363508, Untitled [ JavaScript ]

by Guest
 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
import {EventBus} from './services/eventBus';
import {Header} from './components/header/header';
import {Settings} from './pages/settings/settings';
import {Reports} from './pages/reports/reports';
import {TaskList} from './pages/task-list/task-list';
import {Timer} from './pages/timer/timer';

class Router {
  constructor() {
    if (Router.instanse) {
      return Router.instanse;
    }
    this.pages = {
      "/settings": Settings,
      "/reports": Reports,
      "/task-list": TaskList,
      "/timer": Timer
    };
    
    Router.instanse = this;
  }
  
  init() {
    const query = window.location.href.split('?')[1];
    
    if (!document.querySelector('.header')) {
      Header();
      EventBus.publish('activeLink');
    }
    
    document.querySelector('#wrapper').innerHTML = '';
    sessionStorage.isNewUser = (!sessionStorage.isNewUser);
    
    if (window.location.pathname === '/') {
      EventBus.publish('pageChanged', window.history.pushState(null, null, '/task-list'));
    }
    
    EventBus.publish('pageChanged', this.pages[window.location.pathname](query));
  }
}

export {Router}