src/lib/components/chat.component.ts
The main UI component. Should be instantiated near the root of your application.
<!-- plain usage, no configuration -->
<ngx-chat></ngx-chat>
<!-- if supplied, translations contain an object with the structure of the Translations interface. -->
<ngx-chat translations="{'contacts': 'Kontakte', ...}"></ngx-chat>
<!-- if supplied, the contacts input attribute takes an Observable<Contact[]> as source for your roster list -->
<ngx-chat contacts="..."></ngx-chat>
<!-- if supplied, userAvatar$ contains an Obervable<string>, which is used as the src attribute of the img for the current user. -->
<ngx-chat userAvatar$="Observable.of('http://...')"></ngx-chat>
selector | ngx-chat |
styleUrls | ./chat.component.less |
templateUrl | ./chat.component.html |
Properties |
Methods |
|
Inputs |
Accessors |
constructor(chatService: ChatService)
|
||||||
Defined in src/lib/components/chat.component.ts:89
|
||||||
Parameters :
|
contactRequestsReceived$ | |
Type : Observable<Contact[]>
|
|
Defined in src/lib/components/chat.component.ts:61
|
|
If supplied, the contacts input attribute takes an [Observable<Contact[]>]Contact as source for your incoming contact requests list. |
contactRequestsSent$ | |
Type : Observable<Contact[]>
|
|
Defined in src/lib/components/chat.component.ts:68
|
|
If supplied, the contacts input attribute takes an [Observable<Contact[]>]Contact as source for your outgoing contact requests list. |
contacts | |
Type : Observable<Contact[]>
|
|
Defined in src/lib/components/chat.component.ts:54
|
|
If supplied, the contacts input attribute takes an [Observable<Contact[]>]Contact as source for your roster list. |
contactsUnaffiliated$ | |
Type : Observable<Contact[]>
|
|
Defined in src/lib/components/chat.component.ts:75
|
|
If supplied, the contacts input attribute takes an [Observable<Contact[]>]Contact as source for your unaffiliated contact list. |
rosterState | |
Type : "shown" | "hidden"
|
|
Defined in src/lib/components/chat.component.ts:87
|
|
'shown' shows roster list, 'hidden' hides it. |
translations | |
Type : Partial<Translations>
|
|
Defined in src/lib/components/chat.component.ts:36
|
|
If supplied, translations contain an object with the structure of the Translations interface. |
userAvatar$ | |
Type : Observable<string>
|
|
Defined in src/lib/components/chat.component.ts:81
|
|
If supplied, userAvatar$ contains an Observable |
ngOnChanges | ||||||
ngOnChanges(changes: SimpleChanges)
|
||||||
Defined in src/lib/components/chat.component.ts:105
|
||||||
Parameters :
Returns :
void
|
ngOnInit |
ngOnInit()
|
Defined in src/lib/components/chat.component.ts:96
|
Returns :
void
|
Private onChatStateChange | ||||||
onChatStateChange(state: string)
|
||||||
Defined in src/lib/components/chat.component.ts:111
|
||||||
Parameters :
Returns :
void
|
onRosterStateChanged | ||||||
onRosterStateChanged(state: "shown" | "hidden")
|
||||||
Defined in src/lib/components/chat.component.ts:116
|
||||||
Parameters :
Returns :
void
|
Private updateBodyClass |
updateBodyClass()
|
Defined in src/lib/components/chat.component.ts:121
|
Returns :
void
|
showChatComponent |
Default value : false
|
Defined in src/lib/components/chat.component.ts:89
|
translations | ||||||
settranslations(translations: Partial<Translations>)
|
||||||
Defined in src/lib/components/chat.component.ts:36
|
||||||
If supplied, translations contain an object with the structure of the Translations interface.
Parameters :
Returns :
void
|
import { Component, Inject, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
import { Observable } from 'rxjs';
import { Contact } from '../core/contact';
import { Translations } from '../core/translations';
import { defaultTranslations } from '../core/translations-default';
import { CHAT_SERVICE_TOKEN, ChatService } from '../services/chat-service';
/**
* The main UI component. Should be instantiated near the root of your application.
*
* ```html
* <!-- plain usage, no configuration -->
* <ngx-chat></ngx-chat>
*
* <!-- if supplied, translations contain an object with the structure of the Translations interface. -->
* <ngx-chat translations="{'contacts': 'Kontakte', ...}"></ngx-chat>
*
* <!-- if supplied, the contacts input attribute takes an Observable<Contact[]> as source for your roster list -->
* <ngx-chat contacts="..."></ngx-chat>
*
* <!-- if supplied, userAvatar$ contains an Obervable<string>, which is used as the src attribute of the img for the current user. -->
* <ngx-chat userAvatar$="Observable.of('http://...')"></ngx-chat>
* ```
*/
@Component({
selector: 'ngx-chat',
templateUrl: './chat.component.html',
styleUrls: ['./chat.component.less'],
})
export class ChatComponent implements OnInit, OnChanges {
/**
* If supplied, translations contain an object with the structure of the Translations interface.
*/
@Input()
public set translations(translations: Partial<Translations>) {
const defaultTranslation = defaultTranslations();
if (translations) {
this.chatService.translations = {
...defaultTranslation,
...translations,
presence: {
...defaultTranslation.presence,
...translations.presence,
},
};
}
}
/**
* If supplied, the contacts input attribute takes an [Observable<Contact[]>]{@link Contact} as source for your roster list.
*/
@Input()
public contacts?: Observable<Contact[]>;
/**
* If supplied, the contacts input attribute takes an [Observable<Contact[]>]{@link Contact} as source for your incoming contact
* requests list.
*/
@Input()
contactRequestsReceived$?: Observable<Contact[]>;
/**
* If supplied, the contacts input attribute takes an [Observable<Contact[]>]{@link Contact} as source for your outgoing contact
* requests list.
*/
@Input()
contactRequestsSent$?: Observable<Contact[]>;
/**
* If supplied, the contacts input attribute takes an [Observable<Contact[]>]{@link Contact} as source for your unaffiliated contact
* list.
*/
@Input()
contactsUnaffiliated$?: Observable<Contact[]>;
/**
* If supplied, userAvatar$ contains an Observable<string>, which is used as the src attribute of the img for the current user.
*/
@Input()
public userAvatar$?: Observable<string>;
/**
* 'shown' shows roster list, 'hidden' hides it.
*/
@Input()
rosterState: 'shown' | 'hidden';
showChatComponent = false;
constructor(
@Inject(CHAT_SERVICE_TOKEN) private chatService: ChatService,
) {
}
ngOnInit() {
this.chatService.state$.subscribe($e => this.onChatStateChange($e));
this.onRosterStateChanged(this.rosterState);
if (this.userAvatar$) {
this.userAvatar$.subscribe(avatar => this.chatService.userAvatar$.next(avatar));
}
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.rosterState) {
this.onRosterStateChanged(changes.rosterState.currentValue);
}
}
private onChatStateChange(state: string) {
this.showChatComponent = state === 'online';
this.updateBodyClass();
}
onRosterStateChanged(state: 'shown' | 'hidden') {
this.rosterState = state;
this.updateBodyClass();
}
private updateBodyClass() {
const rosterClass = 'has-roster';
if (this.showChatComponent && this.rosterState !== 'hidden') {
document.body.classList.add(rosterClass);
} else {
document.body.classList.remove(rosterClass);
}
}
}
<ngx-chat-window-list *ngIf="showChatComponent"
[rosterState]="rosterState">
</ngx-chat-window-list>
<ngx-chat-roster-list [rosterState]="rosterState"
[contacts]="contacts"
[contactRequestsReceived$]="contactRequestsReceived$"
[contactRequestsSent$]="contactRequestsSent$"
[contactsUnaffiliated$]="contactsUnaffiliated$"
*ngIf="showChatComponent"
(rosterStateChanged)="onRosterStateChanged($event)">
</ngx-chat-roster-list>
./chat.component.less