Skip to content

Commit

Permalink
Less property chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 16, 2014
1 parent 639c9bc commit 43df2ac
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion js/render/drawraster.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function findParent(tile) {
var source = tile.source;
if (!source) return;
var parentTiles = {};
source._findLoadedParent(tile.id, source.options.minZoom, parentTiles);
source._findLoadedParent(tile.id, source.minzoom, parentTiles);
return source.tiles[Object.keys(parentTiles)[0]];
}

Expand Down
6 changes: 2 additions & 4 deletions js/source/geojsonsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ var GeoJSONSource = module.exports = function(options) {
this.maxTileZoom = this.zooms[this.zooms.length - 1];

this.loadNewTiles = true;
this.tileJSON = {
minZoom: 1,
maxZoom: 13
};
this.minzoom = 1;
this.maxzoom = 13;

this.data = options.data;
};
Expand Down
43 changes: 24 additions & 19 deletions js/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ module.exports = Source;

function Source(options) {
this.enabled = false;
this.type = options.type;
if (this.type === 'vector' && options.tileSize && options.tileSize !== 512) {

util.extend(this, util.pick(options,
'type', 'url', 'tileSize'));

if (this.type === 'vector' && this.tileSize !== 512) {
throw new Error('vector tile sources must have a tileSize of 512');
}

this.Tile = this.type === 'vector' ? VectorTile : RasterTile;
this.options = util.inherit(this.options, options);

this._tiles = {};
this._cache = new Cache(this.options.cacheSize, function(tile) {
this._cache = new Cache(this.cacheSize, function(tile) {
tile.remove();
});

Expand All @@ -32,16 +35,18 @@ function Source(options) {
if (!tileJSON.tiles)
throw new Error('missing tiles property');

this.tileJSON = util.extend({ minzoom: 0, maxzoom: 22 }, tileJSON);
util.extend(this, util.pick(tileJSON,
'tiles', 'minzoom', 'maxzoom', 'attribution'));

this.loadNewTiles = true;
this.enabled = true;
this.update();

if (this.map) this.map.fire('source.add', {source: this});
}.bind(this);

if (options.url) {
ajax.getJSON(normalizeURL(options.url), loaded);
if (this.url) {
ajax.getJSON(normalizeURL(this.url), loaded);
} else {
loaded(null, options);
}
Expand All @@ -50,10 +55,10 @@ function Source(options) {
}

Source.prototype = util.inherit(Evented, {
options: {
tileSize: 512,
cacheSize: 20
},
minzoom: 0,
maxzoom: 22,
tileSize: 512,
cacheSize: 20,

onAdd: function(map) {
this.map = map;
Expand Down Expand Up @@ -121,16 +126,16 @@ Source.prototype = util.inherit(Evented, {

// get the zoom level adjusted for the difference in map and source tilesizes
_getZoom: function() {
var zOffset = Math.log(this.map.transform.tileSize/this.options.tileSize) / Math.LN2;
var zOffset = Math.log(this.map.transform.tileSize / this.tileSize) / Math.LN2;
return this.map.transform.zoom + zOffset;
},

_coveringZoomLevel: function(zoom) {
for (var z = this.tileJSON.maxzoom; z >= this.tileJSON.minzoom; z--) {
for (var z = this.maxzoom; z >= this.minzoom; z--) {
if (z <= zoom) {
if (this.type === 'raster') {
// allow underscaling by rounding to the nearest zoom level
if (z < this.tileJSON.maxzoom) {
if (z < this.maxzoom) {
z += Math.round(zoom - z);
}
}
Expand All @@ -141,8 +146,8 @@ Source.prototype = util.inherit(Evented, {
},

_childZoomLevel: function(zoom) {
zoom = Math.max(this.tileJSON.minzoom, zoom + 1);
return zoom <= this.tileJSON.maxzoom ? zoom : null;
zoom = Math.max(this.minzoom, zoom + 1);
return zoom <= this.maxzoom ? zoom : null;
},

_getCoveringTiles: function(zoom) {
Expand Down Expand Up @@ -254,8 +259,8 @@ Source.prototype = util.inherit(Evented, {
var tile;

// Determine the overzooming/underzooming amounts.
var minCoveringZoom = Math.max(this.tileJSON.minzoom, zoom - 10);
var maxCoveringZoom = this.tileJSON.minzoom;
var minCoveringZoom = Math.max(this.minzoom, zoom - 10);
var maxCoveringZoom = this.minzoom;
while (maxCoveringZoom < zoom + 1) {
var level = this._childZoomLevel(maxCoveringZoom);
if (level === null) break;
Expand Down Expand Up @@ -335,7 +340,7 @@ Source.prototype = util.inherit(Evented, {

if (pos.w === 0) {
// console.time('loading ' + pos.z + '/' + pos.x + '/' + pos.y);
var url = TileCoord.url(id, this.tileJSON.tiles);
var url = TileCoord.url(id, this.tiles);
tile = this._tiles[id] = new this.Tile(id, this, url, tileComplete);
} else {
var wrapped = TileCoord.toID(pos.z, pos.x, pos.y, 0);
Expand Down
7 changes: 3 additions & 4 deletions js/source/vectortile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ function VectorTile(id, source, url, callback) {
this.url = url;
this.zoom = TileCoord.fromID(id).z;
this.map = source.map;
this.options = source.options;
this.id = util.uniqueId();
this.callback = callback;
this.source = source;

if (this.zoom >= source.tileJSON.maxzoom) {
if (this.zoom >= source.maxzoom) {
this.depth = this.map.options.maxZoom - this.zoom;
} else {
this.depth = 1;
Expand All @@ -37,8 +36,8 @@ VectorTile.prototype = util.inherit(Tile, {
url: this.url,
id: this.id,
zoom: this.zoom,
maxZoom: this.source.tileJSON.maxzoom,
tileSize: this.options.tileSize,
maxZoom: this.source.maxzoom,
tileSize: this.source.tileSize,
source: this.source.id,
depth: this.depth
}, function(err, data) {
Expand Down
4 changes: 2 additions & 2 deletions js/ui/control/attribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Attribution.prototype = util.inherit(Control, {
var attrObj = {};
for (var id in this._map.sources) {
var source = this._map.sources[id];
if (source.tileJSON && source.tileJSON.attribution) {
attrObj[source.tileJSON.attribution] = true;
if (source.attribution) {
attrObj[source.attribution] = true;
}
}
var attributions = [];
Expand Down
11 changes: 11 additions & 0 deletions js/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ exports.inherit = function (parent, props) {
return proto;
};

exports.pick = function (src) {
var result = {};
for (var i = 1; i < arguments.length; i++) {
var k = arguments[i];
if (k in src) {
result[k] = src[k];
}
}
return result;
};

var id = 1;

exports.uniqueId = function () {
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/source.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"tiles": ["http://example.com/{z}/{x}/{y}.png"]
"tiles": ["http://example.com/{z}/{x}/{y}.png"],
"minzoom": 1,
"maxzoom": 10,
"attribution": "Mapbox"
}
13 changes: 11 additions & 2 deletions test/js/source/source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ test('Source', function(t) {
t.test('can be constructed from TileJSON', function(t) {
var source = new Source({
type: "vector",
minzoom: 1,
maxzoom: 10,
attribution: "Mapbox",
tiles: ["http://example.com/{z}/{x}/{y}.png"]
});

t.ok(source.enabled);
t.deepEqual(source.tileJSON.tiles, ["http://example.com/{z}/{x}/{y}.png"]);
t.deepEqual(source.tiles, ["http://example.com/{z}/{x}/{y}.png"]);
t.deepEqual(source.minzoom, 1);
t.deepEqual(source.maxzoom, 10);
t.deepEqual(source.attribution, "Mapbox");
t.end();
});

Expand All @@ -32,7 +38,10 @@ test('Source', function(t) {
source.map = {
fire: function() {
t.ok(source.enabled);
t.deepEqual(source.tileJSON.tiles, ["http://example.com/{z}/{x}/{y}.png"]);
t.deepEqual(source.tiles, ["http://example.com/{z}/{x}/{y}.png"]);
t.deepEqual(source.minzoom, 1);
t.deepEqual(source.maxzoom, 10);
t.deepEqual(source.attribution, "Mapbox");
t.end();
}
};
Expand Down

0 comments on commit 43df2ac

Please sign in to comment.