From 96f7787916ca4250d9e81b9ac60a700dcfe91fc3 Mon Sep 17 00:00:00 2001
From: hellekin <hellekin@cepheide.org>
Date: Thu, 5 Apr 2018 21:25:29 +0200
Subject: Properly remove section selection from map (fixes #4)

---
 assets/mapper.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 46 insertions(+), 4 deletions(-)

(limited to 'assets')

diff --git a/assets/mapper.js b/assets/mapper.js
index 2fc373d..ccd4f38 100644
--- a/assets/mapper.js
+++ b/assets/mapper.js
@@ -7,7 +7,7 @@
 // We don't have working GeoJSON support yet
 USE_GEOJSON = false
 
-VERSION = '0.0.1'
+VERSION = '0.0.3'
 
 // Subset of Dewey Maps' Deck
 // See https://lite.framacalc.org/XNQBPjpQuO
@@ -189,7 +189,7 @@ function navSetup() {
 		} else {
 		    showActiveCategoryMarkers(c.id)
 		}
-		$(this).toggleClass('active')
+		$(this).toggleClass('active')		
 	    })
 	    .append(listSections(c.subcategories))
 	ul.append(li)
@@ -215,6 +215,7 @@ function listSections(sections) {
 		.removeClass('pending')
 		.toggleClass('active')
 	    toggleMarkers(sec_id)
+	    removeSectionSelectionIfEmpty(s)
 	    return false
 	})
 	ul.append(li)
@@ -239,23 +240,47 @@ function toggleMarkers(sec_id) {
 **/
 function toggleAllSections(cat_id, active) {
     console.log('toggleAllSections(' + cat_id + ', ' + active + ')')
-    categorySections(cat_id).forEach(function(s) {
+    var csec = categorySections(cat_id)
+    var cseclen = csec.length
+    var i = 0
+    csec.forEach(function(s) {
 	var section = $('#s-' + s.id)
+	sectionOffMap(s.id)
 	if (active) {
 	    sectionOnMap(s.id)
 	    section.addClass('active')
 	} else {
 	    sectionOffMap(s.id)
 	    section.removeClass('active')
+	    i++
 	}
     })
+    // Remove the .selected on category if all sections are unchecked
+    if (i == cseclen) {
+	uncheckCategoryToggler(cat_id)
+    }
 }
 
 function categorySections(cat_id) {
     return Categories.find(function(c) { return c.id == cat_id }).subcategories
 }
-    
 
+function uncheckCategoryToggler(cat_id) {
+    $('#c-' + cat_id + ' h3 span').removeClass('selected')
+}
+
+function removeSectionSelectionIfEmpty(s) {
+    var sc = sectionCategory(s)
+    if ($('#c-' + sc.id + '.active li.active').length == 0) {
+	uncheckCategoryToggler(sc.id)
+    }
+}
+
+function sectionCategory(s) {
+    var c = Categories.find(function(c) { return (c.subcategories.includes(s)) })
+    console.log(c)
+    return c
+}
 
 /**
  * Retrieve markers for given section from the JSON API
@@ -306,6 +331,7 @@ function toGeoJSON(data) {
     }
 }
 
+
 /**
  * Hide markers in all sections for given category
  *
@@ -362,6 +388,10 @@ function sectionOnMap(sec_id) {
 	    markers.push(layer)
 	})
     }
+    /* Remove any existing layer, replacing with the new one (refs #4) */
+    if (!$.isEmptyObject(SectionLayers[sec_id])) {
+	map.removeLayer(SectionLayers[sec_id])
+    }
     SectionLayers[sec_id] = L.featureGroup.subGroup(mcg, markers)
 //    mcg.addTo(map)
     SectionLayers[sec_id].addTo(map)
@@ -378,6 +408,8 @@ function sectionOffMap(sec_id) {
 //	console.log('sectionOffMap(' + sec_id + ')')
 	map.removeLayer(SectionLayers[sec_id])
 	section.removeClass('on-map')
+    } else {
+	console.log("section " + sec_id + " is not .on-map")
     }
 }
 
@@ -433,9 +465,19 @@ function selected_sections() {
     $('.on-map').each(function() {
 	s.push($(this).attr('id').split('s-')[1]);
     });
+    console.log("selected sections: " + s)
     return s;
 }
 
+function categoryHasSectionsOnMap(cat_id) {
+    var s = selected_sections()
+    s.forEach(function(s) {
+	console.log(s)
+	
+    })
+
+}
+
 function current_state() {
     var c = map.getCenter();
     var z = map.getZoom();
-- 
cgit v1.2.3