{"version":3,"file":"frame.js","sources":["../../../resources/modern-javascript/features/quick-add/lazy.js","../../../resources/modern-javascript/entrypoint/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') ? 'quick-add/comparison' : '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    await quickAdd(data);\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 () => { await loadQuickAdd(); }, 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"],"names":["loadQuickAdd","once","async","import","then","n","K","initShortlistButtons","document","addEventListener","e","target","Element","button","closest","HTMLElement","dataset","quickAdd","preventDefault","data","JSON","parse","category","trackD3REvent","action","label","value","_extends","pathname","window","location","onDOMContentLoaded","whenIdle","querySelector","initQuickAddButtons"],"mappings":"2cAOO,MAAMA,EAAeC,GAAKC,eAAkBC,OAAO,iCAAUC,MAAA,SAAAC,GAAA,OAAAA,EAAAC,CAAA,MCDpEC,ID8BmCL,WACjCM,SAASC,iBAAiB,SAASP,UACjC,KAAMQ,EAAEC,kBAAkBC,SACxB,OAAO,EAGT,MAAMC,EAASH,EAAEC,OAAOG,QAAQ,oBAChC,KAAMD,aAAkBE,aAAiBF,EAAOG,QAAQC,UACtD,OAGFP,EAAEQ,iBAIF,MAAMC,EAAOC,KAAKC,MAAMR,EAAOG,QAAQC,UAGvC,IAAIK,EAAWT,EAAOC,QAAQ,0CAA4C,uBAAyB,YAEnGS,EAAc,CACZD,SAAUA,EACVE,OAAQ,kBACRC,MAAO,gCACPC,MAAKC,EAAA,CAAA,EAAOR,EAAI,CAAES,SAAUC,OAAOC,SAASF,kBA9C1B1B,WACtB,MAAMe,SAAEA,SAAmBjB,IAC3B,aAAaiB,EAASE,EAAK,EA+CnBF,CAASE,EAAK,IAKtBY,GAAmB,KACjBC,GAAS,KACHxB,SAASyB,cAAc,qBACzBD,GAAS9B,gBAAoBF,GAAc,GAAK,IACjD,GACD,GACF,ECnEJkC"}