{"version":3,"file":"toggletip.min.js","sources":["../../src/toggletips/toggletip.js"],"sourcesContent":["//version=1.0.0\nwindow.isDebug = false;\n\n(function () {\n    function init() {\n        document.querySelectorAll('.toggletip').forEach((toggletip, i) => {\n            const name = toggletip.innerText;\n            const id = `${name.toLowerCase().replace(' ', '_')}-${i}`;\n            const content = toggletip.getAttribute('data-tooltip-content');\n            if (!content) {\n                toggletip.classList.remove('toggletip');\n                return;\n            }\n\n            toggletip.insertAdjacentHTML(\"afterend\", tooltipContainer(id, name, content));\n\n            toggletip.setAttribute('aria-haspopup', true);\n            toggletip.setAttribute('aria-describedby', id);\n            toggletip.setAttribute('data-toggletip-active', false);\n            toggletip.setAttribute('popovertarget', id);\n\n            toggletip.parentElement.style.position = 'relative';\n            toggletip.addEventListener('click', clickHandler);\n        });\n    }\n\n\n\n    function tooltipContainer(id, name, content) {\n        const title = name[0].toUpperCase() + name.slice(1);\n        const container =\n            `\n        <div id=\"${id}\" class=\"toggletip-container\"  aria-hidden=\"true\" role=\"tooltip\">\n            <div>\n                <dd>${content}</dd>\n            </div>\n             <header>\n                <dt aria-hidden=\"true\">${title}</dt> \n                <button class=\"close\" aria-label=\"close dialog\">X</button>\n            </header>\n        </div>\n        `;\n        return container;\n    }\n\n    function clickHandler(e) {\n        const toggle = e.target;\n        const id = toggle.getAttribute('aria-describedby');\n        const tip = document.getElementById(id);\n\n        // Calculate the vertical position of the toggletip\n        const height = tip.offsetHeight;\n        tip.getBoundingClientRect().top < height ? tip.classList.add('below') : tip.classList.remove('below');\n\n        // Calculate the horizontal positions of the toggletip, tip, and parent container\n        // The parent of the toggletip is set to position: relative; So the math is always relative to the parent container\n        const windowWidth = window.innerWidth;\n        const tipPosition = tip.getBoundingClientRect();\n        const togglePosition = toggle.getBoundingClientRect();\n        const parentPosition = toggle.parentElement.getBoundingClientRect();\n\n        const tipWidth = tipPosition.width;\n        const toggleWidth = togglePosition.width;\n        const parentWidth = parentPosition.width;\n        const relativeTipPosition = togglePosition.left - parentPosition.left;\n\n\n        if (window.isDebug) {\n            window.moveLeft = (amount) => tip.style.left = amount + 'px';\n            window.moveRight = (amount) => tip.style.right = amount + 'px';\n            window.toggle = toggle;\n            window.tip = tip;\n\n\n            console.log('TogglePositionLeft', togglePosition.left);\n            console.log('ToggleWidth', toggleWidth);\n\n            console.log('TipPositionLeft', tipPosition.left);\n            console.log('TipWidth', tipWidth);\n\n            console.log('toggleParentPositionLeft', parentPosition.left);\n            console.log('toggleParentPositionRight', parentPosition.right);\n\n            console.log('relativeTipPosition:', relativeTipPosition);\n        }\n\n\n\n\n        if (tipWidth + tipPosition.left > windowWidth) {\n            // If the tip is larger than the window\n            tip.style.width = windowWidth - 20 + 'px';\n            tip.style.left = -parentPosition.left + 10 + 'px';\n        } else if (relativeTipPosition + (toggleWidth / 2) - (tipWidth / 2) < 0) {\n            // If the tip would go off the left side of the parent container\n            tip.style.width = windowWidth - 20 + 'px';\n            tip.style.left = -parentPosition.left + 10 + 'px';\n        } else if (relativeTipPosition + (toggleWidth / 2) + (tipWidth / 2) > parentWidth) {\n            // If the tip would to go off the right side of the parent container\n            tip.style.left = parentWidth - tipWidth + 'px';\n        } else {\n            // Center the tip within the parent container\n            tip.style.left = relativeTipPosition + (toggleWidth / 2) - (tipWidth / 2) + 'px';\n        }\n\n\n\n\n\n        const isActive = toggle.getAttribute('data-toggletip-active') === 'true';\n        toggle.setAttribute('data-toggletip-active', !isActive);\n        tip.setAttribute('aria-hidden', isActive);\n\n\n        const closeHandler = () => {\n            toggle.setAttribute('data-toggletip-active', false);\n            tip.setAttribute('aria-hidden', true);\n        };\n\n        if (isActive) {tip.focus();}\n\n        tip.addEventListener('keydown', () => {\n            if (e.keyCode === 27) {\n                closeHandler();\n            }\n        });\n\n        tip.querySelector('.close').addEventListener('click', () => {\n            toggle.focus();\n            closeHandler();\n        });\n\n        document.addEventListener('click', function (e) {\n            if (e.target !== toggle && tip.contains(e.target) === false) {\n                closeHandler();\n            }\n        });\n    }\n\n    document.addEventListener('DOMContentLoaded', init);\n})();\n\n\nif (window.isDebug) {\n    const title = document.querySelector('a.deptTitle');\n    document.onmousemove = function (e) {\n        var x = e.pageX;\n        var y = e.pageY;\n        title.innerText = \"X \" + x + \", Y \" + y;\n    };\n}\n\n"],"names":["window","isDebug","clickHandler","e","toggle","target","id","getAttribute","tip","document","getElementById","height","offsetHeight","getBoundingClientRect","top","classList","add","remove","windowWidth","innerWidth","tipPosition","togglePosition","parentPosition","parentElement","tipWidth","width","toggleWidth","parentWidth","relativeTipPosition","left","moveLeft","amount","style","moveRight","right","console","log","isActive","setAttribute","closeHandler","focus","addEventListener","keyCode","querySelector","contains","querySelectorAll","forEach","toggletip","i","name","innerText","toLowerCase","replace","content","insertAdjacentHTML","title","toUpperCase","slice","container","tooltipContainer","position","onmousemove","x","pageX","y","pageY"],"mappings":"yBA+IA,GA9IAA,OAAOC,SAAU,EAEjB,WA0CI,SAASC,EAAaC,GAClB,MAAMC,EAASD,EAAEE,OACXC,EAAKF,EAAOG,aAAa,oBACzBC,EAAMC,SAASC,eAAeJ,GAG9BK,EAASH,EAAII,aACnBJ,EAAIK,wBAAwBC,IAAMH,EAASH,EAAIO,UAAUC,IAAI,SAAWR,EAAIO,UAAUE,OAAO,SAI7F,MAAMC,EAAclB,OAAOmB,WACrBC,EAAcZ,EAAIK,wBAClBQ,EAAiBjB,EAAOS,wBACxBS,EAAiBlB,EAAOmB,cAAcV,wBAEtCW,EAAWJ,EAAYK,MACvBC,EAAcL,EAAeI,MAC7BE,EAAcL,EAAeG,MAC7BG,EAAsBP,EAAeQ,KAAOP,EAAeO,KAG7D7B,OAAOC,UACPD,OAAO8B,SAAYC,GAAWvB,EAAIwB,MAAMH,KAAOE,EAAS,KACxD/B,OAAOiC,UAAaF,GAAWvB,EAAIwB,MAAME,MAAQH,EAAS,KAC1D/B,OAAOI,OAASA,EAChBJ,OAAOQ,IAAMA,EAGb2B,QAAQC,IAAI,qBAAsBf,EAAeQ,MACjDM,QAAQC,IAAI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