Repository: yagoferrer/map-tools
Pull request #202 (2015-03-23 20:45:06)

Yago/remove marker


Patch:
diff --git a/lib/map-tools/findMarker.js b/lib/map-tools/findMarker.js
new file mode 100644
index 0000000..20d9966
--- /dev/null
+++ b/lib/map-tools/findMarker.js
@@ -0,0 +1,16 @@
+"use strict";
+
+module.exports = function(global, that) {
+
+  function find(marker) {
+    if (marker.data && marker.data.uid) {
+      return marker;
+    }
+
+    if (marker.uid && global.mapTools.maps[that.id].markers.all[marker.uid]) {
+      return global.mapTools.maps[that.id].markers.all[marker.uid];
+    }
+  }
+
+  return find;
+};
diff --git a/lib/map-tools/findUpdateMarker.js b/lib/map-tools/findUpdateMarker.js
deleted file mode 100644
index 09a5f80..0000000
--- a/lib/map-tools/findUpdateMarker.js
+++ /dev/null
@@ -1,45 +0,0 @@
-/*jslint node: true */
-"use strict";
-module.exports = function (global, that) {
-
-  function updateMarker(marker, options) {
-
-    if (options.custom) {
-      if (options.custom.move) {
-        marker.setAnimation(global.google.maps.Animation[options.custom.move.toUpperCase()]);
-      }
-
-      if (options.custom.lat && options.custom.lng) {
-        marker.setPosition(new global.google.maps.LatLng(options.custom.lat, options.custom.lng));
-      }
-
-      if (options.custom.infoWindow && options.custom.infoWindow.content) {
-        marker.infoWindow.instance.setContent(options.custom.infoWindow.content);
-      }
-    }
-
-    if (options.defaults) {
-      marker.setOptions(options.defaults);
-    }
-
-    return marker;
-  }
-  /**
-   * Transforms flat keys to Setters. For example visible becomes: setVisible.
-   * @param options
-   * @returns {{count: number, setterKey: *, setters: {}}}
-   * @private
-   */
-  function findAndUpdateMarker(marker, options) {
-    if (marker.data && marker.data.uid) {
-      return updateMarker(marker, options);
-    }
-
-    if (marker.uid && global.mapTools.maps[that.id].markers.all[marker.uid]) {
-      return updateMarker(global.mapTools.maps[that.id].markers.all[marker.uid], options);
-    }
-  }
-
-
-  return findAndUpdateMarker;
-};
diff --git a/lib/map-tools/groups.js b/lib/map-tools/groups.js
index 77648ed..e1f7307 100644
--- a/lib/map-tools/groups.js
+++ b/lib/map-tools/groups.js
@@ -4,7 +4,7 @@ var utils = require('map-tools/utils');
 var config = require('map-tools/config');
 
 module.exports = function (global, that) {
-  var findAndUpdateMarker = require('map-tools/findUpdateMarker')(global, that);
+  var updateMarker = require('map-tools/updateMarker')(global, that);
 
   /**
    * Adds a New Group
@@ -29,7 +29,7 @@ module.exports = function (global, that) {
     if (global.mapTools.maps[that.id].markers.groups && global.mapTools.maps[that.id].markers.groups[name]) {
       for (item in global.mapTools.maps[that.id].markers.groups[name]) {
         if (global.mapTools.maps[that.id].markers.groups[name].hasOwnProperty(item)) {
-          instance = findAndUpdateMarker(global.mapTools.maps[that.id].markers.groups[name][item], preparedOptions);
+          instance = updateMarker.customUpdate(global.mapTools.maps[that.id].markers.groups[name][item], preparedOptions);
           result.push(instance);
         }
       }
diff --git a/lib/map-tools/index.js b/lib/map-tools/index.js
index a8d1d94..7641ae5 100644
--- a/lib/map-tools/index.js
+++ b/lib/map-tools/index.js
@@ -11,11 +11,12 @@ module.exports = function (global) {
     var that = this;
 
     this.addMarker = require('map-tools/addMarker')(global, that);
+    this.removeMarker = require('map-tools/removeMarker')(global, that);
     this.addTopoJson = require('map-tools/addFeature')(global, that).addTopoJson;
     this.addGeoJson = require('map-tools/addFeature')(global, that).addGeoJson;
     this.updateFeature = require('map-tools/updateFeature')(global, that);
     this.addPanel = require('map-tools/addPanel')(global, that);
-    this.updateMarker = require('map-tools/updateMarker')(global, that);
+    this.updateMarker = require('map-tools/updateMarker')(global, that).update;
     this.filterFeature = require('map-tools/filter')(global, that, 'json');
     this.filterMarker = require('map-tools/filter')(global, that, 'markers');
     this.addGroup = require('map-tools/groups')(global, that).addGroup;
diff --git a/lib/map-tools/removeMarker.js b/lib/map-tools/removeMarker.js
new file mode 100644
index 0000000..a48ac22
--- /dev/null
+++ b/lib/map-tools/removeMarker.js
@@ -0,0 +1,32 @@
+"use strict";
+module.exports = function(global, that) {
+
+  var findMarker = require('map-tools/findMarker')(global, that);
+
+  function removeMarker(args) {
+
+    var type = Object.prototype.toString.call(args);
+
+    if (type === '[object Object]') {
+      return remove(findMarker(args));
+    }
+
+    if (type === '[object Array]') {
+      var marker, x;
+      for (x in args) {
+        if (args.hasOwnProperty(x)) {
+          marker = args[x];
+          remove(findMarker(marker));
+        }
+      }
+    }
+  }
+
+  function remove(marker) {
+    marker.setMap(null);
+    delete global.mapTools.maps[that.id].markers.all[marker.data.uid];
+  }
+
+  return removeMarker;
+
+};
diff --git a/lib/map-tools/updateMarker.js b/lib/map-tools/updateMarker.js
index 9cca65c..991ad46 100644
--- a/lib/map-tools/updateMarker.js
+++ b/lib/map-tools/updateMarker.js
@@ -5,21 +5,21 @@ var config = require('map-tools/config');
 
 module.exports = function (global, that) {
 
-  var findAndUpdateMarker = require('map-tools/findUpdateMarker')(global, that);
+  var findMarker = require('map-tools/findMarker')(global, that);
 
-  function updateMarker(args, options) {
+  function update(args, options) {
     var type = Object.prototype.toString.call(args);
     var preparedOptions = utils.prepareOptions(options, config.customMarkerOptions);
 
     if (type === '[object Object]') {
-      return findAndUpdateMarker(args, preparedOptions);
+      return customUpdate(findMarker(args), preparedOptions);
     }
     if (type === '[object Array]') {
       var marker, results = [], instance, x;
       for (x in args) {
         if (args.hasOwnProperty(x)) {
           marker = args[x];
-          instance = findAndUpdateMarker(marker, preparedOptions);
+          instance = customUpdate(findMarker(marker), preparedOptions);
           results.push(instance);
         }
       }
@@ -27,6 +27,33 @@ module.exports = function (global, that) {
     }
   }
 
-  return updateMarker;
+
+  function customUpdate(marker, options) {
+
+    if (options.custom) {
+      if (options.custom.move) {
+        marker.setAnimation(global.google.maps.Animation[options.custom.move.toUpperCase()]);
+      }
+
+      if (options.custom.lat && options.custom.lng) {
+        marker.setPosition(new global.google.maps.LatLng(options.custom.lat, options.custom.lng));
+      }
+
+      if (options.custom.infoWindow && options.custom.infoWindow.content) {
+        marker.infoWindow.instance.setContent(options.custom.infoWindow.content);
+      }
+    }
+
+    if (options.defaults) {
+      marker.setOptions(options.defaults);
+    }
+
+    return marker;
+  }
+
+  return {
+    update: update,
+    customUpdate: customUpdate
+  }
 
 };


Test patch:
diff --git a/test/functional/removeMarker.Spec.js b/test/functional/removeMarker.Spec.js
new file mode 100644
index 0000000..beb98bf
--- /dev/null
+++ b/test/functional/removeMarker.Spec.js
@@ -0,0 +1,45 @@
+describe('when calling removeMarker()', function () {
+  "use strict";
+  it('should remove the Marker from the Map', function () {
+    var map = new mapTools({async: false, id: 'mymap', lat: 41.3833, lng: 2.1833});
+
+    var options = {
+      lat: 41.3833,
+      lng: 2.1833,
+      title: 'Barcelona',
+      visible: false,
+      infoWindow: {
+        content: 'Barcelona!'
+      }
+    };
+
+    var instance = map.addMarker(options);
+    map.removeMarker(instance);
+    expect(instance.map).to.equal(null);
+  });
+
+
+  it('should remove multiple Markers from the Map', function() {
+
+    var map = new mapTools({async: false, id: 'mymap', lat: 41.3833, lng: 2.1833});
+
+    var markers = [{
+      lat: 41.3833,
+      lng: 2.1833,
+      title: 'Barcelona'
+    }, {
+      lat: 41.4489,
+      lng: 2.2461,
+      title: 'Badalona'
+    }];
+
+    var result = map.addMarker(markers);
+
+    map.removeMarker(result);
+
+    expect(result[0].map).to.equal(null);
+    expect(result[1].map).to.equal(null);
+
+  });
+
+});
diff --git a/test/mocks/google-maps.js b/test/mocks/google-maps.js
index 8c1118a..d53e480 100644
--- a/test/mocks/google-maps.js
+++ b/test/mocks/google-maps.js
@@ -41,6 +41,7 @@ module.exports = {
     },
     Marker: function(marker) {
 
+
       marker.setAnimation = function() {
         return 'animation set';
       };
@@ -53,6 +54,10 @@ module.exports = {
         for (var option in options) {
           marker[option] = options[option];
         }
+      };
+
+      marker.setMap = function(map) {
+        marker.map = map;
       }
 
       return marker;
diff --git a/test/unit/removeMarker.Spec.js b/test/unit/removeMarker.Spec.js
new file mode 100644
index 0000000..d14a2ab
--- /dev/null
+++ b/test/unit/removeMarker.Spec.js
@@ -0,0 +1,35 @@
+describe('Given the removeMarker Module', function () {
+
+  var removeMarker, markerMock;
+
+  beforeEach(function () {
+
+    var global = {mapTools: {maps: {mymap: {markers: {all: {}}}}}};
+    var that = {id: 'mymap'};
+
+    markerMock = function(uid) {
+      this.setMap =  function(arg){
+        this.map = arg;
+      };
+      this.map = 'myMap';
+      this.data = {uid: uid};
+      return this;
+    };
+
+    removeMarker = require('map-tools/removeMarker')(global, that);
+  });
+
+  it('should remove a Marker from the Map', function () {
+    var marker = new markerMock('markerid1');
+    removeMarker(marker);
+    expect(marker.map).to.equal(null);
+  });
+
+  it('should remove multiples markers from the Map', function () {
+    var markers = [new markerMock('markerid1'), new markerMock('markerid2')];
+    removeMarker(markers);
+    expect(markers[0].map).to.equal(null);
+    expect(markers[1].map).to.equal(null);
+  });
+
+});