Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Component, ElementRef, EventEmitter, HostBinding, HostListener, Input, OnChanges, Output, QueryList, SimpleChanges, TemplateRef, ViewChildren, } from '@angular/core'; import { FocusableElementOwner, Item, Key } from '../utils/util.types'; import { ConversationItemComponent } from '../internal/conversation-item/conversation-item.component'; // Item type BasicItem = Item; export type QuestionItem = BasicItem & { type: 'Question'; time: string; }; export type AnswerItem = BasicItem & { type: 'Answer'; time: string; partial: boolean; rating?: string; }; export type HintItem = BasicItem & { type: 'Hint'; }; export type ConversationItem = QuestionItem | AnswerItem | HintItem; export function isAnswerItem(i: ConversationItem | undefined): i is AnswerItem { return i !== undefined && i.type === 'Answer'; } export function isQuestionItem( i: ConversationItem | undefined, ): i is QuestionItem { return i !== undefined && i.type === 'Question'; } export interface ConversationItemTemplateContext { $implicit: ConversationItem; } @Component({ selector: 'gc-conversation', templateUrl: './conversation.component.html', styleUrl: './conversation.component.css', standalone: false, }) export class ConversationComponent implements FocusableElementOwner, OnChanges { /** emitted when a rate answer button is pressed. */ @Output() public readonly onRateItem: EventEmitter<Key> = new EventEmitter<Key>(); /** The convsation items */ @Input() public items: readonly ConversationItem[] = []; /** Optional icon provider to use for the selected entry */ @Input() public iconProvider?: TemplateRef<ConversationItemTemplateContext>; /** @ignore */ @HostBinding('tabindex') protected _tabIndex = -1; /** @ignore */ @ViewChildren(ConversationItemComponent) private readonly conversationItems?: QueryList<ConversationItemComponent>; /** @ignore */ protected _lastFocusItemKey: Key | undefined; constructor(private readonly host: ElementRef<HTMLElement>) {} /** @ignore */ @HostListener('focus') protected _handle_onfocus() { this._impl_restoreFocus(); } /** @ignore */ @HostListener('keydown', ['$event']) protected _handleKeyDown(e: KeyboardEvent) { switch (e.key) { case 'ArrowDown': this._impl_focusDown(); e.preventDefault(); break; case 'ArrowUp': this._impl_focusUp(); e.preventDefault(); break; case 'Home': this._impl_focusFirst(); e.preventDefault(); break; case 'End': this._impl_focusLast(); e.preventDefault(); break; default: } } /** gives to focus to the item which was focused before. */ public focusChild(): boolean { return this._impl_restoreFocus(); } /** focuses the given item */ public focusItem(item: ConversationItem) { this.conversationItems?.find(it => it.key === item.key)?.focusChild(); } /** @ignore */ ngOnChanges(changes: SimpleChanges): void { if ('items' in changes) { // reset last focus if item was removed const focusItem = this.items.find( it => it.key === this._lastFocusItemKey, ); if (!focusItem) { this._lastFocusItemKey = undefined; } // initialize last focus to last item if (this._lastFocusItemKey === undefined && this.items.length > 0) { this._lastFocusItemKey = this.items[this.items.length - 1].key; } this._impl_ensure_scrolledToBottom(); } } /** @ignore */ protected onItemFocused(it: ConversationItemComponent) { this._lastFocusItemKey = it.key; } /** @ignore */ protected _impl_triggerRating(itemKey: string) { this.onRateItem.emit(itemKey); } /** @ignore */ protected _impl_time(it: ConversationItem) { if (isAnswerItem(it) || isQuestionItem(it)) { return it.time; } return undefined; } /** @ignore */ protected _impl_rating(it: ConversationItem) { if (isAnswerItem(it)) { return it.rating; } return undefined; } /** @ignore */ protected _impl_partial(it: ConversationItem) { if (isAnswerItem(it)) { return it.partial; } return false; } /** @ignore */ private _impl_getLastItem() { return this.conversationItems?.last; } /** @ignore */ private _impl_computeNextKey(key: string): string { const curIdx = this.items.findIndex(it => it.key === key); const nextIdx = Math.min(this.items.length - 1, curIdx + 1); return this.items[nextIdx].key; } /** @ignore */ private _impl_computePrevKey(key: string): string { const curIdx = this.items.findIndex(it => it.key === key); const nextIdx = Math.max(0, curIdx - 1); return this.items[nextIdx].key; } /** @ignore */ private _impl_getFocusItem() { return this.conversationItems?.find( it => it.key === this._lastFocusItemKey, ); } /** @ignore */ private _impl_restoreFocus(): boolean { return this._impl_getFocusItem()?.focusChild() ?? false; } /** @ignore */ private _impl_focusFirst() { const nextKey = this.items[0].key; const nextItem = this.conversationItems?.find(it => it.key === nextKey); nextItem?.focusChild(); } /** @ignore */ private _impl_focusLast() { const nextKey = this.items[this.items.length - 1].key; const nextItem = this.conversationItems?.find(it => it.key === nextKey); nextItem?.focusChild(); } /** @ignore */ private _impl_focusDown() { const curItem = this._impl_getFocusItem() ?? this._impl_getLastItem(); if (curItem?.key) { const nextKey = this._impl_computeNextKey(curItem.key); const nextItem = this.conversationItems?.find(it => it.key === nextKey); nextItem?.focusChild(); } } /** @ignore */ private _impl_focusUp() { const curItem = this._impl_getFocusItem() ?? this._impl_getLastItem(); if (curItem?.key) { const nextKey = this._impl_computePrevKey(curItem.key); const nextItem = this.conversationItems?.find(it => it.key === nextKey); nextItem?.focusChild(); } } /** @ignore */ private _impl_scrollToBottom() { const el = this.host.nativeElement; const bottomPos = el.scrollHeight - el.clientHeight; el.scrollTop = bottomPos; } /** @ignore */ private _impl_isScrolledToBottom() { const el = this.host.nativeElement; const bottomPos = el.scrollHeight - el.clientHeight; return el.scrollTop == bottomPos; } /** @ignore */ private _impl_ensure_scrolledToBottom() { if (this._impl_isScrolledToBottom()) { setTimeout(() => this._impl_scrollToBottom(), 0); } } } |