Skip to content

Commit

Permalink
Allow geoJson.clip to be set after add.
Browse files Browse the repository at this point in the history
Setting geoJson.tile(false) also explicitly, rather than implicitly, disables
clipping. The size of the clipping rect is inferred from the tile size when the
map is set (as before); this may change in the future if the map dispatches an
event when the tile size changes.

Also in this commit, the features associated with a geoJson tile are now
accessible as the `features` attribute of the tile. This is distinct from the
`features` attribute of the event, which only includes the updated features.
  • Loading branch information
Mike Bostock committed Sep 9, 2010
1 parent 0cfd053 commit 692eb1c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 106 deletions.
53 changes: 28 additions & 25 deletions polymaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ if (!org) var org = {};
if (!org.polymaps) org.polymaps = {};
(function(po){

po.version = "2.1+2.1"; // This fork not semver!
po.version = "2.1+2.1+1"; // This fork not semver!

var zero = {x: 0, y: 0};
po.id = (function() {
Expand Down Expand Up @@ -1123,14 +1123,15 @@ po.geoJson = function(fetch) {
container = geoJson.container(),
url = "about:blank",
clip = true,
clipId,
clipPath,
clipRect,
clipId = "org.polymaps." + po.id(),
clipHref = "url(#" + clipId + ")",
clipPath = container.insertBefore(po.svg("clipPath"), container.firstChild),
clipRect = clipPath.appendChild(po.svg("rect")),
zoom = null,
tiles = {},
features;

container.setAttribute("fill-rule", "evenodd");
clipPath.setAttribute("id", clipId);

if (!arguments.length) fetch = po.queue.json;

Expand Down Expand Up @@ -1220,6 +1221,7 @@ po.geoJson = function(fetch) {

function load(tile, proj) {
var g = tile.element = po.svg("g");
tile.features = [];

proj = proj(tile);

Expand Down Expand Up @@ -1252,7 +1254,7 @@ po.geoJson = function(fetch) {
}

tile.ready = true;
updated.push.apply(tiles[tile.key] || (tiles[tile.key] = []), updated);
updated.push.apply(tile.features, updated);
geoJson.dispatch({type: "load", tile: tile, features: updated});
}

Expand All @@ -1261,13 +1263,10 @@ po.geoJson = function(fetch) {
} else {
tile.request = fetch(typeof url == "function" ? url(tile) : url, update);
}

if (clipId) g.setAttribute("clip-path", "url(#" + clipId + ")");
}

function unload(tile) {
if (tile.request) tile.request.abort(true);
delete tiles[tile.key];
}

geoJson.url = function(x) {
Expand All @@ -1286,32 +1285,36 @@ po.geoJson = function(fetch) {

geoJson.clip = function(x) {
if (!arguments.length) return clip;
clip = x;
if (clip) container.removeChild(clipPath);
if (clip = x) container.insertBefore(clipPath, container.firstChild);
var locks = geoJson.cache.locks();
for (var key in locks) {
if (clip) locks[key].element.setAttribute("clip-path", clipHref);
else locks[key].element.removeAttribute("clip-path");
}
return geoJson;
};

var __tile__ = geoJson.tile;
geoJson.tile = function(x) {
if (arguments.length && !x) geoJson.clip(x);
return __tile__.apply(geoJson, arguments);
};

var __map__ = geoJson.map;
geoJson.map = function(x) {
if (x) {
if (clip && geoJson.tile()) {
if (!clipPath) {
clipPath = container.insertBefore(po.svg("clipPath"), container.firstChild);
clipRect = clipPath.appendChild(po.svg("rect"));
clipPath.setAttribute("id", clipId = "org.polymaps." + po.id());
}
var size = x.tileSize();
clipRect.setAttribute("width", size.x);
clipRect.setAttribute("height", size.y);
} else if (clipPath) {
container.removeChild(clipPath);
clipPath = clipRect = clipId = null;
}
if (x && clipRect) {
var size = x.tileSize();
clipRect.setAttribute("width", size.x);
clipRect.setAttribute("height", size.y);
}
return __map__.apply(geoJson, arguments);
};

geoJson.show = function(tile) {
geoJson.dispatch({type: "show", tile: tile, features: tiles[tile.key] || []});
if (clip) tile.element.setAttribute("clip-path", clipHref);
else tile.element.removeAttribute("clip-path");
geoJson.dispatch({type: "show", tile: tile, features: tile.features});
return geoJson;
};

Expand Down
Loading

0 comments on commit 692eb1c

Please sign in to comment.