Repository: yagoferrer/map-tools
Pull request #265 (2015-04-26 20:50:22)

Adds "tagging" code. Feature: Remove Group feature and replace by taggin...
...g #237


Patch:
diff --git a/README.md b/README.md
index 504fe33..8eb8f16 100644
--- a/README.md
+++ b/README.md
@@ -257,6 +257,30 @@ map.findMarker('population', {order: 'DESC', limit: 1})
 You can test this query at this [Live example](http://map-tools.io/examples/data.querying.html)
 
 
+#### Tags
+Tags are a useful helpter to **tag** Markers to later search for them.
+
+You can set a `tag` property to a Marker
+```javascript
+map.addMarker({
+  lat: 42.5000,
+  lng: 1.5167,
+  title: 'Andorra'
+  tag: 'cities'
+});
+```
+
+And then find the Marker's reference that have this tag.
+```javascript
+map.findMarker({tag: 'cities'})
+```
+
+You can also update the `tag` property:
+```javascript
+map.updateMarker(<marker>, {tag: 'countries'})
+```
+
+
 #### Reset Marker
 This is one of my favorite features. Sometimes you just want to reset some Marker properties to the original value that was set on the initial creation. This method allows you to reset **one or multiple** Markers to the initial state.
 
diff --git a/lib/map-tools/addMap.js b/lib/map-tools/addMap.js
index c7eb7da..90a30d8 100644
--- a/lib/map-tools/addMap.js
+++ b/lib/map-tools/addMap.js
@@ -117,7 +117,7 @@ module.exports = function (global, that) {
       };
 
       // Set Global Structure
-      global.mapTools.maps[id].markers = global.mapTools.maps[id].markers || {};
+      global.mapTools.maps[id].markers = global.mapTools.maps[id].markers || {all: {}, tags: {}};
       global.mapTools.maps[id].json = global.mapTools.maps[id].json || {all: {}};
 
       that.markers = global.mapTools.maps[id].markers;
diff --git a/lib/map-tools/addMarker.js b/lib/map-tools/addMarker.js
index 09b482e..c95bdf9 100644
--- a/lib/map-tools/addMarker.js
+++ b/lib/map-tools/addMarker.js
@@ -67,9 +67,18 @@ module.exports = function (global, that) {
     that.markers.all = global.mapTools.maps[that.id].markers.all || {};
     that.markers.all[marker.uid] = instance;
 
+    if (marker.tag) {
+      addMarkerByTag(marker, instance);
+    }
+
+
     return instance;
   }
 
+  function addMarkerByTag(marker, instance) {
+    that.markers.tags[marker.tag] = that.markers.tags[marker.tag] || {};
+    that.markers.tags[marker.tag][instance.data.uid] = instance;
+  }
 
   function addOptions(marker, instance) {
     if (marker.move) {
diff --git a/lib/map-tools/config.js b/lib/map-tools/config.js
index ee8575c..0aa0d85 100644
--- a/lib/map-tools/config.js
+++ b/lib/map-tools/config.js
@@ -5,7 +5,7 @@ module.exports = {
   url: '//maps.googleapis.com/maps/api/js',
   zoom: 8,
   customMapOptions: ['id', 'lat', 'lng', 'type', 'uid'],
-  customMarkerOptions: ['lat', 'lng', 'move', 'infoWindow', 'on', 'callback'],
+  customMarkerOptions: ['lat', 'lng', 'move', 'infoWindow', 'on', 'callback', 'tag'],
   panelPosition: 'TOP_LEFT',
   customInfoWindowOptions: ['open', 'close'],
   customEvents: ['marker_visibility_changed']
diff --git a/lib/map-tools/filter.js b/lib/map-tools/filter.js
index 9fee9f4..fc01d48 100644
--- a/lib/map-tools/filter.js
+++ b/lib/map-tools/filter.js
@@ -32,7 +32,6 @@ module.exports = function(global, that, type) {
     }
 
 
-
     var dimension, order, limit, query;
 
     if (typeof args === 'string') {
@@ -40,6 +39,17 @@ module.exports = function(global, that, type) {
     } else {
       dimension = Object.keys(args)[0];
       query = args[dimension];
+
+      if (dimension === 'tag') {
+
+        if (that[type].tags[query]) {
+          return utils.toArray(that[type].tags[query]);
+        } else {
+          return [];
+        }
+
+      }
+
     }
 
     // Add Crossfilter Dimension if it does not exist.
diff --git a/lib/map-tools/updateMarker.js b/lib/map-tools/updateMarker.js
index 372c861..4517298 100644
--- a/lib/map-tools/updateMarker.js
+++ b/lib/map-tools/updateMarker.js
@@ -62,6 +62,15 @@ module.exports = function (global, that) {
 
   }
 
+  function updateTag(marker, options) {
+    if (options.custom.tag) {
+      delete that.markers.tags[marker.tag][marker.data.uid];
+      that.markers.tags[options.custom.tag] = that.markers.tags[options.custom.tag] || {};
+      that.markers.tags[options.custom.tag][marker.data.uid] = marker;
+      marker.tag = options.custom.tag;
+    }
+  }
+
 
   function customUpdate(marker, options) {
 
@@ -75,10 +84,13 @@ module.exports = function (global, that) {
       }
 
       if (options.custom.infoWindow && options.custom.infoWindow.content) {
-
         marker.infoWindow.content = options.custom.infoWindow.content;
+      }
 
+      if (options.custom.tag) {
+        updateTag(marker, options);
       }
+
     }
 
     if (options.defaults) {


Test patch:
diff --git a/test/functional/addMarker.Spec.js b/test/functional/addMarker.Spec.js
index 065ee43..a924fa2 100644
--- a/test/functional/addMarker.Spec.js
+++ b/test/functional/addMarker.Spec.js
@@ -9,18 +9,40 @@ describe('when using the addMarker() method', function () {
     map = new mapTools({async: false, id: 'mymap', lat: 41.3833, lng: 2.1833});
   });
 
-  it('should add one individual Marker to the Map', function () {
-    var result = map.addMarker({
-      lat: 42.5000,
-      lng: 1.5167,
-      title: 'Andorra'
-    });
+  describe('to add one single Marker', function () {
+    it('should add the marker to the Map', function () {
+      var result = map.addMarker({
+        lat: 42.5000,
+        lng: 1.5167,
+        title: 'Andorra'
+      });
 
-    expect(result.title).to.equal('Andorra');
-    expect(Object.keys(mapTools.maps.mymap.markers.all).length).to.equal(1);
+      expect(result.title).to.equal('Andorra');
+      expect(Object.keys(mapTools.maps.mymap.markers.all).length).to.equal(1);
+    });
   });
+  describe('to add Multiple Markers', function () {
+    it('should add multiple Markers to the Map', function () {
+      var markers = [{
+        lat: 41.3833,
+        lng: 2.1833,
+        title: 'Barcelona',
+        on: {
+          click: function() {}
+        }
+      }, {
+        lat: 41.4489,
+        lng: 2.2461,
+        title: 'Badalona',
+        move: 'bounce'
+      }];
 
-  describe('With a "callback" property', function () {
+      var result = map.addMarker(markers);
+      expect(result).to.have.length.of(2);
+      expect(Object.keys(mapTools.maps.mymap.markers.all).length).to.equal(2);
+    });
+  });
+  describe('with a "callback" property', function () {
     it('should call the "callback" function if provided with an instance of the Marker', function (done) {
       map.addMarker({
         lat: 42.5000,
@@ -34,9 +56,7 @@ describe('when using the addMarker() method', function () {
       });
     });
   });
-
-
-  describe('With a custom UID set as Map option', function () {
+  describe('with a custom UID set as Map option', function () {
     it('should add the Marker using the custom UID', function () {
 
       delete mapTools.maps.mymap;
@@ -55,11 +75,8 @@ describe('when using the addMarker() method', function () {
 
     });
   });
-
-  describe('With a custom UID set as Marker data.property', function () {
-
+  describe('with a custom UID set as Marker data.property', function () {
     it('should add the Marker using the custom UID', function () {
-
       map.addMarker({
         lat: 42,
         lng: 2,
@@ -71,40 +88,15 @@ describe('when using the addMarker() method', function () {
 
       expect(map.markers.all['A1'].lat).to.eql(42);
       expect(map.markers.all['A1'].lng).to.eql(2);
-
     });
-
   });
 
 
 
 
-  it('should add multiple Markers to the Map', function () {
-    var markers = [{
-      lat: 41.3833,
-      lng: 2.1833,
-      title: 'Barcelona',
-      on: {
-        click: function() {}
-      }
-    }, {
-      lat: 41.4489,
-      lng: 2.2461,
-      title: 'Badalona',
-      move: 'bounce'
-    }];
-
-    var result = map.addMarker(markers);
-    expect(result).to.have.length.of(2);
-    expect(Object.keys(mapTools.maps.mymap.markers.all).length).to.equal(2);
-  });
-
-
-
 
 
   describe('with extra options as a second parameter', function () {
-
     it('should merge the options with the Marker options', function () {
       var markers = [{
         lat: 41.3833,
@@ -122,7 +114,6 @@ describe('when using the addMarker() method', function () {
       expect(mapTools.maps.mymap.markers.all[Object.keys(mapTools.maps.mymap.markers.all)[0]].myGroupProp).to.be.ok;
       expect(mapTools.maps.mymap.markers.all[Object.keys(mapTools.maps.mymap.markers.all)[1]].myGroupProp).to.be.ok;
     });
-
   });
 
 
diff --git a/test/functional/tags.Spec.js b/test/functional/tags.Spec.js
new file mode 100644
index 0000000..3caa9c6
--- /dev/null
+++ b/test/functional/tags.Spec.js
@@ -0,0 +1,79 @@
+describe('Given the Tag Feature', function () {
+
+  var map;
+  beforeEach(function () {
+    if (mapTools.maps && mapTools.maps.mymap) {
+      delete mapTools.maps.mymap;
+    }
+    map = new mapTools({async: false, id: 'mymap', lat: 41.3833, lng: 2.1833});
+  });
+  describe('When adding a Marker with a specific Tag', function () {
+
+    it('should add a Marker to markers.tags<tag>', function () {
+      var marker = map.addMarker({
+        lat: 42,
+        lng: 1,
+        title: 'Andorra',
+        tag: 'myTag'
+      });
+
+      expect(mapTools.maps.mymap.markers.tags.myTag[marker.uid]).to.eql(marker);
+    });
+
+  });
+
+  describe('when finding a Marker by Tag', function () {
+
+    it('should find the Marker by specific Tag', function () {
+      var marker = map.addMarker({
+        lat: 42,
+        lng: 1,
+        title: 'Andorra',
+        tag: 'myTag'
+      });
+
+      var markers = map.findMarker({tag: 'myTag'});
+      expect(markers[0]).to.eql(marker);
+    });
+
+    describe('if the Tag does not exist', function () {
+
+      it('should return an empty Array', function () {
+
+        var marker = map.addMarker({
+          lat: 42,
+          lng: 1,
+          title: 'Andorra',
+          tag: 'myTag2'
+        });
+
+        var markers = map.findMarker({tag: 'myTag'});
+        expect(markers).to.eql([]);
+
+      });
+
+    });
+
+  });
+
+
+  describe('when updating the tag of a Marker', function () {
+
+    it('should find the Marker in by the other Tag just set', function() {
+
+      var marker = map.addMarker({
+        lat: 42,
+        lng: 1,
+        title: 'Andorra',
+        tag: 'myTag'
+      });
+
+      map.updateMarker(marker, {tag: 'myTag2'});
+
+      var markers = map.findMarker({tag: 'myTag2'});
+      expect(markers[0]).to.eql(marker);
+
+    });
+
+  });
+});