Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:CartoDB/cartodb.js into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
xavijam committed Jun 27, 2013
2 parents a270c8a + 36fbf74 commit d9fd429
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 27 deletions.
2 changes: 2 additions & 0 deletions doc/API.md
Expand Up @@ -303,6 +303,8 @@ Promise object. You can listen for the following events:
+ **done**: triggered when the layer is created, the layer is passed as first argument. Each layer type has different options, see layers section.
+ **error**: triggered when the layer couldn't be created. The error string is the first argument.

You can call to addTo(map[, position]) in the promise so when the layer is ready it will be added to the map.


###### EXAMPLE

Expand Down
14 changes: 9 additions & 5 deletions examples/gmaps_multilayer.html
Expand Up @@ -25,7 +25,8 @@
<!-- include google maps library *before* load cartodb.js -->
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script>
<!--<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script>-->
<script src="../src/cartodb.js"></script>

<script>

Expand All @@ -50,11 +51,11 @@
sql: 'select * from country_boundaries',
cartocss: '#layer { polygon-fill: #F00; polygon-opacity: 0.3; line-color: #F00; }'
}]
}).done(function(layer) {
})
.addTo(map)
.done(function(layer) {
var population;

map.overlayMapTypes.setAt(0, layer);

// wait a little bit and add the most populated places
setTimeout(function() {
population = layer.createSubLayer({
Expand Down Expand Up @@ -89,7 +90,10 @@

}

window.onload = main;
window.onload = function() {
cdb.load("../src/", main);
}


</script>
</body>
Expand Down
10 changes: 5 additions & 5 deletions examples/leaflet_multilayer.html
Expand Up @@ -23,7 +23,7 @@
<div id="map"></div>

<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script>
<script src="../src/cartodb.js"></script>
<script>

function main() {
Expand All @@ -48,9 +48,10 @@
sql: 'select * from country_boundaries',
cartocss: '#layer { polygon-fill: #F00; polygon-opacity: 0.3; line-color: #F00; }'
}]
}).done(function(layer) {
})
.addTo(map)
.done(function(layer) {
var population;
map.addLayer(layer);

// wait a little bit and add the most populated places
setTimeout(function() {
Expand Down Expand Up @@ -80,10 +81,9 @@
setTimeout(function() {
layer.getSubLayer(0).remove();
}, 10000);


});


}

// you could use $(window).load(main);
Expand Down
32 changes: 20 additions & 12 deletions src/api/layers.js
Expand Up @@ -78,9 +78,16 @@
callback = fn;
}

promise.addTo = function(map, position) {
promise.on('done', function() {
MapType.addLayerToMap(layerView, map, position);
});
return promise;
};

_getLayerJson(layer, function(visData) {

var layerData, MapType;
var layerData;

if(!visData) {
promise.trigger('error');
Expand All @@ -101,17 +108,6 @@
return;
}

// check map type
// TODO: improve checking
if(typeof(map.overlayMapTypes) !== "undefined") {
MapType = cdb.geo.GoogleMapsMapView;
// check if leaflet is loaded globally
} else if(map instanceof L.Map || (window.L && map instanceof window.L.Map)) {
MapType = cdb.geo.LeafletMapView;
} else {
promise.trigger('error', "cartodb.js can't guess the map type");
return;
}

// update options
if(options && !_.isFunction(options)) {
Expand All @@ -124,6 +120,18 @@
https: false
})

// check map type
// TODO: improve checking
if(typeof(map.overlayMapTypes) !== "undefined") {
MapType = cdb.geo.GoogleMapsMapView;
// check if leaflet is loaded globally
} else if(map instanceof L.Map || (window.L && map instanceof window.L.Map)) {
MapType = cdb.geo.LeafletMapView;
} else {
promise.trigger('error', "cartodb.js can't guess the map type");
return promise;
}

// create a dummy viz
var viz = map.viz;
if(!viz) {
Expand Down
6 changes: 6 additions & 0 deletions src/geo/gmaps/gmaps.js
Expand Up @@ -186,6 +186,7 @@ if(typeof(google) != "undefined" && typeof(google.maps) != "undefined") {

},


latLonToPixel: function(latlon) {
return this.projector.latLngToPixel(new google.maps.LatLng(latlon[0], latlon[1]));
},
Expand Down Expand Up @@ -275,6 +276,11 @@ if(typeof(google) != "undefined" && typeof(google.maps) != "undefined") {

}, {

addLayerToMap: function(layer, map, pos) {
pos = pos || 0;
map.overlayMapTypes.setAt(pos, layer);
},

/**
* create the view for the geometry model
*/
Expand Down
13 changes: 13 additions & 0 deletions src/geo/layer_definition.js
Expand Up @@ -19,6 +19,8 @@ function LayerDefinition(layerDefinition, options) {
this._timeout = -1;
this._queue = [];
this._waiting = false;
this.lastTimeUpdated = null;
this._refreshTimer = -1;
}

/**
Expand Down Expand Up @@ -167,7 +169,17 @@ LayerDefinition.prototype = {
},

_requestFinished: function() {
var self = this;
this._waiting = false;
this.lastTimeUpdated = new Date().getTime();

// refresh layer when invalidation time has passed
clearTimeout(this._refreshTimer);
this._refreshTimer = setTimeout(function() {
self.invalidate();
}, this.options.refreshTime || (60*5*1000));

// check request queue
if(this._queue.length) {
this._getLayerToken(this._queue.pop());
}
Expand Down Expand Up @@ -239,6 +251,7 @@ LayerDefinition.prototype = {
var self = this;
var params = [];
callback = callback || function() {};

// if the previous request didn't finish, queue it
if(this._waiting) {
this._queue.push(callback);
Expand Down
7 changes: 6 additions & 1 deletion src/geo/leaflet/leaflet.js
Expand Up @@ -297,8 +297,13 @@
return layer_view;
},

addLayerToMap: function(layer_view, map) {
addLayerToMap: function(layer_view, map, pos) {
map.addLayer(layer_view.leafletLayer);
if(pos != undefined) {
if(v.setZIndex) {
v.setZIndex(pos);
}
}
},

/**
Expand Down
9 changes: 7 additions & 2 deletions src/geo/ui/layer_selector.js
Expand Up @@ -73,6 +73,11 @@ cdb.geo.ui.LayerSelector = cdb.core.View.extend({
var m = new cdb.core.Model(l);
m.set('order', i);
m.set('type', 'layergroup');
if(self.options.layer_names) {
m.set('layer_name', self.options.layer_names[i]);
} else {
m.set('layer_name', l.options.layer_name);
}
var layerView = self._createLayer('LayerViewFromLayerGroup', { model: m, layer_definition: layer_definition });
layerView.bind('switchChanged', self._setCount, self);
layerView.bind('layergroupChanged', self._setLayerGroup, self)
Expand Down Expand Up @@ -220,7 +225,7 @@ cdb.geo.ui.LayerViewFromLayerGroup = cdb.geo.ui.LayerView.extend({

defaults: {
template: '\
<a class="layer" href="#/change-layer"><%= options.layer_name %></a>\
<a class="layer" href="#/change-layer"><%= layer_name %></a>\
<a href="#switch" class="right <%= visible ? "enabled" : "enabled" %> switch"><span class="handle"></span></a>\
'
},
Expand All @@ -231,4 +236,4 @@ cdb.geo.ui.LayerViewFromLayerGroup = cdb.geo.ui.LayerView.extend({

this.trigger('layergroupChanged', this.options.layer_definition);
}
});
});
3 changes: 2 additions & 1 deletion src/vis/overlays.js
Expand Up @@ -125,7 +125,8 @@ cdb.vis.Overlay.register('layer_selector', function(data, vis) {
var layerSelector = new cdb.geo.ui.LayerSelector({
mapView: vis.mapView,
template: template,
dropdown_template: dropdown_template
dropdown_template: dropdown_template,
layer_names: data.layer_names
});


Expand Down
33 changes: 32 additions & 1 deletion test/spec/api/layers.spec.js
Expand Up @@ -28,7 +28,7 @@ describe('api.layers', function() {
err = true;
});
})
waits(10);
waits(100);
runs(function() {
expect(err).toEqual(true);
});
Expand Down Expand Up @@ -259,6 +259,37 @@ describe('api.layers', function() {

});

it("should have addTo", function() {
var layer;
runs(function() {
debugger;
cartodb.createLayer(map, {
type: 'cartodb',
sublayers: [{
sql: 'select * from table',
cartocss: 'test',
interactivity: 'testi'
}]
})
.addTo(map)
.done(function(lyr) {
layer = lyr;
});
});

waits(100);

runs(function() {
expect(layer).not.toEqual(undefined);
if(map.overlayMapTypes) {
expect(layer).toBe(map.overlayMapTypes.getAt(0));
} else {
expect(layer).toBe(map._layers[L.stamp(layer)]);
}
});

});

//});

});
Expand Down
23 changes: 23 additions & 0 deletions test/spec/api/layers/cartodb.spec.js
Expand Up @@ -154,6 +154,29 @@ describe('api.layers.cartodb', function() {
});
});

it("should add to the map when done", function() {
runs(function() {
cartodb.createLayer(map, {
kind: 'cartodb',
options: {
table_name: 'test',
user_name: 'test',
tile_style: 'test'
},
infowindow: {
template: '<div></div>',
fields: [{name: 'test', title: true, order: 0}]
}
}, {
interactivity: 'myname,jaja',
interaction: false
}, function(l) {
addFn(map, l);
layer = l;
});
});
});


};

Expand Down
16 changes: 16 additions & 0 deletions test/spec/geo/layer_definition.spec.js
Expand Up @@ -228,6 +228,22 @@ describe("LayerDefinition", function() {
});
});

it("should set refresh timer after being updated", function() {
layerDefinition.options.refreshTime = 10;
layerDefinition.options.ajax = function(p) {
params = p;
p.success({ layergroupid: 'test' });
};
runs(function() {
layerDefinition.getTiles(function(tiles) {});
});
spyOn(layerDefinition,'invalidate');
waits(200);
runs(function() {
expect(layerDefinition.invalidate).toHaveBeenCalled();
});
});

describe("sublayers", function() {

it("should create sublayer", function() {
Expand Down

0 comments on commit d9fd429

Please sign in to comment.