(function () { 'use strict' const FORM_SELECTOR = '#nova [dusk="content"] form[data-form-unique-id]' const controllers = new Map() let scanFrame = null let started = false function configuredValue(value, allowedValues, fallback) { return allowedValues.includes(value) ? value : fallback } function packageConfig() { const configured = Nova.config('novaFormDock') || {} const positions = ['top', 'bottom', 'left', 'right'] return { mode: configured.mode === 'always' ? 'always' : 'automatic', minimumFormHeight: Number(configured.minimum_form_height) || null, forms: { create: configured.forms?.create !== false, update: configured.forms?.update !== false, }, resources: { only: Array.isArray(configured.resources?.only) ? configured.resources.only : [], except: Array.isArray(configured.resources?.except) ? configured.resources.except : [], }, appearance: { position: configuredValue( configured.appearance?.position, positions, 'bottom', ), mobilePosition: configuredValue( configured.appearance?.mobile_position, positions, 'bottom', ), alignment: configuredValue( configured.appearance?.alignment, ['start', 'center', 'end'], 'end', ), compactMobile: configured.appearance?.compact_mobile !== false, floatingGap: Math.max( 0, Number(configured.appearance?.floating_gap) || 0, ), }, } } function effectivePosition(config) { return window.matchMedia('(max-width: 767px)').matches ? config.appearance.mobilePosition : config.appearance.position } function formDetails(form) { if (form.closest('.modal')) { return null } const actions = form.lastElementChild if (!actions) { return null } if ( actions.querySelector(':scope > [dusk="create-button"]') && actions.querySelector(':scope > [dusk="cancel-create-button"]') ) { return { actions, type: 'create' } } if ( actions.querySelector(':scope > [dusk="update-button"]') && actions.querySelector(':scope > [dusk="cancel-update-button"]') ) { return { actions, type: 'update' } } return null } function currentResource() { const match = window.location.pathname.match(/\/resources\/([^/]+)/) if (!match) { return null } try { return decodeURIComponent(match[1]) } catch { return null } } function resourceIsEnabled(config) { const resource = currentResource() if (!resource || config.resources.except.includes(resource)) { return false } return ( config.resources.only.length === 0 || config.resources.only.includes(resource) ) } function scheduleScan() { if (scanFrame !== null) { return } scanFrame = window.requestAnimationFrame(() => { scanFrame = null scan() }) } function createController(form, details) { const computedStyle = window.getComputedStyle(form) const resizeObserver = window.ResizeObserver ? new ResizeObserver(scheduleScan) : { disconnect() {}, observe() {} } resizeObserver.observe(form) resizeObserver.observe(details.actions) if (details.actions.previousElementSibling) { resizeObserver.observe(details.actions.previousElementSibling) } return { actions: details.actions, form, naturalActionHeight: details.actions.getBoundingClientRect().height, originalPadding: { top: computedStyle.paddingTop, right: computedStyle.paddingRight, bottom: computedStyle.paddingBottom, left: computedStyle.paddingLeft, }, resizeObserver, type: details.type, } } function naturalFormHeight(controller) { const { actions, form } = controller const content = actions.previousElementSibling if (!content) { return form.scrollHeight } const contentRect = content.getBoundingClientRect() const verticalPadding = Number.parseFloat(controller.originalPadding.top) + Number.parseFloat(controller.originalPadding.bottom) return contentRect.height + controller.naturalActionHeight + verticalPadding + 32 } function shouldActivate(controller, config) { if (!config.forms[controller.type] || !resourceIsEnabled(config)) { return false } if (config.mode === 'always') { return true } const availableViewportHeight = Math.max( 320, window.innerHeight - Math.max(0, controller.form.getBoundingClientRect().top), ) const threshold = config.minimumFormHeight || availableViewportHeight return naturalFormHeight(controller) > threshold } function setDockDimensions(controller, config) { const formRect = controller.form.getBoundingClientRect() const actionRect = controller.actions.getBoundingClientRect() const headerBottom = document.querySelector('#nova header') ?.getBoundingClientRect().bottom const floatingGap = config.appearance.floatingGap Object.entries(controller.originalPadding).forEach(([side, value]) => { controller.form.style.setProperty(`--nfd-original-padding-${side}`, value) }) controller.form.style.setProperty('--nfd-actions-height', `${actionRect.height}px`) controller.form.style.setProperty('--nfd-actions-width', `${actionRect.width}px`) controller.actions.style.setProperty( '--nfd-inline-start', `${Math.max(0, formRect.left)}px`, ) controller.actions.style.setProperty( '--nfd-inline-end', `${Math.max(0, window.innerWidth - formRect.right)}px`, ) controller.actions.style.setProperty( '--nfd-floating-gap', `${floatingGap}px`, ) controller.actions.style.setProperty( '--nfd-block-start', `${Math.max(floatingGap, (headerBottom || 0) + floatingGap)}px`, ) } function activate(controller, config) { const position = effectivePosition(config) controller.form.dataset.nfdForm = '' controller.form.dataset.nfdActive = '' controller.form.dataset.nfdPosition = position controller.actions.dataset.nfdActions = '' controller.actions.dataset.nfdType = controller.type controller.actions.dataset.nfdPosition = position controller.actions.dataset.nfdAlignment = config.appearance.alignment if (config.appearance.compactMobile) { controller.actions.dataset.nfdCompactMobile = '' } else { delete controller.actions.dataset.nfdCompactMobile } setDockDimensions(controller, config) } function deactivate(controller) { delete controller.form.dataset.nfdForm delete controller.form.dataset.nfdActive delete controller.form.dataset.nfdPosition delete controller.actions.dataset.nfdActions delete controller.actions.dataset.nfdType delete controller.actions.dataset.nfdPosition delete controller.actions.dataset.nfdAlignment delete controller.actions.dataset.nfdCompactMobile Object.keys(controller.originalPadding).forEach(side => { controller.form.style.removeProperty(`--nfd-original-padding-${side}`) }) controller.form.style.removeProperty('--nfd-actions-height') controller.form.style.removeProperty('--nfd-actions-width') controller.actions.style.removeProperty('--nfd-inline-start') controller.actions.style.removeProperty('--nfd-inline-end') controller.actions.style.removeProperty('--nfd-floating-gap') controller.actions.style.removeProperty('--nfd-block-start') } function destroy(controller) { deactivate(controller) controller.resizeObserver.disconnect() controllers.delete(controller.form) } function scan() { const config = packageConfig() const candidates = new Set() document.querySelectorAll(FORM_SELECTOR).forEach(form => { const details = formDetails(form) if (!details) { return } candidates.add(form) let controller = controllers.get(form) if (!controller || controller.actions !== details.actions) { if (controller) { destroy(controller) } controller = createController(form, details) controllers.set(form, controller) } if (!controller.form.hasAttribute('data-nfd-active')) { controller.naturalActionHeight = details.actions.getBoundingClientRect().height } if (shouldActivate(controller, config)) { activate(controller, config) } else { deactivate(controller) } }) controllers.forEach(controller => { if (!controller.form.isConnected || !candidates.has(controller.form)) { destroy(controller) } }) } function start() { if (started) { return } started = true const observer = new MutationObserver(scheduleScan) observer.observe(document.documentElement, { childList: true, subtree: true, }) window.addEventListener('resize', scheduleScan, { passive: true }) window.addEventListener('scroll', scheduleScan, { passive: true }) Nova.$router?.on('navigate', scheduleScan) scheduleScan() } Nova.booting(start) })()