From ff33dfecaef5d580fc4c39fce5670e7ae61e0b68 Mon Sep 17 00:00:00 2001 From: Marius Vollmer Date: Fri, 27 Sep 2024 11:09:20 +0300 Subject: [PATCH] WIP - move CompiledComponents to utils.jsx --- pkg/shell/base_index.js | 85 +---------------------------------------- pkg/shell/state.jsx | 82 +++++++++++++++++++-------------------- pkg/shell/util.jsx | 72 ++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 126 deletions(-) diff --git a/pkg/shell/base_index.js b/pkg/shell/base_index.js index e50c7c7bb403..818165f5e96f 100644 --- a/pkg/shell/base_index.js +++ b/pkg/shell/base_index.js @@ -21,10 +21,6 @@ import cockpit from "cockpit"; import { decode_location, decode_window_location, push_window_location } from "./util.jsx"; -const shell_embedded = window.location.pathname.indexOf(".html") !== -1; - -// const _ = cockpit.gettext; - function Index() { const self = this; @@ -53,10 +49,7 @@ function Index() { self.last_path_for_host = { }; /* Jumps to a given navigate state */ - self.jump = function (location, replace) { - if (replace) - return; - + self.jump = function (location) { if (typeof (location) === "string") location = decode_location(location); @@ -115,84 +108,8 @@ function Index() { }; } -function CompiledComponents() { - const self = this; - self.items = {}; - - self.load = function(manifests, section) { - Object.entries(manifests || { }).forEach(([name, manifest]) => { - Object.entries(manifest[section] || { }).forEach(([prop, info]) => { - const item = { - section, - label: cockpit.gettext(info.label) || prop, - order: info.order === undefined ? 1000 : info.order, - docs: info.docs, - keywords: info.keywords || [{ matches: [] }], - keyword: { score: -1 } - }; - - // Always first keyword should be page name - const page_name = item.label.toLowerCase(); - if (item.keywords[0].matches.indexOf(page_name) < 0) - item.keywords[0].matches.unshift(page_name); - - // Keywords from manifest have different defaults than are usual - item.keywords.forEach(i => { - i.weight = i.weight || 3; - i.translate = i.translate === undefined ? true : i.translate; - }); - - if (info.path) - item.path = info.path.replace(/\.html$/, ""); - else - item.path = name + "/" + prop; - - /* Split out any hash in the path */ - const pos = item.path.indexOf("#"); - if (pos !== -1) { - item.hash = item.path.substr(pos + 1); - item.path = item.path.substr(0, pos); - } - - /* Fix component for compatibility and normalize it */ - if (item.path.indexOf("/") === -1) - item.path = name + "/" + item.path; - if (item.path.slice(-6) == "/index") - item.path = item.path.slice(0, -6); - self.items[item.path] = item; - }); - }); - }; - - self.ordered = function(section) { - const list = []; - for (const x in self.items) { - if (!section || self.items[x].section === section) - list.push(self.items[x]); - } - list.sort(function(a, b) { - let ret = a.order - b.order; - if (ret === 0) - ret = a.label.localeCompare(b.label); - return ret; - }); - return list; - }; - - self.search = function(prop, value) { - for (const x in self.items) { - if (self.items[x][prop] === value) - return self.items[x]; - } - }; -} - export function new_index_from_proto(proto) { const o = new Object(proto); // eslint-disable-line no-new-object Index.call(o); return o; } - -export function new_compiled() { - return new CompiledComponents(); -} diff --git a/pkg/shell/state.jsx b/pkg/shell/state.jsx index 477c4902bed8..9741e2db5eb3 100644 --- a/pkg/shell/state.jsx +++ b/pkg/shell/state.jsx @@ -21,7 +21,7 @@ import cockpit from "cockpit"; import { Router } from "./router.jsx"; import { machines as machines_factory } from "./machines/machines.js"; -import { decode_window_location, replace_window_location } from "./util.jsx"; +import { decode_window_location, replace_window_location, CompiledComponents } from "./util.jsx"; import * as base_index from "./base_index"; @@ -111,6 +111,23 @@ function compute_frame_url(machine, path) { return url; } +function choose_path(location, compiled) { + /* Go for the first item */ + const menu_items = compiled.ordered("menu"); + if (menu_items.length > 0 && menu_items[0]) + return menu_items[0].path; + + return "system"; +} + +function compile(machine) { + const compiled = new CompiledComponents(); + compiled.load(machine.manifests, "tools"); + compiled.load(machine.manifests, "dashboard"); + compiled.load(machine.manifests, "menu"); + return compiled; +} + export function ShellState(trigger_connection_flow) { /* CONFIG * @@ -166,7 +183,6 @@ export function ShellState(trigger_connection_flow) { /* WATCH DOGS */ - // XXX - whoa can we open channels here already? const watchdog = cockpit.channel({ payload: "null" }); watchdog.addEventListener("close", (event, options) => { const watchdog_problem = options.problem || "disconnected"; @@ -259,11 +275,30 @@ export function ShellState(trigger_connection_flow) { } } + function preload_machine_frames (machine, manifests) { + const compiled = compile(machine); + for (const c in manifests) { + const preload = manifests[c].preload; + if (preload && preload.length) { + for (const p of preload) { + const path = (p == "index") ? c : c + "/" + p; + const item = find_path_manifest_item(path, compiled); + ensure_frame(machine, path, null, item.label); + } + } + } + } + + function preload_frames () { + for (const m of machines.list) + preload_machine_frames(m, m.manifests); + } + /* PAGE STATUS * - * Page status notifications arrive from the Router. We also store - * them in the session storage so that individual pages have - * access to all collected statuses. + * Page status notifications arrive from the Router (see + * below). We also store them in the session storage so that + * individual pages have access to all collected statuses. */ const page_status = { }; @@ -278,7 +313,6 @@ export function ShellState(trigger_connection_flow) { } /* ROUTER - * */ const router_callbacks = { @@ -428,25 +462,6 @@ export function ShellState(trigger_connection_flow) { const index = base_index.new_index_from_proto(index_options); self.index = index; - function preload_host_frames (machine, manifests) { - const compiled = compile(machine); - for (const c in manifests) { - const preload = manifests[c].preload; - if (preload && preload.length) { - for (const p of preload) { - const path = (p == "index") ? c : c + "/" + p; - const item = find_path_manifest_item(path, compiled); - ensure_frame(machine, path, null, item.label); - } - } - } - } - - function preload_frames () { - for (const m of machines.list) - preload_host_frames(m, m.manifests); - } - machines.addEventListener("removed", (ev, machine) => { remove_machine_frames(machine); update(); @@ -569,23 +584,6 @@ export function ShellState(trigger_connection_flow) { } } - function choose_path(location, compiled) { - /* Go for the first item */ - const menu_items = compiled.ordered("menu"); - if (menu_items.length > 0 && menu_items[0]) - return menu_items[0].path; - - return "system"; - } - - function compile(machine) { - const compiled = base_index.new_compiled(); - compiled.load(machine.manifests, "tools"); - compiled.load(machine.manifests, "dashboard"); - compiled.load(machine.manifests, "menu"); - return compiled; - } - self.last_path_for_host = (host) => { return index.last_path_for_host[host]; }; diff --git a/pkg/shell/util.jsx b/pkg/shell/util.jsx index 0821a5f4924c..4d5c1421109c 100644 --- a/pkg/shell/util.jsx +++ b/pkg/shell/util.jsx @@ -85,3 +85,75 @@ export function replace_window_location(location) { export function push_window_location(location) { window.history.pushState(null, "", encode_location(location)); } + +export function CompiledComponents() { + const self = this; + self.items = {}; + + self.load = function(manifests, section) { + Object.entries(manifests || { }).forEach(([name, manifest]) => { + Object.entries(manifest[section] || { }).forEach(([prop, info]) => { + const item = { + section, + label: cockpit.gettext(info.label) || prop, + order: info.order === undefined ? 1000 : info.order, + docs: info.docs, + keywords: info.keywords || [{ matches: [] }], + keyword: { score: -1 } + }; + + // Always first keyword should be page name + const page_name = item.label.toLowerCase(); + if (item.keywords[0].matches.indexOf(page_name) < 0) + item.keywords[0].matches.unshift(page_name); + + // Keywords from manifest have different defaults than are usual + item.keywords.forEach(i => { + i.weight = i.weight || 3; + i.translate = i.translate === undefined ? true : i.translate; + }); + + if (info.path) + item.path = info.path.replace(/\.html$/, ""); + else + item.path = name + "/" + prop; + + /* Split out any hash in the path */ + const pos = item.path.indexOf("#"); + if (pos !== -1) { + item.hash = item.path.substr(pos + 1); + item.path = item.path.substr(0, pos); + } + + /* Fix component for compatibility and normalize it */ + if (item.path.indexOf("/") === -1) + item.path = name + "/" + item.path; + if (item.path.slice(-6) == "/index") + item.path = item.path.slice(0, -6); + self.items[item.path] = item; + }); + }); + }; + + self.ordered = function(section) { + const list = []; + for (const x in self.items) { + if (!section || self.items[x].section === section) + list.push(self.items[x]); + } + list.sort(function(a, b) { + let ret = a.order - b.order; + if (ret === 0) + ret = a.label.localeCompare(b.label); + return ret; + }); + return list; + }; + + self.search = function(prop, value) { + for (const x in self.items) { + if (self.items[x][prop] === value) + return self.items[x]; + } + }; +}