Skip to content

Commit

Permalink
Add loadevent parameter to control map initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Aug 12, 2013
1 parent 814c1d2 commit ca315e6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
4 changes: 1 addition & 3 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ CHANGELOG
**Features**

* Add ability to associate layers attributions from settings

* Add ``auto-include`` key for entries in ``PLUGINS`` setting, in order
to implicity load plugins with ``leaflet_css`` and ``leaflet_js`` tags.

* Rewrote map initialization, into less flexible and obstruvise way.

* Use plugin system for Leaflet.MiniMap.
* Add ``loadevent`` parameter to ``leaflet_map`` tag.


0.6.0 (2013-08-08)
Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ Advanced usage
Useful to put the javascript code in the header or footer instead of the
body of the html document. If used, do not forget to create the div manually.

* ``loadevent``: One or more space-separated *window* events that trigger map initialization.
(Default: ``load``, i.e. all page resources loaded).
If empty values is provided, then map initialization is immediate.
And with a wrong value, the map is never initialized. :)


Projection
----------
Expand Down
6 changes: 4 additions & 2 deletions leaflet/templates/leaflet/map_fragment.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
globals: {{ NO_GLOBALS|yesno:"false,true"}}, callback: {{ callback}}};
L.Map.djangoMap('{{ name }}', options);
}
if (window.addEventListener) window.addEventListener("load", loadmap, false);
else if (window.jQuery) jQuery(window).on("load", loadmap);
var loadevents = {% autoescape off %}{{ loadevents }}{% endautoescape %};
if (loadevents.length === 0) loadmap();
else if (window.addEventListener) for (var i=0; i<loadevents.length; i++) window.addEventListener(loadevents[i], loadmap, false);
else if (window.jQuery) jQuery(window).on(loadevents.join(' '), loadmap);
{% if not NO_GLOBALS %}
// Put initialization function in global scope (like django-leaflet < 0.7)
window['loadmap' + '{{ name }}'] = loadmap;
Expand Down
3 changes: 2 additions & 1 deletion leaflet/templatetags/leaflet_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def leaflet_js(plugins=None):


@register.inclusion_tag('leaflet/map_fragment.html')
def leaflet_map(name, callback=None, fitextent=True, creatediv=True):
def leaflet_map(name, callback=None, fitextent=True, creatediv=True, loadevent='load'):
"""
:param name:
Expand Down Expand Up @@ -75,6 +75,7 @@ def leaflet_map(name, callback=None, fitextent=True, creatediv=True):
return {
# templatetag options
'name': name,
'loadevents': json.dumps(loadevent.split()),
'creatediv': creatediv,
'callback': callback,
# initialization options
Expand Down

0 comments on commit ca315e6

Please sign in to comment.