forked from ng-book/angular2-rxjs-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatWindow.ts
172 lines (150 loc) · 4.39 KB
/
ChatWindow.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {
Component,
OnInit,
ElementRef,
ChangeDetectionStrategy
} from '@angular/core';
import {FORM_DIRECTIVES} from '@angular/common';
import {
MessagesService,
ThreadsService,
UserService
} from '../services/services';
import {FromNowPipe} from '../util/FromNowPipe';
import {Observable} from 'rxjs';
import {User, Thread, Message} from '../models';
@Component({
inputs: ['message'],
selector: 'chat-message',
pipes: [FromNowPipe],
template: `
<div class="msg-container"
[ngClass]="{'base-sent': !incoming, 'base-receive': incoming}">
<div class="avatar"
*ngIf="!incoming">
<img src="{{message.author.avatarSrc}}">
</div>
<div class="messages"
[ngClass]="{'msg-sent': !incoming, 'msg-receive': incoming}">
<p>{{message.text}}</p>
<time>{{message.sender}} • {{message.sentAt | fromNow}}</time>
</div>
<div class="avatar"
*ngIf="incoming">
<img src="{{message.author.avatarSrc}}">
</div>
</div>
`
})
export class ChatMessage implements OnInit {
message: Message;
currentUser: User;
incoming: boolean;
constructor(public userService: UserService) {
}
ngOnInit(): void {
this.userService.currentUser
.subscribe(
(user: User) => {
this.currentUser = user;
if (this.message.author && user) {
this.incoming = this.message.author.id !== user.id;
}
});
}
}
@Component({
selector: 'chat-window',
directives: [ChatMessage,
FORM_DIRECTIVES],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="chat-window-container">
<div class="chat-window">
<div class="panel-container">
<div class="panel panel-default">
<div class="panel-heading top-bar">
<div class="panel-title-container">
<h3 class="panel-title">
<span class="glyphicon glyphicon-comment"></span>
Chat - {{currentThread.name}}
</h3>
</div>
<div class="panel-buttons-container">
<!-- you could put minimize or close buttons here -->
</div>
</div>
<div class="panel-body msg-container-base">
<chat-message
*ngFor="let message of messages | async"
[message]="message">
</chat-message>
</div>
<div class="panel-footer">
<div class="input-group">
<input type="text"
class="chat-input"
placeholder="Write your message here..."
(keydown.enter)="onEnter($event)"
[(ngModel)]="draftMessage.text" />
<span class="input-group-btn">
<button class="btn-chat"
(click)="onEnter($event)"
>Send</button>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
`
})
export class ChatWindow implements OnInit {
messages: Observable<any>;
currentThread: Thread;
draftMessage: Message;
currentUser: User;
constructor(public messagesService: MessagesService,
public threadsService: ThreadsService,
public userService: UserService,
public el: ElementRef) {
}
ngOnInit(): void {
this.messages = this.threadsService.currentThreadMessages;
this.draftMessage = new Message();
this.threadsService.currentThread.subscribe(
(thread: Thread) => {
this.currentThread = thread;
});
this.userService.currentUser
.subscribe(
(user: User) => {
this.currentUser = user;
});
this.messages
.subscribe(
(messages: Array<Message>) => {
setTimeout(() => {
this.scrollToBottom();
});
});
}
onEnter(event: any): void {
this.sendMessage();
event.preventDefault();
}
sendMessage(): void {
let m: Message = this.draftMessage;
m.author = this.currentUser;
m.thread = this.currentThread;
m.isRead = true;
this.messagesService.addMessage(m);
this.draftMessage = new Message();
}
scrollToBottom(): void {
let scrollPane: any = this.el
.nativeElement.querySelector('.msg-container-base');
scrollPane.scrollTop = scrollPane.scrollHeight;
}
}