Debian Codegraph @

addons/web/static/lib/hoot/ui/setup_hoot_ui.js

raw
1 /** @odoo-module */
2
3 import { mount, reactive } from "@odoo/owl";
4 import { HootFixtureElement } from "../core/fixture";
5 import { waitForDocument } from "../hoot_utils";
6 import { getRunner } from "../main_runner";
7 import { patchWindow } from "../mock/window";
8 import {
9 generateStyleSheets,
10 getColorScheme,
11 onColorSchemeChange,
12 setColorRoot,
13 } from "./hoot_colors";
14 import { HootMain } from "./hoot_main";
15
16 /**
17 * @typedef {"failed" | "passed" | "skipped" | "todo"} StatusFilter
18 *
19 * @typedef {ReturnType<typeof makeUiState>} UiState
20 */
21
22 //-----------------------------------------------------------------------------
23 // Global
24 //-----------------------------------------------------------------------------
25
26 const {
27 customElements,
28 document,
29 fetch,
30 HTMLElement,
31 Object: { entries: $entries },
32 } = globalThis;
33
34 //-----------------------------------------------------------------------------
35 // Internal
36 //-----------------------------------------------------------------------------
37
38 /**
39 * @param {string} href
40 */
41 function createLinkElement(href) {
42 const link = document.createElement("link");
43 link.rel = "stylesheet";
44 link.href = href;
45 return link;
46 }
47
48 /**
49 * @param {string} content
50 */
51 function createStyleElement(content) {
52 const style = document.createElement("style");
53 style.innerText = content;
54 return style;
55 }
56
57 function getPrismStyleUrl() {
58 const theme = getColorScheme() === "dark" ? "okaida" : "default";
59 return `/web/static/lib/prismjs/themes/${theme}.css`;
60 }
61
62 function loadAsset(tagName, attributes) {
63 return new Promise((resolve, reject) => {
64 const el = document.createElement(tagName);
65 Object.assign(el, attributes);
66 el.addEventListener("load", resolve);
67 el.addEventListener("error", reject);
68 document.head.appendChild(el);
69 });
70 }
71
72 async function loadBundle(bundle) {
73 const bundleResponse = await fetch(`/web/bundle/${bundle}`);
74 const result = await bundleResponse.json();
75 const promises = [];
76 for (const { src, type } of result) {
77 if (src && type === "link") {
78 loadAsset("link", {
79 rel: "stylesheet",
80 href: src,
81 });
82 } else if (src && type === "script") {
83 promises.push(
84 loadAsset("script", {
85 src,
86 type: "text/javascript",
87 })
88 );
89 }
90 }
91 await Promise.all(promises);
92 }
93
94 class HootContainer extends HTMLElement {
95 constructor() {
96 super(...arguments);
97
98 this.attachShadow({ mode: "open" });
99 }
100
101 connectedCallback() {
102 setColorRoot(this);
103 }
104
105 disconnectedCallback() {
106 setColorRoot(null);
107 }
108 }
109
110 customElements.define("hoot-container", HootContainer);
111
112 //-----------------------------------------------------------------------------
113 // Exports
114 //-----------------------------------------------------------------------------
115
116 export function makeUiState() {
117 return reactive({
118 resultsPage: 0,
119 resultsPerPage: 40,
120 /** @type {string | null} */
121 selectedSuiteId: null,
122 /** @type {"asc" | "desc" | false} */
123 sortResults: false,
124 /** @type {StatusFilter | null} */
125 statusFilter: null,
126 totalResults: 0,
127 });
128 }
129
130 /**
131 * Appends the main Hoot UI components in a container, which itself will be appended
132 * on the current document body.
133 *
134 * @returns {Promise<void>}
135 */
136 export async function setupHootUI() {
137 // - Patch window before code from other modules is executed
138 patchWindow();
139
140 const runner = getRunner();
141
142 const container = document.createElement("hoot-container");
143 container.style.display = "contents";
144
145 await waitForDocument(document);
146
147 document.head.appendChild(HootFixtureElement.styleElement);
148 document.body.appendChild(container);
149
150 const promises = [
151 // Mount main container
152 mount(HootMain, container.shadowRoot, {
153 env: {
154 runner,
155 ui: makeUiState(),
156 },
157 name: "HOOT",
158 }),
159 ];
160
161 if (!runner.headless) {
162 // In non-headless: also wait for lazy-loaded libs (Highlight & DiffMatchPatch)
163 promises.push(loadBundle("web.assets_unit_tests_setup_ui"));
164
165 let colorStyleContent = "";
166 for (const [className, content] of $entries(generateStyleSheets())) {
167 const selector = className === "default" ? ":host" : `:host(.${className})`;
168 colorStyleContent += `${selector}{${content}}`;
169 }
170
171 const prismStyleLink = createLinkElement(getPrismStyleUrl());
172 onColorSchemeChange(() => {
173 prismStyleLink.href = getPrismStyleUrl();
174 });
175
176 container.shadowRoot.append(
177 createStyleElement(colorStyleContent),
178 createLinkElement("/web/static/src/libs/fontawesome/css/font-awesome.css"),
179 prismStyleLink,
180 // Hoot-specific style is loaded last to take priority over other stylesheets
181 createLinkElement("/web/static/lib/hoot/ui/hoot_style.css")
182 );
183 }
184
185 await Promise.all(promises);
186 }