-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-ladder.js
147 lines (134 loc) · 4.51 KB
/
create-ladder.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
(() => {
const ladder = document.getElementById('ladder');
//Show first challenge input field
const showLadder = () => {
const folds = document.getElementsByClassName('fold');
unfoldQ(folds,0);
addEventToSliders();
};
const unfoldQ = (elem,inx) =>{
if(inx != elem.length) {
let current = elem[inx];
current.classList.add('unfolded');
inx = parseInt(inx)+1;
setTimeout(() => {
unfoldQ(elem,inx);
}, 500);
}
}
const showBorders = () => {
const folds = document.querySelectorAll('.fold');
addBorder(folds,0);
}
//Add Border to folds
const addBorder = (folds) =>{
folds.forEach( fold => {
fold.classList.add('border');
})
}
const addStep = document.getElementById('addStep');
addStep.addEventListener('click', () => {
let step = `
<label for="description-1">
<textarea class='challenge__description' id="description-1" rows="4" placeholder="Description:"></textarea>
<span class="challenge_hints">Description</span>
</label>
<label class='anxiety-rating' for="rating-1"> <p class='rating-label'>Low</p><p class='rating-label'>Anxiety rating</p> <p class='rating-label'>High</p></label>
<input class='anxiety-rating__input' id="rating-1" type="range" min="0" max="100" value="50"/>
`
let newStep = document.createElement('SECTION');
newStep.className = 'challenge fold box';
newStep.innerHTML = step;
ladder.appendChild(newStep);
setTimeout(() => {
ladder.lastChild.classList.add('unfolded');
addEventToSliders();
}, 100);
})
const completeLadder = document.getElementById('completeLadder');
completeLadder.addEventListener('click', ()=> {
ladder.style.marginTop = '5rem';
const firstStep =document.getElementById('firstStep');
firstStep.style.display = 'none';
const cloud = document.getElementById('cloud');
cloud.className += ' animated bounceOutUp';
const cloudS = document.getElementById('cloudS');
cloudS.style.display ='none'
const cloudM = document.getElementById('cloudM');
cloudM.style.display ='none'
const cloudTitle = document.getElementById('cloudTitle');
cloudTitle.className += ' animated pulse move-up';
showBorders();
reOrder();
changeButtons();
setText('.challenge__description');
setText('.anxiety-rating__input');
removeElementsByClass('anxiety-rating');
})
//Remove elements by className
const removeElementsByClass = (className) => {
var elements = document.getElementsByClassName(className);
while(elements.length > 0){
elements[0].parentNode.removeChild(elements[0]);
}
}
//Swap button title on completion of ladder
const changeButtons = () => {
addStep.textContent = 'Edit ladder';
completeLadder.textContent = 'Back to my Goals';
}
//Set text of input fields to ladder
const setText = (select) => {
const desc = document.querySelectorAll(select);
desc.forEach( (d) => {
var contents = document.createElement('P');
if(select === '.challenge__description') {
contents.innerText = 'Challenge: ' + d.value;
} else {
contents.innerText = 'Rating: ' + d.value;
}
let classNam = select.substring(1);
contents.classList += `${classNam}--saved`;
d.replaceWith(contents);
})
}
//Slider
const addEventToSliders = () => {
const sliders = document.querySelectorAll(".anxiety-rating__input");
sliders.forEach((slider) => {
slider.addEventListener('input', function() {
const v = this.value;
slider.style.background = "linear-gradient(to right, #3B3561 0%,#3B3561 "+ v +"%,white "+ v +"%,white 100%)";
});
})
}
//REORDER steps
const reOrder = () => {
var nodes = document.querySelectorAll(".box");
var ease = Power1.easeInOut;
var boxes = [];
nodes.forEach( (node,i) => {
var node = nodes[i];
TweenLite.set(node, { x: 0 });
boxes[i] = {
transform: node._gsTransform,
x: node.offsetLeft,
y: node.offsetTop,
node
};
});
ladder.classList.toggle("reorder");
boxes.forEach( (box,i) => {
var box = boxes[i];
var lastX = box.x;
var lastY = box.y;
box.x = box.node.offsetLeft;
box.y = box.node.offsetTop;
if (lastX !== box.x && lastY !== box.y) return;
var x = box.transform.x + lastX - box.x;
var y = box.transform.y + lastY - box.y;
TweenLite.fromTo(box.node, 2, { x, y }, { lastX, lastY, ease });
});
}
showLadder();
})();