-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection.js
196 lines (172 loc) · 5.13 KB
/
section.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
class Section {
static all = [];
constructor({ id, name, place_id }) {
this.id = id;
this.name = name;
this.place_id = place_id;
this.main = document.createElement("div");
this.main.id = `main-${this.name}`;
this.main.classList.add("boxylady");
this.wrapper = document.createElement("div");
this.wrapper.id = `wrapper-${this.name}`;
this.nameDiv = document.createElement("div");
this.nameDiv.id = `section`;
this.tipArea = document.createElement("div");
this.tipArea.id = `tip-section`;
this.wrapper.append(this.nameDiv, this.tipArea);
this.main.append(this.wrapper);
this.main.addEventListener("click", this.onCardClick);
Section.all.push(this);
}
toggleExpansion = (element, to, duration = 350) => {
return new Promise((res) => {
element.animate(
[
{
top: to.top,
left: to.left,
width: to.width,
height: to.height,
},
],
{ duration, fill: "forwards", ease: "ease-in" }
);
setTimeout(res, duration);
});
};
onCardClick = async (e) => {
const places = document.querySelector("#places");
const card = this.main;
const likes = document.querySelectorAll("#likeSection");
// clone the card
const cardClone = document.createElement("div");
cardClone.id = `clone-${this.name}`;
cardCloneCreate(cardClone, this);
renderNewTipForm(cardClone, this);
let newarea = document.createElement("div");
newarea.id = "newarea";
cardClone.appendChild(newarea);
// get the location of the card in the view
const { top, left, width, height } = card.getBoundingClientRect();
// position the clone on top of the original
cardClone.style.position = "fixed";
// hide the original card with opacity
card.style.opacity = "0";
// add card to the same container
places.appendChild(cardClone);
let makeTip = document.getElementById("newTipForm");
makeTip.addEventListener("submit", (e) => {
console.log(this);
e.preventDefault();
let colors = e.currentTarget[1].value;
let sections = e.currentTarget[3].value;
let places = e.currentTarget[2].value;
let newinfo = e.currentTarget[0].value;
newTipCreator(sections, places, newinfo, colors);
window.location.reload();
});
// create a close button to handle the undo
const closeButton = document.createElement("button");
// position the close button top corner
closeButton.style = `
position: absolute;
z-index: 10000;
top: 35px;
right: 35px;
width: 35px;
height: 35px;
border-radius: 50%;
background-color: #11698E;
`;
// attach click event to the close button
closeButton.addEventListener("click", async () => {
// remove the button on close
closeButton.remove();
// remove the display style so the original content is displayed right
cardClone.style.removeProperty("display");
cardClone.style.removeProperty("padding");
// show original card content
[...cardClone.children].forEach((child) =>
child.style.removeProperty("display")
);
this.fadeContent(cardClone, "0");
// shrink the card back to the original position and size
await this.toggleExpansion(cardClone, {
top: `${top}px`,
left: `${left}px`,
width: `${width}px`,
height: `${height}px`,
});
// show the original card again
card.style.removeProperty("opacity");
// remove the clone card
cardClone.remove();
// fade the content away
this.fadeContent(cardClone, "0").then(() => {
[...cardClone.children].forEach(
(child) => (child.style.display = "none")
);
});
let body = document.getElementById("bigone");
body.style.overflow = "auto";
});
// expand the clone card
await this.toggleExpansion(
cardClone,
{
top: "100px",
left: "10%",
width: "80vw",
height: "80vh",
},
300
);
// set the display block so the content will follow the normal flow in case the original card is not display block
cardClone.style.display = "inline-block";
cardClone.style.listStyle = "square";
cardClone.style.textAlign = "left";
// append the close button after the expansion is done
cardClone.appendChild(closeButton);
let body = document.getElementById("bigone");
body.style.overflow = "hidden";
};
fadeContent = (element, opacity, duration = 300) => {
return new Promise((res) => {
[...element.children].forEach((child) => {
requestAnimationFrame(() => {
child.style.transition = `opacity ${duration}ms linear`;
child.style.opacity = opacity;
});
});
setTimeout(res, duration);
});
};
allTips = () => {
return Tip.all
.filter((tip) => tip.section_id == this.id)
.filter((tip) => tip.place_id == this.place_id)
.sort(function (x, y) {
return y.like_count - x.like_count;
});
};
appendTips = () => {
if (this.allTips().length > 0) {
this.allTips()
.map((tip) => tip.renderTip())
.join(" ");
}
};
tipsToPlace = () => {
this.renderTips();
this.appendTips();
};
renderSection = () => {
return (this.nameDiv.innerHTML = `<h3 id="${this.name}"><span>${this.name}</span></h3>`);
};
renderTips = () => {
if (this.allTips().length > 0) {
let tips = this.allTips().slice(0, 3);
tips.forEach((tip) => this.tipArea.append(tip.infoDiv));
}
};
}