class Popup(Element): """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, default 300 The maximal width of the popup. """ def __init__(self, html=None, parse_html=False, max_width=300): super(Popup, self).__init__() self._name = 'Popup' self.header = Element() self.html = Element() self.script = Element() self.header._parent = self self.html._parent = self self.script._parent = self script = not parse_html if isinstance(html, Element): self.html.add_child(html) elif isinstance(html, text_type) or isinstance(html, binary_type): self.html.add_child(Html(text_type(html), script=script)) self.max_width = max_width self._template = Template(u""" var {{this.get_name()}} = L.popup({maxWidth: '{{this.max_width}}'}); {% for name, element in this.html._children.items() %} var {{name}} = $('{{element.render(**kwargs).replace('\\n',' ')}}')[0]; {{this.get_name()}}.setContent({{name}}); {% endfor %} {{this._parent.get_name()}}.bindPopup({{this.get_name()}}); {% for name, element in this.script._children.items() %} {{element.render()}} {% endfor %} """) # noqa def render(self, **kwargs): """Renders the HTML representation of the element.""" for name, child in self._children.items(): child.render(**kwargs) figure = self.get_root() assert isinstance(figure, Figure), ('You cannot render this Element ' 'if it is not in a Figure.') figure.script.add_child(Element( self._template.render(this=self, kwargs=kwargs)), name=self.get_name())
class Popup(Element): """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, default 300 The maximal width of the popup. """ def __init__(self, html=None, parse_html=False, max_width=300): super(Popup, self).__init__() self._name = 'Popup' self.header = Element() self.html = Element() self.script = Element() self.header._parent = self self.html._parent = self self.script._parent = self script = not parse_html if isinstance(html, Element): self.html.add_child(html) elif isinstance(html, text_type) or isinstance(html, binary_type): self.html.add_child(Html(text_type(html), script=script)) self.max_width = max_width self._template = Template(u""" var {{this.get_name()}} = L.popup({maxWidth: '{{this.max_width}}'}); {% for name, element in this.html._children.items() %} var {{name}} = $('{{element.render(**kwargs).replace('\\n',' ')}}')[0]; {{this.get_name()}}.setContent({{name}}); {% endfor %} {{this._parent.get_name()}}.bindPopup({{this.get_name()}}); {% for name, element in this.script._children.items() %} {{element.render()}} {% endfor %} """) # noqa def render(self, **kwargs): """Renders the HTML representation of the element.""" for name, child in self._children.items(): child.render(**kwargs) figure = self.get_root() assert isinstance(figure, Figure), ('You cannot render this Element ' 'if it is not in a Figure.') figure.script.add_child(Element( self._template.render(this=self, kwargs=kwargs)), name=self.get_name())
class Popup(Element): """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 percentages, default '100%' The maximal width of the popup. show: bool, default False True renders the popup open on page load. sticky: bool, default False True prevents map and other popup clicks from closing. """ _template = Template(u""" var {{this.get_name()}} = L.popup({maxWidth: '{{this.max_width}}' {% if this.show or this.sticky %}, autoClose: false{% endif %} {% if this.sticky %}, closeOnClick: false{% endif %}}); {% for name, element in this.html._children.items() %} var {{ name }} = $(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]; {{this.get_name()}}.setContent({{name}}); {% endfor %} {{this._parent.get_name()}}.bindPopup({{this.get_name()}}) {% if this.show %}.openPopup(){% endif %}; {% for name, element in this.script._children.items() %} {{element.render()}} {% endfor %} """) # noqa def __init__(self, html=None, parse_html=False, max_width='100%', show=False, sticky=False): super(Popup, self).__init__() self._name = 'Popup' self.header = Element() self.html = Element() self.script = Element() self.header._parent = self self.html._parent = self self.script._parent = self script = not parse_html if isinstance(html, Element): self.html.add_child(html) elif isinstance(html, text_type) or isinstance(html, binary_type): self.html.add_child(Html(text_type(html), script=script)) self.max_width = max_width self.show = show self.sticky = sticky def render(self, **kwargs): """Renders the HTML representation of the element.""" for name, child in self._children.items(): child.render(**kwargs) figure = self.get_root() assert isinstance(figure, Figure), ('You cannot render this Element ' 'if it is not in a Figure.') figure.script.add_child(Element( self._template.render(this=self, kwargs=kwargs)), name=self.get_name())
class Popup(Element): """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 percentages, default '100%' The maximal width of the popup. show: bool, default False True renders the popup open on page load. sticky: bool, default False True prevents map and other popup clicks from closing. """ _template = Template(u""" var {{this.get_name()}} = L.popup({{ this.options|tojson }}); {% for name, element in this.html._children.items() %} var {{ name }} = $(`{{ element.render(**kwargs).replace('\\n',' ') }}`)[0]; {{ this.get_name() }}.setContent({{ name }}); {% endfor %} {{ this._parent.get_name() }}.bindPopup({{ this.get_name() }}) {% if this.show %}.openPopup(){% endif %}; {% for name, element in this.script._children.items() %} {{element.render()}} {% endfor %} """) # noqa def __init__(self, html=None, parse_html=False, max_width='100%', show=False, sticky=False, **kwargs): super(Popup, self).__init__() self._name = 'Popup' self.header = Element() self.html = Element() self.script = Element() self.header._parent = self self.html._parent = self self.script._parent = self script = not parse_html if isinstance(html, Element): self.html.add_child(html) elif isinstance(html, str): self.html.add_child(Html(html, script=script)) self.show = show self.options = parse_options( max_width=max_width, autoClose=False if show or sticky else None, closeOnClick=False if sticky else None, **kwargs ) def render(self, **kwargs): """Renders the HTML representation of the element.""" for name, child in self._children.items(): child.render(**kwargs) figure = self.get_root() assert isinstance(figure, Figure), ('You cannot render this Element ' 'if it is not in a Figure.') figure.script.add_child(Element( self._template.render(this=self, kwargs=kwargs)), name=self.get_name())