-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainpage.vue
267 lines (211 loc) · 7.02 KB
/
Mainpage.vue
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
<template>
<div class="map" :id="map.id"/>
</template>
<script>
import {createMap, createMapEventMarker} from '@/map_functions'
import {EVENTS_RANGES} from '@/helpers/event'
import apiSettings from '@/settings/api'
export default
{
props: {
gestureHandling: true,
onlyStations: false
},
data() {
return {
map: {
defaultEventsRange: 'lastWeekEvents',
coordinates: [58.651, 142.395],
id: 'map-mainpage',
object: null
}
}
},
methods: {
createMap: function()
{
this.map.object = createMap(this.map.id, this.map.coordinates, {
addToggleShowBuildings: this.$store.getters.user.authenticated,
addToggleShowLDOs: this.$store.getters.user.authenticated,
gestureHandling: this.gestureHandling,
onlyStations: this.onlyStations,
markerPopupHeader: '',
showStations: false,
zoom: 4
})
if(!this.onlyStations) {
this.addMapLegend()
}
},
addMapLegend()
{
const map = this.map.object
const defaultEventsRange = this.map.defaultEventsRange
let $moment = this.$moment
let $http = this.$http
let legend = window.L.control({position: 'bottomright'})
let stateLabel = window.L.DomUtil.create('p')
let markers = []
let addEvents = function(events)
{
markers.forEach(marker => map.removeLayer(marker))
markers = []
events.reverse().forEach(event => {
let marker = createMapEventMarker(event, $moment)
markers.push(marker)
marker.addTo(map)
})
}
let _this = this
legend.onAdd = function()
{
/** @type HTMLElement */
let btnGroup = window.L.DomUtil.create('div', 'btn-group btn-group-toggle map-legend map-legend-mainpage')
let getCheckedBtn = () => { return btnGroup.querySelector('input[type=radio]:checked') }
let eventsRangeRequests = {}
let disableBtns = function(disabled = true) {
let btns = btnGroup.querySelectorAll('input[type=radio]')
Array.prototype.forEach.call(btns, btn => { btn.disabled = disabled })
}
let setReloadTimer = (() => {
let reloadTimeout = 300 // sec
let reloadTimer
return () => {
if(reloadTimer) {
clearTimeout(reloadTimer)
}
reloadTimer = setTimeout(() => {
eventsRangeRequests[getCheckedBtn().dataset.rangeName]().then(() => {
_this.mapNotify('Данные о землетрясениях обновлены')
})
}, 1000 * reloadTimeout)
}
})()
let appendBtn = function(eventsRangeName, callBack)
{
/** @type HTMLElement */
let btn = window.L.DomUtil.create('label', 'btn btn-sm btn-default')
let eventsRange = EVENTS_RANGES[eventsRangeName]
let checked = (eventsRangeName === defaultEventsRange)
let changeHandler = function() {
stateLabel.innerText = eventsRange.title
callBack()
}
btn.innerHTML =
`<input type="radio" name="__map_report__" ${checked ? 'checked' : ''} />
<span>${eventsRange.label}</span>`
/** @type HTMLInputElement */
let radio = btn.querySelector('input[type=radio]')
radio.addEventListener('change', changeHandler, false)
if (checked) changeHandler()
radio.setAttribute('data-range-name', eventsRangeName)
btn.style.backgroundColor = eventsRange.color
btn.setAttribute('title', eventsRange.title)
btnGroup.appendChild(btn)
}
Object.keys(EVENTS_RANGES).forEach(eventsRangeName => {
let eventsRange = EVENTS_RANGES[eventsRangeName]
let minDateSubtract = eventsRange.minDateSubtract
let request = function()
{
let minDate = $moment.utc().subtract(minDateSubtract[0], minDateSubtract[1])
disableBtns()
return $http.get(apiSettings.endpointEventsLight, {
params: {
datetime_min: minDate.format('YYYY-MM-DD HH:mm:ss'),
limit: eventsRange.limit || 1000
}
})
.then(response => {
addEvents(response.data.data)
disableBtns(false)
setReloadTimer()
})
}
eventsRangeRequests[eventsRangeName] = request
appendBtn(eventsRangeName, () => {
map.spin(true)
request().then(() => {
map.spin(false)
})
})
})
let checkedBtn = getCheckedBtn()
if (!checkedBtn) {
checkedBtn = btnGroup.querySelector('input[type=radio]:first-child')
checkedBtn.checked = true
checkedBtn.dispatchEvent(new Event('change'))
}
return btnGroup
}
legend.addTo(map)
let text = window.L.control({position: 'bottomright'})
text.onAdd = function() {
let div = window.L.DomUtil.create('div', 'map-text')
div.appendChild(stateLabel)
return div
}
text.addTo(map)
},
mapNotify: function(msg, delay = 3500)
{
let mapElem = document.getElementById(this.map.id)
let mapPopup = document.createElement('div')
let removeTimer
mapPopup.setAttribute('class', 'map-notify')
mapPopup.innerText = msg
mapPopup.addEventListener('transitionend', e => {
if(removeTimer) {
clearTimeout(removeTimer)
}
removeTimer = setTimeout(() => {
if(!mapPopup.classList.contains('show')) {
mapElem.removeChild(mapPopup)
}
}, 100)
}, false)
mapElem.appendChild(mapPopup)
setTimeout(() => {
mapPopup.classList.add('show')
setTimeout(() => {
mapPopup.classList.remove('show')
}, delay)
}, 100)
}
},
mounted() {
this.createMap()
}
}
</script>
<style lang="scss">
@import '~scss/event_map';
.map
{
.map-legend-mainpage {
padding: 0 !important;
}
> .map-notify
{
box-shadow: 0 2px 10px 1px rgba(0,0,0,0.25);
text-shadow: 0 -1px 0 rgba(0,0,0,0.2);
transition: all 250ms ease-out;
color: rgba(255,255,255,0.8);
background-color: cadetblue;
transform: translateX(-50%);
border-radius: 3px;
padding: 10px 20px;
position: absolute;
z-index: 9999999;
font-size: 16px;
opacity: 0;
left: 50%;
top: 0;
&.show {
transition: all 350ms ease-out;
opacity: 1;
top: 20px;
}
}
}
</style>