SPB Git

Repository Public

brand.ts 6.1 KB 122 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : KA Maps Engine8 * File             : server/km-maps/lib/brand.ts9 * Module           : server10 * Repository       : https://git.spboucher.ai/ka-maps-engine11 * Created          : 2026-12-0112 * Last Modified    : 2026-12-0113 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Configuration par application et document de marque.43import { APPS } from './rollout.ts';4445/** Couches de tuiles ka1 que la configuration peut couper, plus l'ombrage du relief. */46export const SOURCE_LAYERS = ['water', 'waterway', 'landcover', 'landuse', 'boundary', 'road', 'transit', 'building', 'place', 'poi', 'housenum', 'parcel'] as const;47export const LAYER_SWITCHES = [...SOURCE_LAYERS, 'hillshade'] as const;48export const KNOWN_FLAGS = ['buildings3d', 'terrain3d', 'offline', 'draw', 'measure', 'heat', 'clusters', 'orbit', 'reveal'] as const;4950export type AppConfig = {51  defaultTheme: string;52  themes: string[];53  brand: { palette?: Record<string, unknown>; markers?: Record<string, unknown>; scales?: Record<string, unknown>; layers?: Record<string, unknown> };54  layers: Record<string, boolean>;55  zoom: { min: number; max: number };56  flags: Record<string, boolean>;57};5859export function defaultAppConfig(themes: string[]): AppConfig {60  return {61    defaultTheme: themes.includes('signature') ? 'signature' : (themes[0] ?? 'signature'),62    themes: [...themes],63    brand: {},64    layers: Object.fromEntries(LAYER_SWITCHES.map((l) => [l, true])),65    zoom: { min: 3, max: 20 },66    flags: Object.fromEntries(KNOWN_FLAGS.map((f) => [f, f !== 'offline'])),67  };68}6970/** Vérifie la forme d'une configuration. Rend la liste des erreurs (chemin, message). */71export function validateAppConfig(c: any, knownThemes: string[]): { path: string; message: string }[] {72  const e: { path: string; message: string }[] = [];73  const obj = (v: any) => v !== null && typeof v === 'object' && !Array.isArray(v);74  if (!obj(c)) return [{ path: '', message: 'objet attendu' }];75  for (const k of Object.keys(c)) if (!['defaultTheme', 'themes', 'brand', 'layers', 'zoom', 'flags'].includes(k)) e.push({ path: k, message: 'clé inconnue' });76  if (!Array.isArray(c.themes) || c.themes.length === 0) e.push({ path: 'themes', message: 'au moins un thème' });77  else {78    c.themes.forEach((t: any, i: number) => {79      if (!knownThemes.includes(t)) e.push({ path: `themes[${i}]`, message: `thème inconnu : ${t}` });80    });81    if (new Set(c.themes).size !== c.themes.length) e.push({ path: 'themes', message: 'doublon' });82    if (typeof c.defaultTheme !== 'string' || !c.themes.includes(c.defaultTheme)) e.push({ path: 'defaultTheme', message: 'doit être un des thèmes activés' });83  }84  if (!obj(c.brand)) e.push({ path: 'brand', message: 'objet attendu' });85  else for (const k of Object.keys(c.brand)) {86    if (!['palette', 'markers', 'scales', 'layers'].includes(k)) e.push({ path: `brand.${k}`, message: 'clé inconnue (palette, markers, scales, layers)' });87    else if (!obj(c.brand[k])) e.push({ path: `brand.${k}`, message: 'objet attendu' });88  }89  if (!obj(c.layers)) e.push({ path: 'layers', message: 'objet attendu' });90  else for (const [k, v] of Object.entries(c.layers)) {91    if (!(LAYER_SWITCHES as readonly string[]).includes(k)) e.push({ path: `layers.${k}`, message: 'couche inconnue' });92    else if (typeof v !== 'boolean') e.push({ path: `layers.${k}`, message: 'booléen attendu' });93  }94  if (!obj(c.zoom) || !Number.isFinite(c.zoom.min) || !Number.isFinite(c.zoom.max)) e.push({ path: 'zoom', message: '{min, max} attendus' });95  else if (c.zoom.min < 0 || c.zoom.max > 20 || c.zoom.min >= c.zoom.max) e.push({ path: 'zoom', message: '0 ≤ min < max ≤ 20' });96  if (!obj(c.flags)) e.push({ path: 'flags', message: 'objet attendu' });97  else for (const [k, v] of Object.entries(c.flags)) {98    if (!/^[a-z][a-zA-Z0-9]{1,30}$/.test(k)) e.push({ path: `flags.${k}`, message: 'nom invalide' });99    else if (typeof v !== 'boolean') e.push({ path: `flags.${k}`, message: 'booléen attendu' });100  }101  return e;102}103104/** Document de marque kastyle/1 tel que le moteur le lit (`Style::with_brand`). */105export function brandDocument(app: string, c: AppConfig): Record<string, unknown> {106  if (!(APPS as readonly string[]).includes(app)) throw new Error(`app inconnue : ${app}`);107  return { kastyle: 1, brand: app, ...c.brand };108}109110/** Retire d'un document de thème les couches de style dont la source est coupée pour l'application. */111export function applyLayerSwitches(doc: any, layers: Record<string, boolean>): any {112  const off = new Set(Object.entries(layers).filter(([, v]) => v === false).map(([k]) => k));113  if (off.size === 0 || !Array.isArray(doc?.layers)) return doc;114  return {115    ...doc,116    layers: doc.layers.filter((l: any) => {117      if (l.type === 'hillshade') return !off.has('hillshade');118      return !(typeof l.source === 'string' && off.has(l.source));119    }),120  };121}122
Files main
Menu

Theme