-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmapbox-gl-control.html
More file actions
213 lines (184 loc) · 5.21 KB
/
mapbox-gl-control.html
File metadata and controls
213 lines (184 loc) · 5.21 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/utils/flattened-nodes-observer.html">
<!--
`mapbox-gl-control` is a generic element for [mapbox controls](https://www.mapbox.com/mapbox-gl-js/api/#icontrol) which you can add to the map.
```html
<mapbox-gl
interactive
access-token="USE_UR_OWN_TOKEN">
<mapbox-gl-control
icontrol-name="NavigationControl"
icontrol-options='{"compass": true}'
position="top-right">
</mapbox-gl-control>
</mapbox-gl>
```
You can also pass in an **instance** of any mapbox controls (i.e. the IControl interface) to the `icontrol` attribute instead.
```html
<mapbox-gl
interactive
access-token="USE_UR_OWN_TOKEN">
<mapbox-gl-control
icontrol="[[someCustomIControlInstance]]"
position="top-right">
</mapbox-gl-control>
</mapbox-gl>
```
You can also create your own custom IControl by declaring it as a child of `mapbox-gl-control`. Note that you cannot style it with external stylesheets.
```html
<mapbox-gl interactive access-token="USE_UR_OWN_TOKEN">
<mapbox-gl-control interactive position="top-right">
<style>
#icontrol {
color: #fff;
text-align: center;
background-color: rgba(10,10,10,0.5);
}
</style>
<div id="icontrol">
<div>I am a custom Control.</div>
<button>Click me!</button>
</div>
</mapbox-gl-control>
</mapbox-gl>
```
@customElement
@polymer
@demo demo/controls.html Adding mapbox controls
-->
<dom-module id="mapbox-gl-control">
<template>
<style>
:host {
display: block;
}
</style>
<slot></slot>
</template>
<script>
class MapboxGlControl extends Polymer.Element {
static get is() {
return 'mapbox-gl-control';
}
static get properties() {
return {
/**
* The mapbox `map` object to add the control to.
* @type {Mapbox.map}
*/
map: {
type: Object
},
/**
* Where to position the control: `top-left` , `top-right`,
* `bottom-left`, and `bottom-right`.
* @type {String}
*/
position: {
type: String,
value: null
},
/**
* The name of the IControl (e.g. `NavigationControl`) to add to the map.
* Alternatively, you can pass in the actual reference to a custom IControl
* with the `icontrol` property.
* @type {String}
*/
icontrolName: {
type: String
},
/**
* The options to pass to the constructor for `icontrol`.
* @type {Object}
*/
icontrolOptions: {
type: Object,
value: null
},
/**
* Reference to the IControl instance to add to the map. If this is
* provided, `icontrol-name` and `icontrol-options` will be ignored.
* @type {Mapbox.IControl}
*/
icontrol: {
type: Object,
notify: true
},
/**
* Apply to custom IControl only. Whether the control can be interacted
* with.
* @type {Boolean}
*/
interactive: {
type: Boolean,
value: false
},
_container: Object,
_childrenObserver: Polymer.FlattenedNodesObserver
};
}
static get observers() {
return [
'_createIControl(map, icontrolName, icontrolOptions)',
'_addControl(map, icontrol, position)'
];
}
connectedCallback() {
super.connectedCallback();
var slot = this.shadowRoot.querySelector('slot');
this._childrenObserver = new Polymer.FlattenedNodesObserver(
slot,
this._slotChanged.bind(this)
);
}
disconnectedCallback() {
super.disconnectedCallback();
if (this._childrenObserver) {
this._childrenObserver.disconnect();
this._childrenObserver = null;
}
}
_slotChanged({ addedNodes }) {
var nodes = this.shadowRoot
.querySelector('slot')
.assignedNodes({ flatten: true });
if (nodes.length === 0 || this.icontrol) return;
var self = this;
// create custom IControl
var CustomIControl = function() {};
CustomIControl.prototype.onAdd = function(map) {
if (nodes.length == 1) {
this._container = nodes[0];
} else {
this._container = document.createElement('div');
nodes.forEach(n => this._container.appendChild(n));
}
// apply mapbox gl default control style
this._container.classList.toggle('interactive', self.interactive);
this._container.classList.toggle('slotted-icontrol', true)
self._container = this._container;
return this._container;
};
CustomIControl.prototype.onRemove = function() {
this._container.parentNode.removeChild(this._container);
self._container = null;
};
this.icontrol = new CustomIControl();
}
_createIControl(map, icontrol, icontrolOptions) {
if (!map || !icontrol || this.icontrol) return;
// name of the control
if (typeof icontrol === 'string') {
this.icontrol = new mapboxgl[icontrol](icontrolOptions);
} else {
this.icontrol = new icontrol(icontrolOptions);
}
}
_addControl(map, icontrol, position) {
if (!map || !icontrol) return;
map.addControl(icontrol, position);
}
}
window.customElements.define(MapboxGlControl.is, MapboxGlControl);
</script>
</dom-module>