import HomeView from './HomeView';
import JoinView from './JoinView';

export default class ViewManager {
	constructor(target) {
		this.target = target;

		this.home = new HomeView();
		this.join = new JoinView();

		this.load('home');
	}

	load(viewName) {
		this[viewName].init().then(view => {

			// First remove all Childs
			while(this.target.firstChild) {
				this.target.removeChild(this.target.firstChild);
			}

			// Then load the view into it
			view.show(this.target);
		});
	}
}

@@ -147,16 +147,46 @@
 arget;%0A%0A
+%09%09// Register all known views%0A
 %09%09this.h
@@ -244,118 +244,835 @@
 %0A%0A%09%09
-this.load('home');%0A%09%7D%0A%0A%09load(viewNam
+// Define default view%0A%09%09this.DEFAULT_VIEW = 'home';%0A%0A%09%09// Init URL bar and history%0A%09%09this.init();%0A%09%7D%0A%0A%09init() %7B%0A%09%09if(location.hash == '')%0A%09%09%09location.hash = '/';%0A%0A%09%09// Load the view given in the URL%0A%09%09this.load(location.hash.substr(2), true);%0A%0A%09%09// Check history back and forward to load the correct view%0A%09%09window.addEventListener('popstate', event =%3E %7B%0A%09%09  this.load(location.hash.substr(2), true);%0A%09%09%7D);%0A%09%7D%0A%0A%09load(viewName = '', nohistory = fals
 e) %7B%0A
+%0A
 %09%09
-this%5BviewName%5D.init().then(view =%3E %7B%0A%0A%09%09%09// First remove all Childs
+// Check if view exists%0A%09%09if(!this%5BviewName %7C%7C this.DEFAULT_VIEW%5D)%0A%09%09%09viewName = 'notfound';%0A%0A%09%09// Init the new view (load template etc.)%0A%09%09this%5BviewName %7C%7C this.DEFAULT_VIEW%5D.init().then(view =%3E %7B%0A%0A%09%09%09// Push the new view to the URL and the history%0A%09%09%09if(!nohistory)%0A%09%09%09%09history.pushState(null, viewName %7C%7C this.DEFAULT_VIEW, %60#/$%7BviewName%7D%60);%0A%0A%09%09%09// Remove the old view
 %0A%09%09%09
@@ -1172,14 +1172,9 @@
 %09// 
-Then l
+L
 oad 
@@ -1185,16 +1185,8 @@
 view
- into it
 %0A%09%09%09

Add URL support with history.pushState