@total_onion/onion-library 3.0.49 → 3.1.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.
@@ -0,0 +1,157 @@
1
+ import {
2
+ generateModal,
3
+ stopVideos,
4
+ revealVideoElement,
5
+ hideVideoElement,
6
+ dataLayerPush,
7
+ } from "./vcUtils.mjs";
8
+
9
+ const players = new Map();
10
+
11
+ function youtubeInit(videoObject) {
12
+ const autoplay = videoObject.autoplay;
13
+ const playerId = `yt-${videoObject.videoid}`;
14
+ videoObject.playerId = playerId;
15
+ if (autoplay) {
16
+ triggerYoutube(videoObject);
17
+ }
18
+ }
19
+
20
+ async function triggerYoutube(videoObject) {
21
+ const { playerId, modal, isAdmin } = videoObject;
22
+ videoObject.elementType = "iframe";
23
+
24
+ if (!window.YT) {
25
+ console.log("YT not loaded yet in triggerYoutube");
26
+ await injectYouTubeIframeScript();
27
+ console.log("YT loaded in triggerYoutube");
28
+ }
29
+
30
+ if (modal && !isAdmin) {
31
+ generateModal(videoObject);
32
+ }
33
+ if (!players.get(playerId)) {
34
+ initializePlayer(videoObject);
35
+ return;
36
+ }
37
+
38
+ const playerData = players.get(playerId);
39
+ if (!playerData) {
40
+ console.error(`no player data found for ${playerId}`);
41
+ return;
42
+ }
43
+
44
+ const { player } = playerData;
45
+ if (playerData.initialized) {
46
+ player.playVideo();
47
+ } else {
48
+ console.error(`YouTube player not initialized for ${playerId}`);
49
+ }
50
+ }
51
+
52
+ async function initializePlayer(videoObject) {
53
+ const playerId = videoObject.playerId;
54
+ const player = createYouTubePlayer(videoObject);
55
+ videoObject.youtubeplayer = player;
56
+ players.set(playerId, { initialized: true, player, videoObject });
57
+ }
58
+
59
+ function createYouTubePlayer(videoObject) {
60
+ const playerId = videoObject.playerId;
61
+ const srcDesktop = videoObject.videocontainer.dataset.youtubedesktop;
62
+ const srcMobile = videoObject.videocontainer.dataset.youtubemobile;
63
+ const videoSrc =
64
+ window.innerWidth < videoObject.globalSettings.srcBreakpoint && srcMobile
65
+ ? srcMobile
66
+ : srcDesktop;
67
+
68
+ return new YT.Player(playerId, {
69
+ videoId: videoSrc,
70
+ playerVars: {
71
+ autoplay: videoObject.autoplay ? 1 : 0,
72
+ mute: videoObject.muted ? 1 : 0,
73
+ controls: videoObject.controls ? 1 : 0,
74
+ loop: videoObject.loop ? 1 : 0,
75
+ },
76
+ events: {
77
+ onStateChange: (event) => onPlayerStateChange(event, videoObject),
78
+ onReady: (event) => {
79
+ videoObject.instance.setVideoReadyState(videoObject, true);
80
+ event.target.playVideo();
81
+ },
82
+ },
83
+ });
84
+ }
85
+
86
+ function onPlayerStateChange(event, videoObject) {
87
+ const { globalSettings } = videoObject;
88
+ switch (event.data) {
89
+ case YT.PlayerState.PLAYING:
90
+ globalSettings.enableDebugLogs && console.log("Playing the video");
91
+ stopVideos(videoObject);
92
+ revealVideoElement(videoObject);
93
+ if (videoObject.dataLayerPush) {
94
+ dataLayerPush({ eventname: "play", videoObject });
95
+ }
96
+ break;
97
+ case YT.PlayerState.PAUSED:
98
+ globalSettings.enableDebugLogs && console.log("Pausing the video");
99
+ if (videoObject.dataLayerPush) {
100
+ dataLayerPush({ eventname: "pause", videoObject });
101
+ }
102
+ break;
103
+
104
+ case YT.PlayerState.ENDED:
105
+ if (videoObject.loop) {
106
+ const player = players.get(videoObject.playerId).player;
107
+ player.playVideo();
108
+ return;
109
+ }
110
+ globalSettings.enableDebugLogs && console.log("Video ended");
111
+ hideVideoElement(videoObject);
112
+ if (videoObject.dataLayerPush) {
113
+ dataLayerPush({ eventname: "ended", videoObject });
114
+ }
115
+ break;
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Injects the youtube iframe api script and waits for the YT object to be instantiated before resolving the promise
121
+ * @returns {promise} Resolves once the YT object is available.
122
+ */
123
+ function injectYouTubeIframeScript() {
124
+ const prom = new Promise((resolve) => {
125
+ if (globalThis.YT) {
126
+ return resolve();
127
+ }
128
+ const tag = document.createElement("script");
129
+ tag.id = "iframe-api";
130
+ tag.src = "https://www.youtube.com/iframe_api";
131
+
132
+ const firstScriptTag = document.getElementsByTagName("script")[0];
133
+ firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
134
+
135
+ tag.addEventListener("load", () => {
136
+ if (!YT.loaded) {
137
+ const loadingCheck = setInterval(() => {
138
+ if (YT.loaded) {
139
+ clearInterval(loadingCheck);
140
+ return resolve(true);
141
+ }
142
+ }, 50);
143
+ } else {
144
+ return resolve(true);
145
+ }
146
+ });
147
+ });
148
+
149
+ return prom;
150
+ }
151
+
152
+ const api = {
153
+ youtubeInit,
154
+ triggerYoutube,
155
+ };
156
+
157
+ export default api;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@total_onion/onion-library",
3
- "version": "3.0.49",
3
+ "version": "3.1.0",
4
4
  "description": "Component library",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,10 +18,8 @@
18
18
  },
19
19
  "homepage": "https://github.com/TotalOnion/onion-library#readme",
20
20
  "dependencies": {
21
- "@total_onion/onion-modalcontroller": "^1.2.4",
22
- "@total_onion/onion-videocontroller": "^1.2.6",
23
21
  "lottie-web": "^5.13.0",
24
- "swiper": "^12.1.3"
22
+ "swiper": "^14.1.0"
25
23
  },
26
24
  "devDependencies": {
27
25
  "autoprefixer": "^10.4.27",