-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal-video.js
163 lines (163 loc) · 5.88 KB
/
modal-video.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"use strict";
var _typeof =
typeof Symbol === "function" && typeof Symbol.iterator === "symbol"
? function (obj) {
return typeof obj;
}
: function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
(function umd(factory) {
"use strict";
if (typeof define === "function" && define.amd) {
define(["jquery"], factory);
} else if ((typeof exports === "undefined" ? "undefined" : _typeof(exports)) === "object") {
module.exports = factory(require("jquery"));
} else {
factory(jQuery);
}
})(function modalVideo($) {
var defaults = {
channel: "youtube",
youtube: {
autoplay: 1,
cc_load_policy: 1,
color: null,
controls: 1,
disablekb: 0,
enablejsapi: 0,
end: null,
fs: 1,
h1: null,
iv_load_policy: 1,
list: null,
listType: null,
loop: 0,
modestbranding: null,
origin: null,
playlist: null,
playsinline: null,
rel: 0,
showinfo: 1,
start: 0,
wmode: "transparent",
theme: "dark",
},
ratio: "16:9",
vimeo: { api: false, autopause: true, autoplay: true, byline: true, callback: null, color: null, height: null, loop: false, maxheight: null, maxwidth: null, player_id: null, portrait: true, title: true, width: null, xhtml: false },
allowFullScreen: true,
animationSpeed: 300,
classNames: {
modalVideo: "modal-video",
modalVideoClose: "modal-video-close",
modalVideoBody: "modal-video-body",
modalVideoInner: "modal-video-inner",
modalVideoIframeWrap: "modal-video-movie-wrap",
modalVideoCloseBtn: "modal-video-close-btn",
},
aria: { openMessage: "You just openned the modal video", dismissBtnMessage: "Close the modal by clicking here" },
};
function getQueryString(obj) {
var url = "";
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key] !== null) {
url += key + "=" + obj[key] + "&";
}
}
}
return url.substr(0, url.length - 1);
}
function getYoutubeUrl(youtube, videoId) {
var query = getQueryString(youtube);
return "https://www.youtube.com/embed/" + videoId + "?" + query;
}
function getVimeoUrl(vimeo, videoId) {
var query = getQueryString(vimeo);
return "https://player.vimeo.com/video/" + videoId + "?" + query;
}
function getVideoUrl(opt, videoId) {
if (opt.channel === "youtube") {
return getYoutubeUrl(opt.youtube, videoId);
} else if (opt.channel === "vimeo") {
return getVimeoUrl(opt.vimeo, videoId);
}
}
function getPadding(ratio) {
var arr = ratio.split(":");
var width = Number(arr[0]);
var height = Number(arr[1]);
var padding = (height * 100) / width;
return padding + "%";
}
function getHtml(opt, videoId) {
var videoUrl = getVideoUrl(opt, videoId);
var padding = getPadding(opt.ratio);
return (
'\n\t\t\t\t\t<div class="' +
opt.classNames.modalVideo +
'" tabindex="-1" role="dialog" aria-label="' +
opt.aria.openMessage +
'">\n\t\t\t\t\t\t<div class="' +
opt.classNames.modalVideoBody +
'">\n\t\t\t\t\t\t\t<div class="' +
opt.classNames.modalVideoInner +
'">\n\t\t\t\t\t\t\t\t<div class="' +
opt.classNames.modalVideoIframeWrap +
'" style="padding-bottom:' +
padding +
'">\n\t\t\t\t\t\t\t\t\t<button class="' +
opt.classNames.modalVideoCloseBtn +
' js-modal-video-dismiss-btn" aria-label="' +
opt.aria.dismissBtnMessage +
"\"/>\n\t\t\t\t\t\t\t\t\t<iframe width='460' height='230' src=\"" +
videoUrl +
"\" frameborder='0' allowfullscreen=" +
opt.allowFullScreen +
' tabindex="-1"/>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</div>\n\t\t\t'
);
}
$.fn.modalVideo = function (opt) {
opt = $.extend({}, defaults, opt);
$(this).each(function () {
if (!$(this).data("video-id")) {
$(this).data("video-id", opt.videoId);
}
});
$(this).click(function () {
var $me = $(this);
var videoId = $me.data("video-id");
var html = getHtml(opt, videoId);
var $modal = $(html);
var $btn = $modal.find(".js-modal-video-dismiss-btn");
var speed = opt.animationSpeed;
$("body").append($modal);
$modal.focus();
$modal.on("click", function () {
var $self = $(this);
$self.addClass(opt.classNames.modalVideoClose);
$self.off("click");
$self.off("keydown");
$btn.off("click");
setTimeout(function () {
$self.remove();
$me.focus();
}, speed);
});
$btn.on("click", function () {
$modal.trigger("click");
});
$modal.on("keydown", function (e) {
if (e.which === 9) {
e.preventDefault();
if ($modal.is(":focus")) {
$btn.focus();
} else {
$modal.attr("aria-label", "");
$modal.focus();
}
}
});
});
};
});