Skip to content

Commit

Permalink
GH-287 Love to the transpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemke committed Jan 31, 2022
1 parent 1d1255c commit b9ce2c5
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 335 deletions.
1 change: 1 addition & 0 deletions cwt-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/jasmine": "^3.3.9",
"@types/node": "^11.11.2",
"@types/p5": "^0.7.3",
"@types/resize-observer-browser": "^0.1.6",
"@zemke/http-mock": "^4.0.2",
"angular2-template-loader": "^0.6.2",
"css-loader": "^2.1.0",
Expand Down
48 changes: 20 additions & 28 deletions cwt-angular/src/main/webapp/app/message/chat-input.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {
ApplicationRef,
Component,
ComponentFactory,
ComponentFactoryResolver,
ComponentRef,
ElementRef,
EventEmitter,
Injector,
Component,
OnInit,
Output,
Input,
Expand All @@ -16,11 +11,9 @@ import {
AfterViewInit,
QueryList
} from '@angular/core';
import {MentionComponent} from './mention.component';
import {Message, User} from "../custom";
import {Message, UserMinimalDto, MessageDto, JwtUser} from "../custom";
import {AuthService} from "../_services/auth.service";
import {RequestService} from "../_services/request.service";
import {Utils} from "../_util/utils";

@Component({
selector: 'cwt-chat-input',
Expand Down Expand Up @@ -88,24 +81,22 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChildren("suggestions")
private suggestionsEl: QueryList<ElementRef<HTMLDivElement>>;

@ViewChildren("recipients")
private recipientsEl: QueryList<ElementRef<HTMLDivElement>>;

suggestions: UserMinimalDto[]? = null;
suggestions: UserMinimalDto[] = null;
recipients: UserMinimalDto[] = [];
tags = [];
tags: {user: UserMinimalDto, style: {width: string; left: string}}[] = [];
disabled = false;
suggestionsSlice = 4;

private authUser: JwtUser;
private allSuggestions: UserMinimalDto[] = [];
private lazyLoadedSuggestions: boolean = false;
private lazyLoadingSuggestions: boolean = false;
private lazySuggestionsResolver: () => void;
private lazySuggestionsPromise: Promise<void>;
private paddingLeft: number = 0;
private resizeObserver: ResizeObserver;
private scrollLeft = 0;
private documentClickListener = (e: ClickEvent) => {
private documentClickListener = (e: MouseEvent) => {
e.target === this.chatInputEl.nativeElement
? this.suggest()
: (this.suggestions = null);
Expand Down Expand Up @@ -142,7 +133,7 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {

document.addEventListener('click', this.documentClickListener);

this.resizeObserver = new ResizeObserver(([entry, ..._]) => {
this.resizeObserver = new ResizeObserver(() => {
window.requestAnimationFrame(() => {
this.updateRecipients();
this.styleOffsetsEl();
Expand Down Expand Up @@ -173,7 +164,7 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
body: this.chatInputEl.nativeElement.value,
recipients: this.recipients,
category: this.recipients?.length ? 'PRIVATE' : 'SHOUTBOX',
};
} as Message;
this.message.emit([message, (success: boolean) => {
this.disabled = false;
if (success) {
Expand All @@ -198,12 +189,12 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
}

private styleDummyEl() {
const {fontSize, fontFamily, paddingLeft} = window.getComputedStyle(this.chatInputEl.nativeElement);
const {fontSize, fontFamily} = window.getComputedStyle(this.chatInputEl.nativeElement);
this.dummyEl.nativeElement.style.fontSize = fontSize;
this.dummyEl.nativeElement.style.fontFamily = fontFamily;
}

private styleDropdownEl(q=null, v=null) {
private styleDropdownEl(q: string = null, v: string = null) {
if (this.dropdownEl?.nativeElement == null) return;
if (q == null || v == null) {
[q, v] = this.getProc();
Expand All @@ -214,7 +205,7 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
window.innerWidth - 200) + 'px';
}

public complete(user, fromClick=false) {
public complete(user: UserMinimalDto, fromClick: boolean = false) {
const inpElem = this.chatInputEl.nativeElement;
const [q, v, caret] = this.getProc();
inpElem.value =
Expand All @@ -228,13 +219,14 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
this.updateRecipients();
}

public onKeydown(e) {
public onKeydown(e: KeyboardEvent) {
const key = e.key === 'Unidentified' ? String.fromCharCode(e.which) : e.key;
if (this.suggestions?.length && ['ArrowDown', 'ArrowUp', 'Tab', 'Enter'].includes(key)) {
e.preventDefault();
const buttons = Array.from(this.suggestionsEl).map(el => el.nativeElement);
const buttons = Array.from(this.suggestionsEl as unknown as Iterable<ElementRef>)
.map(el => el.nativeElement);

let active;
let active: number;
for (let i = 0; i < buttons.length; i++) {
if (buttons[i].classList.contains('active')) {
active = i;
Expand All @@ -261,14 +253,14 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
}
}

public onKeyup(e) {
public onKeyup(e: KeyboardEvent) {
const key = e.key === 'Unidentified' ? String.fromCharCode(e.which) : e.key;
if (key.length > 1 && !['ArrowDown', 'ArrowUp', 'Tab', 'Enter', 'Backspace', 'Delete'].includes(key)) {
this.suggest();
}
}

public onInput(e) {
public onInput() {
this.suggest();
setTimeout(() => this.updateRecipients());
}
Expand Down Expand Up @@ -311,7 +303,7 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
}

private suggest() {
const [q, v, _] = this.getProc();
const [q, v,] = this.getProc();
if (q == null) {
this.suggestions = null;
return;
Expand All @@ -335,7 +327,7 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
return this.lazySuggestionsPromise;
}
this.lazyLoadingSuggestions = true;
return this.requestService.get<UserMinimalDto[]>("user", {minimal: true}).toPromise()
return this.requestService.get<UserMinimalDto[]>("user", {minimal: "true"}).toPromise()
.then(users => this.allSuggestions.push(
...users.filter(user =>
!this.allSuggestions.map(u => u.id).includes(user.id) && user.id !== this.authUser.id)))
Expand All @@ -354,7 +346,7 @@ export class ChatInputComponent implements OnInit, AfterViewInit, OnDestroy {
* @returns {Array} The current typeahead string and the string
* until associated @ sign and the caret index.
*/
private getProc() {
private getProc(): [string, string, number] {
const {selectionStart, value} = this.chatInputEl.nativeElement;
const v = value.substring(0, selectionStart);
if (v.indexOf('@') === -1) return [null, v, selectionStart];
Expand Down
5 changes: 2 additions & 3 deletions cwt-angular/src/main/webapp/app/message/chat.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {ViewChild, Component, Inject, Input, OnDestroy, OnInit} from '@angular/core';
import {Component, Inject, Input, OnDestroy, OnInit} from '@angular/core';
import {RequestService} from "../_services/request.service";
import {JwtUser, Message, MessageCategory, MessageCreationDto, MessageDto, UserMinimaDto} from "../custom";
import {JwtUser, Message, MessageCategory, MessageCreationDto, MessageDto} from "../custom";
import {AuthService} from "../_services/auth.service";
import {Toastr} from "../_services/toastr";
import {APP_CONFIG, AppConfig} from "../app.config";
import {finalize} from "rxjs/operators";

@Component({
selector: 'cwt-chat',
Expand Down
7 changes: 4 additions & 3 deletions cwt-angular/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"compilerOptions": {
"module": "esnext",
"target": "es5",
"target": "es6",
"moduleResolution": "node",
"lib": [
"es2015",
"es2020",
"dom"
],
"emitDecoratorMetadata": true,
Expand All @@ -20,7 +20,8 @@
"experimentalDecorators": true,
"types": [
"node",
"jasmine"
"jasmine",
"resize-observer-browser"
]
},
"include": [
Expand Down
Loading

0 comments on commit b9ce2c5

Please sign in to comment.