Skip to content

Commit

Permalink
WIP - move CompiledComponents to utils.jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer committed Sep 27, 2024
1 parent 52d0669 commit ff33dfe
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 126 deletions.
85 changes: 1 addition & 84 deletions pkg/shell/base_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}
82 changes: 40 additions & 42 deletions pkg/shell/state.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 = { };
Expand All @@ -278,7 +313,6 @@ export function ShellState(trigger_connection_flow) {
}

/* ROUTER
*
*/

const router_callbacks = {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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];
};
Expand Down
72 changes: 72 additions & 0 deletions pkg/shell/util.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
};
}

0 comments on commit ff33dfe

Please sign in to comment.