forked from Jack000/expose-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.js
550 lines (471 loc) · 14.2 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
var current_resolution = 1920;
var disqus_shortname = 'jackventures';
var disqus_loaded = false;
var current_slide = $('.slide').get(0);
$(document).ready(function(){
// pre-set aspect ratio
$('.slide').each(function(){
var width = $(this).data('width');
var height = $(this).data('height');
if(width && height){
//if(!$(this).find('.image').hasClass('active')){
// still loading, set height
var ratio = height/width;
$(this).css('height',Math.ceil($(this).width()*ratio)+'px');
//console.log('set'+Math.ceil($(this).width()*ratio)+'px', $(this).find('.content').width(),$(this).find('.content').height());
//}
}
var polygon = $(this).find('.post').data('polygon');
if(polygon && polygon.length >= 3){
//polygon = JSON.parse(polygon);
//console.log(polygon);
var now = _now();
//console.log($(img).parent().find('.content').get(0));
//setTimeout(function(){
fillpolygon($(this).find('.content').eq(0),polygon);
//},10);
//console.log(_now() - now);
}
// set colors
var bodycolor = $(this).data('color7');
if(bodycolor){
$(this).find('.post').css('color',bodycolor);
}
var bodyhighlight = $(this).data('color6');
if(bodyhighlight){
$(this).find('h2, h3, h4').not('.manual').css('color',bodyhighlight);
}
});
// resolution select
$('#resolution').click(function(){
if($(this).hasClass('active')){
$(this).removeClass('active');
}
else{
$(this).addClass('active');
}
return false;
});
$('#resolution 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('maxresolution')) < current_resolution){
set_res = parseInt($(this).data('maxresolution'));
}
$(this).find('img.image').not('.blank').prop('src',pad(index+1,2)+'/'+set_res+'.jpg');
$(this).find('video.image').not('.blank').prop('src',pad(index+1,2)+'/'+set_res+'.mp4');
});
// set ui
$('#resolution li.active').removeClass('active');
$(this).addClass('active');
var index = $('#resolution li').index($(this));
$('#resolution').css('top','-'+(28*index + 5)+'px');
$.cookie('resolution', current_resolution, { expires: 7, path: '/' });
}
});
// click away from dialog
$('body').click(function(e) {
if (!$(e.target).is('#resolution')) {
$('#resolution').removeClass('active');
}
if (!$(e.target).is('#share')) {
$('#share').removeClass('active');
}
});
// detect resolution
var saved_width = $.cookie('resolution');
var screen_width = $(window).width();
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');
return false;
}
});
}
else{
$('#resolution li').each(function(i){
var val = parseInt($(this).data('res'));
if(screen_width >= val-50){
$(this).trigger('click');
return false;
}
});
if(screen_width < 1024){
$('#resolution li').last().trigger('click');
}
}
$('#resolution').removeClass('active');
scrollcheck();
// 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');
// add back hover behavior erased by color changes
$('#sidebar a').not('.active a').mouseenter(function(){
var color = $(this).css('color');
$(this).removeAttr('style');
$(this).data('prevcolor',color);
//console.log(color)
}).mouseleave(function(){
var color = $(this).data('prevcolor');
if(color){
$(this).css('color',color);
}
});
// browser detect
if($.browser.webkit){
$('.icon').addClass('webkit');
}
// toggle comments
$('#commentbutton').click(function(){
if($('#comments').hasClass('active')){
$('#comments').removeClass('active');
}
else{
$('#comments').addClass('active');
$('#share').removeClass('active');
if(!disqus_loaded){
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
disqus_loaded = true;
}
}
return false;
});
$('#commentclose').click(function(){
$('#comments').removeClass('active');
return false;
});
$('#sharebutton').click(function(){
if($('#share').hasClass('active')){
$('#share').removeClass('active');
}
else{
$('#share').addClass('active');
$('#comments').removeClass('active');
}
return false;
});
// download current
$('#download').click(function(){
var index = $('.slide').index(current_slide);
num = pad(index+1,2);
window.open('/'+dirname+'/'+num+'/'+dirname+'-'+num+'.zip');
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;
});
});
function scrollcheck(){
$('.slide').each(function(){
var currentoverlap = findoverlap(this);
if((currentoverlap > 0.7) && current_slide != this){
current_slide = this;
var index = $('.slide').index(current_slide);
$('.slide').not(this).find('.image').removeClass('active');
// load previous slide, current slide, and the next slide
$('.slide .image').each(function(i){
var set_res = current_resolution;
if(parseInt($(this).parent().data('maxresolution')) < current_resolution){
set_res = parseInt($(this).parent().data('maxresolution'));
}
if(i-index >= -1 && i-index<=1){
num = pad(i+1,2);
if($(this).is('video')){ // video eats up memory, do not save previous one
if(i-index > -1){
$(this).prop('src','/'+dirname+'/'+num+'/'+set_res+'.mp4');
}
else{
$(this).prop('src','//:0');
}
}
else if($(this).is('img')){
$(this).prop('src','/'+dirname+'/'+num+'/'+set_res+'.jpg');
}
else if($(this).is('span')){
// flash fallback
var param = $(this).find('param').last();
var obj = $(this).find('object');
var configjson = param.prop('value').substring(7);
var config = $.parseJSON(configjson);
var furl = config.playlist[0].url;
if(furl.length <= 11 || obj.prop('data').length <= 11){
config.playlist[0].url = '/'+dirname+'/'+num+'/'+set_res+'.mp4';
param.prop('value','config='+JSON.stringify(config));
obj.prop('data','/player/flowplayer.swf?cachebreaker='+(new Date().getTime()));
}
}
$(this).removeClass('blank');
}
else{
// keeping all images takes too much memory, reset as we go along
if($(this).is('video') || $(this).is('img')){
$(this).prop('src','//:0');
}
else if($(this).is('span')){
$(this).find('object').prop('data','//:0');
}
$(this).addClass('blank');
}
});
$(current_slide).find('.image').addClass('active');
// set custom colors
var sidebackground = $(this).data('color2');
if(sidebackground){
$('#sidebar .background').css('background-color',sidebackground);
}
var highcolor = $(this).data('color6');
var sidecolor = $(this).data('color7');
if(sidecolor){
$('#sidebar, #sidebar a').css('color',sidecolor);
$('#sidebar .active a').css('color',highcolor);
}
$('#sidebar .icon.webkit').css('background-color',sidecolor);
$('#sidebar .icon.webkit.monitor').css('background-color',highcolor);
// 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);
function findoverlap(elem)
{
var winHeight = $(window).height();
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + winHeight;
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
var overlap = (Math.min(elemBottom, docViewBottom) - Math.max(elemTop, docViewTop));
if(overlap > 0){
return overlap/(winHeight);
}
return 0;
}
// hide content and put polygon content on top
function fillpolygon(content, polygon){
//content.hide();
if(!polygon || polygon.length < 3){
return false;
}
// loop back
polygon.push(polygon[0]);
//console.log(polygon);
var fill = $('<div class="polygon" />');
content.after(fill);
var cwidth = content.width();
var cheight = content.height();
//console.log('cstuff',cwidth,cheight,content.get(0));
content.contents().each(function(){
//console.log(this, this.nodeName);
var n = this.nodeName.toLowerCase();
// stuff that goes on a new line, we only care about the first intercept. Won't deal with gaps
if(n == 'h1' || n == 'h2' || n == 'h3' || n == 'h4' || n == 'h5' || n == 'h6' || n == 'div' || n== 'img' || n == 'hr' || n == 'ul' || n == 'blockquote'){
// html, no end clip
var clone = $(this).clone();
var coords = intersect(100*($(this).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>');
}
fill.append(clone);
}
else if(n == 'span' || n == 'a' || n == 'strong' || n =='i' || n =='b' || n=='em'){ // stuff that goes inline with text nodes
}
else if(this.nodeType == 3){ // text node
var text = this.nodeValue.trim();
if(!text){
return true;
}
var words = text.match(/\S+/g);
while(words.length > 0){
// wrap in span so we can get dimensions
var span = $('<span class="line"> </span>');
fill.append(span);
var left = 100*(span.position().left/cwidth);
var top = 100*(span.position().top/cheight);
//console.log('wat',left, top,span.position());
if(top > 100){
span.remove();
return false;
break;
}
var min = left;
var max = 100;
//console.log(span.position());
var 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 />');
}
//console.log(max, span.get(0));
words = words.slice(i);
}
}
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;
}