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());
}
});
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, {
geo: true,
sort: '-area'
});
}
addPlaces();
getMapBounds(function(mapBounds) {
if (mapBounds) {
self.map.fitBounds(mapBounds);
@ -143,15 +148,8 @@ Ox.Map = function(options, self) {
// fixme: the following is just a guess
self.boundsChanged = true;
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() {
selectPlace(self.options.selected || null);
@ -188,16 +186,7 @@ Ox.Map = function(options, self) {
key_left: function() {
pan(-1, 0);
},
// FIXME:
'key_meta.left': function() {
self.metaKey = true;
$(document).one({
keyup: function() {
self.metaKey = false;
}
});
},
'key_meta.right': function() {
'key_meta': function() {
self.metaKey = true;
$(document).one({
keyup: function() {
@ -277,6 +266,7 @@ Ox.Map = function(options, self) {
'lat', 'lng', 'south', 'west', 'north', 'east', 'area',
'editable'
].concat(self.options.keys);
self.places = [],
self.resultPlace = null;
self.scaleMeters = [
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) {
// via find, click, shift-click, or new place button
Ox.Log('Map', 'addPlaceToMap', place)
@ -607,7 +578,7 @@ Ox.Map = function(options, self) {
function addPlaceToPlaces(data) {
var place = getSelectedPlace(),
country = Ox.getCountryByGeoname(place.geoname);
country = place ? Ox.getCountryByGeoname(place.geoname) : {};
Ox.extend(place, data);
self.options.selected = place.id;
place.countryCode = country ? country.code : '';
@ -906,8 +877,6 @@ Ox.Map = function(options, self) {
}
that.overlayView.draw();
addPlaces();
Ox.forEach(self.$controls, function($control) {
$control.appendTo(self.$map);
});
@ -971,7 +940,8 @@ Ox.Map = function(options, self) {
}
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) {
var bounds = self.map.getBounds(),
southWest = bounds.getSouthWest(),
@ -997,29 +967,34 @@ Ox.Map = function(options, self) {
range: [0, self.options.maxMarkers],
sort: [{key: 'area', operator: '-'}]
}, function(result) {
Ox.Log('Map', 'RESULT', result)
var ids = [];
var ids = self.options.selected ? [self.options.selected] : [],
previousIds = self.places.map(function(place) {
return place.id;
});
// add new places
result.data.items.forEach(function(item, i) {
var place = getPlaceById(item.id);
if (!place) {
self.places.push(
new Ox.MapPlace(Ox.extend({
map: that
}, item)).add()
);
place = new Ox.MapPlace(Ox.extend({
map: that
}, item)).add();
self.places.push(place);
} else if (!place.visible) {
place.add();
}
ids.push(item.id);
item.id != self.options.selected && ids.push(item.id);
});
self.places.forEach(function(place) {
if (
!place.selected && place.visible
&& ids.indexOf(place.id) == -1
) {
// remove old places
previousIds.forEach(function(id) {
var place = getPlaceById(id);
if (place && ids.indexOf(id) == -1) {
place.remove();
}
});
// update places array
self.places = self.places.filter(function(place) {
return place.visible;
});
});
self.boundsChanged = false;
}