| import * as d3 from 'd3'; |
| import type { GLTR_Text_Box } from '../vis/GLTR_Text_Box'; |
| import type { Histogram } from '../vis/Histogram'; |
| import type { HistogramBinClickEvent } from '../vis/Histogram'; |
| import type { ScatterPlot, ScatterChunkClickEvent } from '../vis/ScatterPlot'; |
| import type { FrontendAnalyzeResult } from '../api/GLTR_API'; |
| import { |
| calculateHighlights, |
| type HistogramType, |
| type HighlightData |
| } from '../utils/highlightUtils'; |
|
|
| export type HighlightCurrentData = { result: FrontendAnalyzeResult; signalProbs?: number[]; pPwValues?: number[]; pwScores?: number[] } | null; |
|
|
| export type HighlightControllerOptions = { |
| stats_frac: Histogram; |
| stats_raw_score_normed?: Histogram; |
| stats_match_score_progress?: ScatterPlot; |
| lmf: GLTR_Text_Box; |
| currentData: HighlightCurrentData; |
| }; |
|
|
| export class HighlightController { |
| private options: HighlightControllerOptions; |
|
|
| constructor(options: HighlightControllerOptions) { |
| this.options = options; |
| } |
|
|
| |
| |
| |
| public clearHighlights(): void { |
| this.options.stats_frac.clearSelection(); |
| this.options.stats_raw_score_normed?.clearSelection(); |
| this.options.stats_match_score_progress?.clearSelection(); |
| this.options.lmf.clearHighlight(); |
| } |
|
|
| |
| |
| |
| public handleHistogramBinClick(ev: HistogramBinClickEvent): void { |
| const { currentData } = this.options; |
| if (!currentData) return; |
|
|
| |
| if (ev.binIndex === -1) { |
| this.clearHighlights(); |
| return; |
| } |
|
|
| const { x0, x1, binIndex, no_bins, source } = ev; |
| const highlightData: HighlightData = { ...currentData.result, signalProbs: currentData.signalProbs, pPwValues: currentData.pPwValues, pwScores: currentData.pwScores }; |
|
|
| let histogramType: HistogramType = 'token'; |
| if (source === 'stats_raw_score_normed') histogramType = 'raw_score_normed'; |
|
|
| if (histogramType === 'raw_score_normed') { |
| this.options.stats_frac.clearSelection(); |
| } else { |
| this.options.stats_raw_score_normed?.clearSelection(); |
| } |
|
|
| this.options.stats_match_score_progress?.clearSelection(); |
|
|
| const { indices, style } = calculateHighlights(histogramType, x0, x1, binIndex, no_bins, highlightData); |
|
|
| this.options.lmf.setHighlightedIndices(indices, style); |
| } |
|
|
| |
| |
| |
| public handleMatchScoreChunkClick(ev: ScatterChunkClickEvent): void { |
| if (ev.source !== 'stats_match_score_progress') return; |
| const { currentData } = this.options; |
| if (!currentData) return; |
|
|
| if (ev.chunkIndex === -1) { |
| this.options.lmf.clearHighlight(); |
| return; |
| } |
|
|
| this.options.stats_frac.clearSelection(); |
| this.options.stats_raw_score_normed?.clearSelection(); |
|
|
| this.options.lmf.setChunkCharRangeHighlight(ev.x0, ev.x1); |
| this.options.lmf.scrollToUnicodeCharOffset(ev.x0); |
| } |
|
|
| |
| public getCurrentData(): HighlightCurrentData { |
| return (this.options as { currentData: HighlightCurrentData }).currentData; |
| } |
|
|
| |
| |
| |
| public updateCurrentData(currentData: HighlightCurrentData): void { |
| (this.options as { currentData: HighlightCurrentData }).currentData = currentData; |
| } |
| } |
|
|
| |
| |
| |
| export const initHighlightClearListeners = ( |
| clearHighlights: () => void |
| ): void => { |
| |
| |
| d3.select('body').on('click.clearHighlight', (event: MouseEvent) => { |
| const target = <HTMLElement>event.target; |
| if (!target) return; |
| |
| |
| const isInteractive = |
| target.closest('.token') || |
| target.closest('button') || |
| target.closest('input') || |
| target.closest('textarea') || |
| target.closest('select') || |
| target.closest('.bar') || |
| target.closest('.hover-area') || |
| target.closest('a') || |
| target.closest('[role="button"]') || |
| target.closest('[onclick]'); |
| |
| |
| if (!isInteractive) { |
| clearHighlights(); |
| } |
| }); |
|
|
| |
| d3.select(window).on('keydown.clearHighlight', (event: KeyboardEvent) => { |
| if (event.key === 'Escape') { |
| clearHighlights(); |
| } |
| }); |
| }; |
|
|
|
|