-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.js
153 lines (121 loc) · 4.68 KB
/
widget.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
class leafletMapWidget {
constructor(wgs84) {
this.WGS84 = wgs84;
this.vGrassCuttingSchedule = null;
}
showMap() {
var bSuccess = false;
var map = null;
try
{
if ((this.WGS84.latitude != 0.0) && (this.WGS84.longitude != 0.0)) {
map = L.map('widgetmap').setView([this.WGS84.latitude, this.WGS84.longitude], 15);
bSuccess = true;
}
}
catch (err) {}
if (!bSuccess) {
map = L.map('widgetmap').setView([53.213, -2.902], 13);
}
var _this = this;
// load a tile layer
var vMapBox = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: '',
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken: 'pk.eyJ1IjoiamR1bmNhbGYiLCJhIjoiY2wxMHJxMm55MDFiZDNjbnM3cGV3YzF3dyJ9.JwiikZcJsfBr2ebIytXcxw'
}).addTo(map);
var vGrassCutting = null;
var vGreenStyle = {
"color": "#00ff00"
};
$.ajax({
url: 'https://fs-filestore-eu.s3.eu-west-1.amazonaws.com/qwest/assets/GeoJSONTest/GrassCutting.geojson',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
type: "GET",
dataType: "json",
async: true,
success: function (result) {
vGrassCutting = L.geoJson(result, {
style: vGreenStyle
})
.bindPopup(function (layer) {
var vPopupText = layer.feature.properties['Feature_ID'] + " : Missing schedule data.";
result = _this.vGrassCuttingSchedule.filter(obj => obj['feature_id'] == layer.feature.properties['Feature_ID']);
var today = new Date();
var date = today.getFullYear()+'/'+(today.getMonth()+1)+'/'+today.getDate();
var nowDate = Date.parse(today);
$.each(result, function(index, value) {
var parts = value["date"].split("/");
var dt = new Date(
parseInt(parts[2], 10),
parseInt(parts[1], 10) - 1,
parseInt(parts[0], 10)
);
let vDateCheck = Date.parse(dt);
if (vDateCheck >= nowDate) {
vPopupText = "Next scheduled cutting is on the " + value["date"];
return false;
}
});
return vPopupText;
});
var baseLayers = {
"Mapbox": vMapBox
};
var overlays = {
"Grass Cutting": vGrassCutting
};
map.addLayer(vGrassCutting);
L.control.layers(baseLayers, overlays).addTo(map);
},
error: function () {
console.log("error");
}
});
}
loadCSVData() {
var _this = this;
$.ajax({
url: 'https://fs-filestore-eu.s3.eu-west-1.amazonaws.com/qwest/assets/GeoJSONTest/GrassCuttingSchedule.csv',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
type: "GET",
dataType: "text",
async: true,
success: function (result) {
_this.vGrassCuttingSchedule = csvJSON(result);
_this.vGrassCuttingSchedule = sortByKey(_this.vGrassCuttingSchedule, "feature_id");
},
error: function () {
console.log("error");
}
});
}
}
function csvJSON(csv) {
var lines=csv.split("\r\n");
var result = [];
var headers=lines[0].split(",");
for(var i=1;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split(",");
for(var j=0;j<headers.length;j++){
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
return result;
}
function sortByKey(array, key) {
return array.sort((a, b) => {
let x = a[key];
let y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}