-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js-v=1423494334
More file actions
453 lines (344 loc) · 11.6 KB
/
app.js-v=1423494334
File metadata and controls
453 lines (344 loc) · 11.6 KB
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
function init() {
FastClick.attach(document.body);
var allSections = [],
sections = [],
navElement = document.querySelector('main > nav'),
mainElement = document.querySelector('main'),
headerElement = document.querySelector('body > header'),
windowHeight = getWindowHeight();
// Makes sure a function isn't called too often.
function buffer(callBack) {
var timeoutId, lastCall = 0, bufferSpan = 200;
var bufferedFunction = function() {
// Already buffered
if (timeoutId) return;
// Last call has been long ago enough
if (Date.now() - lastCall > bufferSpan) {
callBack();
lastCall = Date.now();
}
else {
timeoutId = setTimeout(function() {
timeoutId = null;
callBack();
lastCall = Date.now();
}, bufferSpan);
}
}
return bufferedFunction;
}
function getWindowHeight() { return document.body.getBoundingClientRect().height; }
function Section(element) {
this.element = element;
this.isCurrent = false;
this.navElement = null;
this.parent = null;
this.subSections = [];
this.name = element.innerHTML;
this.id = element.id;
this.level = parseInt(element.tagName.substr(1)) - 1;
this.updatePosition();
}
Section.prototype.getSubSectionsHeight = function() {
var height = 0;
for (var i = 0; i < this.subSections.length; i++) {
height += this.subSections[i].navElement.getBoundingClientRect().height;
}
return height;
}
Section.prototype.addSubSection = function(subSection) {
this.subSections.push(subSection);
subSection.parent = this;
}
Section.prototype.updatePosition = function() {
var top = 0,
obj = this.element;
do {
top += obj.offsetTop;
} while (obj = obj.offsetParent);
this.top = top;
}
Section.prototype.getHtml = function() {
var element = document.createElement('div');
element.classList.add('level-' + this.level);
var link = document.createElement('a');
link.href = '#' + this.id;
link.innerHTML = this.name;
element.appendChild(link);
if (this.subSections.length > 0) {
var subSectionsElement = document.createElement('div');
subSectionsElement.classList.add('sub-sections');
for (var i = 0; i < this.subSections.length; i++) {
subSectionsElement.appendChild(this.subSections[i].getHtml());
}
element.appendChild(subSectionsElement);
this.subSectionsElement = subSectionsElement;
}
this.navElement = element;
this.linkElement = link;
return element;
}
Section.prototype.highlight = function(notCurrentSection) {
if (!notCurrentSection) {
if (this.isCurrent) return;
this.isCurrent = true;
for (var i = 0; i < allSections.length; i ++) {
if (allSections[i] !== this) {
allSections[i].downlight();
}
}
this.linkElement.classList.add('current');
}
this.navElement.classList.add('visible');
if (this.parent) this.parent.highlight(true);
if (this.level == 0 && this.subSectionsElement) {
var height = this.getSubSectionsHeight();
this.subSectionsElement.style.height = height + 'px';
}
}
// He he, funny name
Section.prototype.downlight = function() {
this.isCurrent = false;
this.navElement.classList.remove('visible');
this.linkElement.classList.remove('current');
if (this.level == 0 && this.subSectionsElement) {
this.subSectionsElement.style.height = '0px';
}
}
function parseSections() {
var headlines = document.querySelectorAll('main > section > h1, main > section > h2');
var lastSection;
for (var i = 0; i < headlines.length; i++) {
var headline = headlines[i];
var section = new Section(headline);
if (section.id == 'try-it-out' || section.id == 'news') continue;
if (section.level == 0) {
lastSection = section;
sections.push(section);
}
else {
lastSection.addSubSection(section);
}
allSections.push(section);
}
}
function updateSectionPositions() {
for (var i = 0; i < allSections.length; i++) {
allSections[i].updatePosition();
}
}
parseSections();
for (var i = 0; i < sections.length; i++) {
navElement.appendChild(sections[i].getHtml());
}
function setHeaderSize() {
// headerElement.style.height = mainElement.style.marginTop = windowHeight + 'px';
}
window.addEventListener('resize', buffer(handleResize));
function handleResize() {
windowHeight = getWindowHeight();
handleScroll();
updateSectionPositions();
}
var fixed = false;
window.addEventListener('scroll', buffer(handleScroll));
window.addEventListener('scroll', function() {
if (disableScrollEvents) return true;
// Parallax header... can't be buffered
var translate = 'translateY(' + Math.round(window.pageYOffset / 2) + 'px)';
headerElement.style.WebkitTransform = translate;
headerElement.style.transform = translate;
headerElement.style.opacity = Math.max(0, windowHeight - window.pageYOffset) / windowHeight;
if (window.pageYOffset >= windowHeight) {
if (!fixed) {
fixed = true;
navElement.classList.add('fixed');
}
}
else {
if (fixed) {
fixed = false;
navElement.classList.remove('fixed');
}
}
});
var disableScrollEvents = false;
function handleScroll() {
if (disableScrollEvents) return true;
updateSectionPositions();
highlightCurrentSection();
}
function highlightCurrentSection() {
var scrollTop = window.pageYOffset,
scrollBottom = scrollTop + windowHeight,
scrollMiddle = scrollTop + windowHeight / 3;
var highlightedSection = allSections[0];
if (highlightedSection.top > scrollMiddle) {
// The page is scrolled to the top, so the first section is not visible
for (var i = 0; i < allSections.length; i++) {
allSections[i].downlight();
}
}
else {
for (var i = 0; i < allSections.length; i++) {
var section = allSections[i];
if (section.top < scrollMiddle) {
highlightedSection = section;
}
else {
break;
}
}
highlightedSection.highlight();
}
}
// Smoothscroll
// based on http://en.wikipedia.org/wiki/Smoothstep
var smoothStep = function(start, end, point) {
if(point <= start) { return 0; }
if(point >= end) { return 1; }
var x = (point - start) / (end - start); // interpolation
return x*x*(3 - 2*x);
}
// Mostly taken from: https://coderwall.com/p/hujlhg/smooth-scrolling-without-jquery
var smoothScrollToStart = function() {
var target = windowHeight;
var duration = 600;
var start_time = Date.now();
var end_time = start_time + duration;
var start_top = window.pageYOffset;
var distance = target - start_top;
// This is to keep track of where the scrollTop is
// supposed to be, based on what we're doing
var previous_top = window.pageYOffset;
var done = function() {
// Setting the menu to fixed right away
fixed = true;
navElement.classList.add('fixed');
disableScrollEvents = false;
}
// This is like a think function from a game loop
var scroll_frame = function() {
// if (window.pageYOffset != previous_top) {
// disableScrollEvents = false;
// return;
// }
// set the scrollTop for this frame
var now = Date.now();
var point = smoothStep(start_time, end_time, now);
var frameTop = Math.round(start_top + (distance * point));
window.scrollTo(0, frameTop);
// check if we're done!
if(now >= end_time) {
done();
return;
}
// If we were supposed to scroll but didn't, then we
// probably hit the limit, so consider it done; not
// interrupted.
if(window.pageYOffset === previous_top
&& window.pageYOffset !== frameTop) {
done();
return;
}
previous_top = window.pageYOffset;
// schedule next frame for execution
setTimeout(scroll_frame, 0);
}
// Making sure it goes smoothly
disableScrollEvents = true;
// boostrap the animation process
setTimeout(scroll_frame, 0);
}
document.querySelector('.scroll-invitation a').addEventListener('click', function(e) {
e.preventDefault();
smoothScrollToStart();
});
// Dropzone
var dropzone = new Dropzone('#demo-upload', {
previewTemplate: document.querySelector('#preview-template').innerHTML,
parallelUploads: 2,
thumbnailHeight: 120,
thumbnailWidth: 120,
maxFilesize: 3,
filesizeBase: 1000,
thumbnail: function(file, dataUrl) {
if (file.previewElement) {
file.previewElement.classList.remove("dz-file-preview");
var images = file.previewElement.querySelectorAll("[data-dz-thumbnail]");
for (var i = 0; i < images.length; i++) {
var thumbnailElement = images[i];
thumbnailElement.alt = file.name;
thumbnailElement.src = dataUrl;
}
setTimeout(function() { file.previewElement.classList.add("dz-image-preview"); }, 1);
}
}
});
var minSteps = 6,
maxSteps = 60,
timeBetweenSteps = 100,
bytesPerStep = 100000;
dropzone.uploadFiles = function(files) {
var self = this;
for (var i = 0; i < files.length; i++) {
var file = files[i];
totalSteps = Math.round(Math.min(maxSteps, Math.max(minSteps, file.size / bytesPerStep)));
for (var step = 0; step < totalSteps; step++) {
var duration = timeBetweenSteps * (step + 1);
setTimeout(function(file, totalSteps, step) {
return function() {
file.upload = {
progress: 100 * (step + 1) / totalSteps,
total: file.size,
bytesSent: (step + 1) * file.size / totalSteps
};
self.emit('uploadprogress', file, file.upload.progress, file.upload.bytesSent);
if (file.upload.progress == 100) {
file.status = Dropzone.SUCCESS;
self.emit("success", file, 'success', null);
self.emit("complete", file);
self.processQueue();
}
};
}(file, totalSteps, step), duration);
}
}
}
Dropzone.prototype.filesize = function(size) {
var units = [ 'TB', 'GB', 'MB', 'KB', 'b' ],
selectedSize, selectedUnit;
for (var i = 0; i < units.length; i++) {
var unit = units[i],
cutoff = Math.pow(this.options.filesizeBase, 4 - i) / 10;
if (size >= cutoff) {
selectedSize = size / Math.pow(this.options.filesizeBase, 4 - i);
selectedUnit = unit;
break;
}
}
selectedSize = Math.round(10 * selectedSize) / 10;
return '<strong>' + selectedSize + '</strong> ' + selectedUnit;
}
// filesize: (size) ->
// if size >= 1024 * 1024 * 1024 * 1024 / 10
// size = size / (1024 * 1024 * 1024 * 1024 / 10)
// string = "TiB"
// else if size >= 1024 * 1024 * 1024 / 10
// size = size / (1024 * 1024 * 1024 / 10)
// string = "GiB"
// else if size >= 1024 * 1024 / 10
// size = size / (1024 * 1024 / 10)
// string = "MiB"
// else if size >= 1024 / 10
// size = size / (1024 / 10)
// string = "KiB"
// else
// size = size * 10
// string = "b"
// "<strong>#{Math.round(size)/10}</strong> #{string}"
dropzone.on('complete', function(file) {
file.previewElement.classList.add('dz-complete');
});
}
document.addEventListener("DOMContentLoaded", init);