Path: blob/main/src/format/dashboard/format-dashboard-toolbar.ts
6451 views
/*1* format-dashboard-toolbar.ts2*3* Copyright (C) 2020-2022 Posit Software, PBC4*/56import { asCssSize } from "../../core/css.ts";7import { Document, Element } from "../../core/deno-dom.ts";8import { recursiveApplyFillClasses } from "./format-dashboard-layout.ts";9import { makeEl, processAndRemoveAttr } from "./format-dashboard-shared.ts";1011const kToolbarPanelClass = "toolbar-panel";12const kToolbarClass = "toolbar";13const kToolbarContentClass = "toolbar-content";1415const kToolbarAttrHeight = "data-height";1617const kToolbarAttrPosition = "data-position";18const kToolbarAttrPositionEnd = "end";19const kToolbarBottomClass = "toolbar-bottom";2021const kToolbarTopLevelClass = "toolbar-toplevel";22const kDashboardToolbarClass = "dashboard-toolbar";2324export function processToolbars(doc: Document) {25// use a counter to provision ids26const toolbarNodes = doc.querySelectorAll(`.${kToolbarPanelClass}`);2728for (const toolbarNode of toolbarNodes) {29const toolbarInputEl = toolbarNode as Element;3031// Create the sidebar container32const toolbarContainerEl = makeEl("div", {33classes: ["html-fill-item", "html-fill-container"],34attributes: {},35}, doc);3637// convert to an aside (class sidebar)38const toolbarEl = toolbarInputEl.querySelector(`.${kToolbarClass}`);3940// See if there is a width41if (toolbarEl) {42// Read the position and apply class if needed43processAndRemoveAttr(44toolbarEl,45kToolbarAttrPosition,46(el: Element, value: string) => {47if (value === kToolbarAttrPositionEnd) {48el.classList.add(kToolbarBottomClass);49}50},51);5253processAndRemoveAttr(54toolbarEl,55kToolbarAttrHeight,56(_el: Element, value: string) => {57const size = asCssSize(value);5859const styleRaw = toolbarInputEl.parentElement?.getAttribute("style");60const styleVal = styleRaw !== null ? styleRaw : "";61const newStyle = styleVal + " --bslib-sidebar-height: " + size;62toolbarInputEl.parentElement?.setAttribute("style", newStyle);63},64);6566toolbarEl.classList.add("html-fill-container");67toolbarContainerEl.appendChild(toolbarEl);68}6970// Capture the content (the sidebar's next sibling)71const toolbarMainContentsEl = toolbarInputEl.querySelector(72`.${kToolbarContentClass}`,73);74if (toolbarMainContentsEl !== null) {75toolbarContainerEl.appendChild(toolbarMainContentsEl);76}7778recursiveApplyFillClasses(toolbarContainerEl);7980toolbarInputEl.replaceWith(toolbarContainerEl);81toolbarContainerEl.classList.add(82"dashboard-toolbar-container",83);8485// Decorate the body of the document if there is a top level toolbar panel86const topLevelToolbar = doc.querySelector(87".page-layout-custom > .dashboard-toolbar-container",88);89if (topLevelToolbar !== null) {90topLevelToolbar.classList.add(kToolbarTopLevelClass);91doc.body.classList.add(kDashboardToolbarClass);92}93}94}959697