Ox.Map bugfixes & cleanup: make setting 'places' and 'selected' options at the same time work, fix meta key handler, remove misnamed 'addPlaces', fix potential JS error in 'addPlaceToPlaces', make sure self.places contains only the visible places

This commit is contained in:
rlx 2012-09-05 10:04:23 +00:00
parent d5da93f59e
commit 4105f79106

View file

@ -127,12 +127,17 @@ Ox.Map = function(options, self) {
place.id = Ox.encodeBase32(Ox.uid()); place.id = Ox.encodeBase32(Ox.uid());
} }
}); });
if (self.options.selected && !Ox.getObjectById(
self.options.places, self.options.selected
)) {
self.options.selected = '';
selectPlace(null);
}
self.options.places = Ox.api(self.options.places, { self.options.places = Ox.api(self.options.places, {
geo: true, geo: true,
sort: '-area' sort: '-area'
}); });
} }
addPlaces();
getMapBounds(function(mapBounds) { getMapBounds(function(mapBounds) {
if (mapBounds) { if (mapBounds) {
self.map.fitBounds(mapBounds); self.map.fitBounds(mapBounds);
@ -143,15 +148,8 @@ Ox.Map = function(options, self) {
// fixme: the following is just a guess // fixme: the following is just a guess
self.boundsChanged = true; self.boundsChanged = true;
mapChanged(); mapChanged();
self.options.selected && selectPlace(self.options.selected);
}); });
if (self.options.selected) {
if (getSelectedPlace()) {
selectPlace(self.options.selected);
} else {
self.options.selected = '';
that.triggerEvent('select', null);
}
}
}, },
selected: function() { selected: function() {
selectPlace(self.options.selected || null); selectPlace(self.options.selected || null);
@ -188,16 +186,7 @@ Ox.Map = function(options, self) {
key_left: function() { key_left: function() {
pan(-1, 0); pan(-1, 0);
}, },
// FIXME: 'key_meta': function() {
'key_meta.left': function() {
self.metaKey = true;
$(document).one({
keyup: function() {
self.metaKey = false;
}
});
},
'key_meta.right': function() {
self.metaKey = true; self.metaKey = true;
$(document).one({ $(document).one({
keyup: function() { keyup: function() {
@ -277,6 +266,7 @@ Ox.Map = function(options, self) {
'lat', 'lng', 'south', 'west', 'north', 'east', 'area', 'lat', 'lng', 'south', 'west', 'north', 'east', 'area',
'editable' 'editable'
].concat(self.options.keys); ].concat(self.options.keys);
self.places = [],
self.resultPlace = null; self.resultPlace = null;
self.scaleMeters = [ self.scaleMeters = [
50000000, 20000000, 10000000, 50000000, 20000000, 10000000,
@ -543,25 +533,6 @@ Ox.Map = function(options, self) {
); );
} }
function addPlaces() {
Ox.forEach(self.$placeControls, function($placeControl) {
$placeControl.hide();
});
self.places && self.places.forEach(function(place) {
place.remove();
});
self.places = [];
/*
if (!self.isAsync) {
self.options.places.forEach(function(place) {
self.places.push(new Ox.MapPlace(Ox.extend({
map: that
}, place)));
});
}
*/
}
function addPlaceToMap(place) { function addPlaceToMap(place) {
// via find, click, shift-click, or new place button // via find, click, shift-click, or new place button
Ox.Log('Map', 'addPlaceToMap', place) Ox.Log('Map', 'addPlaceToMap', place)
@ -607,7 +578,7 @@ Ox.Map = function(options, self) {
function addPlaceToPlaces(data) { function addPlaceToPlaces(data) {
var place = getSelectedPlace(), var place = getSelectedPlace(),
country = Ox.getCountryByGeoname(place.geoname); country = place ? Ox.getCountryByGeoname(place.geoname) : {};
Ox.extend(place, data); Ox.extend(place, data);
self.options.selected = place.id; self.options.selected = place.id;
place.countryCode = country ? country.code : ''; place.countryCode = country ? country.code : '';
@ -906,8 +877,6 @@ Ox.Map = function(options, self) {
} }
that.overlayView.draw(); that.overlayView.draw();
addPlaces();
Ox.forEach(self.$controls, function($control) { Ox.forEach(self.$controls, function($control) {
$control.appendTo(self.$map); $control.appendTo(self.$map);
}); });
@ -971,7 +940,8 @@ Ox.Map = function(options, self) {
} }
function mapChanged() { function mapChanged() {
// gets called after panning or zooming // This is the handler that actually adds the places to the map.
// Gets called after panning or zooming, and when the map is idle.
if (self.boundsChanged) { if (self.boundsChanged) {
var bounds = self.map.getBounds(), var bounds = self.map.getBounds(),
southWest = bounds.getSouthWest(), southWest = bounds.getSouthWest(),
@ -997,29 +967,34 @@ Ox.Map = function(options, self) {
range: [0, self.options.maxMarkers], range: [0, self.options.maxMarkers],
sort: [{key: 'area', operator: '-'}] sort: [{key: 'area', operator: '-'}]
}, function(result) { }, function(result) {
Ox.Log('Map', 'RESULT', result) var ids = self.options.selected ? [self.options.selected] : [],
var ids = []; previousIds = self.places.map(function(place) {
return place.id;
});
// add new places
result.data.items.forEach(function(item, i) { result.data.items.forEach(function(item, i) {
var place = getPlaceById(item.id); var place = getPlaceById(item.id);
if (!place) { if (!place) {
self.places.push( place = new Ox.MapPlace(Ox.extend({
new Ox.MapPlace(Ox.extend({ map: that
map: that }, item)).add();
}, item)).add() self.places.push(place);
);
} else if (!place.visible) { } else if (!place.visible) {
place.add(); place.add();
} }
ids.push(item.id); item.id != self.options.selected && ids.push(item.id);
}); });
self.places.forEach(function(place) { // remove old places
if ( previousIds.forEach(function(id) {
!place.selected && place.visible var place = getPlaceById(id);
&& ids.indexOf(place.id) == -1 if (place && ids.indexOf(id) == -1) {
) {
place.remove(); place.remove();
} }
}); });
// update places array
self.places = self.places.filter(function(place) {
return place.visible;
});
}); });
self.boundsChanged = false; self.boundsChanged = false;
} }