Skip to content

Commit 9226b95

Browse files
committed
- Change rollup to produce two targets: mapml-viewer.js and web-map.js
- Refactor index.js, create index-web-map.js as separate start point for web-map.js build / rollup target - All .js files are bundled into the main targets. To Do: tree-shaking? - Move import statements into .js files that depend on the import. - Remove most symbols from the M global variable, leaving TCRS mostly - Create and import GlobalM.js. Rename options.js->DefaultMapOptions.js - pmtilesRules.js now does not require M.protomapsL usage, just use protomapsL import symbols directly (will affect experiment). - Update tests to keep them running / not flaky if possible
1 parent 7e351d1 commit 9226b95

38 files changed

+1121
-1180
lines changed

Gruntfile.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,8 @@ module.exports = function(grunt) {
2323
},
2424
dist: {
2525
files: {
26-
'dist/mapml.js': ['<%= rollup.main.dest %>'],
27-
'dist/web-map.js': ['src/web-map.js'],
28-
'dist/mapml-viewer.js': ['src/mapml-viewer.js'],
29-
'dist/map-caption.js': ['src/map-caption.js'],
30-
'dist/map-feature.js': ['src/map-feature.js'],
31-
'dist/map-extent.js': ['src/map-extent.js'],
32-
'dist/map-input.js': ['src/map-input.js'],
33-
'dist/map-select.js': ['src/map-select.js'],
34-
'dist/map-link.js': ['src/map-link.js'],
35-
'dist/map-style.js': ['src/map-style.js'],
36-
'dist/map-area.js': ['src/map-area.js'],
37-
'dist/layer.js': ['src/layer.js'],
38-
'dist/leaflet.js': ['dist/leaflet-src.js',
39-
'dist/proj4-src.js',
40-
'dist/proj4leaflet.js',
41-
'dist/L.Control.Locate.js']
26+
'dist/mapml-viewer.js': ['<%= rollup.main.dest %>'],
27+
'dist/web-map.js': ['<%= rollup.webmap.dest %>']
4228
}
4329
}
4430
},
@@ -187,7 +173,7 @@ module.exports = function(grunt) {
187173
},
188174
clean: {
189175
dist: ['dist'],
190-
tidyup: ['dist/leaflet-src.js','dist/proj4-src.js','dist/proj4leaflet.js','dist/L.Control.Locate.js'],
176+
tidyup: ['dist/leaflet-src.js','dist/proj4-src.js','dist/proj4leaflet.js','dist/L.Control.Locate.js','dist/mapmlviewer.js','dist/webmap.js'],
191177
experiments: {
192178
options: {force: true},
193179
src: ['../experiments/dist']
@@ -207,13 +193,17 @@ module.exports = function(grunt) {
207193
},
208194
rollup: {
209195
options: {
210-
format: 'iife',
196+
format: 'es',
211197
plugins: [nodeResolve()],
212198
external: './pmtilesRules.js'
213199
},
214200
main: {
215-
dest: 'dist/mapml.js',
201+
dest: 'dist/mapmlviewer.js',
216202
src: 'src/mapml/index.js' // Only one source file is permitted
203+
},
204+
webmap: {
205+
dest: 'dist/webmap.js',
206+
src: 'src/mapml/index-web-map.js' // Only one source file is permitted
217207
}
218208
},
219209
prettier: {

src/layer.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import './leaflet.js'; // a lightly modified version of Leaflet for use as browser module
2-
import './mapml.js'; // modified URI to make the function a property of window scope (possibly a bad thing to do).
1+
import { Util } from './mapml/utils/Util';
2+
import { MapMLLayer, mapMLLayer } from './mapml/layers/MapMLLayer';
3+
import { createLayerControlHTML } from './mapml/elementSupport/layers/createLayerControlForLayer';
34

45
export class MapLayer extends HTMLElement {
56
static get observedAttributes() {
@@ -67,7 +68,7 @@ export class MapLayer extends HTMLElement {
6768
}
6869
return this._layer
6970
? Object.assign(
70-
M._convertAndFormatPCRS(
71+
Util._convertAndFormatPCRS(
7172
this._layer.bounds,
7273
M[this.getProjection()],
7374
this.getProjection()
@@ -165,7 +166,7 @@ export class MapLayer extends HTMLElement {
165166
/* jshint ignore:start */
166167
this.#hasConnected = true;
167168
/* jshint ignore:end */
168-
this._createLayerControlHTML = M._createLayerControlHTML.bind(this);
169+
this._createLayerControlHTML = createLayerControlHTML.bind(this);
169170
const doConnected = this._onAdd.bind(this);
170171
const doRemove = this._onRemove.bind(this);
171172
this.parentElement
@@ -241,7 +242,7 @@ export class MapLayer extends HTMLElement {
241242
this.checkForPreferredContent();
242243
})
243244
.then(() => {
244-
this._layer = M.mapMLLayer(new URL(this.src, base).href, this, {
245+
this._layer = mapMLLayer(new URL(this.src, base).href, this, {
245246
projection: this.getProjection(),
246247
opacity: this.opacity
247248
});
@@ -278,7 +279,7 @@ export class MapLayer extends HTMLElement {
278279
this.checkForPreferredContent();
279280
})
280281
.then(() => {
281-
this._layer = M.mapMLLayer(null, this, {
282+
this._layer = mapMLLayer(null, this, {
282283
projection: this.getProjection(),
283284
opacity: this.opacity
284285
});
@@ -398,7 +399,7 @@ export class MapLayer extends HTMLElement {
398399
let projection = this.parentElement.projection;
399400
if (mapml.querySelector('map-meta[name=projection][content]')) {
400401
projection =
401-
M._metaContentToObject(
402+
Util._metaContentToObject(
402403
mapml
403404
.querySelector('map-meta[name=projection]')
404405
.getAttribute('content')
@@ -793,13 +794,13 @@ export class MapLayer extends HTMLElement {
793794

794795
let maxZoom = extent.zoom.maxZoom,
795796
minZoom = extent.zoom.minZoom;
796-
map.setView(center, M.getMaxZoom(layerBounds, map, minZoom, maxZoom), {
797+
map.setView(center, Util.getMaxZoom(layerBounds, map, minZoom, maxZoom), {
797798
animate: false
798799
});
799800
});
800801
}
801802
mapml2geojson(options = {}) {
802-
return M.mapml2geojson(this, options);
803+
return Util.mapml2geojson(this, options);
803804
}
804805
pasteFeature(feature) {
805806
switch (typeof feature) {

src/map-area.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import './leaflet.js'; // a lightly modified version of Leaflet for use as browser module
2-
import './mapml.js'; // refactored URI usage, replaced with URL standard
3-
41
export class MapArea extends HTMLAreaElement {
52
static get observedAttributes() {
63
return ['coords', 'alt', 'href', 'shape', 'rel', 'type', 'target'];

src/map-extent.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { Util } from './mapml/utils/Util';
2+
import { extentLayer } from './mapml/layers/ExtentLayer';
3+
import { createLayerControlExtentHTML } from './mapml/elementSupport/extents/createLayerControlForExtent';
4+
15
/* global M */
26
export class MapExtent extends HTMLElement {
37
static get observedAttributes() {
@@ -54,7 +58,7 @@ export class MapExtent extends HTMLElement {
5458
get extent() {
5559
const getExtent = (extent) => {
5660
return Object.assign(
57-
M._convertAndFormatPCRS(
61+
Util._convertAndFormatPCRS(
5862
extent._extentLayer.bounds,
5963
M[extent.units],
6064
extent.units
@@ -121,16 +125,16 @@ export class MapExtent extends HTMLElement {
121125
center = map.options.crs.unproject(bounds.getCenter(true)),
122126
maxZoom = extent.zoom.maxZoom,
123127
minZoom = extent.zoom.minZoom;
124-
map.setView(center, M.getMaxZoom(bounds, map, minZoom, maxZoom), {
128+
map.setView(center, Util.getMaxZoom(bounds, map, minZoom, maxZoom), {
125129
animate: false
126130
});
127131
}
128132

129133
getMapEl() {
130-
return M.getClosest(this, 'mapml-viewer,map[is=web-map]');
134+
return Util.getClosest(this, 'mapml-viewer,map[is=web-map]');
131135
}
132136
getLayerEl() {
133-
return M.getClosest(this, 'layer-');
137+
return Util.getClosest(this, 'layer-');
134138
}
135139
attributeChangedCallback(name, oldValue, newValue) {
136140
if (this.#hasConnected /* jshint ignore:line */) {
@@ -226,7 +230,7 @@ export class MapExtent extends HTMLElement {
226230
// Always call super first in constructor
227231
super();
228232
this._createLayerControlExtentHTML =
229-
M._createLayerControlExtentHTML.bind(this);
233+
createLayerControlExtentHTML.bind(this);
230234
this._changeHandler = this._handleChange.bind(this);
231235
}
232236
async connectedCallback() {
@@ -257,7 +261,7 @@ export class MapExtent extends HTMLElement {
257261
// this._opacity is used to record the current opacity value (with or without updates),
258262
// the initial value of this._opacity should be set as opacity attribute value, if exists, or the default value 1.0
259263
this._opacity = this.opacity || 1.0;
260-
this._extentLayer = M.extentLayer({
264+
this._extentLayer = extentLayer({
261265
opacity: this.opacity,
262266
crs: M[this.units],
263267
extentZIndex: Array.from(
@@ -493,12 +497,12 @@ export class MapExtent extends HTMLElement {
493497

494498
// initialize bounds from this.scope > map-meta
495499
let bounds = this.querySelector(':scope > map-meta[name=extent][content]')
496-
? M.getBoundsFromMeta(this) // TODO rewrite this pile of doo doo
500+
? Util.getBoundsFromMeta(this) // TODO rewrite this pile of doo doo
497501
: undefined;
498502

499503
// initialize zoom bounds from this.scope > map-meta
500504
let zoomBounds = this.querySelector(':scope > map-meta[name=zoom][content]')
501-
? M.getZoomBoundsFromMeta(this) // TODO rewrite this pile of doo doo
505+
? Util.getZoomBoundsFromMeta(this) // TODO rewrite this pile of doo doo
502506
: undefined;
503507

504508
// bounds should be able to be calculated unconditionally, not depend on map-extent.checked

src/map-feature.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Util } from './mapml/utils/Util';
2+
13
export class MapFeature extends HTMLElement {
24
static get observedAttributes() {
35
return ['zoom', 'min', 'max'];
@@ -10,7 +12,8 @@ export class MapFeature extends HTMLElement {
1012
// for templated or queried features ** native zoom is only used for zoomTo() **
1113
let meta = {},
1214
metaEl = this.getMeta('zoom');
13-
if (metaEl) meta = M._metaContentToObject(metaEl.getAttribute('content'));
15+
if (metaEl)
16+
meta = Util._metaContentToObject(metaEl.getAttribute('content'));
1417
if (this._parentEl.nodeName === 'MAP-LINK') {
1518
// nativeZoom = zoom attribute || (sd.map-meta zoom 'value' || 'max') || this._initialZoom
1619
return +(this.hasAttribute('zoom')
@@ -43,7 +46,8 @@ export class MapFeature extends HTMLElement {
4346
// for templated or queried features ** native zoom is only used for zoomTo() **
4447
let meta = {},
4548
metaEl = this.getMeta('zoom');
46-
if (metaEl) meta = M._metaContentToObject(metaEl.getAttribute('content'));
49+
if (metaEl)
50+
meta = Util._metaContentToObject(metaEl.getAttribute('content'));
4751
let projectionMinZoom = 0;
4852
if (this._parentEl.nodeName === 'MAP-LINK') {
4953
// minZoom = min attribute || sd.map-meta min zoom || map-link minZoom
@@ -82,7 +86,8 @@ export class MapFeature extends HTMLElement {
8286
// for templated or queried features ** native zoom is only used for zoomTo() **
8387
let meta = {},
8488
metaEl = this.getMeta('zoom');
85-
if (metaEl) meta = M._metaContentToObject(metaEl.getAttribute('content'));
89+
if (metaEl)
90+
meta = Util._metaContentToObject(metaEl.getAttribute('content'));
8691
let projectionMaxZoom =
8792
this.getMapEl()._map.options.crs.options.resolutions.length - 1;
8893
if (this._parentEl.nodeName === 'MAP-LINK') {
@@ -129,10 +134,10 @@ export class MapFeature extends HTMLElement {
129134
}
130135
}
131136
getMapEl() {
132-
return M.getClosest(this, 'mapml-viewer,map[is=web-map]');
137+
return Util.getClosest(this, 'mapml-viewer,map[is=web-map]');
133138
}
134139
getLayerEl() {
135-
return M.getClosest(this, 'layer-');
140+
return Util.getClosest(this, 'layer-');
136141
}
137142

138143
attributeChangedCallback(name, oldValue, newValue) {
@@ -231,7 +236,7 @@ export class MapFeature extends HTMLElement {
231236
// layerToRemoveFrom is the L.LayerGroup or L.FeatureGroup to remove this
232237
// feature from...
233238
layerToRemoveFrom.removeLayer(this._geometry);
234-
// TODO: MOVE THIS LOGIC TO layerToRemoveFrom.removeLayer(M.Geometry)
239+
// TODO: MOVE THIS LOGIC TO layerToRemoveFrom.removeLayer(Geometry)
235240
// if (layerToRemoveFrom._staticFeature) {
236241
// if (layerToRemoveFrom._features[this.zoom]) {
237242
// this._removeInFeatureList(this.zoom);
@@ -302,7 +307,7 @@ export class MapFeature extends HTMLElement {
302307
// the fallback 'gcrs' SHOULD be specified by the MapML spec
303308
// per https://github.com/Maps4HTML/MapML/issues/257
304309
return csMeta
305-
? M._metaContentToObject(csMeta.getAttribute('content')).content
310+
? Util._metaContentToObject(csMeta.getAttribute('content')).content
306311
: 'gcrs';
307312
}
308313

@@ -341,7 +346,7 @@ export class MapFeature extends HTMLElement {
341346
}
342347
let topLeft = L.point(bboxExtent[0], bboxExtent[1]);
343348
let bottomRight = L.point(bboxExtent[2], bboxExtent[3]);
344-
let pcrsBound = M.boundsToPCRSBounds(
349+
let pcrsBound = Util.boundsToPCRSBounds(
345350
L.bounds(topLeft, bottomRight),
346351
zoom,
347352
map.options.projection,
@@ -360,14 +365,14 @@ export class MapFeature extends HTMLElement {
360365
pcrsBound.min,
361366
M[projection].scale(+this.zoom || maxZoom)
362367
);
363-
pcrsBound = M.pixelToPCRSBounds(
368+
pcrsBound = Util.pixelToPCRSBounds(
364369
L.bounds(pixel.subtract(tileCenter), pixel.add(tileCenter)),
365370
this.zoom || maxZoom,
366371
projection
367372
);
368373
}
369374
let result = Object.assign(
370-
M._convertAndFormatPCRS(
375+
Util._convertAndFormatPCRS(
371376
pcrsBound,
372377
map.options.crs,
373378
map.options.projection
@@ -389,14 +394,14 @@ export class MapFeature extends HTMLElement {
389394
.split(/[<>\ ]/g);
390395
switch (shape.tagName.toUpperCase()) {
391396
case 'MAP-POINT':
392-
bboxExtent = M._updateExtent(bboxExtent, +data[0], +data[1]);
397+
bboxExtent = Util._updateExtent(bboxExtent, +data[0], +data[1]);
393398
break;
394399
case 'MAP-LINESTRING':
395400
case 'MAP-POLYGON':
396401
case 'MAP-MULTIPOINT':
397402
case 'MAP-MULTILINESTRING':
398403
for (let i = 0; i < data.length; i += 2) {
399-
bboxExtent = M._updateExtent(bboxExtent, +data[i], +data[i + 1]);
404+
bboxExtent = Util._updateExtent(bboxExtent, +data[i], +data[i + 1]);
400405
}
401406
break;
402407
default:
@@ -433,7 +438,7 @@ export class MapFeature extends HTMLElement {
433438
newZoom = this.zoom;
434439
} else {
435440
// if not, calculate the maximum zoom level that can show the feature completely
436-
newZoom = M.getMaxZoom(bound, this.getMapEl()._map, minZoom, maxZoom);
441+
newZoom = Util.getMaxZoom(bound, this.getMapEl()._map, minZoom, maxZoom);
437442
if (this.max < newZoom) {
438443
// if the calculated zoom is greater than the value of max zoom attribute, go with max zoom attribute
439444
newZoom = this.max;
@@ -495,7 +500,7 @@ export class MapFeature extends HTMLElement {
495500
} else if (el.querySelector('table')) {
496501
// setting properties when table presented
497502
let table = el.querySelector('table').cloneNode(true);
498-
json.properties = M._table2properties(table);
503+
json.properties = Util._table2properties(table);
499504
} else {
500505
// when no table present, strip any possible html tags to only get text
501506
json.properties = {
@@ -530,11 +535,11 @@ export class MapFeature extends HTMLElement {
530535
json.geometry.geometries = [];
531536
for (let shape of shapes) {
532537
json.geometry.geometries.push(
533-
M._geometry2geojson(shape, source, dest, options.transform)
538+
Util._geometry2geojson(shape, source, dest, options.transform)
534539
);
535540
}
536541
} else {
537-
json.geometry = M._geometry2geojson(
542+
json.geometry = Util._geometry2geojson(
538543
shapes[0],
539544
source,
540545
dest,

0 commit comments

Comments
 (0)