<commit_before>import {
  Component,
  OnInit,
  ElementRef,
  ChangeDetectionStrategy,
  Input
} from '@angular/core';

import {FORM_DIRECTIVES} from '@angular/common';

import {
  MessageService,
  ThreadService,
  UserService
} from '../../shared';

import { Observable } from 'rxjs/Observable';
import { User, Thread, Message, SocketService } from '../../shared';

import { ChatMessageComponent } from './chat-message.component';

@Component({
  selector: 'chat-window',
  directives: [ChatMessageComponent,
               FORM_DIRECTIVES],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: 'app/+chat-container/+chat-window/chat-window.component.html',
  styleUrls: ['app/+chat-container/+chat-window/chat-window.component.css']
})
export class ChatWindowComponent implements OnInit {
  isPressEnter: boolean = true;
  messages: Observable<any>;
  currentThread: Thread;
  draftMessage: Message;
  currentUser: User;
  lastAuthor: User;
  @Input() parentHeight: number;

  constructor(private messageService: MessageService,
              private threadService: ThreadService,
              private userService: UserService,
              private socketService: SocketService,
              private el: ElementRef) {
  }

  ngOnInit(): void {
    // Calculate height of body box
    this.fixWindow();

    this.messages = this.threadService.currentThreadMessages;

    this.draftMessage = new Message();

    this.threadService.currentThread.subscribe(
      (thread: Thread) => {
        this.currentThread = thread;
      });

    this.userService.currentUser.subscribe((user) => {
      this.currentUser = new User(user);
    });

    this.messages
      .subscribe(
        (messages: Array<Message>) => {
          setTimeout(() => {
            this.scrollToBottom();
          });
        });

  }
  onEnter(event: any): void {
    if(!this.isPressEnter && !event.ctrlKey)
      return;
    this.sendMessage();
    event.preventDefault();
  }

  sendMessage(): void {
    let m: Message = this.draftMessage;
    m.author = this.currentUser;
    m.thread = this.currentThread;
    m.isRead = true;
    // this.messageService.addMessage(m);
    this.socketService.addMessage(m);
    this.draftMessage = new Message();
  }

  scrollToBottom(): void {
    // let scrollPane: any = this.el
    //   .nativeElement.querySelector('.msg-container-base');
    // scrollPane.scrollTop = scrollPane.scrollHeight;
  }

  updateLastAuthor(author: User): void {
    this.lastAuthor = author;
  }

  private fixWindow() {
    let elHeader = this.el.nativeElement.children[0].children[0];
    let elBody = this.el.nativeElement.children[0].children[1];
    let elFooter = this.el.nativeElement.children[0].children[2];

    let minHeight = this.parentHeight - (elHeader.offsetHeight + elFooter.offsetHeight + 70);      
    elBody.style.height = minHeight + 'px';
  }
}
<commit_msg>Auto scroll down when message come<commit_after>import {
  Component,
  OnInit,
  ElementRef,
  ChangeDetectionStrategy,
  Input
} from '@angular/core';

import {FORM_DIRECTIVES} from '@angular/common';

import {
  MessageService,
  ThreadService,
  UserService
} from '../../shared';

import { Observable } from 'rxjs/Observable';
import { User, Thread, Message, SocketService } from '../../shared';

import { ChatMessageComponent } from './chat-message.component';

@Component({
  selector: 'chat-window',
  directives: [ChatMessageComponent,
               FORM_DIRECTIVES],
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: 'app/+chat-container/+chat-window/chat-window.component.html',
  styleUrls: ['app/+chat-container/+chat-window/chat-window.component.css']
})
export class ChatWindowComponent implements OnInit {
  isPressEnter: boolean = true;
  messages: Observable<any>;
  currentThread: Thread;
  draftMessage: Message;
  currentUser: User;
  lastAuthor: User;
  @Input() parentHeight: number;

  constructor(private messageService: MessageService,
              private threadService: ThreadService,
              private userService: UserService,
              private socketService: SocketService,
              private el: ElementRef) {
  }

  ngOnInit(): void {
    // Calculate height of body box
    this.fixWindow();

    this.messages = this.threadService.currentThreadMessages;

    this.draftMessage = new Message();

    this.threadService.currentThread.subscribe(
      (thread: Thread) => {
        this.currentThread = thread;
      });

    this.userService.currentUser.subscribe((user) => {
      this.currentUser = new User(user);
    });

    this.messages
      .subscribe(
        (messages: Array<Message>) => {
          setTimeout(() => {
            this.scrollToBottom();
          });
        });

  }
  onEnter(event: any): void {
    if(!this.isPressEnter && !event.ctrlKey)
      return;
    this.sendMessage();
    event.preventDefault();
  }

  sendMessage(): void {
    let m: Message = this.draftMessage;
    m.author = this.currentUser;
    m.thread = this.currentThread;
    m.isRead = true;
    // this.messageService.addMessage(m);
    this.socketService.addMessage(m);
    this.draftMessage = new Message();
  }

  scrollToBottom(): void {
    let scrollPane: any = this.el
      .nativeElement.querySelector('#chat-box');
    scrollPane.scrollTop = scrollPane.scrollHeight;
  }

  updateLastAuthor(author: User): void {
    this.lastAuthor = author;
  }

  private fixWindow() {
    let elHeader = this.el.nativeElement.children[0].children[0];
    let elBody = this.el.nativeElement.children[0].children[1];
    let elFooter = this.el.nativeElement.children[0].children[2];

    let minHeight = this.parentHeight - (elHeader.offsetHeight + elFooter.offsetHeight + 70);      
    elBody.style.height = minHeight + 'px';
  }
}

{'fraction_non_alphanumeric': 0.12797927461139896, 'fraction_numerical': 0.002763385146804836, 'mean_word_length': 3.1572146446518308, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820659', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1673, 'n_words': 341}