Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
add basic router
Browse files Browse the repository at this point in the history
  • Loading branch information
criticalbh committed May 17, 2017
1 parent df0ccd6 commit ef1c2c8
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 45 deletions.
36 changes: 33 additions & 3 deletions client/components/layout/c-nav.comp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"use strict";

import {RouterHandler} from '../../router/router-handler';

export class CNavComponent extends HTMLElement {
constructor() {
super();
Expand All @@ -22,7 +25,7 @@ export class CNavComponent extends HTMLElement {
<ul class="nav navbar-nav pull-xs-right">
<li class="nav-item">
<!-- Add "active" class when you're on that page" -->
<a class="nav-link active" href="">Home</a>
<a style="cursor: pointer;" route="/" class="nav-link active">Home</a>
</li>
<!--<li class="nav-item">-->
<!--<a class="nav-link" href="">-->
Expand All @@ -35,10 +38,10 @@ export class CNavComponent extends HTMLElement {
<!--</a>-->
<!--</li>-->
<li class="nav-item">
<a class="nav-link" href="">Sign in</a>
<a style="cursor: pointer;" route="/login" class="nav-link">Sign in</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Sign up</a>
<a style="cursor: pointer;" class="nav-link">Sign up</a>
</li>
</ul>
</div>
Expand All @@ -47,6 +50,33 @@ export class CNavComponent extends HTMLElement {


this.innerHTML = template;
let links = this.querySelectorAll('a.nav-link');
links.forEach(link => {
link.addEventListener('click', () => {
var routingTo = link.getAttribute('route');
let state = {};
state.route = routingTo;
history.pushState(state, null, routingTo);
});
});

this.updateActive(location.pathname);

RouterHandler.onChange((routeTo) => {
this.updateActive(routeTo);
});

}

updateActive(route) {
let links = this.querySelectorAll('a.nav-link');
links.forEach(link => {
link.className = 'nav-link';
let linkRoute = link.getAttribute('route');
if (linkRoute === route) {
link.className = 'nav-link active';
}
});
}


Expand Down
8 changes: 1 addition & 7 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@
<body>
<c-nav></c-nav>
<div class="home-page">

<c-banner></c-banner>

<div class="container page">
<router-outlet></router-outlet>
</div>

<router-outlet></router-outlet>
</div>
<c-footer></c-footer>
</body>
Expand Down
27 changes: 13 additions & 14 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import {layoutComponents} from "./components/layout/index";
import {ArticleComponent} from "./components/article.comp";
import {HomeComponent} from "./pages/home.comp";
import {CLoginComponent} from "./pages/login.comp";
import {RouterOutlet} from "./router/router-outlet";
import {ComponentRegistry} from "./component-registry";
import {RouterHandler} from "./router/router-handler";


class App {
constructor() {
this.registerComponents();

const url = location.pathname;
const routerHandler = new RouterHandler();
console.log(url);
RouterHandler.navigate(url);

// console.log(globalFeed);
}

registerComponents() {
const components = [
...layoutComponents,
{
Expand All @@ -24,21 +31,13 @@ class App {
{
tagName: 'c-home',
component: HomeComponent
},
{
tagName: 'c-login',
component: CLoginComponent
}
];
ComponentRegistry.register(components);

let n = new HomeComponent();
const outlet = document.querySelector('router-outlet');
while (outlet.firstChild) {
outlet.removeChild(outlet.firstChild);
}
outlet.appendChild(n);




// console.log(globalFeed);
}

}
Expand Down
45 changes: 25 additions & 20 deletions client/pages/home.comp.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
<div class="row">
<div class="col-md-9">
<div class="feed-toggle">
<ul id="feedOptions" class="nav nav-pills outline-active">
<!--<li class="nav-item">-->
<!--<a class="nav-link disabled" href="">Your Feed</a>-->
<!--</li>-->
<li class="nav-item">
<a class="nav-link active" href="">Global Feed</a>
</li>
</ul>
</div>
<div id="globalFeed">
</div>
</div>
<div>
<c-banner></c-banner>
<div class="container page">
<div class="row">
<div class="col-md-9">
<div class="feed-toggle">
<ul id="feedOptions" class="nav nav-pills outline-active">
<!--<li class="nav-item">-->
<!--<a class="nav-link disabled" href="">Your Feed</a>-->
<!--</li>-->
<li class="nav-item">
<a class="nav-link active" href="">Global Feed</a>
</li>
</ul>
</div>
<div id="globalFeed">
</div>
</div>

<div class="col-md-3">
<div class="sidebar">
<p>Popular Tags</p>
<div id="tagList" class="tag-list">
<div class="col-md-3">
<div class="sidebar">
<p>Popular Tags</p>
<div id="tagList" class="tag-list">
</div>
</div>
</div>

</div>
</div>

</div>
2 changes: 1 addition & 1 deletion client/pages/home.comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export class HomeComponent extends HTMLElement {
createNewTagElement(tag) {
const tagEl = document.createElement('a');
tagEl.className = 'tag-pill tag-default';
tagEl.href = '#';
tagEl.innerHTML = tag;
tagEl.style="cursor: pointer;";
return tagEl;
}

Expand Down
56 changes: 56 additions & 0 deletions client/pages/login.comp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"use strict";
export class CLoginComponent extends HTMLElement {
constructor() {
super();
// this.shadow = this.createShadowRoot();
}

static get observedAttributes() {
return [];
}

attributeChangedCallback(name, oldValue, newValue) {

}

connectedCallback() {
var template = `
<div class="auth-page">
<div class="container page">
<div class="row">
<div class="col-md-6 offset-md-3 col-xs-12">
<h1 class="text-xs-center">Sign up</h1>
<p class="text-xs-center">
<a href="">Have an account?</a>
</p>
<!--<ul class="error-messages">-->
<!--<li>That email is already taken</li>-->
<!--</ul>-->
<form>
<fieldset class="form-group">
<input class="form-control form-control-lg" type="text" placeholder="Your Name">
</fieldset>
<fieldset class="form-group">
<input class="form-control form-control-lg" type="text" placeholder="Email">
</fieldset>
<fieldset class="form-group">
<input class="form-control form-control-lg" type="password" placeholder="Password">
</fieldset>
<button class="btn btn-lg btn-primary pull-xs-right">
Sign up
</button>
</form>
</div>
</div>
</div>
</div>
`;
this.innerHTML = template;
}


}
61 changes: 61 additions & 0 deletions client/router/router-handler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
import {HomeComponent} from "../pages/home.comp";
import {CLoginComponent} from "../pages/login.comp";
export class RouterHandler {
constructor() {
var pushState = history.pushState;
history.pushState = function (state, a, b) {
if (typeof history.onpushstate == "function") {
history.onpushstate({state: state}, a, b);
}
return pushState.apply(history, arguments);
};

window.onpopstate = history.onpushstate = (state, a, url) => {
let routeTo = null;
if (state.state != null && state.state.route != undefined) {
routeTo = state.state.route;
} else if(url != undefined) {
routeTo = url;
}
RouterHandler.handleButtonChanges(routeTo);
RouterHandler._onChangeCallbacks.forEach(c => {
c(routeTo);
});
// }
};
}

static onChange(callback) {
RouterHandler._onChangeCallbacks.push(callback);
}

static handleButtonChanges(url) {
let page = null;

if (url == undefined || url == '/') {
page = new HomeComponent();
} else {
page = new CLoginComponent();
}
const outlet = document.querySelector('router-outlet');
while (outlet.firstChild) {
outlet.removeChild(outlet.firstChild);
}
outlet.appendChild(page);
}

static navigate(url) {
let page = null;

if (url == '/') {
page = new HomeComponent();
} else {
page = new CLoginComponent();
}
const outlet = document.querySelector('router-outlet');
while (outlet.firstChild) {
outlet.removeChild(outlet.firstChild);
}
// history.pushState({}, null, url);
outlet.appendChild(page);
}
}
RouterHandler._onChangeCallbacks = [];
new RouterHandler();

0 comments on commit ef1c2c8

Please sign in to comment.