Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/format/dashboard/format-dashboard-toolbar.ts
6451 views
1
/*
2
* format-dashboard-toolbar.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { asCssSize } from "../../core/css.ts";
8
import { Document, Element } from "../../core/deno-dom.ts";
9
import { recursiveApplyFillClasses } from "./format-dashboard-layout.ts";
10
import { makeEl, processAndRemoveAttr } from "./format-dashboard-shared.ts";
11
12
const kToolbarPanelClass = "toolbar-panel";
13
const kToolbarClass = "toolbar";
14
const kToolbarContentClass = "toolbar-content";
15
16
const kToolbarAttrHeight = "data-height";
17
18
const kToolbarAttrPosition = "data-position";
19
const kToolbarAttrPositionEnd = "end";
20
const kToolbarBottomClass = "toolbar-bottom";
21
22
const kToolbarTopLevelClass = "toolbar-toplevel";
23
const kDashboardToolbarClass = "dashboard-toolbar";
24
25
export function processToolbars(doc: Document) {
26
// use a counter to provision ids
27
const toolbarNodes = doc.querySelectorAll(`.${kToolbarPanelClass}`);
28
29
for (const toolbarNode of toolbarNodes) {
30
const toolbarInputEl = toolbarNode as Element;
31
32
// Create the sidebar container
33
const toolbarContainerEl = makeEl("div", {
34
classes: ["html-fill-item", "html-fill-container"],
35
attributes: {},
36
}, doc);
37
38
// convert to an aside (class sidebar)
39
const toolbarEl = toolbarInputEl.querySelector(`.${kToolbarClass}`);
40
41
// See if there is a width
42
if (toolbarEl) {
43
// Read the position and apply class if needed
44
processAndRemoveAttr(
45
toolbarEl,
46
kToolbarAttrPosition,
47
(el: Element, value: string) => {
48
if (value === kToolbarAttrPositionEnd) {
49
el.classList.add(kToolbarBottomClass);
50
}
51
},
52
);
53
54
processAndRemoveAttr(
55
toolbarEl,
56
kToolbarAttrHeight,
57
(_el: Element, value: string) => {
58
const size = asCssSize(value);
59
60
const styleRaw = toolbarInputEl.parentElement?.getAttribute("style");
61
const styleVal = styleRaw !== null ? styleRaw : "";
62
const newStyle = styleVal + " --bslib-sidebar-height: " + size;
63
toolbarInputEl.parentElement?.setAttribute("style", newStyle);
64
},
65
);
66
67
toolbarEl.classList.add("html-fill-container");
68
toolbarContainerEl.appendChild(toolbarEl);
69
}
70
71
// Capture the content (the sidebar's next sibling)
72
const toolbarMainContentsEl = toolbarInputEl.querySelector(
73
`.${kToolbarContentClass}`,
74
);
75
if (toolbarMainContentsEl !== null) {
76
toolbarContainerEl.appendChild(toolbarMainContentsEl);
77
}
78
79
recursiveApplyFillClasses(toolbarContainerEl);
80
81
toolbarInputEl.replaceWith(toolbarContainerEl);
82
toolbarContainerEl.classList.add(
83
"dashboard-toolbar-container",
84
);
85
86
// Decorate the body of the document if there is a top level toolbar panel
87
const topLevelToolbar = doc.querySelector(
88
".page-layout-custom > .dashboard-toolbar-container",
89
);
90
if (topLevelToolbar !== null) {
91
topLevelToolbar.classList.add(kToolbarTopLevelClass);
92
doc.body.classList.add(kDashboardToolbarClass);
93
}
94
}
95
}
96
97