-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapworkbox.mjs
168 lines (155 loc) · 7.46 KB
/
mapworkbox.mjs
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
/*jshint esversion: 6 */
import { Workbox } from 'https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-window.prod.mjs';
const jsimport = (url) => {
// to allow loading nodejs modules
window.module = window.module || window;
return new Promise((resolve, reject) => {
let s = document.createElement('script');
s.type = 'text/javascript';
s.src = url;
s.defer = true;
s.addEventListener('error', () => reject(s), false);
s.addEventListener('load', () => resolve(s), false);
document.head.appendChild(s);
});
};
mapboxgl.mwb = {};
mapboxgl.mwb.init = (map, layers) => {
if ('serviceWorker' in navigator) {
jsimport('https://cdn.jsdelivr.net/npm/@mapbox/tilebelt@1.0.1/index.min.js')
.then(s => {
const
caller = function () {
try {
let e = new Error;
throw e;
} catch (err) {
let
sep = (typeof InstallTrigger !== 'undefined') ? '@' : 'at ',
i;
let a = err.stack.split('\n').filter(e => e.indexOf(sep) > -1)[1],
b = a.substring(a.indexOf('(') + 1, a.lastIndexOf(')'));
b = (b == '') ? b = a.replace(sep, '') : b;
return b.substring(0, b.lastIndexOf('/') + 1).trim();
}
},
precacher = e => {
const
m = e.target,
z = m.getZoom() | 0,
bb = m.getBounds(),
bba = [bb._sw.lng, bb._sw.lat, bb._ne.lng, bb._ne.lat],
bbt = bboxToTile(bba),
isVisible = (t, b) => {
const
tbb = tileToBBOX(t),
tf = [
{ x: tbb[0], y: tbb[1] },
{ x: tbb[0], y: tbb[3] },
{ x: tbb[2], y: tbb[1] },
{ x: tbb[2], y: tbb[3] }
],
bf = [
{ x: b[0], y: b[1] },
{ x: b[0], y: b[3] },
{ x: b[2], y: b[1] },
{ x: b[2], y: b[3] }
],
contains = (box, p) => {
if (p.x < box[0] || p.x > box[2] || p.y < box[1] || p.y > box[3]) {
return false;
} else {
return true;
}
};
for (let i = 0; i < 4; i++) {
if (contains(bba, tf[i])) {
return true;
break;
}
}
for (let i = 0; i < 4; i++) {
if (contains(tbb, bf[i])) {
return true;
break;
}
}
return false;
},
getParentZ = (t, z) => {
if (t[2] == z) {
return [getParent(t)];
} else if (t[2] == z - 1) {
return [t];
} else {
let
tt = [t];
for (let i = t[2]; i < z; i++) {
tt.filter(a => a[2] == i && isVisible(a, bba)).forEach(a => tt.push(...getChildren(a)));
}
return tt.filter(a => a[2] == z - 1);
}
},
getChildrenZ = (t, z) => {
let
tt = [t];
for (let i = t[2]; i < z + 1; i++) {
tt.filter(a => a[2] == i && isVisible(a, bba)).forEach(a => tt.push(...getChildren(a)));
}
return tt.filter(a => a[2] == z + 1);
},
tile2url = (t, url) => {
},
tocache = [...getParentZ(bbt, z), ...getChildrenZ(bbt, z)],
urlsToCache = [];
mapboxgl.mwb.layers.forEach(l => {
let
c = tocache
.filter(t => t[2] >= l.minzoom && t[2] <= l.maxzoom)
.map(t => {
let u = l.url;
urlsToCache.push(u.replace('{x}', t[0])
.replace('{y}', t[1])
.replace('{z}', t[2]));
});
});
mwb.messageSW({
type: 'CACHE_URLS',
payload: { urlsToCache },
});
},
path = caller() + 'mwb.js',
mwb = new Workbox(path);
mapboxgl.mwb.layers = layers.map(l => {
const source = map.getLayer(l.name).source;
l.source = source;
l.url = map.getSource(source).tiles[0];
l.minzoom = l.minzoom || map.getSource(source).minzoom;
l.maxzoom = l.maxzoom || map.getSource(source).maxzoom;
return l;
});
mwb.addEventListener('activated', e => {
console.log('mwb activated');
});
map.on('moveend', e => {
//#region benchmark
console.log('Start');
console.timeEnd('benchmark');
console.time('benchmark');
if (mapboxgl.mwb.layers.reduce((acc, l) => acc && map.isSourceLoaded(l.source), true)){
console.timeEnd('benchmark');
console.log('End-inmediate');
}else{
map.once('sourcedata', e=>{
console.timeEnd('benchmark');
console.log('End');
});
}
//#endregion
precacher(e);
});
mwb.register();
precacher({target:map});
});
}
};