Spaces:
Running
Running
File size: 16,650 Bytes
76b5743 | 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | /**
* 设置菜单管理器
*/
import * as d3 from 'd3';
import { AdminManager } from './adminManager';
import { showDialog, showAlertDialog } from '../../shared/ui/dialog';
import { TextAnalysisAPI } from '../../shared/api/GLTR_API';
import { tr } from '../../shared/lang/i18n-lite';
import type { ThemeManager } from '../../shared/ui/theme';
import type { LanguageManager } from '../../shared/ui/language';
import { createSettingsDropdown } from '../../shared/ui/settingsDropdown';
import { getTokenRenderStyle, setTokenRenderStyle, type TokenRenderStyle } from './tokenRenderStyle';
import { getSemanticAnalysisEnabled, setSemanticAnalysisEnabled } from './semanticAnalysisManager';
import { getDigitsMergeEnabled, setDigitsMergeEnabled } from './digitsMergeManager';
import { getForceNarrowScreen, setForceNarrowScreen, FORCE_NARROW_CHANGE_EVENT } from '../core/responsive';
import { getSemanticMatchThreshold } from './semanticThresholdManager';
import {
getInfoDensityRenderDisabled,
setInfoDensityRenderDisabled,
} from '../../features/analysis/infoDensityRenderManager';
import { showVisitStatsDialog } from './visitStatsDialog';
import { showModelManageDialog } from './modelManageDialog';
export type SettingsMenuCallbacks = {
onMinimapToggle?: (enabled: boolean) => void;
onThemeChange?: () => void;
onLanguageToggle?: () => void;
onSemanticAnalysisToggle?: (enabled: boolean) => void;
};
export type SettingsMenuContext = 'common' | 'analysis';
export class SettingsMenuManager {
private settingsBtn: d3.Selection<Element, unknown, HTMLElement, any>;
private settingsMenu: d3.Selection<Element, unknown, HTMLElement, any>;
private adminModeBtn: d3.Selection<Element, unknown, HTMLElement, any>;
private modelManageBtn: d3.Selection<Element, unknown, HTMLElement, any>;
private visitStatsBtn: d3.Selection<Element, unknown, HTMLElement, any>;
private tokenRenderStyleDropdown: { updateCurrent: (v: TokenRenderStyle) => void } | null = null;
private minimapToggle: d3.Selection<HTMLInputElement, unknown, HTMLElement, any>;
private semanticAnalysisToggle: d3.Selection<HTMLInputElement, unknown, HTMLElement, any>;
private digitsMergeToggle: d3.Selection<HTMLInputElement, unknown, HTMLElement, any>;
private forceNarrowToggle: d3.Selection<HTMLInputElement, unknown, HTMLElement, any>;
private semanticThresholdInput: d3.Selection<HTMLInputElement, unknown, HTMLElement, any>;
private semanticThresholdItem: d3.Selection<HTMLElement, unknown, HTMLElement, any>;
private semanticSubmodeRow: d3.Selection<HTMLElement, unknown, HTMLElement, any>;
private disableInfoDensityToggle: d3.Selection<HTMLInputElement, unknown, HTMLElement, any>;
private themeDropdownContainer: d3.Selection<Element, unknown, HTMLElement, any>;
private adminManager: AdminManager;
private api: TextAnalysisAPI;
private onAdminStateChange?: () => void;
private callbacks: SettingsMenuCallbacks;
private themeManager?: ThemeManager;
private languageManager?: LanguageManager;
private readonly menuContext: SettingsMenuContext;
constructor(
settingsBtnSelector: string,
settingsMenuSelector: string,
adminModeBtnSelector: string,
adminManager: AdminManager,
api: TextAnalysisAPI,
onAdminStateChange?: () => void,
callbacks?: SettingsMenuCallbacks,
themeManager?: ThemeManager,
languageManager?: LanguageManager,
menuContext: SettingsMenuContext = 'analysis'
) {
this.menuContext = menuContext;
this.settingsBtn = d3.select(settingsBtnSelector);
this.settingsMenu = d3.select(settingsMenuSelector);
this.adminModeBtn = d3.select(adminModeBtnSelector);
this.modelManageBtn = d3.select('#model_manage_btn');
this.visitStatsBtn = d3.select('#visit_stats_btn');
this.tokenRenderStyleDropdown =
menuContext === 'analysis' ? this.initTokenRenderStyleDropdown() : null;
this.minimapToggle = d3.select<HTMLInputElement, any>('#enable_minimap_toggle');
this.semanticAnalysisToggle = d3.select<HTMLInputElement, any>('#semantic_analysis_toggle');
this.digitsMergeToggle = d3.select<HTMLInputElement, any>('#enable_digits_merge_toggle');
this.forceNarrowToggle = d3.select<HTMLInputElement, any>('#force_narrow_toggle');
this.semanticThresholdInput = d3.select<HTMLInputElement, any>('#semantic_threshold_input');
this.semanticThresholdItem = d3.select<HTMLElement, any>('#semantic_threshold_item');
this.semanticSubmodeRow = d3.select<HTMLElement, any>('#semantic_submode_row');
this.disableInfoDensityToggle = d3.select<HTMLInputElement, any>('#disable_info_density_toggle');
this.themeDropdownContainer = d3.select('#theme_dropdown');
this.adminManager = adminManager;
this.api = api;
this.onAdminStateChange = onAdminStateChange;
this.callbacks = callbacks || {};
this.themeManager = themeManager;
this.languageManager = languageManager;
this.initialize();
}
private initialize(): void {
// 点击齿轮按钮切换菜单
this.settingsBtn.on('click', (event: MouseEvent) => {
event.stopPropagation();
this.toggleMenu();
});
// 点击页面其他区域关闭菜单
d3.select('body').on('click.settings-menu', () => {
this.closeMenu();
});
// 阻止菜单内部点击关闭菜单
this.settingsMenu.on('click', (event: MouseEvent) => {
event.stopPropagation();
});
if (this.minimapToggle.node()) {
this.minimapToggle.on('change', () => {
const enabled = (this.minimapToggle.node() as HTMLInputElement)?.checked || false;
if (this.callbacks.onMinimapToggle) {
this.callbacks.onMinimapToggle(enabled);
}
});
}
if (this.digitsMergeToggle.node()) {
this.digitsMergeToggle.on('change', () => {
const enabled = (this.digitsMergeToggle.node() as HTMLInputElement)?.checked ?? false;
setDigitsMergeEnabled(enabled);
});
}
if (this.forceNarrowToggle.node()) {
this.forceNarrowToggle.on('change', () => {
const enabled = (this.forceNarrowToggle.node() as HTMLInputElement)?.checked ?? false;
setForceNarrowScreen(enabled);
});
// 跨标签:其他标签更改时同步 checkbox 视觉状态
window.addEventListener(FORCE_NARROW_CHANGE_EVENT, () => {
this.setCheckboxChecked(this.forceNarrowToggle, getForceNarrowScreen());
});
}
if (this.menuContext === 'analysis' && this.semanticAnalysisToggle.node()) {
this.semanticAnalysisToggle.on('change', () => {
const enabled = (this.semanticAnalysisToggle.node() as HTMLInputElement)?.checked || false;
setSemanticAnalysisEnabled(enabled);
this.updateSemanticThresholdVisibility();
this.updateSemanticSubmodeRowVisibility();
setInfoDensityRenderDisabled(enabled);
this.setDisableInfoDensity(enabled);
window.dispatchEvent(new CustomEvent('info-density-render-change'));
if (this.callbacks.onSemanticAnalysisToggle) {
this.callbacks.onSemanticAnalysisToggle(enabled);
}
});
}
if (this.menuContext === 'analysis' && this.disableInfoDensityToggle.node()) {
this.disableInfoDensityToggle.on('change', () => {
const disabled = (this.disableInfoDensityToggle.node() as HTMLInputElement)?.checked || false;
setInfoDensityRenderDisabled(disabled);
window.dispatchEvent(new CustomEvent('info-density-render-change'));
});
}
// Language dropdown - 由 languageManager 初始化,这里只需要确保容器存在
// 语言切换逻辑在 language.ts 中处理
// Theme dropdown - 由 themeManager 初始化,这里只需要确保容器存在
// 主题切换逻辑在 theme.ts 中处理
// 管理员模式入口(录入 token / 退出)
if (this.adminModeBtn.node()) {
this.adminModeBtn.on('click', () => {
this.closeMenu();
this.handleAdminModeClick();
});
}
if (this.modelManageBtn.node()) {
this.modelManageBtn.on('click', () => {
this.closeMenu();
void showModelManageDialog(this.api);
});
}
if (this.visitStatsBtn.node()) {
this.visitStatsBtn.on('click', () => {
this.closeMenu();
void showVisitStatsDialog(this.api);
});
}
if (this.menuContext === 'analysis' && this.semanticAnalysisToggle.node()) {
this.setSemanticAnalysisEnabled(getSemanticAnalysisEnabled());
}
this.setDigitsMergeCheckbox(getDigitsMergeEnabled());
this.setCheckboxChecked(this.forceNarrowToggle, getForceNarrowScreen());
if (this.menuContext === 'analysis' && this.semanticThresholdInput.node()) {
this.setSemanticThresholdValue(getSemanticMatchThreshold());
}
if (this.menuContext === 'analysis' && this.disableInfoDensityToggle.node()) {
this.setDisableInfoDensity(getInfoDensityRenderDisabled());
}
this.applyAdminUiState();
}
private initTokenRenderStyleDropdown(): { updateCurrent: (v: TokenRenderStyle) => void } {
const container = d3.select('#token_render_style_dropdown');
if (!container.node()) {
throw new Error('initTokenRenderStyleDropdown: #token_render_style_dropdown missing on analysis page');
}
const options: Array<{ value: TokenRenderStyle; label: string }> = [
{ value: 'classic', label: 'Classic' },
{ value: 'density', label: 'Density' },
];
const dropdown = createSettingsDropdown<TokenRenderStyle>({
container,
classPrefix: 'token-render-style',
options: options.map((o) => ({ value: o.value, html: `<span>${o.label}</span>` })),
dataAttr: 'data-style',
bodyClickNamespace: 'token-render-style-dropdown',
onSelect: (v) => {
setTokenRenderStyle(v);
dropdown.updateCurrent(v);
window.dispatchEvent(new CustomEvent('token-render-style-change'));
},
});
dropdown.updateCurrent(getTokenRenderStyle());
return dropdown;
}
private closeMenu(): void {
this.settingsMenu.style('display', 'none');
}
private toggleMenu(): void {
const cur = this.settingsMenu.style('display');
this.settingsMenu.style('display', cur === 'none' || cur === '' ? 'block' : 'none');
}
/**
* 根据管理员模式更新菜单项文案
*/
public applyAdminUiState(): void {
const isAdmin = this.adminManager.isInAdminMode();
this.adminModeBtn.text(isAdmin ? 'Exit' : 'Enter');
this.adminModeBtn.classed('active', isAdmin);
// 显示/隐藏所有带 data-admin-only 的菜单项
this.settingsMenu.selectAll<HTMLElement, unknown>('.settings-menu-item[data-admin-only]')
.style('display', isAdmin ? null : 'none');
this.tokenRenderStyleDropdown?.updateCurrent(getTokenRenderStyle());
if (this.menuContext === 'analysis' && this.semanticAnalysisToggle.node()) {
this.setSemanticAnalysisEnabled(getSemanticAnalysisEnabled());
}
this.setDigitsMergeCheckbox(getDigitsMergeEnabled());
this.setCheckboxChecked(this.forceNarrowToggle, getForceNarrowScreen());
if (this.menuContext === 'analysis' && this.semanticThresholdInput.node()) {
this.setSemanticThresholdValue(getSemanticMatchThreshold());
this.updateSemanticThresholdVisibility();
}
if (this.menuContext === 'analysis' && this.disableInfoDensityToggle.node()) {
this.setDisableInfoDensity(getInfoDensityRenderDisabled());
}
// 通知外部更新 UI
if (this.onAdminStateChange) {
this.onAdminStateChange();
}
}
/**
* 设置 minimap 的初始状态
*/
public setMinimapEnabled(enabled: boolean): void {
const checkbox = this.minimapToggle.node() as HTMLInputElement | null;
if (checkbox) {
checkbox.checked = enabled;
}
}
/**
* 设置 semantic analysis 的初始状态
*/
public setSemanticAnalysisEnabled(enabled: boolean): void {
const checkbox = this.semanticAnalysisToggle.node() as HTMLInputElement | null;
if (checkbox) {
checkbox.checked = enabled;
}
}
public setDigitsMergeCheckbox(checked: boolean): void {
this.setCheckboxChecked(this.digitsMergeToggle, checked);
}
private setCheckboxChecked(
sel: d3.Selection<HTMLInputElement, unknown, HTMLElement, any>,
checked: boolean
): void {
const el = sel.node() as HTMLInputElement | null;
if (el) el.checked = checked;
}
private updateSemanticThresholdVisibility(): void {
if (!this.semanticThresholdItem.node()) return;
const isAdmin = this.adminManager.isInAdminMode();
const semanticOn = getSemanticAnalysisEnabled();
this.semanticThresholdItem.style('display', isAdmin && semanticOn ? null : 'none');
}
private updateSemanticSubmodeRowVisibility(): void {
if (!this.semanticSubmodeRow.node()) return;
const isAdmin = this.adminManager.isInAdminMode();
this.semanticSubmodeRow.style('display', isAdmin ? null : 'none');
}
private setSemanticThresholdValue(value: number): void {
const input = this.semanticThresholdInput.node() as HTMLInputElement | null;
if (input) {
input.value = String(value);
}
}
/**
* 设置 disable info density 的初始状态
*/
public setDisableInfoDensity(disabled: boolean): void {
const checkbox = this.disableInfoDensityToggle.node() as HTMLInputElement | null;
if (checkbox) {
checkbox.checked = disabled;
}
}
private handleAdminModeClick(): void {
if (this.adminManager.isInAdminMode()) {
this.adminManager.clearAdminTokenAndNotify();
// 刷新页面以让 demoManager 等基于配置的模块重新初始化
window.location.reload();
return;
}
showDialog({
title: 'Admin Mode',
content: (dialog) => {
const container = dialog.append('div').attr('class', 'dialog-form-container');
container.append('label')
.attr('class', 'dialog-label')
.text('Please enter admin token:');
const input = container.append('input')
.attr('type', 'password')
.attr('class', 'dialog-input')
.attr('placeholder', 'INFORADAR_ADMIN_TOKEN');
return {
getValue: () => (input.node() as HTMLInputElement | null)?.value?.trim() || '',
validate: () => ((input.node() as HTMLInputElement | null)?.value?.trim() || '').length > 0,
focus: () => {
const n = input.node() as HTMLInputElement | null;
if (n) n.focus();
}
};
},
onConfirm: async (token: string) => {
const { success, message } = await this.adminManager.setAdminTokenAndNotify(token);
if (!success) {
showAlertDialog(tr('Error'), message || 'Admin token verification failed.');
return;
}
// 注入到 API,随后刷新页面以启用文件夹操作等(初始化期配置)
this.api.setAdminToken(this.adminManager.getAdminToken());
window.location.reload();
},
onCancel: () => {},
confirmText: 'Enter',
cancelText: tr('Cancel'),
width: 'clamp(300px, 90vw, 420px)'
});
}
}
|