-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
326 lines (269 loc) · 10.1 KB
/
script.js
File metadata and controls
326 lines (269 loc) · 10.1 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
// Code written by April LaRosa (https://github.com/aaprilfoolss)
// Part of this code was taken from https://codepen.io/RobVermeer/pen/japZpY (credit to https://codepen.io/RobVermeer) and edited to better suit the project's needs
'use strict';
var quizContainer = document.querySelector('.quiz');
var allCards = document.querySelectorAll('.quiz--card');
var nope = document.getElementById('nope');
var love = document.getElementById('love');
var skip = document.getElementById('skip');
var cont = document.getElementById('continue');
var exit = document.getElementById('close');
var exit2 = document.getElementById('close2');
var help = document.getElementById('help');
var isOnDiv;
var cardIndex = 1;
var xThreshold = 100;
var mobile = false;
var chameleon = 0, historian = 0, sensitive = 0, thrillSeeker = 0, traditionalist = 0;
function initCards() {
var newCards = document.querySelectorAll('.quiz--card:not(.removed)');
newCards.forEach(function (card, index) {
card.style.zIndex = allCards.length - index;
});
quizContainer.classList.add('loaded');
}
initCards();
allCards.forEach(function (el) {
var hammertime = new Hammer(el);
hammertime.on('pan', function (event) {
el.classList.add('moving');
});
//handle when user is moving card
hammertime.on('pan', function (event) {
if (event.deltaX === 0) return;
if (event.center.x === 0 && event.center.y === 0) return;
var xMulti = event.deltaX * 0.03;
var yMulti = event.deltaY / 80;
var rotate = xMulti * yMulti;
//if event target registers as card content, set it to the card
if (event.target.parentElement == document.querySelector('.quiz--card')) {
event.target = event.target.parentElement;
}
//don't allow users to grab something other than the cards themselves
else if (!event.target.isEqualNode(document.querySelector('.quiz--card'))) {
return;
}
//don't allow users to interact with cards other than the top card
if (event.target.style.getPropertyValue('z-index') != 10) { return; }
//add active animations to like/dislike buttons for users who are swiping (deltaX threshold)
if (event.deltaX > xThreshold) {
love.firstChild.classList.add('active');
} else if (event.deltaX < -xThreshold) {
nope.firstChild.classList.add('active');
} else {
love.firstChild.classList.remove('active');
nope.firstChild.classList.remove('active');
}
event.target.style.transform = 'translate(' + event.deltaX + 'px, ' + event.deltaY + 'px) rotate(' + rotate + 'deg)';
});
//handle after user stops moving card
hammertime.on('panend', function (event) {
// console.log(event.target);
el.classList.remove('moving');
var moveOutWidth = document.body.clientWidth;
var keep = Math.abs(event.deltaX) < 170 && Math.abs(event.velocityX) < 0.5;
//add icon animation when user swipes quickly (aka doesn't hit deltaX threshold)
if(event.velocityX >= 0.5) {
love.firstChild.classList.add('active');
} else if (event.velocityX <= -0.5) {
nope.firstChild.classList.add('active');
}
//only allow following transformations on cards
if (!event.target.isEqualNode(document.querySelector('.quiz--card'))) {
return;
};
event.target.classList.toggle('removed', !keep);
if (keep) {
event.target.style.transform = '';
} else {
var endX = Math.max(Math.abs(event.velocityX) * moveOutWidth, moveOutWidth);
var toX = event.deltaX > 0 ? endX : -endX;
var endY = Math.abs(event.velocityY) * moveOutWidth;
var toY = event.deltaY > 0 ? endY : -endY;
var xMulti = event.deltaX * 0.03;
var yMulti = event.deltaY / 80;
var rotate = xMulti * yMulti;
setTimeout(() => {
love.firstChild.classList.remove('active');
nope.firstChild.classList.remove('active');
}, 400);
event.target.style.transform = 'translate(' + toX + 'px, ' + (toY + event.deltaY) + 'px) rotate(' + rotate + 'deg)';
initCards();
//add to appropriate result sum
if (event.deltaX > 0) {
switch(event.target.getElementsByTagName('img')[0].classList[0]){
case 'chameleon':
chameleon++;
break;
case 'historian':
historian++;
break;
case 'sensitive':
sensitive++;
break;
case 'thrillSeeker':
thrillSeeker++;
break;
case 'traditionalist':
traditionalist++;
break;
default:
console.log('error: no type');
}
}
var cards = document.querySelectorAll('.quiz--card:not(.removed)');
if (!cards.length) result();
setTimeout(() => {
event.target.remove();
}, 500);
cardIndex++;
//updating progress display
if (cardIndex <= 10) {
document.getElementById('progress').innerHTML = cardIndex + " of 10";
}
//update skip confirmation message when user is more than halfway through
if (cardIndex == 6) {
document.getElementById('overlayText').innerHTML = 'You\'re so close to discovering your unique artistic preferences and style!';
}
}
});
});
function result() {
var types = {
'chameleon': chameleon,
'historian': historian,
'sensitive': sensitive,
'thrillSeeker': thrillSeeker,
'traditionalist': traditionalist
};
//redirect to appropriate result type
switch(Object.keys(types).reduce((a, b) => types[a] > types[b] ? a : b)) {
case ('chameleon'): window.location.href = 'chameleon.html'; break;
case ('historian'): window.location.href = 'historian.html'; break;
case ('sensitive'): window.location.href = 'sensitive.html'; break;
case ('thrillSeeker'): window.location.href = 'thrillSeeker.html'; break;
case ('traditionalist'): window.location.href = 'traditionalist.html'; break;
}
}
//handle 'like'/'dislike' icon events
function createButtonListener(love) {
return function (event) {
if ((document.getElementById('helpOverlay').style.visibility === 'visible' || document.getElementById('overlay').style.visibility === 'visible')) {
return;
}
var cards = document.querySelectorAll('.quiz--card:not(.removed)');
var moveOutWidth = document.body.clientWidth * 1.5;
if (!cards.length) return false;
var card = cards[0];
if (love) {
card.style.transform = 'translate(' + moveOutWidth + 'px, -100px) rotate(-30deg)';
//add to appropriate result sum
switch(card.getElementsByTagName('img')[0].classList[0]){
case 'chameleon':
chameleon++;
break;
case 'historian':
historian++;
break;
case 'sensitive':
sensitive++;
break;
case 'thrillSeeker':
thrillSeeker++;
break;
case 'traditionalist':
traditionalist++;
break;
default:
console.log('error: no type');
}
} else {
card.style.transform = 'translate(-' + moveOutWidth + 'px, -100px) rotate(30deg)';
initCards();
}
card.classList.add('removed');
var cards = document.querySelectorAll('.quiz--card:not(.removed)');
if (!cards.length) result();
setTimeout(() => {
card.remove();
}, 500);
cardIndex++;
//updating progress display
if (cardIndex <= 10) {
document.getElementById('progress').innerHTML = cardIndex + " of 10";
}
//update skip confirmation message when user is more than halfway through
if (cardIndex == 6) {
document.getElementById('overlayText').innerHTML = 'You\'re so close to discovering your unique artistic preferences and style!';
}
event.preventDefault();
};
}
//buttons that open/close the skip overlay
function skipListener() {
if(document.getElementById('helpOverlay').style.visibility === 'visible') {
return;
}
document.getElementById('overlay').style.visibility = 'visible';
}
function helpListener() {
if(document.getElementById('overlay').style.visibility === 'visible') {
return;
}
document.getElementById('helpOverlay').style.visibility = 'visible';
}
function exitHelpListener() {
document.getElementById('helpOverlay').style.visibility = 'hidden';
}
function contListener() {
document.getElementById('overlay').style.visibility = 'hidden';
}
function exitListener() {
document.getElementById('overlay').style.visibility = 'hidden';
}
function exit2Listener() {
document.getElementById('helpOverlay').style.visibility = 'hidden';
}
var nopeListener = createButtonListener(false);
var loveListener = createButtonListener(true);
nope.addEventListener('click', nopeListener);
love.addEventListener('click', loveListener);
skip.addEventListener('click', skipListener);
cont.addEventListener('click', contListener);
exit.addEventListener('click', exitListener);
exit2.addEventListener('click', exit2Listener);
help.addEventListener('mouseover', helpListener);
help.addEventListener('mouseout', exitHelpListener);
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|OperaMini/i.test(navigator.userAgent) ) {
help.addEventListener('click', helpListener);
document.getElementById('helpOverlay').style.padding = 'padding: 2em;'
} else {
document.getElementById('close2').remove();
}
window.onload = () => {
if (screen.width > 400) {
xThreshold = 170;
}
// demo of swiping to show users how to interact
var demo = document.querySelector('.quiz--card');
demo.style.transform ='translate(50px,0px) rotate(10deg)';
demo.style.transition = '0.5s';
setTimeout(() => {
demo.style.transform ='translate(-50px,0px) rotate(-10deg)';
}, 500);
setTimeout(() => {
demo.style.transform ='';
}, 1000);
initCards();
};
window.addEventListener( "pageshow", function ( event ) {
//reload page if user goes backwards
if(window.performance.getEntriesByType('navigation')[0] == null) return;
var historyTraversal = event.persisted ||
( typeof window.performance != "undefined" &&
window.performance.getEntriesByType("navigation")[0].type === "back_forward");
if ( historyTraversal ) {
// Handle page restore.
window.location.reload();
}
});