// ES6 Compliant Syntax
// Copilot name: GitHub Copilot, version: 1.0.0, model: GPT-4, version: 2024-06, date: 2025-09-29
// File: toolbar-customization/ui-elements.js
// Class with static UI elements and related functions for the toolbar customization demo
class UIElements {
// The list of registered panels in the webviewer
static viewerPanels = null;
// Convert a label to a valid ID by removing spaces
static labelToId = (label) => label.replace(/\s+/g, '');
// The tab panel, representing the webviewer left panel
static tabPanel = {
handle: null,
dataElement: 'tabPanel'
};
// The customize toolbar sub-panel to be registered
static customizeToolbarPanel = {
handle: null,
dataElement: 'customizeToolbarPanel',
render: null,
};
// The UI elements list to be used in showing / hiding controls in the webviewer
static uiElementsMap = [
{
label: 'Top Header',
id: 'default-top-header',
checked: true,
},
{
label: 'Tools Header',
id: 'tools-header',
checked: true,
},
{
label: 'Download PDF',
id: 'downloadButton',
checked: true,
},
{
label: 'Print PDF',
id: 'printButton',
checked: true,
},
{
label: 'Annotation Tools',
id: 'toolbarGroup-Annotate',
checked: true,
list: [
{
label: 'Highlight',
id: 'highlightToolButton',
checked: true,
},
{
label: 'Underline',
id: 'underlineToolButton',
checked: true,
},
{
label: 'Strikeout',
id: 'strikeoutToolButton',
checked: true,
},
{
label: 'Squiggly',
id: 'squigglyToolButton',
checked: true,
},
{
label: 'Free Hand',
id: 'freeHandToolButton',
checked: true,
},
{
label: 'Free Hand Highlight',
id: 'freeHandHighlightToolButton',
checked: true,
},
{
label: 'Free Text',
id: 'freeTextToolButton',
checked: true,
},
{
label: 'Insert Text',
id: 'markInsertTextToolButton',
checked: true,
},
{
label: 'Replace Text',
id: 'markReplaceTextToolButton',
checked: true,
},
{
label: 'Sticky',
id: 'stickyToolButton',
checked: true,
},
{
label: 'Callout',
id: 'calloutToolButton',
checked: true,
},
{
label: 'Eraser',
id: 'eraserToolButton',
checked: true,
},
],
},
{
label: 'Overlays',
id: 'Overlays',
checked: true,
list: [
{
label: 'View Modes',
id: 'view-controls-toggle-button',
checked: true,
},
{
label: 'Left Panel',
id: 'leftPanelButton',
checked: true,
},
{
label: 'Search Panel',
id: 'searchPanelToggle',
checked: true,
},
{
label: 'Overflow Menu',
id: 'menuButton',
checked: true,
},
{
label: 'Page Number',
id: 'page-nav-floating-header',
checked: true,
},
],
},
];
// Create the customize toolbar panel elements.
static createCustomizeToolbarPanelElements = () => {
let panelDiv = document.createElement('div');
panelDiv.id = 'customizeToolbar';
let paragraph = document.createTextNode('A demo of the UI flexibility of WebViewer, a JavaScript-based PDF SDK for web apps. Easily hide buttons, change colors, or use your own icons via simple APIs.');
panelDiv.appendChild(paragraph);
let dividerDiv = document.createElement('div');
dividerDiv.style.borderTop = '1px solid #ccc';
dividerDiv.style.margin = '10px 0';
panelDiv.appendChild(dividerDiv);
// Hide / Show division
let hideShowDiv = document.createElement('div');
hideShowDiv.id = 'hideShow';
let hideShowTitle = document.createElement("h3");
hideShowTitle.textContent = "Hide / Show features in the UI";
hideShowDiv.appendChild(hideShowTitle);
hideShowDiv.appendChild(document.createElement('p'));
panelDiv.appendChild(hideShowDiv);
// Create checkboxes for items in the uiElementsMap array
this.uiElementsMap.forEach(element => {
this.createCheckbox(panelDiv, element);
let hasSubCheckboxes = (element.list && element.list.length > 0);
if (hasSubCheckboxes)
element.list.forEach(item => this.createCheckbox(panelDiv, item, hasSubCheckboxes));
});
panelDiv.appendChild(dividerDiv.cloneNode());
return panelDiv;
};
// Create a checkbox and its label, and add them to the customize toolbar panel
static createCheckbox = (panelDiv, element, isSubCheckbox = false) => {
// Checkbox input
let checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `${this.labelToId(element.label)}${checkbox.type}`;
checkbox.checked = element.checked;
checkbox.onmouseover = () => controlOnMouseOver(checkbox, element);
checkbox.onmouseleave = () => controlOnMouseLeave(checkbox, element);
checkbox.onclick = () => this.controlOnClick(element);
if (isSubCheckbox)
checkbox.style.marginLeft = '20px';
// Checkbox label
let label = document.createElement('label');
label.textContent = element.label;
label.onmouseover = () => controlOnMouseOver(label, element);
label.onmouseleave = () => controlOnMouseLeave(label, element);
label.onclick = () => {
checkbox.checked = !checkbox.checked;
this.controlOnClick(element);
};
panelDiv.appendChild(document.createElement('p'));
panelDiv.appendChild(checkbox);
panelDiv.appendChild(label);
};
// Handle click event, either for a checkbox or label control:
// - toggle the UI element visibility
// - toggle the UI element highlight
static controlOnClick = (element) => {
// If a UI element has sub-checkboxes, toggle them as well
if (element.list !== null && element.list !== undefined && element.list.length > 0) {
element.list.forEach(item => {
// Toggle the sub-checkbox UI element visibility
toggleElementVisibility(item);
const subCheckBox = this.checkBoxControl(item);
subCheckBox.checked = !subCheckBox.checked;
});
}
// Otherwise, just toggle the current UI element visibility
else
toggleElementVisibility(element);
// Toggle the UI element highlight
toggleElementHighlight(element);
};
// Get the checkbox control based on an item from the uiElementsMap array
static checkBoxControl = (item) => {
const checkbox = this.customizeToolbarPanel.render.querySelector(`#${this.labelToId(item.label)}checkbox`);
return checkbox;
};
}