-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
321 lines (273 loc) · 9.78 KB
/
script.js
File metadata and controls
321 lines (273 loc) · 9.78 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
import {
addClass, addText,
append,
createArc,
createNode, createDivisionTimeLine,
getElemBySelector,
getPositionX,
getPositionY, createRect, getDateForTooltip, toggleClass, removeClass
} from './utils/helpers.js'
import EVENTS from "./data/events.json" assert { type: "json" }
import NAMES from "./data/names.json" assert { type: "json" }
export const sortArrByDate = sortByDate(EVENTS)
const arrLastHundredDates = createUniqDates(sortArrByDate) // ['2022-06-21', ...] length(100)
const lastHundredDaysWithEvents = createEventsArrForTimeLine(sortArrByDate, arrLastHundredDates) // [[{event},{event}..], ...] length(100)
const affectTypeByDate = totalDayAffectedType(lastHundredDaysWithEvents) // [[{2: 28, 3: 38, 4: 0, 5: 0, 6: 7}], ...] length(100)
const indexOfFirstTimeLine = getFirstIndexActionOnTimeLine(sortArrByDate, arrLastHundredDates) // 15482
const eventsBeforeTimeLine = sortArrByDate.slice(0, indexOfFirstTimeLine) // [{event}...] length 15482
const affectTypeBeforeTimeLine = affectTypeTotal(eventsBeforeTimeLine) // {2: 3685, 3: 3667, 4: 11, 5: 1585, 6: 2185}
const affectTypeAllDays = affectTypeTotal(sortArrByDate) // {2: 4643, 3: 5743, 4: 13, 5: 1985, 6: 2540}
const totalDayAffectedNumbers = getValueForDivision(affectTypeByDate) // [73, 44, 26..]
const timeLineHeightOnPixel = getAllHeightDivision(totalDayAffectedNumbers) // [1,3,4,5 ...] length100, value 0-60 (px)
const timeLineFullInfo = createTimeLineArr()
let intervalIdThumb
////////canvas map///////////////////////////////
const canvas = getElemBySelector('#map')
const ctx = canvas.getContext('2d')
canvas.width = 687
canvas.height = 457
function createPointOnMap(events) {
ctx.clearRect(0, 0, canvas.width, canvas.height)
events.map((point) => {
createArc(ctx, getPositionX(point.lon), getPositionY(point.lat))
})
}
//default All Point on map
createPointOnMap(sortArrByDate)
///////// left sidebar ////////////////////////////
function getUserLanguage() {
const userLanguage = window.navigator.language
if (NAMES[userLanguage]) {
return userLanguage
}
return 'en'
}
const page_language = getUserLanguage()
function updateInfoEvents(affectTypeAllDays) {
const leftContainer = getElemBySelector('.sidebar-container')
addText(leftContainer, '')
const affTypeObj = NAMES[page_language].affected_type
for( let key in affTypeObj ) {
const div = createNode('div')
const h3 = createNode('h3')
addText(h3, affectTypeAllDays[key])
addClass(h3, 'header-sidebar')
const span = createNode('span')
addClass(span, 'subheader-sidebar')
addText(span, affTypeObj[key] )
append(leftContainer, div)
append(div, h3)
append(div, span)
}
}
// default info all days
updateInfoEvents(affectTypeAllDays)
////////////////////// canvas Line //////////////////////
const canvasTimeLine = getElemBySelector('#time-line-canvas')
const ctxTimeLine = canvasTimeLine.getContext('2d')
canvasTimeLine.width = 1200
canvasTimeLine.height = 62
ctxTimeLine.fillStyle = '#292929'
ctxTimeLine.fillRect(0, 60, 1040, 2) // create line
// canvas Divisions
function createTimeLineCanvas(ctx, values) {
let positionX = 21
let positionY = 0
values.forEach((height) => {
positionY = 60 - height
createRect(ctxTimeLine, positionX, positionY, 8, height)
positionX += 10
})
}
//create default Timeline Canvas
createTimeLineCanvas(ctx, timeLineHeightOnPixel)
////////// create Div Division ///////////
function createDivisions(fullInfoDay) {
const timeLineDivisions = getElemBySelector('.time-line-divisions')
addText(timeLineDivisions, '')
fullInfoDay.forEach((day) => {
const division = createDivisionTimeLine(onDivisionClick, day.date)
append(timeLineDivisions, division)
})
}
function drawThumb(fullInfoDay) {
let prevThumb = getElemBySelector('.thumb')
if (prevThumb) {
const parent = prevThumb.parentElement
addText(parent, '')
}
const activeIndex = fullInfoDay.indexOf(fullInfoDay.find((day) => day.isActive))
const day = fullInfoDay[activeIndex]
const allDivisionBlocks = document.querySelectorAll('.division-block')
const parent = allDivisionBlocks[activeIndex]
const thumb = createNode('div')
const tooltipSpan = createNode('span')
addText(tooltipSpan, getDateForTooltip(day.date))
addClass(tooltipSpan, 'tooltip-text')
thumb.dataset.date = day.date
addClass(thumb, 'thumb')
addClass(thumb, 'tooltip')
append(parent, thumb)
append(thumb, tooltipSpan)
}
//default division and thumb creation
createDivisions(timeLineFullInfo)
drawThumb(timeLineFullInfo)
////////////////////// HANDLERS ////////////////////////
function onDivisionClick() {
const prevActiveIndex = timeLineFullInfo.indexOf(timeLineFullInfo.find((day) => day.isActive))
timeLineFullInfo[prevActiveIndex].isActive = false
const newActiveIndex = timeLineFullInfo.indexOf(timeLineFullInfo.find((day) => day.date === this.dataset.date))
timeLineFullInfo[newActiveIndex].isActive = true
updateInfoEvents(timeLineFullInfo[newActiveIndex].affectedTypeWithNumber)
createPointOnMap(timeLineFullInfo[newActiveIndex].events)
drawThumb(timeLineFullInfo)
}
function onPlayHandler() {
const activeIndex = timeLineFullInfo.indexOf(timeLineFullInfo.find((el) => el.isActive))
if (activeIndex === 99) {
return
}
nextActiveDivision()
toggleClass(playButton, 'hidden')
toggleClass(pauseButton, 'hidden')
}
function onPauseHandler() {
toggleClass(playButton, 'hidden')
toggleClass(pauseButton, 'hidden')
clearInterval(intervalIdThumb)
}
///////////// PLAY / PAUSE ///////////////
const playButton = getElemBySelector('.button-play')
const pauseButton = getElemBySelector('.button-pause')
playButton.addEventListener('click', onPlayHandler)
pauseButton.addEventListener('click', onPauseHandler)
function nextActiveDivision() {
intervalIdThumb = setInterval(() => {
const activeIndex = timeLineFullInfo.indexOf(timeLineFullInfo.find((el) => el.isActive))
if (activeIndex === 99) {
removeClass(playButton, 'hidden')
addClass(pauseButton, 'hidden')
clearInterval(intervalIdThumb)
return
}
timeLineFullInfo[activeIndex].isActive = false
timeLineFullInfo[activeIndex + 1].isActive = true
updateInfoEvents(timeLineFullInfo[activeIndex + 1].affectedTypeWithNumber)
createPointOnMap(timeLineFullInfo[activeIndex + 1].events)
drawThumb(timeLineFullInfo)
}, 600)
}
//////// function for Created constants //////////
function sortByDate(arr) {
const result = [...arr].sort((a, b) => {
const value1 = a.from
const value2 = b.from
if (value1 > value2) {
return 1
}
if (value1 < value2) {
return -1
}
return 0
})
return result
}
function createUniqDates(events) {
const timeLineLastHundred = new Set()
events.forEach((event) => timeLineLastHundred.add(event.from))
return [...timeLineLastHundred].splice(-100, 100)
}
function createEventsArrForTimeLine(events, dates) {
const result = []
events.forEach((event) => {
if (dates.includes(event.from)) {
const index = dates.indexOf(event.from)
if (result[index]) {
result[index].push(event)
} else {
result[index] = []
result[index].push(event)
}
}
})
return result
}
function totalDayAffectedType(eventsGroupByDate) {
const result = []
eventsGroupByDate.forEach((dayEvents) => {
const affectTotal = {
2: 0,
3: 0,
4: 0,
5: 0,
6: 0
}
dayEvents.forEach((event) => {
if (event.affected_type) {
affectTotal[event.affected_type] += Number(event.affected_number)
}
})
result.push(affectTotal)
})
return result
}
function getFirstIndexActionOnTimeLine(sortArr, timeLineDatesArr) {
return sortArr.indexOf(sortArr.find((elem) => elem.from === timeLineDatesArr[0]))
}
function affectTypeTotal(events) {
const affectTotal = {
2: 0,
3: 0,
4: 0,
5: 0,
6: 0
}
events.forEach((event) => {
if (event.affected_type) {
affectTotal[event.affected_type] += Number(event.affected_number)
}
})
return affectTotal
}
function getValueForDivision(daysTypes) {
return daysTypes.map((types) => {
return Object.values(types).reduce((prevValue, currValue) => prevValue += currValue, 0)
})
}
function getAllHeightDivision(totalDayAffectNumbers) {
const maxNumber = Math.max.apply(null, totalDayAffectNumbers) // maxNumber - max height 60 px division
return totalDayAffectNumbers.map((number) => {
return Math.ceil((60 * number) / maxNumber)
})
}
function createTimeLineArr() {
const result = []
for (let i = 0; i < arrLastHundredDates.length; i++) {
const firstArrType = i === 0 ? affectTypeBeforeTimeLine : result[i - 1].affectedTypeWithNumber
const firstArrEvents = i === 0 ? eventsBeforeTimeLine : result[i - 1].events
const affectType = addAffectsType(firstArrType, affectTypeByDate[i])
const eventsTotal = getTotalEvents(firstArrEvents, lastHundredDaysWithEvents[i])
const day = {
date: arrLastHundredDates[i],
totalAffectedNumber: totalDayAffectedNumbers[i],
affectedTypeWithNumber: affectType,
events: eventsTotal,
personalEvents: lastHundredDaysWithEvents[i],
pixelHeight: timeLineHeightOnPixel[i],
isActive: i === 99
}
result.push(day)
}
return result
}
function addAffectsType(prevValues, addValues) {
const newObj = {}
for (let key in addValues) {
newObj[key] = addValues[key] + prevValues[key]
}
return newObj
}
function getTotalEvents(prevValues, addValue) {
return [...prevValues, ...addValue]
}