-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
433 lines (368 loc) · 13.8 KB
/
utils.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
var sectionLoaded = [];
var slideLoaded = [];
// keeping central set of classnames and selectors
var WRAPPER = 'fullpage-wrapper';
var WRAPPER_SEL = '.' + WRAPPER;
// slimscroll
var SCROLLABLE = 'fp-scrollable';
var SCROLLABLE_SEL = '.' + SCROLLABLE;
// util
var RESPONSIVE = 'fp-responsive';
var NO_TRANSITION = 'fp-notransition';
var DESTROYED = 'fp-destroyed';
var ENABLED = 'fp-enabled';
var VIEWING_PREFIX = 'fp-viewing';
var ACTIVE = 'active';
var ACTIVE_SEL = '.' + ACTIVE;
var COMPLETELY = 'fp-completely';
var COMPLETELY_SEL = '.' + COMPLETELY;
// section
var SECTION_DEFAULT_SEL = '.section';
var SECTION = 'fp-section';
var SECTION_SEL = '.' + SECTION;
var SECTION_ACTIVE_SEL = SECTION_SEL + ACTIVE_SEL;
var SECTION_FIRST_SEL = SECTION_SEL + ':first';
var SECTION_LAST_SEL = SECTION_SEL + ':last';
var TABLE_CELL = 'fp-tableCell';
var TABLE_CELL_SEL = '.' + TABLE_CELL;
var AUTO_HEIGHT = 'fp-auto-height';
var AUTO_HEIGHT_SEL = '.fp-auto-height';
var NORMAL_SCROLL = 'fp-normal-scroll';
var NORMAL_SCROLL_SEL = '.fp-normal-scroll';
// section nav
var SECTION_NAV = 'fp-nav';
var SECTION_NAV_SEL = '#' + SECTION_NAV;
var SECTION_NAV_TOOLTIP = 'fp-tooltip';
var SECTION_NAV_TOOLTIP_SEL='.'+SECTION_NAV_TOOLTIP;
var SHOW_ACTIVE_TOOLTIP = 'fp-show-active';
// slide
var SLIDE_DEFAULT_SEL = '.slide';
var SLIDE = 'fp-slide';
var SLIDE_SEL = '.' + SLIDE;
var SLIDE_ACTIVE_SEL = SLIDE_SEL + ACTIVE_SEL;
var SLIDES_WRAPPER = 'fp-slides';
var SLIDES_WRAPPER_SEL = '.' + SLIDES_WRAPPER;
var SLIDES_CONTAINER = 'fp-slidesContainer';
var SLIDES_CONTAINER_SEL = '.' + SLIDES_CONTAINER;
var TABLE = 'fp-table';
// slide nav
var SLIDES_NAV = 'fp-slidesNav';
var SLIDES_NAV_SEL = '.' + SLIDES_NAV;
var SLIDES_NAV_LINK_SEL = SLIDES_NAV_SEL + ' a';
var SLIDES_ARROW = 'fp-controlArrow';
var SLIDES_ARROW_SEL = '.' + SLIDES_ARROW;
var SLIDES_PREV = 'fp-prev';
var SLIDES_PREV_SEL = '.' + SLIDES_PREV;
var SLIDES_ARROW_PREV = SLIDES_ARROW + ' ' + SLIDES_PREV;
var SLIDES_ARROW_PREV_SEL = SLIDES_ARROW_SEL + SLIDES_PREV_SEL;
var SLIDES_NEXT = 'fp-next';
var SLIDES_NEXT_SEL = '.' + SLIDES_NEXT;
var SLIDES_ARROW_NEXT = SLIDES_ARROW + ' ' + SLIDES_NEXT;
var SLIDES_ARROW_NEXT_SEL = SLIDES_ARROW_SEL + SLIDES_NEXT_SEL;
var new_fullpage;
var oldConsole = console;
var lazyLoadElements = `
<img id="img-src" data-src="https://www.w3schools.com/html/pulpitrock.jpg" />
<img id="img-srcet" data-srcset="https://www.w3schools.com/html/pulpitrock.jpg 320w, https://www.w3schools.com/html/pulpitrock.jpg 420w" />
<video width="320" height="240" controls>
<source class="source-src" data-src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
<source class="source-src" data-src="https://www.w3schools.com/html/mov_bbb.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
<video id="source-srcset" width="320" height="240" controls>
<source class="source-srcset" data-srcset="https://www.w3schools.com/html/mov_bbb.mp4 768w, videos/tablet-video.mp4 1200w">
<source class="source-srcset" data-srcset="https://www.w3schools.com/html/mov_bbb.mp4 768w, videos/tablet-video.mp4 1200w">
</video>
<video id="video-src" data-src="https://www.w3schools.com/html/mov_bbb.ogg" controls>
Your browser does not support the video tag.
</video>
<audio id="audio-src" data-src="https://www.w3schools.com/html/horse.ogg" controls>
Your browser does not support the audio element.
</audio>
<iframe id="iframe-src" data-src="http://google.com"></iframe>
<picture>
<source data-srcset="https://www.w3schools.com/html/pulpitrock.jpg?v=1" media="(min-width: 1440px)">
<source data-srcset="https://www.w3schools.com/html/pulpitrock.jpg?v=2" media="(min-width: 1024px)">
<img class="background-image background-image-lazy" data-src="https://www.w3schools.com/html/pulpitrock.jpg?v=3">
</picture>
`;
function areAllLazyLoaded(element){
var lazyLoadElements = element.find('[data-src], [data-srcset]');
return !lazyLoadElements.length;
}
function isAnyLazyLoaded(element){
return element.find('[src], [srcset]').length;
}
function areOthersLazyLoaded(){
var isAnyOtherLoaded = false;
$(SECTION_SEL).each(function(){
var section = $(this);
var slides = $(this).find(SLIDE_SEL);
if(slides.length){
slides.each(function(){
var slide = $(this);
if(!section.hasClass(ACTIVE) || section.hasClass(ACTIVE) && !slide.hasClass(ACTIVE)){
isAnyOtherLoaded = isAnyOtherLoaded || isAnyLazyLoaded(slide);
}
});
}
else if(!section.hasClass(ACTIVE)){
isAnyOtherLoaded = isAnyOtherLoaded || isAnyLazyLoaded(section);
}
});
return isAnyOtherLoaded;
}
//appends lazy elements to all sections and slides
function appendLazyElements(id){
$(id).find('.section').each(function(index, section){
var slides = $(section).find('.slide');
if(slides.length){
slides.append(lazyLoadElements);
}
else{
$(section).append(lazyLoadElements);
}
});
}
var sectionsCallbacks = {
afterLoad: function(anchorLink, index) {
sectionLoaded[index - 1] = true;
afterLoad = `${anchorLink}, ${index}`;
},
onLeave: function(index, nextIndex, direction) {
sectionLoaded[index - 1] = false;
onLeave = `${index}, ${nextIndex}, ${direction}`;
},
afterRender() {
afterRender = [$(SECTION_ACTIVE_SEL).index(), $(SECTION_ACTIVE_SEL).find(SLIDE_ACTIVE_SEL).index()];
},
afterResponsive: function(isResponsive){
afterResponsive = isResponsive;
},
afterResize: function(){
afterResize = true;
},
afterReBuild: function(){
afterReBuild = true;
}
};
var sectionsCallbacksV3 = {
afterLoad: function(origin, destination, direction) {
sectionLoaded[destination.index ] = true;
afterLoad = {
origin: origin,
destination: destination,
direction: direction
};
},
onLeave: function(origin, destination, direction) {
sectionLoaded[origin.index] = false;
console.log(origin);
console.log(destination);
onLeave = {
origin: origin,
destination: destination,
direction: direction
};
console.log("-------");
},
afterRender() {
afterRender = [$(SECTION_ACTIVE_SEL).index(), $(SECTION_ACTIVE_SEL).find(SLIDE_ACTIVE_SEL).index()];
},
afterResponsive: function(isResponsive){
afterResponsive = isResponsive;
},
afterResize: function(width, height){
afterResize = `${width}, ${height}`;
},
afterReBuild: function(){
afterReBuild = true;
}
};
$('html').click(function(){
$(window).trigger('pepe');
});
var onSlideLeave = new_fullpage ? {} : ``;
var onLeave = new_fullpage ? {} : ``;
var afterLoad = new_fullpage ? {} : ``;
var afterSlideLoad = new_fullpage ? {} : ``;
var afterResponsive = false;
var afterResize = false;
var afterReBuild = false;
var afterRender = [];
var locationReplace = null;
var slidesCallbacks = {
afterSlideLoad: function(anchorLink, index, slideAnchor, slideIndex){
slideLoaded[slideIndex] = true;
afterSlideLoad = `${anchorLink}, ${index}, ${slideAnchor}, ${slideIndex}`;
},
onSlideLeave: function(anchorLink, index, slideIndex, direction, nextSlideIndex){
slideLoaded[slideIndex] = false;
onSlideLeave = `${anchorLink}, ${index}, ${slideIndex}, ${direction}, ${nextSlideIndex}`;
}
};
var slidesCallbacksV3 = {
afterSlideLoad: function(section, origin, destination, direction){
slideLoaded[destination.index] = true;
afterSlideLoad = {
section: section,
origin: origin,
destination: destination,
direction: direction
};
},
onSlideLeave: function(section, origin, destination, direction){
slideLoaded[origin.index] = false;
onSlideLeave = {
section: section,
origin: origin,
destination: destination,
direction: direction
};
}
};
var anchors = { anchors: ['page1', 'page2', 'page3', 'page4']};
var sectionsAndSlidesCallbacks = Object.assign({}, sectionsCallbacks, slidesCallbacks);
var sectionsAndSlidesCallbacksV3 = Object.assign({}, sectionsCallbacksV3, slidesCallbacksV3);
var allBasicOptions = Object.assign({}, anchors, sectionsCallbacks, slidesCallbacks);
var allBasicOptionsV3 = Object.assign({}, anchors, sectionsCallbacksV3, slidesCallbacksV3);
function getTransform(translate) {
var translateRegex = new RegExp('translate3d\\((.*)px,\\s(.*)px,\\s(.*)px\\)');
var parts = translateRegex.exec(translate);
parts = parts.map(function(item){
return item == '-0' ? 0: item;
});
return [''+parts[1]+'', ''+parts[2]+'', ''+parts[3]+''];
}
function getTransformFromElement($el) {
var results = $el.css('-webkit-transform').match(/matrix(?:(3d)\(-{0,1}\d+(?:, -{0,1}\d+)*(?:, (-{0,1}\d+))(?:, (-{0,1}\d+))(?:, (-{0,1}\d+)), -{0,1}\d+\)|\(-{0,1}\d+(?:, -{0,1}\d+)*(?:, (-{0,1}\d+))(?:, (-{0,1}\d+))\))/);
if(!results) return ["0", "0", "0"];
if(results[1] == '3d') return results.slice(2,5);
results.push("0");
return results.slice(5, 8);
}
function getWindowPosition(){
return $(window).scrollTop();
}
function getTop(selector){
return $(selector).css('top');
}
//overwritting the scrollTop function of jQuery to simulate scroll
//when using autoScrolling:false or scrollBar:true
function simulateScroll(scrollValue){
window.pageYOffset = (function(){
return scrollValue;
})();
trigger(window, 'scroll');
}
function simulateMouseWheel(type){
var multiplier = type === 'up' ? -1 : 1;
//moving down with mouse wheel
var mouseWheel = new Event('wheel', { 'bubbles': true });
mouseWheel.deltaY = 10 * multiplier;
document.dispatchEvent(mouseWheel);
}
function simulateKeydown(type){
var keyCode = type === 'up' ? 38 : 40;
//moving down with mouse wheel
var keydownEvent = new Event('keydown', { 'bubbles': true });
keydownEvent.keyCode = keyCode;
document.dispatchEvent(keydownEvent);
}
//not used yet
function simulateTouchMove(target, type){
var pageY = 100, pageX = 100, differenceX = 0, differenceY = 0;
switch(type){
case 'down': differenceY = 500; break;
case 'up': differenceY = -500; break;
case 'left': differenceX = 500;break;
case 'right': differenceX = -500; break;
}
//touch start
var touchStartEvent = new Event('touchstart', { 'bubbles': true });
touchStartEvent.pageX = pageX + differenceX;
touchStartEvent.pageY = pageY + differenceY;
target.dispatchEvent(touchStartEvent);
//moving down with mouse wheel
var touchMoveEvent = new Event('touchmove', { 'bubbles': true });
touchMoveEvent.pageX = pageX;
touchMoveEvent.pageY = pageY;
target.dispatchEvent(touchMoveEvent);
}
function trigger(el, eventName, data){
var event;
data = typeof data === 'undefined' ? {} : data;
// Native
if (window.CustomEvent) {
event = new CustomEvent(eventName, {detail: data});
}
else{
event = document.createEvent('CustomEvent');
event.initCustomEvent(eventName, true, true, data);
}
el.dispatchEvent(event);
}
function destroyTest(){
//destroying it if it existed
if(fullpage_api != null){
fullpage_api.destroy('all');
}
else if($.fn.fullpage != null){
$.fn.fullpage.destroy('all');
}
console.warn = oldConsole.warn;
console.log = oldConsole.log;
console.error = oldConsole.error;
//removing the URL hash
window.location.hash = '';
//resetting the loaded sections array flag
$.each(sectionLoaded, function(index, value) {
sectionLoaded[index] = false;
});
$('.fullpage-layout.active').removeClass('active');
onSlideLeave = ``;
onLeave = ``;
afterLoad = ``;
afterSlideLoad = ``;
afterResponsive = false;
afterResize = false;
afterReBuild = false;
afterRender = [];
locationReplace = null;
}
function setLoadedState(){
sectionLoaded = [];
slideLoaded = [];
// "Loaded" flag for each section to fill "onLoad"
$('.section:visible').each(function(index) {
sectionLoaded.push(false);
});
$('.slide:visible').each(function(index){
slideLoaded.push(false);
});
}
function areOthersLoaded(elements){
return elements.filter(function(x){
return x === true;
}).length;
}
function initFullpage(id, options){
$(id).addClass('active');
setLoadedState();
$(id).fullpage(options);
}
function initFullpageNew(id, options){
$(id).addClass('active');
setLoadedState();
new_fullpage = new fullpage(id, options);
new_fullpage.test.isTesting = true;
return new_fullpage;
}
QUnit.module("module", {
beforeEach: function(assert) {
console.warn(assert.test.testName);
},
afterEach: function(assert) {
destroyTest();
}
});