-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.js
622 lines (509 loc) · 15.9 KB
/
global.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
var loadnext = 6 // preload next 6 slides
var loadprev = 3; // keep previous 3 slides in case user scrolls up
var videoloadnext=8; // preload videos
var resourcepath;
var current_resolution = 1920;
var resolution = [];
var disqus_loaded = false;
var current_slide = $('.slide').get(0);
var video_formats={
h265: { extension: "mp4", type: "video/mp4; codecs=hev1.1.2.L93.B0"},
h264: { extension: "mp4", type: "video/mp4"},
vp9: { extension: "webm", type: "video/webm; codecs=vp9"},
vp8: { extension: "webm", type: "video/webm; codecs=vp8"},
ogv: { extension: "ogv", type: "video/ogg"}
};
function drawtext(){
var screen_width = $(window).width();
// set font size based on actual resolution, normalized at 14px/22px for 720
var fontsize = Math.floor(14*(screen_width/1280));
var lineheight = Math.floor(22*(screen_width/1280));
if(fontsize < 11){
fontsize = 11;
}
if(lineheight < 14){
lineheight = 14;
}
$('body').css('font-size',fontsize+'px');
$('body').css('line-height',lineheight+'px');
// polygon boundary feature
$('.slide').each(function(){
var polygon = $(this).data('polygon');
if(polygon && $.isArray(polygon) && polygon.length >= 3){
fillpolygon($(this).find('.content').eq(0),polygon);
}
});
}
function redrawtext(){
$('.slide .polygon').remove();
$('.slide .content').show();
drawtext();
}
$(document).ready(function(){
// set slide heights to prevent reflow
$('.slide').each(function(){
$(this).css('padding-top', (100*$(this).data('imageheight')/$(this).data('imagewidth')) + '%');
});
resourcepath = $('body').data('respath');
// detect resolution
var saved_width = $.cookie('resolution');
var screen_width = $(window).width();
drawtext();
// build resolution selector
$.each(String($('body').data('resolution')).split(" "),function(i, v){
if(v){
resolution.push(parseInt(v));
}
});
resolution.sort(function(a, b){return b-a;});
var selector = '';
$.each(resolution, function(i, v){
// use 16:9 XXXp conventions for labels. eg. 1080p, 1440p etc
var label;
if(i == resolution.length-1 && v <= 1024){
label = 'mobile';
}
else if(v == 3840){
label = '4K';
}
else{
var h = parseInt((9/16)*v);
label = h+'p';
}
selector += '<li data-res="'+v+'"><a href="#">'+label+'</a></li>';
});
selector = '<ul>'+selector+'</ul>';
$('#resolution').append(selector);
// resolution select
$('#resolution ul li').click(function(){
var new_resolution = $(this).data('res');
if(current_resolution != new_resolution){
current_resolution = new_resolution;
// update all urls
$('.slide').each(function(index){
var set_res = current_resolution;
if(parseInt($(this).data('imagewidth')) < current_resolution){
set_res = parseInt($(this).data('imagewidth'));
}
var url = resourcepath + $(this).find('img.image').data('url');
$(this).find('img.image').not('.blank').prop('src',url+'/'+set_res+'.jpg');
});
// set ui
$('#resolution li.active').removeClass('active');
$(this).addClass('active');
$.cookie('resolution', current_resolution, { expires: 7, path: '/' });
}
$('#resbutton .restext').text($(this).find('a').text());
$('#resolution').removeClass('active');
return false;
});
// click away from dialog
$('body').click(function(e) {
if (!$(e.target).is('#resolution')) {
$('#resolution').removeClass('active');
}
});
if(saved_width){
// used cookie value if set
$('#resolution li').each(function(i){
var val = parseInt($(this).data('res'));
if(saved_width == val){
$(this).trigger('click');
$(this).addClass('active');
return false;
}
});
}
else{
// assume large->small order
var found = false;
var sidebar_width = $('#marker').width() + $('#sidebar').width();
// account for pixel density and sidebar width
var device_ratio = window.devicePixelRatio ? window.devicePixelRatio : 1;
var adjusted_screen_width = ($(window).width() - sidebar_width) * device_ratio;
function setResolution(res){
$(res).trigger('click');
$(res).addClass('active');
found=true;
}
$('#resolution li').each(function(i){
var val = parseInt($(this).data('res'));
var image_density = val / adjusted_screen_width;
if(image_density >= 0.9) {
setResolution(this);
}
});
// when largest image has density lower than threashold (large screen in use)
if(!found){
$('#resolution li').first().trigger('click');
}
}
scrollcheck();
// add back hover behavior erased by color changes
$('#sidebar a, #resolution a').not('#nav .active a').mouseenter(function(){
var color = $(this).css('color');
$(this).css('color','#ffffff');
$(this).data('prevcolor',color);
}).mouseleave(function(){
var color = $(this).data('prevcolor');
if(color){
$(this).css('color',color);
}
});
// browser detect
if($.browser.webkit){
$('.icon').addClass('webkit');
}
$('#resbutton').click(function(){
if($('#resolution').hasClass('active')){
$('#resolution').removeClass('active');
}
else{
$('#resolution').addClass('active');
}
return false;
});
// text toggle
$('#textbutton').click(function(){
if($(this).hasClass('active')){
$('.post').addClass('hidden');
$(this).removeClass('active');
$(this).find('.text').text('show text');
}
else{
$('.post').removeClass('hidden');
$(this).addClass('active');
$(this).find('.text').text('hide text');
}
return false;
});
// add marker
var mheight = 100/$('.slide').length;
$('.slide').each(function(i, v){
var color1 = $(this).data('color1');
var color7 = $(this).data('color7');
if(!color1){
color1 = '#000';
}
if(!color7){
color7 = '#fff';
}
$('#marker').append('<li style="background-color: '+color1+'; height: '+mheight+'%"><a href="#'+(i+1)+'" style="background-color: '+color7+'"></a></li>');
});
});
function scrollcheck(){
// current slide is the one with the largest overlap with screen
var new_slide = false;
var maxoverlap = 0;
$('.slide').each(function(){
var overlap = findoverlap(this);
if(overlap > maxoverlap){
maxoverlap = overlap;
new_slide = this;
}
});
if(new_slide && new_slide != current_slide){
current_slide = new_slide;
var index = $('.slide').index(current_slide);
$('.slide .image').removeClass('active');
$('.slide video').removeClass('active');
// load next N slides as per config
// remove videos from dom as we scroll past
$('.slide').each(function(i){
var img = $(this).find('img.image');
var set_res = current_resolution;
if(parseInt($(this).data('imagewidth')) < current_resolution){
set_res = parseInt($(this).data('imagewidth'));
}
if(i-index >= -loadprev && i-index<=loadnext){
var url = resourcepath + img.data('url');
if($(this).data('type') == 'video'){
if(i-index >= 0 && i-index <= videoloadnext){ // preload next N videos
if($(this).find('video').length === 0){
var formats = String($(this).data("videoformats")).split(" ");
if(formats.length > 0){
var vidstring = '<div class="progress active"><div class="bar" style="background-color: '+$(this).parent().data('textcolor')+'"></div></div>';
vidstring += '<video class="image" poster="' + url+'/'+set_res+'.jpg" alt="" autoplay="autoplay" loop="loop" preload="auto" width="'+$(this).width()+'" height="'+$(this).outerHeight()+'">';
$.each(formats, function(i, v){
if(v){
vformat = video_formats[v];
var sourceurl = url+'/'+set_res+'-'+v+'.'+vformat.extension;
vidstring += '<source src="'+sourceurl+'" type="'+vformat.type+'" data-extension="'+(v+'.'+vformat.extension)+'"></source>';
}
});
vidstring += '</video>';
$(this).append(vidstring);
$(this).find('video').get(0).addEventListener('progress', function() {
try{
var percent = 100 * this.buffered.end(0) / this.duration;
$(this).parent().find('.progress .bar').css('width',percent+'%');
if(percent > 90){
$(this).parent().find('.progress').removeClass('active');
}
}
catch (e) {}
});
}
}
}
}
img.prop('src',url+'/'+set_res+'.jpg').removeClass('blank');
}
else{
// keeping all videos takes too much memory, reset as we go along
$(this).find('img.image').addClass('blank');
$(this).find('video, .progress').remove();
}
});
$(current_slide).find('.image').addClass('active');
$(current_slide).nextAll().filter('.slide').slice(0,5).find('img.image').addClass('active');
$(current_slide).prevAll().filter('.slide').slice(0,2).find('img.image').addClass('active');
$(current_slide).prevAll().filter('.slide').slice(0,2).find('video.image').addClass('active');
$(current_slide).nextAll().filter('.slide').slice(0,2).find('video.image').addClass('active');
// set custom colors
var sidebackground = $(current_slide).data('color2');
if(sidebackground){
$('#sidebar .background').css('background-color',sidebackground);
}
var resbackground = $(current_slide).data('color3');
if(resbackground){
$('#resolution').css('background-color',resbackground);
}
var highcolor = $(current_slide).data('textcolor');
var sidecolor = $(current_slide).data('color6');
if(sidecolor){
$('#sidebar, #sidebar a, #resolution a').css('color',sidecolor);
$('#sidebar .active a, #resolution .active a').css('color',highcolor).css('border-color', highcolor);
}
$('#sidebar .icon.webkit').css('background-color',sidecolor);
// highlight nav
if(index >= 0){
$('#marker li.active').removeClass('active');
$('#marker li').eq(index).addClass('active');
}
return false;
}
}
_now = Date.now || function() { return new Date().getTime(); };
throttle = function(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
options || (options = {});
var later = function() {
previous = options.leading === false ? 0 : _now();
timeout = null;
result = func.apply(context, args);
context = args = null;
};
return function() {
var now = _now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
var throttled = throttle(scrollcheck, 700);
$(window).scroll(throttled);
var redrawtext_throttled = throttle(redrawtext, 1000);
$(window).resize(redrawtext_throttled);
function findoverlap(elem)
{
var winHeight = $(window).height();
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + winHeight;
var elemTop = $(elem).offset().top;
var elemHeight = $(elem).outerHeight();
var elemBottom = elemTop + elemHeight;
var overlap = (Math.min(elemBottom, docViewBottom) - Math.max(elemTop, docViewTop));
if(overlap > 0){
return overlap/(winHeight);
}
return 0;
}
// given content element and a polygon definition, fill the polygon with content text and hide original content
function fillpolygon(content, polygon){
if(!polygon || polygon.length < 3){
return false;
}
// loop back to first vertex
polygon.push(polygon[0]);
var fill = $('<div class="polygon" />');
content.after(fill);
var cwidth = content.width();
var cheight = content.height();
content.contents().each(function(){
var isblock = false;
var samefont = true;
if(this.nodeType == 1){
isblock = $(this).css("display") == "block";
samefont = $(content).css("font-size") == $(this).css("font-size");
}
// stuff like h1 h2 etc will be difficult to wrap, put them on the first intercept and don't attempt to wrap
var coords;
if(this.nodeType == 1 && isblock && !samefont){
// html, no end clip
var clone = $(this).clone();
fill.append(clone);
coords = intersect(100*(clone.position().top/content.height()), polygon);
if(coords.length === 0){
coords = 0;
}
else{
coords = coords.shift();
}
if(coords > 0){
clone.prepend('<span class="filler" style="width: '+coords+'%"></span>');
}
}
else if(this.nodeType == 3 || this.nodeType == 1){ // text node
var text;
if(this.nodeType == 3){
text = this.nodeValue.trim();
}
else{
text = $(this).text();
}
if(!text){
return true;
}
var words = text.match(/\S+/g);
while(words.length > 0){
// wrap in span so we can get dimensions
var span;
if(this.nodeType == 3 || isblock){
span = $('<span class="line"> </span>');
}
else{
span = $(this).clone().text('');
}
fill.append(span);
var left = 100*(span.position().left/cwidth);
var top = 100*(span.position().top/cheight);
if(top > 100){
span.remove();
return false;
}
var min = left;
var max = 100;
coords = intersect(top, polygon);
if(!coords || coords.length < 2){
min = 0;
max = 100;
}
// depending on the x position of the span, we may not care about certain intercepts
for(var i=0; i<coords.length; i += 2){
if(coords[i] >= left){
min = coords[i];
max = coords[i+1];
break;
}
}
// shift to min
span.before('<span class="filler" style="width: '+(min-left)+'%" />');
// type out text until wraps
for(i=1; i<=words.length; i++){
var height = span.height();
span.text(words.slice(0, i).join(' '));
var width = 100*(span.width()/cwidth);
if(min+width > max){
break;
}
}
if(coords.length < 3 || min+max > 100){
fill.append('<br />');
}
words = words.slice(i);
}
if(this.nodeType != 3 && isblock){
fill.append('<br />');
}
}
else if(this.nodeType == 1){
fill.append($(this).clone());
}
});
content.hide();
}
// find all intersections between a horizontal line at height, and the given polygon
function intersect(height, polygon){
if(polygon.length < 3){
return false;
}
var points = [];
for(var i=0; i<polygon.length-1; i++){
var p1 = polygon[i];
var p2 = polygon[i+1];
// horizontal line
if(p1.y == p2.y && height == p1.y){
if(p1.x < p2.x){
points.push(p1.x);
}
else{
points.push(p2.x);
}
}
if(p1.y == height && $.inArray(p1.x, points) < 0){
points.push(p1.x);
}
if(p2.y == height && $.inArray(p2.x, points) < 0){
points.push(p2.x);
}
if(p1.y < height && p2.y > height){
points.push(p1.x + ((height-p1.y)/(p2.y-p1.y))*(p2.x-p1.x));
}
else if(p1.y > height && p2.y < height){
points.push(p2.x + ((height-p2.y)/(p1.y-p2.y))*(p1.x-p2.x));
}
}
// sort intercepts left to right
points.sort(function(a,b){
return a-b;
});
return points;
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
// add back browser detect
jQuery.uaMatch = function( ua ) {
ua = ua.toLowerCase();
var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
/(webkit)[ \/]([\w.]+)/.exec( ua ) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
/(msie) ([\w.]+)/.exec( ua ) ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
[];
return {
browser: match[ 1 ] || "",
version: match[ 2 ] || "0"
};
};
if ( !jQuery.browser ) {
matched = jQuery.uaMatch( navigator.userAgent );
browser = {};
if ( matched.browser ) {
browser[ matched.browser ] = true;
browser.version = matched.version;
}
// Chrome is Webkit, but Webkit is also Safari.
if ( browser.chrome ) {
browser.webkit = true;
} else if ( browser.webkit ) {
browser.safari = true;
}
jQuery.browser = browser;
}