-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
212 lines (186 loc) · 6.92 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
mapboxgl.accessToken = 'pk.eyJ1Ijoic2F2ZWxldmdlbyIsImEiOiJjbDEzc3kwcGUwMHBtM2NwZHcxMWtzejMxIn0.j4Y_L3ZDgghN6MfthNOYRg'
document.addEventListener('DOMContentLoaded', () => {
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/savelevgeo/cl18ediq6002g14qghfltfp29',
center: [55, 55],
zoom: 3
});
// disable map rotation using right click + drag
map.dragRotate.disable();
// disable map rotation using touch rotation gesture
map.touchZoomRotate.disableRotation();
//https://stackoverflow.com/a/326076/13348813
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
};
// Checking if webpage is embedded
if (inIframe) {
// The page is in an iFrame
map.scrollZoom.disable();
map.addControl(new mapboxgl.NavigationControl({showCompass: false}));
}
const gDocLink = 'https://docs.google.com/spreadsheets/d/183Rw_ES98k4C2_0VyYfx8XW52ECW5wKIRy7KnRZ74k8/gviz/tq?tqx=out:csv&sheet=Лист1';
fetch(gDocLink)
.then(csvResponse => csvResponse.text())
.then(csvData => addToMap(csvData));
function addToMap(csvText) {
csv2geojson.csv2geojson(csvText, {
latfield: 'lat',
lonfield: 'lon',
delimiter: ','
},
(err, data) => {
map.addSource('vacancies', {
type: 'geojson',
data: data,
cluster: true,
clusterRadius: 20,
attribution: 'by <b><a href="https://github.com/SavelevGeo">SavelevGeo</a></b> | <b><a href="https://cartetika.ru/geovacancy">Страница геовакансий</a></b>'
});
//clustered_points
map.addLayer({
id: 'clusters',
source: 'vacancies',
type: 'circle',
filter: ['has', 'point_count'],
paint: {
'circle-color': '#fae67f',
'circle-stroke-width': 1,
'circle-stroke-color': '#3b3334',
'circle-radius': [
'step',
['get', 'point_count'],
15,
5,
20,
10,
25
]
}
});
//unclustered points
map.addLayer({
id: 'unclustered',
source: 'vacancies',
type: 'circle',
filter: ['!', ['has', 'point_count']],
paint: {
'circle-color': '#fae67f',
'circle-stroke-width': 1,
'circle-stroke-color': '#3b3334',
'circle-radius': 10
}
});
//location labels for unclustered
map.addLayer({
id: 'location-labels',
type: 'symbol',
source: 'vacancies',
filter: ['!', ['has', 'point_count']],
layout: {
'text-allow-overlap': true,
'text-anchor': 'left',
'text-field': '{Город}',
'text-font': ['Roboto Light', 'Arial Unicode MS Bold'],
'text-justify': 'left',
'text-offset': [1, 0],
'text-size': 12
}
});
//cluster labels
map.addLayer({
id: 'cluster-count-labels',
type: 'symbol',
source: 'vacancies',
filter: ['has', 'point_count'],
layout: {
'text-field': '{point_count}',
'text-font': ['Roboto Black', 'Arial Unicode MS Bold'],
'text-size': 12,
'text-offset': [-0.05, 0.1]
}
});
//##POPUPS##
//expanding vacancy card
function expandVac(evt) {
let vacElements = evt.target.parentNode.querySelectorAll(':not(.vacancy__header)');
vacElements.forEach((e) => e.hidden = !e.hidden);
}
//filling up vacancy data
function getVacancyDesc(feat) {
let vacElement = document.createElement('span');
vacElement.className = 'vacancy';
vacElement.style.position = 'relative';
let vacHeader = document.createElement('h3');
vacHeader.className = 'vacancy__header';
vacHeader.innerHTML = feat.properties['Вакансия'];
let vacCompany = document.createElement('div');
vacCompany.className = 'vacancy__company';
vacCompany.innerHTML = feat.properties['Работодатель'];
vacCompany.hidden = true;
let cartLogo = document.createElement('img');
cartLogo.src = './img/cartetica_logo.png';
cartLogo.width = '25';
cartLogo.hidden = true;
let cartLink = document.createElement('a');
cartLogo.className = 'cart_link';
cartLink.href = feat.properties['Ссылка на сайте Картетики'];
cartLink.appendChild(cartLogo);
cartLink.hidden = true;
let vacSalary = document.createElement('div');
vacSalary.className = 'vacancy__salary';
vacSalary.innerHTML = feat.properties['ЗП'];
vacSalary.hidden = true;
vacElement.append(vacHeader, vacCompany, vacSalary, cartLink);
return vacElement.outerHTML;
};
//popup initializing
const popup = new mapboxgl.Popup({
anchor: 'right',
className: 'popup'
});
const clusterSource = map.getSource('vacancies');
function displayPopup(e) {
//geting clicked feature and its geometry
const feat = e.features[0];
const coordinates = feat.geometry.coordinates.slice();
if (feat.properties.cluster) {
clusterSource.getClusterLeaves(
feat.properties.cluster_id,
feat.properties.point_count,
0, (error, clusteredFeatures) => {
//fill popup
popupHTML = '';
clusteredFeatures.forEach(f => popupHTML += getVacancyDesc(f));
addPopup();
});
}
else {
//create popup
popupHTML = getVacancyDesc(feat);
addPopup();
};
function addPopup() {
popup.setLngLat(coordinates).setHTML(popupHTML).addTo(map);
//event listeners for expanding vacancies data
var vacElements = document.querySelectorAll('.vacancy__header');
vacElements.forEach((e) => e.addEventListener('click', expandVac));
};
};
//hovering from cluster point event
function hidePopup() { map.getCanvas().style.cursor = ''; }
//linking hovering events to displaying/hiding popups
map.on('click', 'clusters', displayPopup);
map.on('click', 'unclustered', displayPopup);
map.on('mouseenter', 'clusters', () => map.getCanvas().style.cursor = 'pointer');
map.on('mouseenter', 'unclustered', () => map.getCanvas().style.cursor = 'pointer');
map.on('mouseleave', 'clusters', hidePopup);
map.on('mouseleave', 'unclustered', hidePopup);
});
};
});