Create a Popup instance that can be linked to a Layer. Parameters ---------- html: string or Element Content of the Popup. parse_html: bool, default False True if the popup is a template that needs to the rendered first. max_width: int for pixels or text for perc
| 496 | |
| 497 | |
| 498 | class Popup(MacroElement): |
| 499 | """Create a Popup instance that can be linked to a Layer. |
| 500 | |
| 501 | Parameters |
| 502 | ---------- |
| 503 | html: string or Element |
| 504 | Content of the Popup. |
| 505 | parse_html: bool, default False |
| 506 | True if the popup is a template that needs to the rendered first. |
| 507 | max_width: int for pixels or text for percentages, default '100%' |
| 508 | The maximal width of the popup. |
| 509 | show: bool, default False |
| 510 | True renders the popup open on page load. |
| 511 | sticky: bool, default False |
| 512 | True prevents map and other popup clicks from closing. |
| 513 | lazy: bool, default False |
| 514 | True only loads the Popup content when clicking on the Marker. |
| 515 | """ |
| 516 | |
| 517 | _template = Template( |
| 518 | """ |
| 519 | var {{this.get_name()}} = L.popup({{ this.options|tojavascript }}); |
| 520 | |
| 521 | {% for name, element in this.html._children.items() %} |
| 522 | {% if this.lazy %} |
| 523 | {{ this._parent.get_name() }}.once('click', function() { |
| 524 | {{ this.get_name() }}.setContent($(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]); |
| 525 | }); |
| 526 | {% else %} |
| 527 | var {{ name }} = $(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]; |
| 528 | {{ this.get_name() }}.setContent({{ name }}); |
| 529 | {% endif %} |
| 530 | {% endfor %} |
| 531 | |
| 532 | {{ this._parent.get_name() }}.bindPopup({{ this.get_name() }}) |
| 533 | {% if this.show %}.openPopup(){% endif %}; |
| 534 | |
| 535 | {% for name, element in this.script._children.items() %} |
| 536 | {{element.render()}} |
| 537 | {% endfor %} |
| 538 | """ |
| 539 | ) # noqa |
| 540 | |
| 541 | def __init__( |
| 542 | self, |
| 543 | html: Union[str, Element, None] = None, |
| 544 | parse_html: bool = False, |
| 545 | max_width: Union[str, int] = "100%", |
| 546 | show: bool = False, |
| 547 | sticky: bool = False, |
| 548 | lazy: bool = False, |
| 549 | **kwargs: TypeJsonValue, |
| 550 | ): |
| 551 | super().__init__() |
| 552 | self._name = "Popup" |
| 553 | self.header = Element() |
| 554 | self.html = Element() |
| 555 | self.script = Element() |