55
66Leaflet.Terminator is a simple plug-in to the Leaflet library to overlay day and night regions on maps.
77"""
8- try :
9- from urllib .request import urlopen as _urlopen
10- except :
11- from urllib import urlopen as _urlopen
8+ from jinja2 import Template
129
13- from . plugin import Plugin
10+ from folium . element import JavascriptLink , MacroElement , Figure
1411
15- # As LO.Terminator.js is not served on both HTTP and HTTPS, we need to embed it explicitely into the code.
16- _request = _urlopen ("http://rawgithub.com/joergdietrich/Leaflet.Terminator/master/L.Terminator.js" )
17- assert _request .getcode ()== 200 , "Error while loading Leaflet.terminator.js"
18- _terminator_script = _request .read ().decode ('utf8' )
19-
20- class Terminator (Plugin ):
12+ class Terminator (MacroElement ):
2113 """Leaflet.Terminator is a simple plug-in to the Leaflet library to overlay day and night regions on maps."""
2214 def __init__ (self ):
2315 """Creates a Terminator plugin to append into a map with
@@ -27,12 +19,20 @@ def __init__(self):
2719 ----------
2820 """
2921 super (Terminator , self ).__init__ ()
30- self .plugin_name = 'Terminator'
22+ self ._name = 'Terminator'
23+
24+ self ._template = Template (u"""
25+ {% macro script(this, kwargs) %}
26+ L.terminator().addTo({{this._parent.get_name()}});
27+ {% endmacro %}
28+ """ )
29+ def render (self ,** kwargs ):
30+ super (Terminator ,self ).render (** kwargs )
3131
32- def render_header ( self , nb ):
33- """Generates the header part of the plugin."" "
34- return '<script type="text/javascript">' + _terminator_script + '</script>' if nb == 0 else ""
32+ figure = self . get_root ()
33+ assert isinstance ( figure , Figure ), ( "You cannot render this Element "
34+ "if it's not in a Figure." )
3535
36- def render_js ( self , nb ):
37- """Generates the Javascript part of the plugin."""
38- return "L.terminator().addTo(map);" if nb == 0 else ""
36+ figure . header . add_children (\
37+ JavascriptLink ( "https://rawgithub.com/joergdietrich/Leaflet.Terminator/master/L.Terminator.js" ),
38+ name = 'markerclusterjs' )
0 commit comments