forked from Jack000/Expose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.js
246 lines (207 loc) · 7.57 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
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"}
};
var resolution = [];
var resourcepath;
$(document).ready(function(){
resourcepath = $('body').data('respath');
// set slide heights to prevent reflow
var mainwidth = $('#main').width();
$('.image.index1').css('width','125%'); // first image is masthead, do not allow content override
$('.image').each(function(){
$(this).css('padding-top', (100*$(this).data('maxheight')/$(this).data('maxwidth'))*($(this).width()/mainwidth) + '%');
});
$.each(String($('body').data('resolution')).split(" "),function(i, v){
if(v){
resolution.push(parseInt(v));
}
});
resolution.sort(function(a, b){return a-b});
// remove empty posts
$('.post').filter(function() {
return $.trim($(this).text()) === '' && $(this).children().length == 0
}).remove();
// set ui colors to first image
var color = $('.image.index1').data('color6');
$('#top .author img, .arrow_circle').css('border-color',color);
$('#top .title .subscript, #nav_toggle').css('color',color);
$('.image.index1').append('<div class="overlay" style="background-color: '+$('.image.index1').data('color2')+'"></div>');
$('.image').not('.index1, .fullwidth').click(function(){
// full screen mode
$('#fullscreen').addClass('active').append($(this).find('img').clone());
var img = $('#fullscreen img');
var imgwidth = img.prop('width');
var imgheight = img.prop('height');
var screenwidth = $(window).width();
var screenheight = $(window).height();
// check aspect ratio
if(imgwidth/imgheight > screenwidth/screenheight){
img.css('width','100%').css('height','auto').css('margin-top',(0.5*(screenheight-img.height()))+'px');
}
else{
img.css('height','100%').css('width','auto');
}
width = img.width();
var url = resourcepath + img.data('url');
var maxwidth = $(this).data('maxwidth');
var displaywidth = maxwidth;
$.each(resolution, function(i, v){
if(v >= width && v <= maxwidth && v < displaywidth){
displaywidth = v;
}
});
img.prop('src',url+'/'+displaywidth+'.jpg');
// set video
$('#fullscreenvideo').addClass('active').append($(this).find('video, .progress').clone());
var video = $('#fullscreenvideo video');
if(imgwidth/imgheight > screenwidth/screenheight){
video.css('width','100%').css('height','auto').css('margin-top',(0.5*(screenheight-video.height()))+'px');
}
else{
video.css('height','100%').css('width','auto');
}
$('#fullscreenvideo video source').each(function(){
$(this).prop('src', url+'/'+displaywidth+'-'+$(this).data('format')+'.'+$(this).data('extension'));
});
$('#fullscreenvideo .progress').addClass('active');
if(video.length > 0){
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) {}
});
}
$('#fullscreen').css('background-color',$(this).data('color1'));
return false;
});
$('#fullscreen, #fullscreenvideo').click(function(){
$('#fullscreen, #fullscreenvideo').empty();
$('#fullscreen, #fullscreenvideo').removeClass('active');
});
if($('#nav li').length <= 1){
$('#nav_toggle').remove();
$('#top .author').addClass('center').css('margin-left','-'+(0.5*$('.author').width())+'px');
}
else{
$('#nav_toggle').click(function(){
if($('#top').hasClass('active')){
$('#top').removeClass('active');
$('#top').css('background','transparent');
$('#nav_toggle .moretext').text($('#nav_toggle .moretext').data('text'));
}
else{
$('#top').css('background',$('.image.index1').data('color1'));
$('#top').addClass('active');
$('#nav_toggle .moretext').data('text', $('#nav_toggle .moretext').text()).text('Hide');
}
});
$('#nav li.gallery').not('.active').each(function(){
$(this).append('<img src="'+$(this).find('a').prop('href')+'/'+$(this).data('image')+'/'+resolution[0]+'.jpg" />');
});
}
scrollcheck();
});
function scrollcheck(){
$('.image').each(function(){
var overlap = findoverlap(this);
if( overlap > -1){
var img = $(this).find('img');
var url = resourcepath + img.data('url');
var width = img.width();
var maxwidth = $(this).data('maxwidth');
var displaywidth = maxwidth;
$.each(resolution, function(i, v){
if(v >= width && v <= maxwidth && v < displaywidth){
displaywidth = v;
}
});
img.prop('src',url+'/'+displaywidth+'.jpg');
$(this).removeClass('blank');
// videos
if($(this).data('type') == 'video'){
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).data('textcolor')+'"></div></div>';
vidstring += '<video poster="'+url+'/'+displaywidth+'.jpg" alt="" autoplay="autoplay" loop="loop" preload="auto">'
$.each(formats, function(i, v){
if(v){
vformat = video_formats[v];
var sourceurl = url+'/'+displaywidth+'-'+v+'.'+vformat.extension
vidstring += '<source src="'+sourceurl+'" type="'+vformat.type+'" data-source="'+sourceurl+'" data-format="'+v+'" data-extension="'+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) {}
});
}
}
}
}
else{
// keeping all videos takes too much memory, reset as we go along
$(this).find('video, .progress').remove();
}
});
}
_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 elemHeight = $(elem).outerHeight();
var elemBottom = elemTop + elemHeight;
var overlap = (Math.min(elemBottom, docViewBottom) - Math.max(elemTop, docViewTop));
return overlap/(winHeight);
}