MCPcopy Index your code
hub / github.com/Maps4HTML/MapML.js / _onAdd

Method _onAdd

src/layer.js:266–401  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

264 }
265
266 _onAdd() {
267 new Promise((resolve, reject) => {
268 this.addEventListener(
269 'changestyle',
270 function (e) {
271 e.stopPropagation();
272 // if user changes the style in layer control
273 if (e.detail) {
274 this.src = e.detail.src;
275 }
276 },
277 { once: true }
278 );
279 let base = this.baseURI ? this.baseURI : document.baseURI;
280 const headers = new Headers();
281 headers.append('Accept', 'text/mapml');
282 if (this.src) {
283 fetch(this.src, { headers: headers })
284 .then((response) => {
285 if (!response.ok) {
286 throw new Error(`HTTP error! Status: ${response.status}`);
287 }
288 return response.text();
289 })
290 .then((mapml) => {
291 let content = new DOMParser().parseFromString(mapml, 'text/xml');
292 if (
293 content.querySelector('parsererror') ||
294 !content.querySelector('mapml-')
295 ) {
296 // cut short whenReady with the _fetchError property
297 this._fetchError = true;
298 console.log('Error fetching layer content:\n\n' + mapml + '\n');
299 throw new Error('Parser error');
300 }
301 return content;
302 })
303 .then((content) => {
304 this.copyRemoteContentToShadowRoot(content.querySelector('mapml-'));
305 let elements = this.shadowRoot.querySelectorAll('*');
306 let elementsReady = [];
307 for (let i = 0; i < elements.length; i++) {
308 if (elements[i].whenReady)
309 elementsReady.push(elements[i].whenReady());
310 }
311 return Promise.allSettled(elementsReady);
312 })
313 .then(() => {
314 // may throw:
315 this.selectAlternateOrChangeProjection();
316 })
317 .then(() => {
318 this._layer = mapLayer(new URL(this.src, base).href, this, {
319 projection: this.getProjection(),
320 opacity: this.opacity
321 });
322 this._createLayerControlHTML();
323 this._setLocalizedDefaultLabel();

Callers 3

_registerMediaQueryMethod · 0.95

Calls 10

getProjectionMethod · 0.95
_attachedToMapMethod · 0.95
_runMutationObserverMethod · 0.95
_bindMutationObserverMethod · 0.95
_validateDisabledMethod · 0.95
mapLayerFunction · 0.90
whenReadyMethod · 0.45

Tested by

no test coverage detected