1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (global, factory) {
|
9 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
10 | typeof define === 'function' && define.amd ? define(factory) :
|
11 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.lgVideo = factory());
|
12 | }(this, (function () { 'use strict';
|
13 |
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var __assign = function() {
|
30 | __assign = Object.assign || function __assign(t) {
|
31 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
32 | s = arguments[i];
|
33 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
34 | }
|
35 | return t;
|
36 | };
|
37 | return __assign.apply(this, arguments);
|
38 | };
|
39 |
|
40 | var videoSettings = {
|
41 | autoplayFirstVideo: true,
|
42 | youTubePlayerParams: false,
|
43 | vimeoPlayerParams: false,
|
44 | wistiaPlayerParams: false,
|
45 | gotoNextSlideOnVideoEnd: true,
|
46 | autoplayVideoOnSlide: false,
|
47 | videojs: false,
|
48 | videojsTheme: '',
|
49 | videojsOptions: {},
|
50 | };
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | var lGEvents = {
|
58 | afterAppendSlide: 'lgAfterAppendSlide',
|
59 | init: 'lgInit',
|
60 | hasVideo: 'lgHasVideo',
|
61 | containerResize: 'lgContainerResize',
|
62 | updateSlides: 'lgUpdateSlides',
|
63 | afterAppendSubHtml: 'lgAfterAppendSubHtml',
|
64 | beforeOpen: 'lgBeforeOpen',
|
65 | afterOpen: 'lgAfterOpen',
|
66 | slideItemLoad: 'lgSlideItemLoad',
|
67 | beforeSlide: 'lgBeforeSlide',
|
68 | afterSlide: 'lgAfterSlide',
|
69 | posterClick: 'lgPosterClick',
|
70 | dragStart: 'lgDragStart',
|
71 | dragMove: 'lgDragMove',
|
72 | dragEnd: 'lgDragEnd',
|
73 | beforeNextSlide: 'lgBeforeNextSlide',
|
74 | beforePrevSlide: 'lgBeforePrevSlide',
|
75 | beforeClose: 'lgBeforeClose',
|
76 | afterClose: 'lgAfterClose',
|
77 | rotateLeft: 'lgRotateLeft',
|
78 | rotateRight: 'lgRotateRight',
|
79 | flipHorizontal: 'lgFlipHorizontal',
|
80 | flipVertical: 'lgFlipVertical',
|
81 | autoplay: 'lgAutoplay',
|
82 | autoplayStart: 'lgAutoplayStart',
|
83 | autoplayStop: 'lgAutoplayStop',
|
84 | };
|
85 |
|
86 | var param = function (obj) {
|
87 | return Object.keys(obj)
|
88 | .map(function (k) {
|
89 | return encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]);
|
90 | })
|
91 | .join('&');
|
92 | };
|
93 | var paramsToObject = function (url) {
|
94 | var paramas = url
|
95 | .slice(1)
|
96 | .split('&')
|
97 | .map(function (p) { return p.split('='); })
|
98 | .reduce(function (obj, pair) {
|
99 | var _a = pair.map(decodeURIComponent), key = _a[0], value = _a[1];
|
100 | obj[key] = value;
|
101 | return obj;
|
102 | }, {});
|
103 | return paramas;
|
104 | };
|
105 | var getYouTubeParams = function (videoInfo, youTubePlayerParamsSettings) {
|
106 | if (!videoInfo.youtube)
|
107 | return '';
|
108 | var slideUrlParams = videoInfo.youtube[2]
|
109 | ? paramsToObject(videoInfo.youtube[2])
|
110 | : '';
|
111 |
|
112 | var defaultYouTubePlayerParams = {
|
113 | wmode: 'opaque',
|
114 | autoplay: 0,
|
115 | mute: 1,
|
116 | enablejsapi: 1,
|
117 | };
|
118 | var playerParamsSettings = youTubePlayerParamsSettings || {};
|
119 | var youTubePlayerParams = __assign(__assign(__assign({}, defaultYouTubePlayerParams), playerParamsSettings), slideUrlParams);
|
120 | var youTubeParams = "?" + param(youTubePlayerParams);
|
121 | return youTubeParams;
|
122 | };
|
123 | var isYouTubeNoCookie = function (url) {
|
124 | return url.includes('youtube-nocookie.com');
|
125 | };
|
126 | var getVimeoURLParams = function (defaultParams, videoInfo) {
|
127 | if (!videoInfo || !videoInfo.vimeo)
|
128 | return '';
|
129 | var urlParams = videoInfo.vimeo[2] || '';
|
130 | var defaultPlayerParams = defaultParams && Object.keys(defaultParams).length !== 0
|
131 | ? '&' + param(defaultParams)
|
132 | : '';
|
133 |
|
134 | var urlWithHash = videoInfo.vimeo[0].split('/').pop() || '';
|
135 | var urlWithHashWithParams = urlWithHash.split('?')[0] || '';
|
136 | var hash = urlWithHashWithParams.split('#')[0];
|
137 | var isPrivate = videoInfo.vimeo[1] !== hash;
|
138 | if (isPrivate) {
|
139 | urlParams = urlParams.replace("/" + hash, '');
|
140 | }
|
141 | urlParams =
|
142 | urlParams[0] == '?' ? '&' + urlParams.slice(1) : urlParams || '';
|
143 |
|
144 | var vimeoPlayerParams = "?autoplay=0&muted=1" + (isPrivate ? "&h=" + hash : '') + defaultPlayerParams + urlParams;
|
145 | return vimeoPlayerParams;
|
146 | };
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | var Video = (function () {
|
174 | function Video(instance) {
|
175 |
|
176 | this.core = instance;
|
177 | this.settings = __assign(__assign({}, videoSettings), this.core.settings);
|
178 | return this;
|
179 | }
|
180 | Video.prototype.init = function () {
|
181 | var _this = this;
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 | this.core.LGel.on(lGEvents.hasVideo + ".video", this.onHasVideo.bind(this));
|
188 | this.core.LGel.on(lGEvents.posterClick + ".video", function () {
|
189 | var $el = _this.core.getSlideItem(_this.core.index);
|
190 | _this.loadVideoOnPosterClick($el);
|
191 | });
|
192 | this.core.LGel.on(lGEvents.slideItemLoad + ".video", this.onSlideItemLoad.bind(this));
|
193 |
|
194 | this.core.LGel.on(lGEvents.beforeSlide + ".video", this.onBeforeSlide.bind(this));
|
195 |
|
196 | this.core.LGel.on(lGEvents.afterSlide + ".video", this.onAfterSlide.bind(this));
|
197 | };
|
198 | |
199 |
|
200 |
|
201 |
|
202 |
|
203 | Video.prototype.onSlideItemLoad = function (event) {
|
204 | var _this = this;
|
205 | var _a = event.detail, isFirstSlide = _a.isFirstSlide, index = _a.index;
|
206 |
|
207 | if (this.settings.autoplayFirstVideo &&
|
208 | isFirstSlide &&
|
209 | index === this.core.index) {
|
210 |
|
211 | setTimeout(function () {
|
212 | _this.loadAndPlayVideo(index);
|
213 | }, 200);
|
214 | }
|
215 |
|
216 | if (!isFirstSlide &&
|
217 | this.settings.autoplayVideoOnSlide &&
|
218 | index === this.core.index) {
|
219 | this.loadAndPlayVideo(index);
|
220 | }
|
221 | };
|
222 | |
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | Video.prototype.onHasVideo = function (event) {
|
230 | var _a = event.detail, index = _a.index, src = _a.src, html5Video = _a.html5Video, hasPoster = _a.hasPoster;
|
231 | if (!hasPoster) {
|
232 |
|
233 | this.appendVideos(this.core.getSlideItem(index), {
|
234 | src: src,
|
235 | addClass: 'lg-object',
|
236 | index: index,
|
237 | html5Video: html5Video,
|
238 | });
|
239 |
|
240 | this.gotoNextSlideOnVideoEnd(src, index);
|
241 | }
|
242 | };
|
243 | |
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | Video.prototype.onBeforeSlide = function (event) {
|
253 | if (this.core.lGalleryOn) {
|
254 | var prevIndex = event.detail.prevIndex;
|
255 | this.pauseVideo(prevIndex);
|
256 | }
|
257 | };
|
258 | |
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | Video.prototype.onAfterSlide = function (event) {
|
268 | var _this = this;
|
269 | var _a = event.detail, index = _a.index, prevIndex = _a.prevIndex;
|
270 |
|
271 | var $slide = this.core.getSlideItem(index);
|
272 | if (this.settings.autoplayVideoOnSlide && index !== prevIndex) {
|
273 | if ($slide.hasClass('lg-complete')) {
|
274 | setTimeout(function () {
|
275 | _this.loadAndPlayVideo(index);
|
276 | }, 100);
|
277 | }
|
278 | }
|
279 | };
|
280 | Video.prototype.loadAndPlayVideo = function (index) {
|
281 | var $slide = this.core.getSlideItem(index);
|
282 | var currentGalleryItem = this.core.galleryItems[index];
|
283 | if (currentGalleryItem.poster) {
|
284 | this.loadVideoOnPosterClick($slide, true);
|
285 | }
|
286 | else {
|
287 | this.playVideo(index);
|
288 | }
|
289 | };
|
290 | |
291 |
|
292 |
|
293 |
|
294 | Video.prototype.playVideo = function (index) {
|
295 | this.controlVideo(index, 'play');
|
296 | };
|
297 | |
298 |
|
299 |
|
300 |
|
301 | Video.prototype.pauseVideo = function (index) {
|
302 | this.controlVideo(index, 'pause');
|
303 | };
|
304 | Video.prototype.getVideoHtml = function (src, addClass, index, html5Video) {
|
305 | var video = '';
|
306 | var videoInfo = this.core.galleryItems[index]
|
307 | .__slideVideoInfo || {};
|
308 | var currentGalleryItem = this.core.galleryItems[index];
|
309 | var videoTitle = currentGalleryItem.title || currentGalleryItem.alt;
|
310 | videoTitle = videoTitle ? 'title="' + videoTitle + '"' : '';
|
311 | var commonIframeProps = "allowtransparency=\"true\"\n frameborder=\"0\"\n scrolling=\"no\"\n allowfullscreen\n mozallowfullscreen\n webkitallowfullscreen\n oallowfullscreen\n msallowfullscreen";
|
312 | if (videoInfo.youtube) {
|
313 | var videoId = 'lg-youtube' + index;
|
314 | var youTubeParams = getYouTubeParams(videoInfo, this.settings.youTubePlayerParams);
|
315 | var isYouTubeNoCookieURL = isYouTubeNoCookie(src);
|
316 | var youtubeURL = isYouTubeNoCookieURL
|
317 | ? '//www.youtube-nocookie.com/'
|
318 | : '//www.youtube.com/';
|
319 | video = "<iframe allow=\"autoplay\" id=" + videoId + " class=\"lg-video-object lg-youtube " + addClass + "\" " + videoTitle + " src=\"" + youtubeURL + "embed/" + (videoInfo.youtube[1] + youTubeParams) + "\" " + commonIframeProps + "></iframe>";
|
320 | }
|
321 | else if (videoInfo.vimeo) {
|
322 | var videoId = 'lg-vimeo' + index;
|
323 | var playerParams = getVimeoURLParams(this.settings.vimeoPlayerParams, videoInfo);
|
324 | video = "<iframe allow=\"autoplay\" id=" + videoId + " class=\"lg-video-object lg-vimeo " + addClass + "\" " + videoTitle + " src=\"//player.vimeo.com/video/" + (videoInfo.vimeo[1] + playerParams) + "\" " + commonIframeProps + "></iframe>";
|
325 | }
|
326 | else if (videoInfo.wistia) {
|
327 | var wistiaId = 'lg-wistia' + index;
|
328 | var playerParams = param(this.settings.wistiaPlayerParams);
|
329 | playerParams = playerParams ? '?' + playerParams : '';
|
330 | video = "<iframe allow=\"autoplay\" id=\"" + wistiaId + "\" src=\"//fast.wistia.net/embed/iframe/" + (videoInfo.wistia[4] + playerParams) + "\" " + videoTitle + " class=\"wistia_embed lg-video-object lg-wistia " + addClass + "\" name=\"wistia_embed\" " + commonIframeProps + "></iframe>";
|
331 | }
|
332 | else if (videoInfo.html5) {
|
333 | var html5VideoMarkup = '';
|
334 | for (var i = 0; i < html5Video.source.length; i++) {
|
335 | html5VideoMarkup += "<source src=\"" + html5Video.source[i].src + "\" type=\"" + html5Video.source[i].type + "\">";
|
336 | }
|
337 | if (html5Video.tracks) {
|
338 | var _loop_1 = function (i) {
|
339 | var trackAttributes = '';
|
340 | var track = html5Video.tracks[i];
|
341 | Object.keys(track || {}).forEach(function (key) {
|
342 | trackAttributes += key + "=\"" + track[key] + "\" ";
|
343 | });
|
344 | html5VideoMarkup += "<track " + trackAttributes + ">";
|
345 | };
|
346 | for (var i = 0; i < html5Video.tracks.length; i++) {
|
347 | _loop_1(i);
|
348 | }
|
349 | }
|
350 | var html5VideoAttrs_1 = '';
|
351 | var videoAttributes_1 = html5Video.attributes || {};
|
352 | Object.keys(videoAttributes_1 || {}).forEach(function (key) {
|
353 | html5VideoAttrs_1 += key + "=\"" + videoAttributes_1[key] + "\" ";
|
354 | });
|
355 | video = "<video class=\"lg-video-object lg-html5 " + (this.settings.videojs && this.settings.videojsTheme
|
356 | ? this.settings.videojsTheme + ' '
|
357 | : '') + " " + (this.settings.videojs ? ' video-js' : '') + "\" " + html5VideoAttrs_1 + ">\n " + html5VideoMarkup + "\n Your browser does not support HTML5 video.\n </video>";
|
358 | }
|
359 | return video;
|
360 | };
|
361 | |
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | Video.prototype.appendVideos = function (el, videoParams) {
|
368 | var _a;
|
369 | var videoHtml = this.getVideoHtml(videoParams.src, videoParams.addClass, videoParams.index, videoParams.html5Video);
|
370 | el.find('.lg-video-cont').append(videoHtml);
|
371 | var $videoElement = el.find('.lg-video-object').first();
|
372 | if (videoParams.html5Video) {
|
373 | $videoElement.on('mousedown.lg.video', function (e) {
|
374 | e.stopPropagation();
|
375 | });
|
376 | }
|
377 | if (this.settings.videojs && ((_a = this.core.galleryItems[videoParams.index].__slideVideoInfo) === null || _a === void 0 ? void 0 : _a.html5)) {
|
378 | try {
|
379 | return videojs($videoElement.get(), this.settings.videojsOptions);
|
380 | }
|
381 | catch (e) {
|
382 | console.error('lightGallery:- Make sure you have included videojs');
|
383 | }
|
384 | }
|
385 | };
|
386 | Video.prototype.gotoNextSlideOnVideoEnd = function (src, index) {
|
387 | var _this = this;
|
388 | var $videoElement = this.core
|
389 | .getSlideItem(index)
|
390 | .find('.lg-video-object')
|
391 | .first();
|
392 | var videoInfo = this.core.galleryItems[index].__slideVideoInfo || {};
|
393 | if (this.settings.gotoNextSlideOnVideoEnd) {
|
394 | if (videoInfo.html5) {
|
395 | $videoElement.on('ended', function () {
|
396 | _this.core.goToNextSlide();
|
397 | });
|
398 | }
|
399 | else if (videoInfo.vimeo) {
|
400 | try {
|
401 |
|
402 | new Vimeo.Player($videoElement.get()).on('ended', function () {
|
403 | _this.core.goToNextSlide();
|
404 | });
|
405 | }
|
406 | catch (e) {
|
407 | console.error('lightGallery:- Make sure you have included //github.com/vimeo/player.js');
|
408 | }
|
409 | }
|
410 | else if (videoInfo.wistia) {
|
411 | try {
|
412 | window._wq = window._wq || [];
|
413 |
|
414 | window._wq.push({
|
415 | id: $videoElement.attr('id'),
|
416 | onReady: function (video) {
|
417 | video.bind('end', function () {
|
418 | _this.core.goToNextSlide();
|
419 | });
|
420 | },
|
421 | });
|
422 | }
|
423 | catch (e) {
|
424 | console.error('lightGallery:- Make sure you have included //fast.wistia.com/assets/external/E-v1.js');
|
425 | }
|
426 | }
|
427 | }
|
428 | };
|
429 | Video.prototype.controlVideo = function (index, action) {
|
430 | var $videoElement = this.core
|
431 | .getSlideItem(index)
|
432 | .find('.lg-video-object')
|
433 | .first();
|
434 | var videoInfo = this.core.galleryItems[index].__slideVideoInfo || {};
|
435 | if (!$videoElement.get())
|
436 | return;
|
437 | if (videoInfo.youtube) {
|
438 | try {
|
439 | $videoElement.get().contentWindow.postMessage("{\"event\":\"command\",\"func\":\"" + action + "Video\",\"args\":\"\"}", '*');
|
440 | }
|
441 | catch (e) {
|
442 | console.error("lightGallery:- " + e);
|
443 | }
|
444 | }
|
445 | else if (videoInfo.vimeo) {
|
446 | try {
|
447 | new Vimeo.Player($videoElement.get())[action]();
|
448 | }
|
449 | catch (e) {
|
450 | console.error('lightGallery:- Make sure you have included //github.com/vimeo/player.js');
|
451 | }
|
452 | }
|
453 | else if (videoInfo.html5) {
|
454 | if (this.settings.videojs) {
|
455 | try {
|
456 | videojs($videoElement.get())[action]();
|
457 | }
|
458 | catch (e) {
|
459 | console.error('lightGallery:- Make sure you have included videojs');
|
460 | }
|
461 | }
|
462 | else {
|
463 | $videoElement.get()[action]();
|
464 | }
|
465 | }
|
466 | else if (videoInfo.wistia) {
|
467 | try {
|
468 | window._wq = window._wq || [];
|
469 |
|
470 | window._wq.push({
|
471 | id: $videoElement.attr('id'),
|
472 | onReady: function (video) {
|
473 | video[action]();
|
474 | },
|
475 | });
|
476 | }
|
477 | catch (e) {
|
478 | console.error('lightGallery:- Make sure you have included //fast.wistia.com/assets/external/E-v1.js');
|
479 | }
|
480 | }
|
481 | };
|
482 | Video.prototype.loadVideoOnPosterClick = function ($el, forcePlay) {
|
483 | var _this = this;
|
484 |
|
485 | if (!$el.hasClass('lg-video-loaded')) {
|
486 |
|
487 | if (!$el.hasClass('lg-has-video')) {
|
488 | $el.addClass('lg-has-video');
|
489 | var _html = void 0;
|
490 | var _src = this.core.galleryItems[this.core.index].src;
|
491 | var video = this.core.galleryItems[this.core.index].video;
|
492 | if (video) {
|
493 | _html =
|
494 | typeof video === 'string' ? JSON.parse(video) : video;
|
495 | }
|
496 | var videoJsPlayer_1 = this.appendVideos($el, {
|
497 | src: _src,
|
498 | addClass: '',
|
499 | index: this.core.index,
|
500 | html5Video: _html,
|
501 | });
|
502 | this.gotoNextSlideOnVideoEnd(_src, this.core.index);
|
503 | var $tempImg = $el.find('.lg-object').first().get();
|
504 |
|
505 | $el.find('.lg-video-cont').first().append($tempImg);
|
506 | $el.addClass('lg-video-loading');
|
507 | videoJsPlayer_1 &&
|
508 | videoJsPlayer_1.ready(function () {
|
509 | videoJsPlayer_1.on('loadedmetadata', function () {
|
510 | _this.onVideoLoadAfterPosterClick($el, _this.core.index);
|
511 | });
|
512 | });
|
513 | $el.find('.lg-video-object')
|
514 | .first()
|
515 | .on('load.lg error.lg loadedmetadata.lg', function () {
|
516 | setTimeout(function () {
|
517 | _this.onVideoLoadAfterPosterClick($el, _this.core.index);
|
518 | }, 50);
|
519 | });
|
520 | }
|
521 | else {
|
522 | this.playVideo(this.core.index);
|
523 | }
|
524 | }
|
525 | else if (forcePlay) {
|
526 | this.playVideo(this.core.index);
|
527 | }
|
528 | };
|
529 | Video.prototype.onVideoLoadAfterPosterClick = function ($el, index) {
|
530 | $el.addClass('lg-video-loaded');
|
531 | this.playVideo(index);
|
532 | };
|
533 | Video.prototype.destroy = function () {
|
534 | this.core.LGel.off('.lg.video');
|
535 | this.core.LGel.off('.video');
|
536 | };
|
537 | return Video;
|
538 | }());
|
539 |
|
540 | return Video;
|
541 |
|
542 | })));
|
543 |
|