This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimeLine.ts
468 lines (398 loc) · 12.1 KB
/
TimeLine.ts
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* Detect collusion from a given range (TriggerProints)
* */
class TimeLineCollusionRanger {
static triggerPoint = {
height: 90,
top: (() => {
return window.innerWidth < 690 ? 150 : 400;
})(),
drawRanger: false,
};
public collusionsMapper = {
hasCollusion: false, // item is inside mapper
current: <TimelineItem | null>null, // current item inside mapper
prev: <TimelineItem | null>null, // prev item inside mapper
};
constructor( ) {
this.drawVisibleRanger();
}
protected drawVisibleRanger() {
if (
!TimeLineCollusionRanger.triggerPoint.drawRanger ||
document.querySelector(".timeline-ranger-helper")
)
return;
const $div = document.createElement("div");
const points = TimeLineCollusionRanger.triggerPoint;
$div.innerHTML = `<div style="opacity: 0.1;position: fixed;top: ${points.top}px;height: ${points.height}px;width: 100%;background: #ff2b2b" class="timeline-ranger-helper"></div>`;
document.body.appendChild($div);
}
public hasCollusion(){
return this.collusionsMapper.hasCollusion;
}
/**
* Verify is item is inside range
* @item <TimelineItem>
* */
itemIsInsideRange(item: TimelineItem) {
const range = TimeLineCollusionRanger;
const { top, height } = (<HTMLElement>(
item.getParentElement()
)).getBoundingClientRect();
const isInsideTop =
top > range.triggerPoint.top &&
top < range.triggerPoint.top + range.triggerPoint.height;
const isInsideBottom =
top + height > range.triggerPoint.top &&
top + height < range.triggerPoint.top + range.triggerPoint.height;
if (isInsideTop || isInsideBottom) {
return true;
}
}
/**
* Verify is item is inside browser viewport
* @item <TimelineItem>
* @setValue saves the value to the pased item
* */
itemIsInView(item: TimelineItem, setValue = true) {
const { top, height } = (<HTMLElement>(
item.getParentElement()
)).getBoundingClientRect();
if (setValue) {
item.isInView = top < window.innerHeight && top + height >= 0;
return item.isInView;
} else {
return top < window.innerHeight && top + height >= 0;
}
}
}
/*-------------------------------------------------------------------------------*/
class TimelineItem {
/* Is the first child element from the container */
isFirst = false;
/* Is betweeen window height and 0 */
isInView = false;
isActivated = true;
state = false;
constructor(private $item: HTMLElement) {
this.initListeners();
this.isFirst = !$item.parentElement?.previousElementSibling;
}
get element(): HTMLElement {
return this.$item;
}
initListeners() {
// animation
this.$item.ontransitionend = (ev) => {
this.isActivated = this.$item.classList.contains("active");
if (!this.state && this.isActivated) {
this.$item.classList.remove("active");
}
};
// image
const parent: any = this.getParentElement();
const imgs = [
...parent.parentElement.parentElement.querySelectorAll(
".timeline-item-content--images img"
),
];
imgs.map(($img) => {
if (!$img.dataset.listener) {
$img.dataset.listener = $img.addEventListener("click", (ev: any) =>
this.ON.click.image(this, ev)
);
}
});
}
getParentElement() {
return this.$item.parentElement;
}
getSideBarElements(): HTMLElement[] {
if (this.isFirst) {
return [
...(<any>(
this.getParentElement()?.parentElement?.parentElement?.querySelectorAll(
".timeline-item"
)
)),
];
}
return [];
}
setState(state: boolean) {
this.state = state;
if (state) {
if (!this.$item.classList.contains("active"))
this.$item.classList.add("active");
} else {
if (this.isActivated) {
this.$item.classList.remove("active");
}
}
}
ON = {
click: {
image: (item: TimelineItem, ev: any): any => null,
},
};
}
/*-------------------------------------------------------------------------------*/
class Timeline {
static itemSelector = ".bubble";
protected lineRanger = new TimeLineCollusionRanger();
constructor(protected $containerElement: any) {
this.inti();
}
protected inti() {
this.initContainer();
this.initItems();
this.initListeners();
}
private initContainer() {
if (typeof (<any>this.$containerElement) === "string") {
this.$containerElement = document.querySelector(
<any>this.$containerElement
);
}
if (!this.$containerElement)
throw new Error("DomElementNotFound: Container not found");
}
/**
* Initialise all Items with the class<TimelineItem>
* */
private initItems() {
this.$containerElement.items = [
...this.$containerElement.querySelectorAll(Timeline.itemSelector),
].map((v) => new TimelineItem(v));
this.setItemVisibility();
this.setItemColor();
}
/**
* Adding Dom Listeners
* */
protected initListeners() {
/**Global*/
document.addEventListener("scroll", (ev) => this.handleScroll(ev));
window.addEventListener("resize", () => {
this.setItemVisibility();
this.setItemColor();
});
/*Item based*/
this.$containerElement.items.forEach(
(item: TimelineItem) =>
(item.ON.click.image = (item: TimelineItem, ev: any) =>
this.handleItemImageClick(item, ev))
);
}
/**
* Handle Scroll event
* */
protected handleScroll(ev: Event) {
this.lineRanger.collusionsMapper.hasCollusion = false;
let found = null;
for (let i = 0; i < this.$containerElement.items.length; i++) {
let item = this.$containerElement.items[i];
/*
* canTriggerSlideContent
* */
if (this.canTriggerSlideContent(item)) this.triggerSlideContent(item);
if (found) {
found--;
if (found === 0) break;
} else if (this.lineRanger.itemIsInsideRange(item)) {
this.lineRanger.collusionsMapper.hasCollusion = true;
this.lineRanger.collusionsMapper.prev =
this.lineRanger.collusionsMapper.current;
this.lineRanger.collusionsMapper.current = item;
found = 20;
}
}
if (!this.lineRanger.collusionsMapper.hasCollusion) {
this.lineRanger.collusionsMapper.prev =
this.lineRanger.collusionsMapper.current;
this.lineRanger.collusionsMapper.current = null;
}
this.setItemClasses();
}
protected handleItemImageClick(item: TimelineItem, ev: any) {
/*
* Show Modal
* */
const doc = document.createDocumentFragment();
const container = document.createElement("div");
container.classList.add("image-slider-container");
const closeBtn = document.createElement("div");
closeBtn.classList.add('image-slider-container--close')
closeBtn.innerHTML = '<a>X</a>';
const img = document.createElement("img");
img.src = ev.target.src;
[closeBtn, img].forEach((el) => container.append(el));
doc.append(container);
document.body.append(doc);
closeBtn.addEventListener('click', ()=>
document.querySelectorAll(".image-slider-container")
.forEach(el => el.remove()));
}
protected canTriggerSlideContent(item: TimelineItem): boolean {
return (
item.isFirst && !item.isInView && this.lineRanger.itemIsInView(item, true)
);
}
protected triggerSlideContent(item: TimelineItem): any {
return item
.getParentElement()
?.parentElement?.parentElement?.querySelector(".contentContainer")
?.classList.add("visible");
}
// item methods
public setItemClasses() {
const { prev, current } = this.lineRanger.collusionsMapper;
prev ? prev.setState(false) : null;
current ? current.setState(true) : null;
}
/*
* Hide elements on mobile view based on content height
* */
public setItemVisibility() {
if (window.innerWidth < 690) {
let $lastContainer: HTMLElement;
let sum = 0;
let maxHeight = 0;
this.$containerElement.items.map((item: TimelineItem) => {
if (item.isFirst) {
sum = 0;
maxHeight = 0;
const getHeight = ($element: HTMLElement) => $element.clientHeight;
const sumHeight = (previousValue: any, currentValue: any) =>
<any>+previousValue + +currentValue;
maxHeight = <any>(
item.getSideBarElements().map(getHeight).reduce(sumHeight)
);
const parent = item.getParentElement()?.parentElement?.parentElement;
$lastContainer = <any>parent;
console.log(maxHeight, item.getSideBarElements());
} else {
const parent = item.getParentElement()?.parentElement?.parentElement;
if (parent?.isEqualNode($lastContainer)) {
let $container = <any>item.getParentElement();
sum += item.element.clientHeight + 40;
if (sum > maxHeight) {
$container.style.display = "none";
$container.visibility = 0;
} else {
$container.style.display = "block";
$container.visibility = 1;
}
}
}
});
} else {
this.$containerElement.items.map((item: TimelineItem) => {
let $container = <any>item.getParentElement();
$container.style.display = "block";
$container.visibility = 1;
});
}
}
public setItemColor() {
var red = 1;
var green = 107;
var blue = 183;
var circleCounter = 0;
var rowCounter = 0;
let $parent: any;
this.$containerElement.items.map((item: TimelineItem) => {
$parent = item.getParentElement();
if ($parent.visibility === 0) return;
if (circleCounter % 9 == 0) rowCounter++;
circleCounter++;
switch (rowCounter) {
case 9:
rowCounter = 1;
break;
case 1:
red += 14.33;
green -= 7.55;
blue -= 11.44;
break;
case 2:
red += 10.44;
green += 3.77;
blue -= 6.77;
break;
case 3:
red += 2.88;
green += 13.11;
blue -= 2.11;
break;
case 4:
red -= 10.11;
green += 0.33;
blue += 0.33;
break;
case 5:
red -= 17.55;
green -= 7.22;
blue -= 0.33;
break;
case 6:
red += 5.11;
green -= 3.77;
blue += 5.22;
break;
case 7:
red += 4.77;
green += 13.55;
blue += 25.44;
break;
case 8:
red -= 8.22;
green -= 13;
blue -= 9.33;
break;
default:
}
let redRounded = parseInt(<any>red);
let greenRounded = parseInt(<any>green);
let blueRounded = parseInt(<any>blue);
item.element.style.backgroundColor =
"rgb(" + redRounded + "," + greenRounded + "," + blueRounded + ")";
if (item.isFirst) {
item.getSideBarElements().forEach((contentItem) => {
const heading: HTMLElement = <any>(
contentItem.querySelector(".timeline-item-heading")
);
if (heading) {
const headingArrowIcon: HTMLElement = <any>(
heading.querySelector(".timeline-item-heading--arrow-end polygon")
);
const headingBackIcon: HTMLElement = <any>(
heading.querySelector(".arrow")
);
[heading, headingArrowIcon, headingBackIcon].map(($el) => {
$el.style.backgroundColor =
"rgb(" +
redRounded +
"," +
greenRounded +
"," +
blueRounded +
")";
$el.style.fill =
"rgb(" +
redRounded +
"," +
greenRounded +
"," +
blueRounded +
")";
});
}
});
}
});
}
}
/*-------------------------------------------------------------------------------*/
((/* Auto Init */) => document.querySelectorAll('div[data-timeline="true"]').forEach((el) => new Timeline(el)))();