Skip to content

Commit 4d1fa34

Browse files
committed
feat(tabs): add topics tab in let menu
1 parent 779cddd commit 4d1fa34

File tree

8 files changed

+96
-28
lines changed

8 files changed

+96
-28
lines changed

src/app/app.component.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@ import { Component, ViewChild } from '@angular/core';
22
import { Nav, Platform } from 'ionic-angular';
33
import { StatusBar } from '@ionic-native/status-bar';
44
import { SplashScreen } from '@ionic-native/splash-screen';
5+
import { TabsService } from '../providers/tabs-service';
6+
import { Tab } from '../classes/tab';
57

68

79
@Component({
8-
templateUrl: 'app.html'
10+
templateUrl: 'app.html',
11+
providers: [TabsService]
912
})
1013
export class MyApp {
1114
@ViewChild(Nav) nav: Nav;
1215

1316
rootPage = 'topics';
1417

15-
pages: Array<{title: string, component: any}>;
18+
tabs: Tab[];
1619

17-
constructor(public platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
20+
constructor(public platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private tabsService: TabsService) {
1821
platform.ready().then(() => {
1922
// Okay, so the platform is ready and our plugins are available.
2023
// Here you can do any higher level native things you might need.
@@ -27,6 +30,14 @@ export class MyApp {
2730
}
2831

2932
ngAfterViewInit() {
30-
console.log('ng after view init')
33+
console.log('ng after view init');
34+
this.tabs = this.tabsService.getTabs();
35+
}
36+
37+
openTab(tab: any) {
38+
console.log('open tab', tab)
39+
this.nav.setRoot('topics', {
40+
tab: tab.value
41+
});
3142
}
3243
}

src/app/app.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
<ion-content>
99
<ion-list>
10-
<button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
11-
{{p.title}}
12-
</button>
10+
<ion-item class="item-divider">
11+
板块
12+
</ion-item>
13+
<ion-item *ngFor="let tab of tabs" (click)="openTab(tab)" menuClose>
14+
{{tab.label}}
15+
</ion-item>
1316
</ion-list>
1417
</ion-content>
1518

1619
</ion-menu>
1720

1821
<!-- Disable swipe-to-go-back because it's poor UX to combine STGB with side menus -->
19-
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
22+
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>

src/classes/tab.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Tab {
2+
value: string;
3+
label: string;
4+
}
5+
6+
File renamed without changes.

src/pages/topic/topic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component } from '@angular/core';
22
import { NavController, NavParams, IonicPage } from 'ionic-angular';
33

4-
import { Topic } from '../topics/topic';
4+
import { Topic } from '../../classes/topic';
55
import { TopicService } from '../../providers/topic-service';
66
/*
77
Generated class for the Topic page.

src/pages/topics/topics.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component } from '@angular/core';
22
import { NavController, NavParams, IonicPage } from 'ionic-angular';
33

4-
import { Topic } from './topic';
4+
import { Topic } from '../../classes/topic';
55

66
import { TopicService } from '../../providers/topic-service';
77

@@ -13,12 +13,13 @@ import { TopicService } from '../../providers/topic-service';
1313
*/
1414

1515
@IonicPage({
16-
name: 'topics'
16+
name: 'topics',
17+
segment: 'topics/:tab'
1718
})
1819
@Component({
1920
selector: 'page-topics',
2021
templateUrl: 'topics.html',
21-
providers: [TopicService]
22+
providers: [TopicService]
2223
})
2324
export class TopicsPage {
2425

@@ -28,31 +29,39 @@ export class TopicsPage {
2829
constructor(public navCtrl: NavController, public navParams: NavParams, private topicService: TopicService) {
2930
}
3031

31-
ngOnInit(): void {
32-
this.getTopics()
33-
}
32+
ngOnInit(): void {
33+
let newTab = this.navParams.get('tab');
34+
if (newTab) {
35+
console.log('changing tab');
36+
this.topicService.currentTab = newTab;
37+
}
38+
console.log('current tab', this.topicService.currentTab);
39+
this.doRefresh();
40+
}
3441

3542
ionViewDidLoad() {
3643
console.log('ionViewDidLoad TopicsPage');
3744
}
3845

39-
onSelect(topic: Topic): void {
40-
console.log(topic)
41-
this.navCtrl.push('topic', {id: topic.id})
42-
}
46+
onSelect(topic: Topic): void {
47+
console.log(topic)
48+
this.navCtrl.push('topic', {id: topic.id})
49+
}
4350

44-
getTopics(): void {
45-
this.topicService.getTopics().then(
46-
topics => this.topics = topics
47-
);
48-
}
51+
getTopics(): void {
52+
this.topicService.getTopics().then(
53+
topics => this.topics = topics
54+
);
55+
}
4956

50-
doRefresh(refresher): void {
57+
doRefresh(refresher = undefined): void {
5158
console.log('do refresh')
5259
this.topicService.refresh().then(
5360
(topics) => {
5461
this.topics = topics;
55-
refresher.complete();
62+
if (refresher) {
63+
refresher.complete();
64+
}
5665
}
5766
);
5867
}

src/providers/tabs-service.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Injectable } from '@angular/core';
2+
3+
import { Tab } from '../classes/tab';
4+
/*
5+
Generated class for the TabsService provider.
6+
7+
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
8+
for more info on providers and Angular 2 DI.
9+
*/
10+
@Injectable()
11+
export class TabsService {
12+
tabs: Tab[] = [
13+
{
14+
value: 'all',
15+
label: '最新'
16+
},
17+
{
18+
value: 'share',
19+
label: '分享'
20+
},
21+
{
22+
value: 'ask',
23+
label: '问答'
24+
},
25+
{
26+
value: 'job',
27+
label: '招聘'
28+
},
29+
{
30+
value: undefined,
31+
label: '其他'
32+
}
33+
]
34+
35+
getTabs(): Tab[] {
36+
return this.tabs;
37+
}
38+
}
39+

src/providers/topic-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Http } from '@angular/http';
33
import 'rxjs/add/operator/map';
44
import 'rxjs/add/operator/toPromise';
55

6-
import { Topic } from '../pages/topics/topic';
6+
import { Topic } from '../classes/topic';
77

88
/*
99
Generated class for the TopicService provider.
@@ -15,7 +15,7 @@ import { Topic } from '../pages/topics/topic';
1515
export class TopicService {
1616
private topicsUrl: string = 'https://cnodejs.org/api/v1/topics'
1717
private topicUrl: string = 'https://cnodejs.org/api/v1/topic'
18-
private currentTab: string = 'all'
18+
currentTab: string = 'all'
1919
private nextPage: number = 2
2020
private hasNextPage: boolean = true
2121
topics: Topic[] = []

0 commit comments

Comments
 (0)