@pernod-ricard-global-cms/jsutils 3.0.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/jsutils.mjs +72 -0
  2. package/package.json +1 -1
package/jsutils.mjs CHANGED
@@ -9,6 +9,21 @@ export function isWpAdmin() {
9
9
  return document.body.classList.contains("wp-admin") ? true : false;
10
10
  }
11
11
 
12
+ export function ttfb() {
13
+ new PerformanceObserver((entryList) => {
14
+ const [pageNav] = entryList.getEntriesByType('navigation');
15
+ const time = pageNav.responseStart;
16
+ console.log(`TTFB: ${time} new`);
17
+ pushData(time);
18
+ }).observe({type: 'navigation', buffered: true});
19
+ }
20
+
21
+ function pushData(time) {
22
+ window.dataLayer = window.dataLayer || [];
23
+ window.dataLayer.push({ttfb: time});
24
+ console.log('ttfb sent');
25
+ }
26
+
12
27
  /**
13
28
  * This function accepts an html element and returns the position of that element on the page.
14
29
  *
@@ -596,6 +611,63 @@ export function appendPreconnect(domain) {
596
611
  }
597
612
  }
598
613
 
614
+ /**
615
+ *
616
+ * @param {string} src The URL src to load
617
+ */
618
+ async function loadScript(src) {
619
+ const script = document.createElement('script');
620
+ script.type = 'text/javascript';
621
+ script.src = src;
622
+ script.async = true;
623
+ document.head.appendChild(script);
624
+ await new Promise((resolve, reject) => {
625
+ script.onload = resolve;
626
+ script.onerror = () => reject(new Error(`Script load error for ${src}`));
627
+ });
628
+ }
629
+
630
+ /**
631
+ *
632
+ * @param {object} event The click event object
633
+ */
634
+ async function handleCTBClick(event) {
635
+ if (!window.isCTBLoaded) {
636
+ const domain = window.location.hostname;
637
+ // Assuming PR-CLICKTOBUY plugin is enabled to make the script load
638
+ await loadScript(`/wp-content/plugins/pr-clicktobuy/public/js/pr-clicktobuy-public.js`);
639
+ window.isCTBLoaded = true;
640
+
641
+ // This is important!! Never remove this code
642
+ setTimeout(() => {
643
+ event.target.click();
644
+ }, 100);
645
+ }
646
+ }
647
+
648
+ /**
649
+ *
650
+ * @param {HTMLElement} element The element which needs to set the Attributes
651
+ */
652
+ function setAttributesToMenuItem(element) {
653
+ const body = document.querySelector('body');
654
+ const localLang = body.dataset.sitelanguage;
655
+ const locale = localLang.replace(/_/g,'-');
656
+ element.setAttribute('data-ctbuy-lang', locale);
657
+ }
658
+
659
+ /**
660
+ *
661
+ * @param {HTMLElement} block The element CTA which triggers
662
+ */
663
+ export function ctbCTAClickHandler(block = document) {
664
+ const elements = block.querySelectorAll('[data-ctbuy]');
665
+ elements.forEach(element => {
666
+ setAttributesToMenuItem(element);
667
+ element.addEventListener('click', handleCTBClick);
668
+ });
669
+ }
670
+
599
671
  const api = {
600
672
  copyToClipboard,
601
673
  checkDevice,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pernod-ricard-global-cms/jsutils",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "description": "Handy collection of Javascript utility functions",
5
5
  "type": "commonjs",
6
6
  "main": "jsutils.mjs",