-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
92 lines (82 loc) · 2.17 KB
/
index.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
'use strict';
import * as Util from "leaflet/src/core/Util";
import * as DomUtil from "leaflet/src/dom/DomUtil";
async function fetchImage(url, callback, headers, abort, requests) {
let _headers = {};
if (headers) {
headers.forEach(h => {
_headers[h.header] = h.value;
});
}
const controller = new AbortController();
const signal = controller.signal;
if (abort) {
abort.subscribe(() => {
controller.abort();
});
}
const request = {
url,
controller
};
requests.push(request);
fetch(url, {
method: "GET",
headers: _headers,
mode: "cors",
signal: signal
}).then(async f => {
const blob = await f.blob();
callback(blob);
});
}
L.TileLayer.WMSHeader = L.TileLayer.WMS.extend({
initialize: function (url, options, headers, abort) {
L.TileLayer.WMS.prototype.initialize.call(this, url, options);
this.headers = headers;
this.abort = abort;
this.requests = [];
},
createTile(coords, done) {
const url = this.getTileUrl(coords);
const img = document.createElement("img");
img.setAttribute("role", "presentation");
img.setAttribute("data-url", url);
fetchImage(
url,
resp => {
const reader = new FileReader();
reader.onload = () => {
img.src = reader.result;
};
reader.readAsDataURL(resp);
done(null, img);
},
this.headers,
this.abort,
this.requests
);
return img;
},
_abortLoading: function() {
for (const i in this._tiles) {
if (this._tiles[i].coords.z !== this._tileZoom) {
const tile = this._tiles[i].el;
tile.onload = Util.falseFn;
tile.onerror = Util.falseFn;
const url = tile.getAttribute("data-url");
const j = this.requests.findIndex(r => r && r.url === url);
if (j >= 0) {
this.requests[j].controller.abort();
tile.src = Util.emptyImageUrl;
DomUtil.remove(tile);
delete this._tiles[i];
delete this.requests[j];
}
}
}
}
});
L.TileLayer.wmsHeader = function (url, options, headers, abort) {
return new L.TileLayer.WMSHeader(url, options, headers, abort);
};