-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
logo_control.js
92 lines (73 loc) · 2.41 KB
/
logo_control.js
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
// @flow
import DOM from '../../util/dom';
import {bindAll} from '../../util/util';
import type Map from '../map';
/**
* A `LogoControl` is a control that adds the Mapbox watermark
* to the map as required by the [terms of service](https://www.mapbox.com/tos/) for Mapbox
* vector tiles and core styles.
*
* @implements {IControl}
* @private
**/
class LogoControl {
_map: Map;
_container: HTMLElement;
constructor() {
bindAll(['_updateLogo'], this);
bindAll(['_updateCompact'], this);
}
onAdd(map: Map) {
this._map = map;
this._container = DOM.create('div', 'mapboxgl-ctrl');
const anchor = DOM.create('a', 'mapboxgl-ctrl-logo');
anchor.target = "_blank";
anchor.rel = "noopener nofollow";
anchor.href = "https://www.mapbox.com/";
anchor.setAttribute("aria-label", this._map._getUIString('LogoControl.Title'));
anchor.setAttribute("rel", "noopener nofollow");
this._container.appendChild(anchor);
this._container.style.display = 'none';
this._map.on('sourcedata', this._updateLogo);
this._updateLogo();
this._map.on('resize', this._updateCompact);
this._updateCompact();
return this._container;
}
onRemove() {
DOM.remove(this._container);
this._map.off('sourcedata', this._updateLogo);
this._map.off('resize', this._updateCompact);
}
getDefaultPosition() {
return 'bottom-left';
}
_updateLogo(e: any) {
if (!e || e.sourceDataType === 'metadata') {
this._container.style.display = this._logoRequired() ? 'block' : 'none';
}
}
_logoRequired() {
if (!this._map.style) return;
const sourceCaches = this._map.style.sourceCaches;
for (const id in sourceCaches) {
const source = sourceCaches[id].getSource();
if (source.mapbox_logo) {
return true;
}
}
return false;
}
_updateCompact() {
const containerChildren = this._container.children;
if (containerChildren.length) {
const anchor = containerChildren[0];
if (this._map.getCanvasContainer().offsetWidth < 250) {
anchor.classList.add('mapboxgl-compact');
} else {
anchor.classList.remove('mapboxgl-compact');
}
}
}
}
export default LogoControl;