Skip to content

Commit

Permalink
feat: Added a simple tabs example (no routing)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubyboy committed Apr 30, 2016
1 parent 0784ea1 commit f7a1787
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 68 deletions.
6 changes: 4 additions & 2 deletions app/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {SideNav} from "./components/side-nav";
import {Forms} from "./components/forms";
import {Tabs} from "./components/tabs";
import {TabsRouting} from "./components/tabs-routing";
import {Dialogs} from "./components/dialogs";
import {Dropdown} from "./components/dropdown";
import {Collapsible} from "./components/collapsible";
Expand All @@ -13,7 +14,7 @@ import "../src/index";

@Component({
selector: "my-app",
directives: [ROUTER_DIRECTIVES, Buttons, Collapsible, Dropdown, Dialogs, Tabs, Forms, SideNav, ModelBindings],
directives: [ROUTER_DIRECTIVES, Buttons, Collapsible, Dropdown, Dialogs, Tabs, TabsRouting, Forms, SideNav, ModelBindings],
styles: [`header, main, footer { padding-left: 260px; }`],
template: `
Expand All @@ -33,7 +34,8 @@ import "../src/index";
{path: "/dialogs", component: Dialogs, name: "Dialogs"},
{path: "/dropdowns", component: Dropdown, name: "Dropdown"},
{path: "/forms", component: Forms, name: "Forms"},
{path: "/tabs/...", component: Tabs, name: "Tabs"},
{path: "/tabs/", component: Tabs, name: "Tabs"},
{path: "/tabs-routing/...", component: TabsRouting, name: "TabsRouting"},
{path: "/datepicker", component: DatePicker, name: "DatePicker"},
{path: "/modelbindings", component: ModelBindings, name: "ModelBindings"},
])
Expand Down
2 changes: 1 addition & 1 deletion app/components/side-nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {Router, RouterLink, Location} from "angular2/router"
})
export class SideNav {

private routeNames = ["Buttons", "Collapsible", "Dialogs", "Dropdown", "Forms", "Tabs", "DatePicker", "ModelBindings"];
private routeNames = ["Buttons", "Collapsible", "Dialogs", "Dropdown", "Forms", "Tabs", "TabsRouting", "DatePicker", "ModelBindings"];

constructor(private _location:Location, private _router:Router) {
}
Expand Down
74 changes: 74 additions & 0 deletions app/components/tabs-routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {MaterializeDirective} from "../../src/index";
import {Component,OnDestroy} from "angular2/core"
import {Subscription} from "rxjs/subscription";
import {Router, RouteConfig, Location, ROUTER_DIRECTIVES} from "angular2/router";

@Component({selector: "tabs-test1",template: `<div id="test1" class="col s12">Test 1</div>`})
class TabsTest1 {}
@Component({selector: "tabs-test2",template: `<div id="test2" class="col s12">Test 2</div>`})
class TabsTest2 {}
@Component({selector: "tabs-test3",template: `<div id="test3" class="col s12">Test 3</div>`})
class TabsTest3 {}
@Component({selector: "tabs-test4",template: `<div id="test4" class="col s12">Test 4</div>`})
class TabsTest4 {}

@Component({
selector: "tabs-routing",
directives: [ROUTER_DIRECTIVES,MaterializeDirective,TabsTest1,TabsTest2,TabsTest3,TabsTest4],
template: `
<div class="row">
<div class="col s12">
<ul materialize="tabs" [materializeParams]="tabSelectionParams" class="tabs">
<li class="tab col s3" [class.disabled]="tab.disabled" *ngFor="#tab of tabs">
<a [href]="'#'+tab.route" (click)="$event.preventDefault();routeTo(tab.route)">{{tab.name}}</a>
</li>
</ul>
</div>
<br/>
<router-outlet></router-outlet>
</div>
`
})
@RouteConfig([
{path: "/test1", component: TabsTest1, name: "TabsTest1", useAsDefault:true},
{path: "/test2", component: TabsTest2, name: "TabsTest2"},
{path: "/test3", component: TabsTest3, name: "TabsTest3"},
{path: "/test4", component: TabsTest4, name: "TabsTest4"}
])
export class TabsRouting implements OnDestroy {

private tabs = [
{name:"Test 1",href:"/tabs/test1",route:"TabsTest1"},
{name:"Test 2",href:"/tabs/test2",route:"TabsTest2"},
{name:"Test 3",href:"/tabs/test3",route:"TabsTest3", disabled:true},
{name:"Test 4",href:"/tabs/test4",route:"TabsTest4"}
];
private routerSubscription:Subscription;
private tabSelectionParams = null;

constructor(private router:Router, private location:Location) {
this.routerSubscription = <Subscription>router.parent.subscribe(() => {
this.updateSelectionParams(router);
});
}

routeTo(route) {
this.router.navigate([route]);
}

updateSelectionParams(router) {
for (var i=0; i<this.tabs.length; i++) {
const tab = this.tabs[i];
if (location.href.lastIndexOf(tab.href)==location.href.length-tab.href.length) {
if (!this.tabSelectionParams || this.tabSelectionParams[0]!=tab.route) {
// switch the view to select that tab
this.tabSelectionParams = ['select_tab',tab.route];
}
}
};
}

ngOnDestroy() {
this.routerSubscription.unsubscribe();
}
}
82 changes: 17 additions & 65 deletions app/components/tabs.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,26 @@
import {MaterializeDirective} from "../../src/index";
import {Component,OnDestroy} from "angular2/core"
import {Subscription} from "rxjs/subscription";
import {Router, RouteConfig, Location, ROUTER_DIRECTIVES} from "angular2/router";

@Component({selector: "tabs-test1",template: `<div id="test1" class="col s12">Test 1</div>`})
class TabsTest1 {}
@Component({selector: "tabs-test2",template: `<div id="test2" class="col s12">Test 2</div>`})
class TabsTest2 {}
@Component({selector: "tabs-test3",template: `<div id="test3" class="col s12">Test 3</div>`})
class TabsTest3 {}
@Component({selector: "tabs-test4",template: `<div id="test4" class="col s12">Test 4</div>`})
class TabsTest4 {}
import {Component} from "angular2/core"

@Component({
selector: "tabs",
directives: [ROUTER_DIRECTIVES,MaterializeDirective,TabsTest1,TabsTest2,TabsTest3,TabsTest4],
directives: [MaterializeDirective],
template: `
<div class="row">
<div class="col s12">
<ul materialize="tabs" [materializeParams]="tabSelectionParams" class="tabs">
<li class="tab col s3" [class.disabled]="tab.disabled" *ngFor="#tab of tabs">
<a [href]="'#'+tab.route" (click)="$event.preventDefault();routeTo(tab.route)">{{tab.name}}</a>
</li>
</ul>
</div>
<br/>
<router-outlet></router-outlet>
</div>
<div class="row">
<div class="col s12">
<ul class="tabs" materialize="tabs">
<li class="tab col s3"><a href="#test1">Test 1</a></li>
<li class="tab col s3"><a class="active" href="#test2">Test 2</a></li>
<li class="tab col s3 disabled"><a href="#test3">Disabled Tab</a></li>
<li class="tab col s3"><a href="#test4">Test 4</a></li>
</ul>
</div>
<div id="test1" class="col s12">Test 1</div>
<div id="test2" class="col s12">Test 2</div>
<div id="test3" class="col s12">Test 3</div>
<div id="test4" class="col s12">Test 4</div>
</div>
`
})
@RouteConfig([
{path: "/test1", component: TabsTest1, name: "TabsTest1", useAsDefault:true},
{path: "/test2", component: TabsTest2, name: "TabsTest2"},
{path: "/test3", component: TabsTest3, name: "TabsTest3"},
{path: "/test4", component: TabsTest4, name: "TabsTest4"}
])
export class Tabs implements OnDestroy {

private tabs = [
{name:"Test 1",href:"/tabs/test1",route:"TabsTest1"},
{name:"Test 2",href:"/tabs/test2",route:"TabsTest2"},
{name:"Test 3",href:"/tabs/test3",route:"TabsTest3", disabled:true},
{name:"Test 4",href:"/tabs/test4",route:"TabsTest4"}
];
private routerSubscription:Subscription;
private tabSelectionParams = null;

constructor(private router:Router, private location:Location) {
this.routerSubscription = <Subscription>router.parent.subscribe(() => {
this.updateSelectionParams(router);
});
}

routeTo(route) {
this.router.navigate([route]);
}

updateSelectionParams(router) {
for (var i=0; i<this.tabs.length; i++) {
const tab = this.tabs[i];
if (location.href.lastIndexOf(tab.href)==location.href.length-tab.href.length) {
if (!this.tabSelectionParams || this.tabSelectionParams[0]!=tab.route) {
// switch the view to select that tab
this.tabSelectionParams = ['select_tab',tab.route];
}
}
};
}
export class Tabs {

ngOnDestroy() {
this.routerSubscription.unsubscribe();
}
}

0 comments on commit f7a1787

Please sign in to comment.