This repository was archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
257 lines (206 loc) · 6.85 KB
/
Copy pathfunction.js
File metadata and controls
257 lines (206 loc) · 6.85 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
$(function() {
console.log("[DEBUG] Slides function v0.0.5");
$('.ui-loader').hide();
//First the variables our app is going to use need to be declared
//References to DOM elements
var $window = $(window);
var $document = $(document);
//Only links that starts with #
var $navButtons = $("nav a").filter("[href^=#]");
var $navGoPrev = $(".go-prev");
var $navGoNext = $(".go-next");
var $mobileGoNext = $(".mobile-go-next");
var $mobileGoPrev = $(".mobile-go-previous");
var $slidesContainer = $(".slides-container");
var $slides = $(".slide");
var $currentSlide = $slides.first();
var $nav_topics = $('#topics .row div');
//Animating flag - is our app animating
var isAnimating = false;
var startTouch;
var start = 0;
//The height of the window
var pageHeight = $window.innerHeight();
//Key codes for up and down arrows on keyboard. We'll be using this to navigate change slides using the keyboard
var keyCodes = {
UP : 38,
DOWN: 40
}
//Going to the first slide
goToSlide($currentSlide);
/*
* Adding event listeners
* */
$window.on("resize", onResize).resize();
$window.on("mousewheel DOMMouseScroll", onMouseWheel);
$window.bind('touchstart', updateTouchPoint);
$window.on("touchmove", onTouchMove);
$document.on("keydown", onKeyDown);
$navButtons.on("click", onNavButtonClick);
$navGoPrev.on("click", goToPrevSlide);
$navGoNext.on("click", goToNextSlide);
$mobileGoNext.on("click", goToNextSlide);
$mobileGoPrev.on("click", goToPrevSlide);
$nav_topics.on("click", goToSpecificSlide);
/*
* When a button is clicked - first get the button href, and then slide to the container, if there's such a container
* */
function onNavButtonClick(event) {
//The clicked button
var $button = $(this);
//The slide the button points to
var $slide = $($button.attr("href"));
//If the slide exists, we go to it
if($slide.length) {
goToSlide($slide);
event.preventDefault();
}
}
function updateTouchPoint(event) {
startTouch = event.originalEvent.touches[0].clientY;
}
function onTouchMove(event) {
var endTouch = event.originalEvent.changedTouches[0].clientY;
if (startTouch > endTouch) {
goToNextSlide();
} else {
goToPrevSlide();
}
}
/*
* Getting the pressed key. Only if it's up or down arrow, we go to prev or next slide and prevent default behaviour
* This way, if there's text input, the user is still able to fill it
* */
function onKeyDown(event) {
var PRESSED_KEY = event.keyCode;
if(PRESSED_KEY == keyCodes.UP) {
goToPrevSlide();
event.preventDefault();
} else if(PRESSED_KEY == keyCodes.DOWN) {
goToNextSlide();
event.preventDefault();
}
}
/*
* When user scrolls with the mouse, we have to change slides
* */
function onMouseWheel(event) {
//Normalize event wheel delta
var delta = event.originalEvent.wheelDelta / 30 || -event.originalEvent.detail;
//If the user scrolled up, it goes to previous slide, otherwise - to next slide
if(delta < -1) {
goToNextSlide();
} else if(delta > 1) {
goToPrevSlide();
}
event.preventDefault();
}
function onTouchScroll(event) {
//If the user scrolled up, it goes to previous slide, otherwise - to next slide
if($(this).scrollTop() < -1) {
goToNextSlide();
} else {
goToPrevSlide();
}
event.preventDefault();
}
/*
* If there's a previous slide, slide to it
* */
function goToPrevSlide() {
if($currentSlide.prev().length) {
goToSlide($currentSlide.prev());
}
}
/*
* If there's a next slide, slide to it
* */
function goToNextSlide() {
if($currentSlide.next().length) {
goToSlide($currentSlide.next());
}
}
function goToSpecificSlide(event) {
var slide_id = $(event.target).data('id');
goToSlide($(_.find($slides, function(element){return element.id == slide_id})));
}
/*
* Actual transition between slides
* */
function goToSlide($slide) {
//If the slides are not changing and there's such a slide
if(!isAnimating && $slide.length) {
//setting animating flag to true
isAnimating = true;
$currentSlide = $slide;
if($currentSlide[0].id === "wishes" && start === 0) {
show_quotes();
start = 1;
}
//Sliding to current slide
TweenLite.to($slidesContainer, 1, {scrollTo: {y: pageHeight * $currentSlide.index() }, onComplete: onSlideChangeEnd, onCompleteScope: this});
//Animating menu items
TweenLite.to($navButtons.filter(".active"), 0.5, {className: "-=active"});
TweenLite.to($navButtons.filter("[href=#" + $currentSlide.attr("id") + "]"), 0.5, {className: "+=active"});
sendGAEventSlide($currentSlide.attr("id"));
}
}
/*
* GoogleAnalytics events
* */
// Send start event
console.log("[DEBUG] event 'start'");
ga('send', 'event', 'slides', 'start', 'user', 1);
function sendGAEventSlide(slide_id){
console.log("[DEBUG] in slide ", slide_id);
ga('send', 'pageview', "/slides/" + slide_id);
if($("#" + slide_id).attr("data-final-slide") == "true") {
console.log("[DEBUG] event 'completed'");
ga('send', 'event', 'slides', 'completed', 'user', 1);
}
}
function sendGAEventVoteClick(){
console.log("[DEBUG] event 'vote click'");
ga('send', 'event', 'vote', 'clicked', 'user', 1);
}
/*
* Once the sliding is finished, we need to restore "isAnimating" flag.
* You can also do other things in this function, such as changing page title
* */
function onSlideChangeEnd() {
isAnimating = false;
}
/*
* When user resize it's browser we need to know the new height, so we can properly align the current slide
* */
function onResize(event) {
//This will give us the new height of the window
var newPageHeight = $window.innerHeight();
/*
* If the new height is different from the old height ( the browser is resized vertically ), the slides are resized
* */
if(pageHeight !== newPageHeight) {
pageHeight = newPageHeight;
//This can be done via CSS only, but fails into some old browsers, so I prefer to set height via JS
TweenLite.set([$slidesContainer, $slides], {height: pageHeight + "px"});
//The current slide should be always on the top
TweenLite.set($slidesContainer, {scrollTo: {y: pageHeight * $currentSlide.index() }});
}
}
function show_quotes() {
var quotes = $(".quotes");
var quoteIndex = -1;
function showNextQuote() {
++quoteIndex;
quotes.eq(quoteIndex % quotes.length)
.fadeIn(2000)
.delay(2000)
.fadeOut(2000, showNextQuote);
}
showNextQuote();
}
//hide created-by from cartoDB
$('.cartodb-text').hide();
// Activate goal events on vote_triggers
$(".vote_trigger").on("click", sendGAEventVoteClick);
});