|
| 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 | +} |
0 commit comments