Repository: yagoferrer/map-tools
Pull request #256 (2015-04-24 03:13:56)

Yago/find marker updates
Fixing findMarker() :dancer: 


Patch:
diff --git a/examples/code.snippet/topojson.snippet.html b/examples/code.snippet/topojson.snippet.html
index 4c86e89..b4eec2b 100644
--- a/examples/code.snippet/topojson.snippet.html
+++ b/examples/code.snippet/topojson.snippet.html
@@ -6,8 +6,8 @@
     strokeWeight: 0.5,
     fillOpacity: 0
   },filters: ['NAME']}]);
-  var colorado = map.filterFeature({NAME:'Colorado'}, {limit: 1});
-  var nevada = map.filterFeature({NAME:'Nevada'}, {limit: 1});
+  var colorado = map.findFeature({NAME:'Colorado'}, {limit: 1});
+  var nevada = map.findFeature({NAME:'Nevada'}, {limit: 1});
 	var options = {
 		style:{
 			fillOpacity: 0.4,
@@ -30,6 +30,6 @@
     }
   }
 
-  var all = map.filterFeature('NAME');
+  var all = map.findFeature();
   map.updateFeature(all), {style: color});
 </code></pre></div>
diff --git a/examples/data.querying.html b/examples/data.querying.html
index 2caad8e..5e7f393 100644
--- a/examples/data.querying.html
+++ b/examples/data.querying.html
@@ -28,7 +28,7 @@
 		    position: 'right center',
 		    events: {
 			    '.top click': function (e) {
-            var marker = map.findMarker('population', {limit: 1});
+            var marker = map.findMarker('population', {limit: 1, order: 'ASC'});
 				    map.updateMarker(marker, {move: 'bounce'});
 				    e.preventDefault();
 			    }
@@ -71,7 +71,7 @@
           population: 679624
         }
       }
-    ], {filters: ['population']});
+    ]);
   }
 
 </script>
diff --git a/examples/info.window.html b/examples/info.window.html
index b2a9a50..0c6bd87 100644
--- a/examples/info.window.html
+++ b/examples/info.window.html
@@ -27,7 +27,8 @@
 		    position: 'right center',
 		    events: {
 			    '.update click': function (e) {
-				    map.updateMarker(marker[0], {infoWindow: {content: '<p>Updated!</p>'}});
+            map.updateMarker(map.findMarker({city: 'Barcelona'}), {infoWindow: {content: '<p>Updated!</p>'}});
+
 				    e.preventDefault();
 			    }
 		    }
