import {TourGuideClient} from "../Tour"; import {computeDots, dotsWrapperHtmlString} from "./dots"; import {arrow, autoPlacement, computePosition as fui_computePosition, offset, shift} from "@floating-ui/dom"; import type {Placement, MiddlewareData} from "@floating-ui/dom"; /** * createTourGuideDialog */ async function createTourGuideDialog(this : TourGuideClient){ // Create base tour dialog element this.dialog = document.createElement('div') this.dialog.classList.add('tg-dialog') // Render HTML content await renderDialogHtml(this).then((html) => { this.dialog.innerHTML = html }) document.body.append(this.dialog) return true } /** * renderDialogHtml * @param tgInstance */ async function renderDialogHtml(tgInstance : TourGuideClient){ /** Update dialog options **/ if(tgInstance.options.dialogClass) tgInstance.dialog.classList.add(tgInstance.options.dialogClass) if(tgInstance.options.dialogZ) tgInstance.dialog.style.zIndex = String(tgInstance.options.dialogZ) tgInstance.dialog.style.width = tgInstance.options.dialogWidth ? (tgInstance.options.dialogWidth + 'px') : 'auto' if(tgInstance.options.dialogMaxWidth) tgInstance.dialog.style.maxWidth = tgInstance.options.dialogMaxWidth + 'px' /** Create dialog HTML **/ let htmlRes = "" htmlRes += `
` // Open header htmlRes += `
` // Title if(tgInstance.options.closeButton){ htmlRes += `
` htmlRes += ` ` htmlRes += `
` // Close button } // end close button // Loading spinner htmlRes += '
' htmlRes += ' ' htmlRes += '
' htmlRes += `
` // end header if(tgInstance.options.progressBar){ htmlRes += `
` // Progress bar } htmlRes += `
` // Body // Append dots if enabled for body as separate element if(tgInstance.options.showStepDots && tgInstance.options.stepDotsPlacement === 'body') { const dotsWrapperHtml = dotsWrapperHtmlString() if(dotsWrapperHtml) htmlRes += dotsWrapperHtml } htmlRes += `` // End footer htmlRes += `
` return htmlRes } /** * updateDialogHtml * @param tgInstance */ function updateDialogHtml(tgInstance : TourGuideClient){ return new Promise((resolve, reject) => { // Check step data const stepData = tgInstance.tourSteps[tgInstance.activeStep] if (!stepData) reject('No active step data') // Title const tgTitle = document.getElementById('tg-dialog-title'); if (tgTitle) tgTitle.innerHTML = stepData.title ? stepData.title : '' // Body const tgBody = document.getElementById('tg-dialog-body'); if (tgBody && stepData) { if (typeof stepData.content === "string"){ tgBody.innerHTML = stepData.content ? stepData.content : '' } else { tgBody.innerHTML = "" tgBody.append(stepData.content) } } // Dots - use computeDots() const tgDots = document.getElementById('tg-dialog-dots') if(tgDots && tgInstance.options.showStepDots && computeDots(tgInstance)) tgDots.innerHTML = computeDots(tgInstance) // Back button const backBtn = document.getElementById('tg-dialog-prev-btn') if(backBtn) { if (tgInstance.activeStep === 0) { backBtn.classList.add('disabled') backBtn.setAttribute("disabled", 'true'); } else { backBtn.classList.remove('disabled') backBtn.removeAttribute("disabled"); } } // Next/Finish button const nextBtn = document.getElementById('tg-dialog-next-btn') if(nextBtn) nextBtn.innerHTML = ((tgInstance.activeStep + 1) >= tgInstance.tourSteps.length ? tgInstance.options.finishLabel : tgInstance.options.nextLabel) as string // Step progress const tgProgress = document.getElementById('tg-step-progress') if(tgProgress) tgProgress.innerHTML = ((tgInstance.activeStep + 1) + '/' + (tgInstance.tourSteps).length) // Progress bar const tgProgBar = document.getElementById('tg-dialog-progbar') if(tgProgBar) { if(tgInstance.options.progressBar) tgProgBar.style.backgroundColor = tgInstance.options.progressBar tgProgBar.style.width = (((tgInstance.activeStep + 1) / tgInstance.tourSteps.length) * 100) + '%' } resolve(true) }) } /** * computeDialogPosition * @param tgInstance */ function computeDialogPosition(tgInstance : TourGuideClient) { return new Promise(async (resolve) => { const arrowElement: HTMLElement | null = document.querySelector('#tg-arrow'); let targetElem = (tgInstance.tourSteps[tgInstance.activeStep].dialogTarget || tgInstance.tourSteps[tgInstance.activeStep].target) as HTMLElement // Center position if body if (targetElem === document.body) { // apply positioning Object.assign(tgInstance.dialog.style, { top: `${(((window.innerHeight / 2.25)) - (tgInstance.dialog.clientHeight / 2))}px`, left: `${((window.innerWidth / 2) - (tgInstance.dialog.clientWidth / 2))}px`, position: 'fixed', }); // Add fixed class tgInstance.dialog.classList.add('tg-dialog-fixed') // hide arrow if(arrowElement) arrowElement.style.display = 'none' return resolve(true) } // Affirm positioning method and arrow visibility tgInstance.dialog.style.position = 'absolute' // Remove fixed class tgInstance.dialog.classList.remove('tg-dialog-fixed') // Display arrow if(arrowElement) arrowElement.style.display = 'inline-block' // Apply floating-ui positioning fui_computePosition(targetElem, tgInstance.dialog, { placement: tgInstance.options.dialogPlacement as Placement, middleware: [ autoPlacement({ autoAlignment: true, padding: 5 }), shift({ crossAxis: tgInstance.options.allowDialogOverlap, padding: 15 }), arrow({element: arrowElement as HTMLElement}), offset(20) ], }).then(({x, y, placement, middlewareData}) => { // apply positioning Object.assign(tgInstance.dialog.style, { left: `${x}px`, top: `${y}px`, }); // Arrow data if(middlewareData.arrow && arrowElement) { Object.assign(arrowElement.style, arrowStyles(middlewareData.arrow, placement, tgInstance.dialog)) // const arrowX = middlewareData.arrow.x // const arrowY = middlewareData.arrow.y // // const staticSide = { // top: "bottom", // right: "left", // bottom: "top", // left: "right", // }[placement.split('-')[0]]; // // // Position arrow // if (arrowElement) Object.assign(arrowElement.style, { // left: arrowX != null ? `${arrowX}px` : '', // top: arrowY != null ? `${arrowY}px` : '', // [staticSide as string]: "-4px", // }); } return resolve(true) }) }) } /** * arrowStyles * @param arrowMiddlewareData * @param placement * @param dialog * @private */ function arrowStyles(arrowMiddlewareData: MiddlewareData["arrow"], placement: Placement, dialog: TourGuideClient["dialog"]) : object { const arrowX = arrowMiddlewareData?.x || 0; const arrowY = arrowMiddlewareData?.y || 0; const arrowSize = 10; const staticSide = { top: "bottom", right: "left", bottom: "top", left: "right", }[placement.split('-')[0]]; const maxWidth = dialog.clientWidth - arrowSize; const maxHeight = dialog.clientHeight - arrowSize; const isAtMaxHeight = Math.abs(arrowY - maxHeight) <= arrowSize ; const isAtMaxWidth = Math.abs(arrowX - maxWidth) <= arrowSize; const isAtMinHeight = Math.abs(arrowY) <= arrowSize; const isAtMinWidth = Math.abs(arrowX) <= arrowSize; const isInCorner = arrowMiddlewareData?.centerOffset !== 0 || ( (isAtMinWidth || isAtMaxWidth) && (isAtMinHeight || isAtMaxHeight) ); return { left: isAtMinWidth ? (staticSide === 'right' ? '' : '0') : (isAtMaxWidth ? `${maxWidth}px` : `${arrowX}px`), top: isAtMinHeight ? (staticSide === 'bottom' ? '' : '0') : (isAtMaxHeight ? `${maxHeight}px` : `${arrowY}px`), [staticSide as string]: isInCorner ? "0" : `-${arrowSize / 2}px`, transform: isInCorner ? "none" : "rotate(45deg)", }; } export {createTourGuideDialog, renderDialogHtml, updateDialogHtml, computeDialogPosition}