LCOV - code coverage report
Current view: top level - pkg/shell - util.tsx Coverage Total Hit
Test: cockpit Lines: 96.3 % 164 158
Test Date: 2026-06-01 17:00:21

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2024 Red Hat, Inc.
       3              :  * SPDX-License-Identifier: LGPL-2.1-or-later
       4              :  */
       5              : 
       6          338 : import cockpit from "cockpit";
       7              : 
       8              : import { ManifestKeyword, ManifestDocs, ManifestSection, Manifest, Manifests } from "./manifests";
       9              : import { Machine } from "./machines/machines";
      10              : 
      11              : export interface Location {
      12              :     host: string;
      13              :     path: string;
      14              :     hash: string;
      15              : }
      16              : 
      17          356 : export function encode_location(location: Partial<Location>): string {
      18          356 :     const shell_embedded = window.location.pathname.indexOf(".html") !== -1;
      19          356 :     if (shell_embedded)
      20           82 :         return window.location.toString();
      21              : 
      22          356 :     const path = [];
      23          356 :     if (location.host && location.host !== "localhost")
      24           92 :         path.push("@" + location.host);
      25          356 :     if (location.path)
      26          356 :         path.push(...location.path.split("/"));
      27          356 :     let string = cockpit.location.encode(path, undefined, true);
      28          257 :     if (location.hash && location.hash !== "/")
      29          232 :         string += "#" + location.hash;
      30          356 :     return string;
      31          356 : }
      32              : 
      33              : /* Decodes navigate state from a string */
      34          362 : export function decode_location(string: string): Location {
      35          362 :     let hash = "";
      36          362 :     const pos = string.indexOf("#");
      37          238 :     if (pos !== -1) {
      38          238 :         hash = string.substring(pos + 1);
      39          238 :         string = string.substring(0, pos);
      40          238 :     }
      41          362 :     if (string[0] != '/')
      42           88 :         string = "/" + string;
      43          362 :     const path = cockpit.location.decode(string);
      44          362 :     let host;
      45          362 :     if (path[0] && path[0][0] == "@")
      46          119 :         host = (path.shift() as string).substring(1);
      47              :     else
      48          362 :         host = "localhost";
      49          362 :     if (path.length && path[path.length - 1] == "index")
      50           88 :         path.pop();
      51          362 :     return { host, path: path.join("/"), hash };
      52          362 : }
      53              : 
      54          362 : export function decode_window_location(): Location {
      55          362 :     const shell_embedded = window.location.pathname.indexOf(".html") !== -1;
      56              : 
      57          362 :     if (shell_embedded)
      58           88 :         return decode_location("/" + window.location.hash);
      59              :     else
      60          362 :         return decode_location(window.location.pathname + window.location.hash);
      61          362 : }
      62              : 
      63          356 : export function replace_window_location(location: Partial<Location>): void {
      64          356 :     window.history.replaceState(null, "", encode_location(location));
      65          356 : }
      66              : 
      67           57 : export function push_window_location(location: Partial<Location>): void {
      68           57 :     window.history.pushState(null, "", encode_location(location));
      69           57 : }
      70              : 
      71              : export interface ManifestItem {
      72              :     path: string;
      73              :     hash: string;
      74              :     section: string;
      75              :     label: string;
      76              :     order: number;
      77              :     docs: ManifestDocs[] | undefined;
      78              :     keywords: ManifestKeyword[];
      79              : }
      80              : 
      81          338 : export class CompiledComponents {
      82              :     manifests: Manifests;
      83          362 :     items: { [path: string] : ManifestItem; } = { };
      84              : 
      85          362 :     constructor(manifests?: Manifests) {
      86           98 :         this.manifests = manifests || { };
      87          362 :     }
      88              : 
      89          362 :     load(section: string, getter: (man: Manifest) => ManifestSection | undefined): void {
      90          362 :         Object.entries(this.manifests).forEach(([name, manifest]) => {
      91          362 :             const manifest_section = getter(manifest) || {};
      92          362 :             Object.entries(manifest_section).forEach(([prop, info]) => {
      93          362 :                 const item: ManifestItem = {
      94          362 :                     path: "", // set below
      95          362 :                     hash: "", // set below
      96          362 :                     section,
      97           88 :                     label: info.label ? cockpit.gettext(info.label) : prop,
      98          362 :                     order: info.order === undefined ? 1000 : info.order,
      99          362 :                     docs: info.docs,
     100          362 :                     keywords: info.keywords || [{ matches: [], weight: 3, translate: true, goto: undefined }],
     101          362 :                 };
     102              : 
     103              :                 // Always first keyword should be page name
     104          362 :                 const page_name = item.label.toLowerCase();
     105          362 :                 if (item.keywords[0].matches.indexOf(page_name) < 0)
     106          362 :                     item.keywords[0].matches.unshift(page_name);
     107              : 
     108              :                 // Keywords from manifest have different defaults than are usual
     109          362 :                 item.keywords.forEach(i => {
     110          362 :                     i.weight = i.weight || 3;
     111          362 :                     i.translate = i.translate === undefined ? true : i.translate;
     112          362 :                 });
     113              : 
     114          362 :                 if (info.path)
     115           93 :                     item.path = info.path.replace(/\.html$/, "");
     116              :                 else
     117          362 :                     item.path = name + "/" + prop;
     118              : 
     119              :                 /* Split out any hash in the path */
     120          362 :                 const pos = item.path.indexOf("#");
     121           89 :                 if (pos !== -1) {
     122           89 :                     item.hash = item.path.substring(pos + 1);
     123           89 :                     item.path = item.path.substring(0, pos);
     124           89 :                 }
     125              : 
     126              :                 /* Fix component for compatibility and normalize it */
     127          362 :                 if (item.path.indexOf("/") === -1)
     128           93 :                     item.path = name + "/" + item.path;
     129          362 :                 if (item.path.slice(-6) == "/index")
     130          362 :                     item.path = item.path.slice(0, -6);
     131          362 :                 this.items[item.path] = item;
     132          362 :             });
     133          362 :         });
     134          362 :     }
     135              : 
     136          335 :     ordered(section: string): ManifestItem[] {
     137          335 :         const list: ManifestItem[] = [];
     138          335 :         for (const x in this.items) {
     139          335 :             if (!section || this.items[x].section === section)
     140          335 :                 list.push(this.items[x]);
     141          335 :         }
     142          335 :         list.sort(function(a, b) {
     143          335 :             let ret = a.order - b.order;
     144          335 :             if (ret === 0)
     145          335 :                 ret = a.label.localeCompare(b.label);
     146          335 :             return ret;
     147          335 :         });
     148          335 :         return list;
     149          335 :     }
     150              : 
     151          362 :     find_path_item(path: string): ManifestItem {
     152          362 :         let component = path;
     153          360 :         if (this.items[path] === undefined) {
     154          360 :             let s = path;
     155          356 :             while (s && this.items[s] === undefined)
     156          356 :                 s = s.substring(0, s.lastIndexOf("/"));
     157          360 :             component = s;
     158          360 :         }
     159              : 
     160              :         // Still don't know where it comes from, check for parent
     161          108 :         if (!component) {
     162          108 :             const comp = this.manifests[path];
     163           98 :             if (comp && comp.parent && comp.parent.component)
     164           98 :                 component = comp.parent.component;
     165          108 :         }
     166              : 
     167          362 :         const item = this.items[component];
     168          362 :         if (item)
     169          362 :             return item;
     170              : 
     171              :         // Return something that can be used when the user navigates
     172              :         // to a URL for a non-existing component.
     173              : 
     174           98 :         return {
     175           98 :             path,
     176           98 :             label: path,
     177           98 :             section: "menu",
     178           98 :             hash: "",
     179           98 :             order: 3,
     180           98 :             keywords: [],
     181           98 :             docs: undefined
     182           98 :         };
     183          362 :     }
     184              : 
     185          362 :     find_path_manifest(path: string): Manifest {
     186          362 :         const parts = path.split("/");
     187          362 :         const pkg = parts[0];
     188              : 
     189           98 :         return (this.manifests[pkg]) || { };
     190          362 :     }
     191          338 : }
     192              : 
     193          362 : export function compile_manifests(manifests?: Manifests): CompiledComponents {
     194          362 :     const compiled = new CompiledComponents(manifests);
     195          362 :     compiled.load("tools", m => m.tools);
     196          362 :     compiled.load("dashboard", m => m.dashboard);
     197          362 :     compiled.load("menu", m => m.menu);
     198          362 :     return compiled;
     199          362 : }
     200              : 
     201            0 : function component_checksum(machine: Machine, path: string): string | undefined {
     202            0 :     const parts = path.split("/");
     203            0 :     const pkg = parts[0];
     204            0 :     if (machine.manifests && machine.manifests[pkg] && machine.manifests[pkg][".checksum"])
     205            0 :         return "$" + machine.manifests[pkg][".checksum"];
     206            0 : }
     207              : 
     208          335 : export function compute_frame_url(machine: Machine, path: string): string {
     209          335 :     let base;
     210          335 :     let checksum;
     211          335 :     if (machine.manifests && machine.manifests[".checksum"])
     212           61 :         checksum = "$" + machine.manifests[".checksum"];
     213              :     else
     214          335 :         checksum = machine.checksum;
     215              : 
     216           61 :     if (checksum && checksum == component_checksum(machine, path)) {
     217           61 :         if (machine.connection_string === "localhost")
     218           61 :             base = "..";
     219              :         else
     220           61 :             base = "../../" + checksum;
     221           61 :     } else {
     222              :         /* If we don't have any checksums, or if the component specifies a different
     223              :            checksum than the machine, load it via a non-caching @<host> path.  This
     224              :            makes sure that we get the right files, and also that we don't poisen the
     225              :            cache with wrong files.
     226              : 
     227              :            We can't use a $<component-checksum> path since cockpit-ws only knows how to
     228              :            route the machine checksum.
     229              : 
     230              :            TODO - make it possible to use $<component-checksum>.
     231              :         */
     232          335 :         base = "../../@" + machine.connection_string;
     233          335 :     }
     234              : 
     235          335 :     let url = base + "/" + path;
     236          335 :     if (path.indexOf("/") === -1)
     237          280 :         url += "/index";
     238          335 :     url += ".html";
     239              : 
     240          335 :     return url;
     241          335 : }
        

Generated by: LCOV version 2.0-1