@@ -50,7 +51,7 @@
       lng: 2.1833,
       title: 'Barcelona',
       data: {
-        city: 'Barcelona!'
+        city: 'Barcelona'
       }
     },
       {
diff --git a/examples/topojson.html b/examples/topojson.html
index bdda445..5f6386d 100644
--- a/examples/topojson.html
+++ b/examples/topojson.html
@@ -56,14 +56,16 @@
         position: 'right center',
         events: {
           '.single click': function (e) {
-            var colorado = map.filterFeature({NAME:'Colorado'}, {limit: 1});
-            var nevada = map.filterFeature({NAME:'Nevada'}, {limit: 1});
+            var colorado = map.findFeature({NAME:'Colorado'}, {limit: 1});
+            var nevada = map.findFeature({NAME:'Nevada'}, {limit: 1});
             map.updateFeature([colorado, nevada], {style: {fillOpacity: 0.4, fillColor:'black', strokeColor: 'black'}});
             e.preventDefault();
           },
 
           '.choro click': function (e) {
-            var all = map.filterFeature('NAME');
+            var all = map.findFeature();
+
+
             map.updateFeature(all, {style: color});
             e.preventDefault();
           }
diff --git a/examples/update.marker.html b/examples/update.marker.html
index 9b8e672..08b9c2a 100644
--- a/examples/update.marker.html
+++ b/examples/update.marker.html
@@ -26,15 +26,15 @@
 
 			map.addPanel({
 				template: '<div class="actions">' +
-				'<div class="update-crossfilter">Update Marker by using markerFilter()</div>' +
+				'<div class="update-find">Update Marker by using findMarker()</div>' +
 				'<div class="update-reference">Update Marker by using it\'s own reference</div>' +
 				'<div class="update-reference-location">Update Marker to another location</div>' +
 				'</div>',
 				position: 'right center',
 				events: {
-					'.update-crossfilter click': function (e) {
-						var marker = map.findMarker({cityName: 'barcelona'}, {limit: 1});
-						map.updateMarker(marker, {move: 'drop'});
+					'.update-find click': function (e) {
+						var marker = map.findMarker({data: {cityName: 'barcelona'}}, {limit: 1});
+						map.updateMarker([marker], {move: 'drop'});
 						e.preventDefault();
 					},
 
diff --git a/lib/map-tools/addFeature.js b/lib/map-tools/addFeature.js
index c7dddb0..02822df 100644
--- a/lib/map-tools/addFeature.js
+++ b/lib/map-tools/addFeature.js
@@ -5,7 +5,7 @@ var utils = require('map-tools/utils');
 
 module.exports = function (global, that) {
 
-  var addFilter = require('map-tools/addFilter')(global, that);
+  var addFilter = require('map-tools/addFilter')(global, that, 'json');
 
   /**
    * Adds GeoJSON Feature Options like: style
@@ -21,8 +21,12 @@ module.exports = function (global, that) {
       if (features.hasOwnProperty(x)) {
         feature = features[x];
 
+        var uid =  utils.createUid();
+        feature.uid = uid;
         var data = feature.k;
-        feature.k.uid = utils.createUid();
+        feature.k.uid = uid;
+
+
 
         Object.defineProperty(feature, 'data', {
           value: data,
@@ -33,19 +37,20 @@ module.exports = function (global, that) {
 
         if (options) {
           if (options.filters) {
-            // Only add filters if not defined.
-            if (!global.mapTools.maps[that.id].json.filter) {
-              addFilter('json', options.filters);
+
+            // Add filters if not defined.
+            if (!that.json.filter) {
+              addFilter(options.filters);
             }
 
-            global.mapTools.maps[that.id].json.crossfilter.add([feature.data]);
+            that.json.crossfilter.add([feature]);
           }
 
           if (options.style) {
             that.instance.data.overrideStyle(feature, options.style);
           }
         }
-        global.mapTools.maps[that.id].json.all[feature.data.uid] = feature;
+        that.json.all[feature.data.uid] = feature;
       }
     }
   }
diff --git a/lib/map-tools/addFilter.js b/lib/map-tools/addFilter.js
index 5c7dd88..f0eda02 100644
--- a/lib/map-tools/addFilter.js
+++ b/lib/map-tools/addFilter.js
@@ -1,15 +1,15 @@
 var crossfilter = require('crossfilter');
 
-module.exports = function(global, that) {
+var utils = require('map-tools/utils');
 
-  function defaultDimension(item) {
-    return function(d) {return d[item];};
-  }
+module.exports = function(global, that, type) {
+
+
+  function addFilter(filters) {
 
-  function addFilter(type, filters) {
+    that[type].crossfilter = that[type].crossfilter || crossfilter([]);
+    that[type].filter = that[type].filter || {};
 
-    global.mapTools.maps[that.id][type].crossfilter = global.mapTools.maps[that.id][type].crossfilter || crossfilter([]);
-    global.mapTools.maps[that.id][type].filter = global.mapTools.maps[that.id][type].filter || {};
     var dimension, item;
 
     if (typeof filters === 'string') {
@@ -20,9 +20,9 @@ module.exports = function(global, that) {
       if (filters.hasOwnProperty(dimension)) {
         item = filters[dimension];
         if (typeof item === 'string') {
-          global.mapTools.maps[that.id][type].filter[item] = global.mapTools.maps[that.id][type].crossfilter.dimension(defaultDimension(item));
+          that[type].filter[item] = that[type].crossfilter.dimension(utils.defaultDimension(item));
         } else {
-          global.mapTools.maps[that.id][type].filter[Object.keys(item)[0]] = global.mapTools.maps[that.id][type].crossfilter.dimension(item[Object.keys(item)[0]]);
+          that[type].filter[Object.keys(item)[0]] = that[type].crossfilter.dimension(item[Object.keys(item)[0]]);
         }
       }
     }
diff --git a/lib/map-tools/addMarker.js b/lib/map-tools/addMarker.js
index 39ae3e3..56c2aad 100644
--- a/lib/map-tools/addMarker.js
+++ b/lib/map-tools/addMarker.js
@@ -1,10 +1,11 @@
 /*jslint node: true */
 "use strict";
 var utils = require('map-tools/utils');
+var crossfilter = require('crossfilter');
 
 module.exports = function (global, that) {
   var infoWindow = require('map-tools/infoWindow')(global, that);
-  var addFilter = require('map-tools/addFilter')(global, that);
+  var addFilter = require('map-tools/addFilter')(global, that, 'markers');
 
   function addMarker(marker, options) {
 
@@ -44,23 +45,33 @@ module.exports = function (global, that) {
     }
 
 
-
     if (that.uid && marker[that.uid]) {
       marker.data.uid = marker[that.uid];
+      marker.uid = marker.data.uid;
+    }
+
+    if (marker.uid && !marker.data.uid) {
+      marker.data.uid = marker.uid;
     }
 
-    if (!marker.data.uid) {
+
+    if (!marker.uid) {
       marker.data.uid = utils.createUid();
+      marker.uid = marker.data.uid;
     }
 
+
     if (options && options.filters) {
       // Only add filters if not defined.
       if (!global.mapTools.maps[that.id].markers.filter) {
-        addFilter('markers', options.filters);
+        addFilter(options.filters);
       }
-      that.markers.crossfilter.add([marker.data]);
     }
 
+    that.markers.crossfilter = that.markers.crossfilter || crossfilter([]);
+    that.markers.filter = that.markers.filter || {};
+    that.markers.crossfilter.add([marker]);
+
     var instance = new global.google.maps.Marker(marker);
 
     if (marker.move) {
@@ -85,7 +96,7 @@ module.exports = function (global, that) {
 
     // Adds Marker Reference of each Marker to "markers.all"
     that.markers.all = global.mapTools.maps[that.id].markers.all || {};
-    that.markers.all[marker.data.uid] = instance;
+    that.markers.all[marker.uid] = instance;
 
     return instance;
   }
diff --git a/lib/map-tools/filter.js b/lib/map-tools/filter.js
index e3315ff..fd43e67 100644
--- a/lib/map-tools/filter.js
+++ b/lib/map-tools/filter.js
@@ -2,6 +2,8 @@ var utils = require('map-tools/utils');
 
 module.exports = function(global, that, type) {
 
+  var addFilter = require('map-tools/addFilter')(global, that, type);
+
   var orderLookup = {
     ASC: 'top',
     DESC: 'bottom'
@@ -9,12 +11,13 @@ module.exports = function(global, that, type) {
 
   function filter(args, options) {
 
+    // Return All items if no arguments are supplied
     if (typeof args === 'undefined' && typeof options === 'undefined') {
       return utils.toArray(that[type].all);
     };
 
 
-    var dimension, order, limit, query, result;
+    var dimension, order, limit, query;
 
     if (typeof args === 'string') {
       dimension = args;
@@ -23,7 +26,10 @@ module.exports = function(global, that, type) {
       query = args[dimension];
     }
 
-    if (that[type].filter[dimension]) {
+    // Add Crossfilter Dimension if it does not exist.
+    if (!that[type].filter[dimension]) {
+      addFilter(dimension);
+    }
 
       order = (options && options.order && orderLookup[options.order]) ?
         orderLookup[options.order]
@@ -31,18 +37,27 @@ module.exports = function(global, that, type) {
 
       limit = (options && options.limit) ? options.limit : Infinity;
 
-      query = query || null;
 
-      result = that[type].filter[dimension].filter(query)[order](limit);
+      if (typeof query === 'undefined') {
+        query = null;
+      }
+
+      var finds = that[type].filter[dimension].filter(query)[order](limit);
+
+      //console.log(type, dimension, query, order, limit, finds);
+
+      var result = [], x;
+
+      for (x in finds) {
+          result.push(that[type].all[finds[x].uid]);
+      }
 
       if (limit === 1) {
         return result[0];
       }
 
-      return result;
-    }
 
-    return false;
+      return result;
 
   }
 
diff --git a/lib/map-tools/index.js b/lib/map-tools/index.js
index 7ade1cb..2e6ee7f 100644
--- a/lib/map-tools/index.js
+++ b/lib/map-tools/index.js
@@ -18,7 +18,7 @@ module.exports = function (global) {
     this.updateFeature = require('map-tools/updateFeature')(global, that);
     this.addPanel = require('map-tools/addPanel')(global, that);
     this.updateMarker = require('map-tools/updateMarker')(global, that).update;
-    this.filterFeature = require('map-tools/filter')(global, that, 'json');
+    this.findFeature = require('map-tools/filter')(global, that, 'json');
     this.findMarker = require('map-tools/filter')(global, that, 'markers');
     this.addGroup = require('map-tools/groups')(global, that).addGroup;
     this.updateGroup = require('map-tools/groups')(global, that).updateGroup;
diff --git a/lib/map-tools/utils.js b/lib/map-tools/utils.js
index 75a5d14..714b40a 100644
--- a/lib/map-tools/utils.js
+++ b/lib/map-tools/utils.js
@@ -52,10 +52,26 @@ function toArray(obj) {
   return Object.keys(obj).map(function(key){ return obj[key] });
 }
 
+function defaultDimension(item) {
+  return function(d) {
+
+    if (typeof(d.data[item]) === "undefined" && typeof(d[item]) !== "undefined") {
+      return d[item];
+    }
+
+    if (typeof(d.data[item]) === "undefined" && typeof(d[item]) === "undefined") {
+      return null;
+    }
+
+    return d.data[item];
+  };
+}
+
 module.exports = {
   clone: clone,
   createUid: createUid,
   prepareOptions: prepareOptions,
   isArray: isArray,
-  toArray: toArray
+  toArray: toArray,
+  defaultDimension: defaultDimension
 };


Test patch:
diff --git a/test/unit/addFilter.Spec.js b/test/unit/addFilter.Spec.js
index d416eba..6b38f5f 100644
--- a/test/unit/addFilter.Spec.js
+++ b/test/unit/addFilter.Spec.js
@@ -3,16 +3,14 @@ describe('Given the addFilter Module', function () {
   it('should add a filter to Markers', function () {
     var global = {mapTools: {
       maps: {mymap: {
-        markers: {
-
-        }
+        markers: {}
       }}
     }};
-    var that = {id: 'mymap'};
-    var addFilter = require('map-tools/addFilter')(global, that);
-    addFilter('markers', 'myFilter');
-    expect(global.mapTools.maps.mymap.markers.filter.myFilter).to.be.a('object');
-    expect(global.mapTools.maps.mymap.markers.filter.myFilter.top()).to.eql([]);
+    var that = {id: 'mymap', markers: {}};
+    var addFilter = require('map-tools/addFilter')(global, that, 'markers');
+    addFilter('myFilter');
+    expect(that.markers.filter.myFilter).to.be.a('object');
+    expect(that.markers.filter.myFilter.top()).to.eql([]);
 
   });
 
@@ -22,11 +20,11 @@ describe('Given the addFilter Module', function () {
         markers: {}
       }}
     }};
-    var that = {id: 'mymap'};
-    var addFilter = require('map-tools/addFilter')(global, that);
-    addFilter('markers', ['myFilter1', 'myFilter2']);
-    expect(global.mapTools.maps.mymap.markers.filter.myFilter1).to.be.a('object');
-    expect(global.mapTools.maps.mymap.markers.filter.myFilter2).to.be.a('object');
+    var that = {id: 'mymap', markers: {}};
+    var addFilter = require('map-tools/addFilter')(global, that, 'markers');
+    addFilter(['myFilter1', 'myFilter2']);
+    expect(that.markers.filter.myFilter1).to.be.a('object');
+    expect(that.markers.filter.myFilter2).to.be.a('object');
 
   });
 });
diff --git a/test/unit/filter.Spec.js b/test/unit/filter.Spec.js
index 9d402e6..ca04e58 100644
--- a/test/unit/filter.Spec.js
+++ b/test/unit/filter.Spec.js
@@ -3,18 +3,29 @@ describe('Given the filterFeature Module', function () {
   var that, filterFeature, data;
 
   beforeEach(function () {
-    data = [{NAME: 'Gert'}, {NAME: 'Peter', age: 23}, {NAME: 'Peter', age: 35}, {NAME: 'Max'}];
+    data = [
+      {data: {NAME: 'Gert'}, uid: 1},
+      {data: {NAME: 'Peter', age: 23}, uid: 2},
+      {data: {NAME: 'Peter', age: 35}, uid: 3},
+      {data: {NAME: 'Max'}, uid: 4}
+    ];
 
     var cf = crossfilter(data);
 
+
     that = {
       json: {
+        all: {},
         filter: {
-          NAME: cf.dimension(function(d) {return d.NAME;})
+          NAME: cf.dimension(function(d) {return d.data.NAME;})
         }
       }
     };
 
+    for (var x in data) {
+      that.json.all[data[x].uid] = data[x];
+    }
+
     filterFeature = require('map-tools/filter')({}, that, 'json');
   });