{"version":3,"file":"frame.js","names":[],"sources":["../resources/javascript/features/quick-add/lazy.js","../resources/javascript/frame.js"],"sourcesContent":["// @ts-check\n\nimport { once } from 'lodash-es';\nimport { whenIdle } from '../../util/idle';\nimport { onDOMContentLoaded } from '../../util/loaded';\nimport { trackD3REvent } from '../../util/tracking';\n\nexport const loadQuickAdd = once(async () => await import('./index'));\n\n/**\n * Open the quick add overlay for a product.\n *\n * @param {object} data\n */\nexport const quickAdd = async data => {\n    const { quickAdd } = await loadQuickAdd();\n    return await quickAdd(data);\n};\n\n/**\n * Initialise quick add buttons.\n *\n * Any element with the `data-quick-add` attribute will be treated as a quick\n * add button. The value of the attribute should be a JSON encoded object. The\n * object must contain either a `productUrlname` or `key` property. It may\n * optionally contain an `options` property containing product options and the\n * quantity to add to the basket.\n *\n * See the PHP class method App\\Products\\Option\\OptionSet::getQuickAddButton for\n * example button HTML.\n *\n * Quick add functionality is lazy-loaded when the browser is idle, if a quick\n * add button is present on the page at the time initQuickAddButtons is called.\n * It is also lazy-loaded immediately after a quick add button is clicked, if\n * not already loaded.\n */\nexport const initQuickAddButtons = async () => {\n    document.addEventListener('click', async e => {\n        if (!(e.target instanceof Element)) {\n            return false;\n        }\n\n        const button = e.target.closest('[data-quick-add]');\n        if (!(button instanceof HTMLElement) || !button.dataset.quickAdd) {\n            return;\n        }\n\n        e.preventDefault();\n\n        // TODO should we support a loading indicator?\n\n        const data = JSON.parse(button.dataset.quickAdd);\n\n        // get the context of where the quick add button was clicked\n        let category = button.closest('.product-comparison-table__primary-cta')\n            ? 'quick-add/comparison'\n            : 'quick-add';\n\n        trackD3REvent({\n            category: category,\n            action: 'Added to basket',\n            label: 'User clicked quick add button',\n            value: { ...data, pathname: window.location.pathname },\n        });\n\n        if (data.suppressOverlay) {\n            button.disabled = true;\n            button.classList.add('button--added');\n            try {\n                await quickAdd(data);\n                setTimeout(() => {\n                    button.disabled = false;\n                    button.classList.remove('button--added');\n                }, 2000);\n            } catch {\n                button.disabled = false;\n                button.classList.remove('button--added');\n            }\n        } else {\n            await quickAdd(data);\n        }\n    });\n\n    // If any quick add buttons are currently on the page, lazy-load quick add\n    // functionality when the browser is idle.\n    onDOMContentLoaded(() => {\n        whenIdle(() => {\n            if (document.querySelector('[data-quick-add]')) {\n                whenIdle(async () => {\n                    await loadQuickAdd();\n                }, 3000);\n            }\n        });\n    });\n};\n","// @ts-check\n\nimport './features/dixa/globals';\nimport { initQuickAddButtons } from './features/quick-add/lazy';\nimport { initShortlistButtons } from './features/shortlist/lazy';\n\ninitShortlistButtons();\ninitQuickAddButtons();\n"],"mappings":"2QAOA,MAAa,EAAe,EAAK,SAAY,MAAM,OAAO,iCAAW,CAOxD,EAAW,KAAM,IAAQ,CAClC,GAAM,CAAE,YAAa,MAAM,GAAc,CACzC,OAAO,MAAM,EAAS,EAAK,ECV/B,GAAsB,ED8Ba,SAAY,CAC3C,SAAS,iBAAiB,QAAS,KAAM,IAAK,CAC1C,GAAI,EAAE,EAAE,kBAAkB,SACtB,MAAO,GAGX,IAAM,EAAS,EAAE,OAAO,QAAQ,mBAAmB,CACnD,GAAI,EAAE,aAAkB,cAAgB,CAAC,EAAO,QAAQ,SACpD,OAGJ,EAAE,gBAAgB,CAIlB,IAAM,EAAO,KAAK,MAAM,EAAO,QAAQ,SAAS,CAchD,GAPA,EAAc,CACV,SALW,EAAO,QAAQ,yCAAyC,CACjE,uBACA,YAIF,OAAQ,kBACR,MAAO,gCACP,MAAO,CAAE,GAAG,EAAM,SAAU,OAAO,SAAS,UAC/C,CAAC,CAEE,EAAK,gBAAiB,CACtB,EAAO,SAAW,GAClB,EAAO,UAAU,IAAI,gBAAgB,CACrC,GAAI,CACA,MAAM,EAAS,EAAK,CACpB,eAAiB,CACb,EAAO,SAAW,GAClB,EAAO,UAAU,OAAO,gBAAgB,EACzC,IAAK,MACJ,CACJ,EAAO,SAAW,GAClB,EAAO,UAAU,OAAO,gBAAgB,OAG5C,MAAM,EAAS,EAAK,EAE1B,CAIF,MAAyB,CACrB,MAAe,CACP,SAAS,cAAc,mBAAmB,EAC1C,EAAS,SAAY,CACjB,MAAM,GAAc,EACrB,IAAK,EAEd,EACJ,ICtFe"}