-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
142 lines (111 loc) · 3.33 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { bootstrap } from "angular2/platform/browser";
import { Component } from "angular2/core";
class Article {
title: string;
link: string;
votes: number;
constructor(title: string, link: string, votes?: number){
this.title = title;
this.link = link;
this.votes = votes || 0;
}
voteUp(): void {
this.votes += 1;
}
voteDown(): void {
this.votes -= 1;
}
}
@Component({
selector:'reddit-article',
host:{
class:'row'
},
inputs: ['article'],
template:`
<div class="four wide column center aligned votes">
<div class="ui statistic">
<div class="value">
{{ article.votes }}
</div>
<div class="label">
Points
</div>
</div>
</div>
<div class="twelve wide column">
<a class="ui large header" href="{{ article.link }}">
{{ article.title }}
</a>
<ul class="ui big horizontal list voters">
<li class="item">
<a href (click)="voteUp()">
<i class="arrow up icon">Upvote</i>
</a>
</li>
<li class="item">
<a href (click)="voteDown()">
<i class="arrow down icon">Downvote</i>
</a>
</li>
</ul>
</div>
`
})
class ArticleComponent{
article: Article;
constructor(){
}
voteDown(): boolean {
this.article.voteDown();
return false;
}
voteUp(): boolean {
this.article.voteUp();
return false;
}
}
@Component({
selector:"reddit",
directives:[ArticleComponent],
template:`
<form class="ui large form segment">
<h3 class="ui header">Add a Link</h3>
<div class="field">
<label for="title">Title:</label>
<input name="title" #newTitle/>
</div>
<div class="field">
<label for="link">Link:</label>
<input name="link" #newLink/>
</div>
<button (click)="addArticle(newTitle, newLink)" class="ui positive right floated button">
Submit Link
</button>
<div class="ui grid posts">
<reddit-article
*ngFor = "#article of articles"
[article] = "article" >
</reddit-article>
</div>
</form>
`
})
class RedditApp{
articles: Article[];
constructor(){
this.articles = [
new Article("ms", "http://www.google.com", 10),
new Article("google", "http://www.google.com", 5),
new Article("fb", "http://www.facebook.com", 4),
new Article("apple", "http://www.apple.com", 8)
];
}
addArticle(title:HTMLInputElement, link:HTMLInputElement) : void {
console.log(`Adding article title ${title.value} and link as ${link.value}`);
this.articles.push(new Article(title.value, link.value, 0));
title.value = '';
link.value = '';
}
}
bootstrap(RedditApp);