-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmap-style.js
More file actions
145 lines (138 loc) · 4.61 KB
/
Copy pathmap-style.js
File metadata and controls
145 lines (138 loc) · 4.61 KB
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
import { Util } from './mapml/utils/Util.js';
export class HTMLStyleElement extends HTMLElement {
static get observedAttributes() {
return ['media'];
}
/* jshint ignore:start */
#hasConnected;
/* jshint ignore:end */
get media() {
return this.getAttribute('media');
}
set media(val) {
this.setAttribute('media', val);
}
async attributeChangedCallback(name, oldValue, newValue) {
if (this.#hasConnected /* jshint ignore:line */) {
switch (name) {
case 'media':
if (oldValue !== newValue) {
await this._registerMediaQuery(newValue);
}
break;
}
}
}
async _registerMediaQuery(mq) {
if (!this._changeHandler) {
// Define and bind the change handler once
this._changeHandler = () => {
this._disconnect();
if (this._mql.matches) {
this._connect();
}
};
}
if (mq) {
let map = this.getMapEl();
if (!map) return;
// have to wait until map has an extent i.e. is ready, because the
// matchMedia function below relies on it for map related queries
await map.whenReady();
// Remove listener from the old media query (if it exists)
if (this._mql) {
this._mql.removeEventListener('change', this._changeHandler);
}
// Set up the new media query and listener
this._mql = map.matchMedia(mq);
this._changeHandler(); // Initial evaluation
this._mql.addEventListener('change', this._changeHandler);
} else if (this._mql) {
// Clean up the existing listener
this._mql.removeEventListener('change', this._changeHandler);
delete this._mql;
this._disconnect();
this._connect();
}
}
getMapEl() {
return Util.getClosest(this, 'mapml-viewer,map[is=web-map]');
}
constructor() {
// Always call super first in constructor
super();
}
_connect() {
this.styleElement = document.createElement('style');
this.styleElement.mapStyle = this;
this.styleElement.textContent = this.textContent;
copyAttributes(this, this.styleElement);
// IMPORTANT: Only render if the correct container exists for THIS element's parent.
// Don't fall back to _extentLayer if this is a layer child - wait for _layer instead.
const isLayerChild = this._stylesheetHost.tagName === 'MAP-LAYER';
const isExtentChild = this._stylesheetHost.tagName === 'MAP-EXTENT';
if (isLayerChild && this._stylesheetHost._layer) {
this._stylesheetHost._layer.renderStyles(this);
} else if (isExtentChild && this._stylesheetHost._extentLayer) {
this._stylesheetHost._extentLayer.renderStyles(this);
} else if (this._stylesheetHost._templatedLayer) {
this._stylesheetHost._templatedLayer.renderStyles(this);
}
function copyAttributes(source, target) {
return Array.from(source.attributes).forEach((attribute) => {
if (
attribute.nodeName !== 'media' &&
attribute.nodeName !== 'data-testid'
) {
target.setAttribute(attribute.nodeName, attribute.nodeValue);
}
});
}
// use observer to monitor the changes in mapStyle textContent
this._observer = new MutationObserver(() => {
this.styleElement.textContent = this.textContent;
});
this._observer.observe(this, {
childList: true,
subtree: true,
characterData: true
});
}
_disconnect() {
if (this._observer) {
this._observer.disconnect();
}
if (this._stylesheetHost) {
if (this.styleElement) {
this.styleElement.remove();
delete this.styleElement;
}
}
}
async connectedCallback() {
/* jshint ignore:start */
this.#hasConnected = true;
/* jshint ignore:end */
// if the parent element is a map-link, the stylesheet should
// be created as part of a templated layer processing i.e. on moveend / when connected
// and the generated <style> that implements this <map-style> should be located
// in the parent <map-link>._templatedLayer.container root node if
// the _templatedLayer is an instance of TemplatedTileLayer or TemplatedFeaturesLayer
//
// if the parent node (or the host of the shadow root parent node) is map-layer, the link should be created in the _layer
// container
this._stylesheetHost =
this.getRootNode() instanceof ShadowRoot
? this.getRootNode().host
: this.parentElement;
if (this._stylesheetHost === undefined) return;
if (this.media) {
await this._registerMediaQuery(this.media);
} else {
this._connect();
}
}
disconnectedCallback() {
this._disconnect();
}
}