Skip to content

Commit d703f81

Browse files
committed
Merge pull request #95 from donmccurdy/feat-single-select
Add touch support for tract selection/highlighting. (with multi-selection)
2 parents 28a7f5f + da218c0 commit d703f81

File tree

3 files changed

+102
-52
lines changed

3 files changed

+102
-52
lines changed

src/scripts/censusLayer.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -277,31 +277,29 @@ define([
277277
});
278278

279279
var self = this;
280-
$.each(geojsonLayer._layers,function(idx,layer){
281-
var feature = layer.feature;
282-
layer.feature.map_mouse_over = false;
283-
hoverTract.watchForValue(feature.id,
284-
function() {
285-
layer.setStyle(self._styleFunction(feature, true));
286-
},
287-
function() {
288-
layer.setStyle(self._styleFunction(feature));
289-
});
290-
layer.on("mouseover", function (e) {
291-
hoverTract.select(feature.id,
292-
feature,
293-
self.getPropertyData(),
294-
colorbrewer[self._colorBrewerName]);
295-
feature.map_mouse_over = true;
296-
console.log(feature);
297-
});
298-
layer.on("mouseout", function (e) {
299-
feature.map_mouse_over = false;
300-
// TODO: Move this somewhere else
301-
hoverTract.select(null);
302-
});
303-
304-
280+
$.each(geojsonLayer._layers,function(idx,layer){
281+
var feature = layer.feature;
282+
feature.highlighted = false;
283+
hoverTract.watchForValue(feature.id,
284+
function() {
285+
feature.highlighted = true;
286+
layer.setStyle(self._styleFunction(feature));
287+
}, function() {
288+
feature.highlighted = false;
289+
layer.setStyle(self._styleFunction(feature));
290+
});
291+
layer.on("mouseover click", function (e) {
292+
if (!hoverTract.contains(feature.id)) {
293+
hoverTract.clear();
294+
hoverTract.select(feature.id,
295+
feature,
296+
self.getPropertyData(),
297+
colorbrewer[self._colorBrewerName]);
298+
}
299+
});
300+
layer.on("mouseout", function (e) {
301+
hoverTract.deselect(feature.id);
302+
});
305303
});
306304

307305
if (this._layers_id[add[i]]) {
@@ -318,7 +316,7 @@ define([
318316

319317
},
320318

321-
_styleFunction : function(feature,highlight) {
319+
_styleFunction : function(feature) {
322320

323321
if (this._itemStyle){
324322
return this._itemStyle(feature);
@@ -331,8 +329,8 @@ define([
331329
var val = parseFloat(feature.properties[this._currentProperty]);
332330

333331
if (val) {
334-
if ((typeof(highlight)!=="undefined") || feature.map_mouse_over==true) {
335-
return {"fillColor": this._getColor(prop.serie, val) , "color" : "#404040" , "weight": 4 , "opacity" : 1.0, "fillOpacity": 0.6};
332+
if (feature.highlighted) {
333+
return {"fillColor": this._getColor(prop.serie, val) , "color" : "#404040" , "weight": 4 , "opacity" : 1.0, "fillOpacity": 0.6};
336334
} else {
337335
return {"fillColor": this._getColor(prop.serie,val) , "weight": 0 , "opacity" : 0.0, "fillOpacity": 0.6};
338336
}

src/scripts/iwindow.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,14 @@ define(['jquery', 'underscore', "hoverTract", "variables", "colors"], function($
9999
currentVar = varname;
100100
render();
101101
});
102-
hoverTract.watch(function(_, _, feature, prop, colors) {
103-
//console.log(feature);
104-
currentFeature = feature;
105-
currentProp = prop;
106-
currentColors = colors;
102+
103+
// Watch for changes, ignoring any selected
104+
// tracts after the first.
105+
hoverTract.watch(function(tracts) {
106+
var tract = _.first(tracts) || {};
107+
currentFeature = tract[0];
108+
currentProp = tract[1];
109+
currentColors = tract[2];
107110
render();
108111
});
109112
}

src/scripts/subSelect.js

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ define(["underscore"], function(_) {
88

99
// Callbacks that run whenever the selection changes.
1010
callbacks: [],
11-
selected: null
11+
12+
// Selected values, stored as key/value pairs.
13+
selected: {}
1214
};
1315

1416
return {
@@ -37,30 +39,77 @@ define(["underscore"], function(_) {
3739
pub.callbacks.push(handler);
3840
},
3941

40-
// Sets the currently selected value to v and informs
41-
// all subscribers.
42-
select: function(v) {
43-
var old = pub.selected,
44-
args = _.rest(arguments);
42+
/**
43+
* Publish updates to all subscribers, and (optionally)
44+
* all watchers on a given key.
45+
*
46+
* @param {array|string} keys Key/keys for which to
47+
* update subscribers.
48+
*/
49+
publish: function(keys) {
50+
keys = keys || [];
51+
keys = _.isArray(keys) ? keys : [keys];
4552

46-
if (old) {
47-
_.each(_.pluck(pub.subscribers[old], 1), function(f) {
48-
if (f) f.apply(this, args);
49-
});
50-
}
53+
var selected = _.values(pub.selected);
5154

52-
if (v) {
53-
_.each(_.pluck(pub.subscribers[v], 0), function(f) {
54-
if (f) f.apply(this, args);
55-
});
56-
}
55+
var self = this;
56+
_.each(keys, function (key) {
57+
_.chain(pub.subscribers[key])
58+
.pluck(self.contains(key) ? 0 : 1)
59+
.each(function (f) {
60+
if (f) f.apply(this);
61+
});
62+
});
5763

5864
_.each(pub.callbacks, function(f) {
59-
if (f) f.apply(this, [v, old].concat(args));
65+
if (f) f.apply(this, [selected]);
6066
});
67+
},
6168

62-
pub.selected = v;
63-
return old;
69+
/**
70+
* Adds v to the currently selected set and informs
71+
* all subscribers.
72+
*
73+
* @param {string} v Key/ID of selection.
74+
* @param {Object...} args Arguments associated with the selection.
75+
*/
76+
select: function(v) {
77+
var args = _.rest(arguments);
78+
pub.selected[v] = args;
79+
this.publish(v);
80+
return _.values(pub.selected);
81+
},
82+
83+
/**
84+
* Removes v from the currently selected set and
85+
* informs all subscribers.
86+
*
87+
* @param {string} v Key/ID of selection.
88+
* @param {Object...} args Arguments associated with the selection.
89+
*/
90+
deselect: function(v) {
91+
pub.selected = _.omit(pub.selected, v.toString());
92+
this.publish(v);
93+
return _.values(pub.selected);
94+
},
95+
96+
/**
97+
* Clear all selections and inform subscribers.
98+
*/
99+
clear: function() {
100+
var keys = _.keys(pub.selected);
101+
pub.selected = {};
102+
this.publish(keys);
103+
return pub.selected;
104+
},
105+
106+
/**
107+
* Returns true if the key is selected, else false.
108+
*
109+
* @param {string} v Key/ID of selection.
110+
*/
111+
contains: function(v) {
112+
return !!pub.selected[v];
64113
}
65114
};
66115
}

0 commit comments

Comments
 (0)