-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.js
311 lines (256 loc) · 8.53 KB
/
plugin.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* @license MIT
* Copyright (c) 2018-present, Silamedia
*/
(function (w, d) {
const version = '1.2',
flag = 'SilaInteractiviewLoaded';
if (typeof w[flag] !== 'undefined') {
return;
}
console.log('SilaInteractiview ' + version);
w[flag] = true;
const styles = `
.sila-interactiview {
position: relative;
margin-bottom: 15px;
}
.sila-interactiview ul {
list-style: none !important;
margin: 0;
padding: 0;
}
.sila-interactiview .sila-description {
float: left;
width: 300px;
margin: 0 15px 15px 0;
}
.sila-interactiview .sila-questions {
padding-left: 310px;
}
.sila-interactiview .sila-questions li{
padding-bottom: 5px;
}
.sila-interactiview .sila-questions h3{
padding: 0;
margin: 10px 0;
}
.sila-interactiview .sila-questions li:last-child{
padding-bottom: 0;
}
.sila-interactiview .sila-video-player {
padding-left: 310px;
}
.sila-interactiview .sila-branding {
opacity: 0.5;
color: #666;
font-size: 11px;
font-family: Arial;
position: absolute;
left:0;
bottom: 3px;
}
.sila-interactiview .sila-branding:hover {
opacity: 1;
}
.sila-interactiview .sila-branding span {
color: #e67417;
}
.sila-interactiview .sila-branding a {
color: #666;
text-decoration: none;
}
.sila-interactiview .sila-poster {
}
@media screen and (max-width: 761px) {
.sila-interactiview .sila-description {
float: none;
width: 100%;
margin: 0 0 15px 0;
}
.sila-interactiview .sila-questions {
padding-left: 0;
}
.sila-interactiview .sila-questions li{
padding-bottom: 10px;
}
.sila-interactiview .sila-video-player {
padding-left: 0;
}
.sila-interactiview .sila-video-player img {
width: 100%;
}
.sila-interactiview .sila-video-player iframe {
width: 100% !important;
}
.sila-interactiview .sila-branding {
margin: 10px auto;
text-align: center;
position: relative;
}
}`;
var defaultSettings = {
css: false,
autoScroll: true,
video: {
width: "640px",
height: "360px"
}
};
function render(data) {
var cnt = d.getElementById(data.id);
if (!cnt) {
return;
}
var videoSettings = data.video || defaultSettings.video;
var title = d.createElement('h2');
title.innerHTML = data.title;
cnt.appendChild(title);
var descr = d.createElement('div');
descr.className = 'sila-description';
descr.innerHTML = data.description;
cnt.appendChild(descr);
var video = d.createElement('div');
video.className = 'sila-video-player';
video.innerHTML = '<img class="sila-poster" src="' + data.poster + '" style="max-width:'+videoSettings.width+'">';
cnt.appendChild(video);
var qcnt = d.createElement('div');
qcnt.className = 'sila-questions';
var qlabel = d.createElement('h3');
qlabel.innerHTML = 'Выберите вопрос';
qcnt.appendChild(qlabel);
var ul = d.createElement('ul');
qcnt.appendChild(ul);
for (var qi in data.questions) {
if (data.questions.hasOwnProperty(qi)) {
var li = d.createElement('li');
var id = data.id + '_radio_' + qi;
li.innerHTML = '<label for="' + id + '">' +
'<input type="radio" name="' + data.id + '_radio" id="' + id + '" value="' + qi + '" class="sila-question"> ' + data.questions[qi].text +
'</label>';
(function (url) {
li.addEventListener('click', function (e) {
console.log(url);
video.innerHTML = '<iframe style="width:' + defaultSettings.video.width + '; height:' + defaultSettings.video.height + ';" src="' + url + '" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
if (defaultSettings.autoScroll) {
scrollTo(video, 200);
}
});
})(prepareUrl(data.questions[qi].url));
ul.appendChild(li);
}
}
cnt.appendChild(qcnt);
const branding = d.createElement('div');
branding.className = 'sila-branding';
branding.innerHTML = '<a href="https://sila.media/interactiview">Interactiview by <span>Silamedia</span></a>';
cnt.appendChild(branding);
}
function prepareUrl(url) {
let id = youtubeUrlParser(url);
let query = id + '?autoplay=1';
let t = getParameterByName('t', url);
if (t) {
query += '&start=' + convertTime(t);
}
return 'https://www.youtube-nocookie.com/embed/' + query;
}
function convertTime(duration) {
var total = 0;
var hours = duration.match(/(\d+)h/);
var minutes = duration.match(/(\d+)m/);
var seconds = duration.match(/(\d+)s$/);
var seconds2 = duration.match(/(\d+)$/);
if (hours) total += parseInt(hours[1]) * 3600;
if (minutes) total += parseInt(minutes[1]) * 60;
if (seconds) total += parseInt(seconds[1]);
if (seconds2) total += parseInt(seconds2[1]);
return total;
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function youtubeUrlParser(url) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[7].length === 11) {
return match[7];
} else {
return null
}
}
function appendToHead(el) {
(d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(el);
}
function injectStyles(css) {
var c = d.createElement('style');
appendToHead(c);
c.type = 'text/css';
c.innerHTML = css;
}
function scrollTo(element, duration) {
var start = window.pageYOffset || document.documentElement.scrollTop,
change = calculateScrollOffset(element, -20) - start,
currentTime = 0,
increment = 20;
if(change >= 0) {
return;
}
var animateScroll = function () {
currentTime += increment;
window.scrollTo(0, easeInOutQuad(currentTime, start, change, duration));
if (currentTime < duration) {
setTimeout(animateScroll, increment);
}
};
animateScroll();
}
function calculateScrollOffset(elem, additionalOffset, alignment) {
var body = document.body,
html = document.documentElement;
var elemRect = elem.getBoundingClientRect();
var clientHeight = html.clientHeight;
var documentHeight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
additionalOffset = additionalOffset || 0;
var scrollPosition;
if (alignment === 'bottom') {
scrollPosition = elemRect.bottom - clientHeight;
} else if (alignment === 'middle') {
scrollPosition = elemRect.bottom - clientHeight / 2 - elemRect.height / 2;
} else { // top and default
scrollPosition = elemRect.top;
}
var maxScrollPosition = documentHeight - clientHeight;
return Math.min(scrollPosition + additionalOffset + window.pageYOffset,
maxScrollPosition);
}
//t = current time
//b = start value
//c = change in value
//d = duration
function easeInOutQuad(t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t + b;
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
}
d.addEventListener("DOMContentLoaded", function (event) {
var SilaInteractiview = w['SilaInteractiview'] || [];
if (!SilaInteractiview.length) {
return;
}
injectStyles(styles);
for (var i in SilaInteractiview) {
if (SilaInteractiview.hasOwnProperty(i)) {
render(SilaInteractiview[i]);
}
}
});
})(window, document);