Auto-uncheck the data and notes layers at low zoom
authorJohn Firebaugh <john.firebaugh@gmail.com>
Tue, 3 Dec 2013 01:01:13 +0000 (17:01 -0800)
committerTom Hughes <tom@compton.nu>
Tue, 3 Dec 2013 08:55:15 +0000 (08:55 +0000)
Reenable them when returning to high zoom in the
same session.

Fixes #592

app/assets/javascripts/leaflet.layers.js

index 7703529b8799e35d94b06ddefc1fb9d4e60705d1..bb00f716c8c28d969c3f90c05db97970b4f1aabb 100644 (file)
@@ -132,15 +132,18 @@ L.OSM.layers = function(options) {
         var label = $('<label>')
           .appendTo(item);
 
+        var checked = map.hasLayer(layer);
+
         var input = $('<input>')
           .attr('type', 'checkbox')
-          .prop('checked', map.hasLayer(layer))
+          .prop('checked', checked)
           .appendTo(label);
 
         label.append(name);
 
         input.on('change', function() {
-          if (input.is(':checked')) {
+          checked = input.is(':checked');
+          if (checked) {
             map.addLayer(layer);
           } else {
             map.removeLayer(layer);
@@ -155,6 +158,16 @@ L.OSM.layers = function(options) {
         map.on('zoomend', function() {
           var disabled = map.getBounds().getSize() >= maxArea;
           $(input).prop('disabled', disabled);
+
+          if (disabled && $(input).is(':checked')) {
+            $(input).prop('checked', false)
+              .trigger('change');
+            checked = true;
+          } else if (!disabled && !$(input).is(':checked') && checked) {
+            $(input).prop('checked', true)
+              .trigger('change');
+          }
+
           $(item).attr('class', disabled ? 'disabled' : '');
           item.attr('data-original-title', disabled ?
             I18n.t('javascripts.site.' + refName + '_zoom_in_tooltip') : '');