Skip to content

Commit 6189d4f

Browse files
committed
Extract Leaflet's custom "autoWidth" option into "leaflet_plus.ts"
1 parent 53375a3 commit 6189d4f

File tree

5 files changed

+117
-14
lines changed

5 files changed

+117
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## development
44
- Add test for customization of Leaflet Popup's "autoWidth" option
5+
- Extract Leaflet's custom "autoWidth" option into "leaflet_plus.ts"
56

67
## v0.13.0
78
- Update basemaps URL to the new CARTO CDN URL. Thanks, @skgsergio!

src/libs/leaflet.js

Lines changed: 7 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libs/leaflet.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './leaflet_plus';
12
import * as L from './leaflet';
23

34
describe('Leaflet', () => {

src/libs/leaflet_plus.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// 04bcb59 added an autoWidth setting to the Popup options.
2+
// This option, when disabled, will mask the whole section for computing
3+
// the width for the popup automatically within the _updateLayout method.
4+
// https://github.com/panodata/grafana-map-panel/issues/79#issuecomment-723290476
5+
import { Popup } from './leaflet';
6+
7+
// @option autoWidth: Boolean = true
8+
// Set it to `false` if you don't want to compute
9+
// and set the width of a opened popup automatically.
10+
Popup.prototype.options.autoWidth = true;
11+
12+
Popup.prototype._updateLayout = function() {
13+
var container = this._contentNode,
14+
style = container.style;
15+
16+
if (this.options.autoWidth) {
17+
style.width = '';
18+
style.whiteSpace = 'nowrap';
19+
20+
var width = container.offsetWidth;
21+
width = Math.min(width, this.options.maxWidth);
22+
width = Math.max(width, this.options.minWidth);
23+
24+
style.width = width + 1 + 'px';
25+
style.whiteSpace = '';
26+
}
27+
28+
style.height = '';
29+
30+
var height = container.offsetHeight,
31+
maxHeight = this.options.maxHeight,
32+
scrolledClass = 'leaflet-popup-scrolled';
33+
34+
if (maxHeight && height > maxHeight) {
35+
style.height = maxHeight + 'px';
36+
addClass(container, scrolledClass);
37+
} else {
38+
removeClass(container, scrolledClass);
39+
}
40+
41+
this._containerWidth = this._container.offsetWidth;
42+
};
43+
44+
// Have to include these helper functions...
45+
46+
// @function hasClass(el: HTMLElement, name: String): Boolean
47+
// Returns `true` if the element's class attribute contains `name`.
48+
function hasClass(el, name) {
49+
if (el.classList !== undefined) {
50+
return el.classList.contains(name);
51+
}
52+
var className = getClass(el);
53+
return className.length > 0 && new RegExp('(^|\\s)' + name + '(\\s|$)').test(className);
54+
}
55+
56+
// @function addClass(el: HTMLElement, name: String)
57+
// Adds `name` to the element's class attribute.
58+
function addClass(el, name) {
59+
if (el.classList !== undefined) {
60+
var classes = splitWords(name);
61+
for (var i = 0, len = classes.length; i < len; i++) {
62+
el.classList.add(classes[i]);
63+
}
64+
} else if (!hasClass(el, name)) {
65+
var className = getClass(el);
66+
setClass(el, (className ? className + ' ' : '') + name);
67+
}
68+
}
69+
70+
// @function removeClass(el: HTMLElement, name: String)
71+
// Removes `name` from the element's class attribute.
72+
function removeClass(el, name) {
73+
if (el.classList !== undefined) {
74+
el.classList.remove(name);
75+
} else {
76+
setClass(el, trim((' ' + getClass(el) + ' ').replace(' ' + name + ' ', ' ')));
77+
}
78+
}
79+
80+
// @function setClass(el: HTMLElement, name: String)
81+
// Sets the element's class.
82+
function setClass(el, name) {
83+
if (el.className.baseVal === undefined) {
84+
el.className = name;
85+
} else {
86+
// in case of SVG element
87+
el.className.baseVal = name;
88+
}
89+
}
90+
91+
// @function getClass(el: HTMLElement): String
92+
// Returns the element's class.
93+
function getClass(el) {
94+
return el.className.baseVal === undefined ? el.className : el.className.baseVal;
95+
}
96+
97+
// @function trim(str: String): String
98+
// Compatibility polyfill for [String.prototype.trim](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)
99+
function trim(str) {
100+
return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
101+
}
102+
103+
// @function splitWords(str: String): String[]
104+
// Trims and splits the string on whitespace and returns the array of parts.
105+
function splitWords(str) {
106+
return trim(str).split(/\s+/);
107+
}

src/worldmap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as _ from 'lodash';
22
import $ from 'jquery';
3+
import './libs/leaflet_plus';
34
import * as L from './libs/leaflet';
45
import WorldmapCtrl from './worldmap_ctrl';
56
import { ColorModes } from './model';

0 commit comments

Comments
 (0)