text: import {ProjectPage, RunDetailsPage, NotFoundPage, StartPage, 
      AutorunnerPage, ObjectsPage, ObjectPage, CreateProjectPage} from './pages.js';
  
  export const base = '/sap/zabapopentest';
  
  export class Router {
    static initialize() {
      window.onpopstate = this.popstate;
      window.navigate = this.navigate;
    }
  
    static popstate(event) {
      Router.run();
    }
  
    static navigate(url) {
      history.pushState(null, null, base + "/" + url);
      Router.run();
    }
  
    static link(to, body) {
      let href = to;
      if (href==="") href = base;
      return "<a href=\""+href+"\" onclick=\"navigate('"+to+"');return false;\">"+body+"</a>";
    }
  
    static run() {
      if (!window.location.pathname.startsWith(base)) {
        throw "unknown url";
      }
  
      const path = window.location.pathname.substr(base.length);
      let page;
  
  // todo, replace this with some kind of configuration
      if (path === "" || path === "/") {
        page = new StartPage();
      }
  
      let result = path.match(/\/projects\/(\w+)$/);
      if (result) {
        page = new ProjectPage(result[1]);
      }
  
      result = path.match(/\/create$/);
      if (result) {
        page = new CreateProjectPage();
      }
  
      result = path.match(/\/projects\/(\w+)\/runner$/);
      if (result) {
        page = new AutorunnerPage(result[1]);
      }
  
      result = path.match(/\/projects\/(\w+)\/runs\/(\w+)$/);
      if (result) {
        page = new RunDetailsPage(result[1], result[2]);
      }
  
      result = path.match(/\/projects\/(\w+)\/runs\/(\w+)\/objects$/);
      if (result) {
        page = new ObjectsPage(result[1], result[2]);
      }
  
      result = path.match(/\/projects\/(\w+)\/runs\/(\w+)\/objects\/(\w+)\/(\w+)$/);
      if (result) {
        page = new ObjectPage(result[1], result[2], result[3], result[4]);
      }
  
      if (!page) {
        page = new NotFoundPage();
      }
  
      if (page instanceof StartPage) {
        document.getElementById("app").innerHTML = page.render();
      } else {
        document.getElementById("app").innerHTML = this.renderHeader() + page.render() + this.renderFooter();
      }
    }
  
    static renderHeader() {
      return this.link("", "abapOpenTest") +" > breadcrumb <hr>";
    }
  
    static renderFooter() {
      return "<hr>footer";
    }
  }
  
  Router.initialize();
  Router.run();