bitrix_task_control/src/core/runtime.ts

72 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { setConfigRequirements } from './config';
import { setReportSheetExtras } from './sheets/reportNotes';
import type { DepartmentProfile, ReportPlugin } from '../reports/types';
export function collectConfigRequirements(profile: DepartmentProfile): {
sheetKeys: string[];
scriptKeys: string[];
} {
const sheetKeys = [...(profile.requiredConfigKeys || [])];
const scriptKeys = [...(profile.requiredScriptProperties || [])];
profile.plugins.forEach(plugin => {
(plugin.requiredConfigKeys || []).forEach(key => sheetKeys.push(key));
(plugin.requiredScriptProperties || []).forEach(key => scriptKeys.push(key));
});
return { sheetKeys, scriptKeys };
}
export function applyProfileConfig(profile: DepartmentProfile): void {
const { sheetKeys, scriptKeys } = collectConfigRequirements(profile);
setConfigRequirements(sheetKeys, scriptKeys);
setReportSheetExtras(profile.reportSheetExtras);
}
export function collectHandlers(plugins: ReportPlugin[]): Record<string, (...args: any[]) => any> {
const handlers: Record<string, (...args: any[]) => any> = {};
plugins.forEach(plugin => {
Object.assign(handlers, plugin.handlers);
});
return handlers;
}
export function exposeGasGlobals(handlers: Record<string, (...args: any[]) => any>): void {
const scopes: any[] = [];
if (typeof globalThis !== 'undefined') {
scopes.push(globalThis);
}
try {
scopes.push(Function('return this')());
} catch (_error) {
// ignore
}
Object.entries(handlers).forEach(([name, fn]) => {
scopes.forEach(scope => {
if (scope) {
scope[name] = fn;
}
});
});
}
export function setupMenuFromPlugins(plugins: ReportPlugin[]): void {
const ui = SpreadsheetApp.getUi();
const mainMenu = ui.createMenu('📋 Работа с отчетами');
plugins.forEach(plugin => {
const subMenu = ui.createMenu(plugin.menuTitle);
plugin.menuItems.forEach(item => {
subMenu.addItem(item.label, item.functionName);
});
mainMenu.addSubMenu(subMenu);
});
mainMenu.addToUi();
}