oxjs/demos/listmap2/js/listmap.js

207 lines
9.2 KiB
JavaScript
Raw Normal View History

Ox.load('UI', {
debug: true,
hideScreen: true,
showScreen: true,
theme: 'modern'
}, function() {
2011-05-21 17:56:15 +00:00
Ox.load('Geo', function() {
2011-05-29 13:33:10 +00:00
Ox.getJSON('json/cities50000.json', function(cities) {
2011-05-29 10:42:38 +00:00
var places = cities.map(function(city, i) {
var countryCode = city.country_code == 'XK' ? 'RS-KO' : city.country_code,
2011-05-29 13:33:10 +00:00
marker = city.population > 10000000 ? {size: 24, color: [255, 0, 0]} :
city.population > 5000000 ? {size: 22, color: [255, 32, 0]} :
city.population > 2000000 ? {size: 20, color: [255, 64, 0]} :
city.population > 1000000 ? {size: 18, color: [255, 96, 0]} :
city.population > 500000 ? {size: 16, color: [255, 128, 0]} :
city.population > 200000 ? {size: 14, color: [255, 160, 0]} :
city.population > 100000 ? {size: 12, color: [255, 192, 0]} :
city.population > 50000 ? {size: 10, color: [255, 224, 0]} :
2011-05-29 10:42:38 +00:00
{size: 8, color: [255, 255, 0]},
2011-05-29 13:33:10 +00:00
area = Math.max(city.population, 1) * 100,
latSize = Math.sqrt(area) / Ox.EARTH_CIRCUMFERENCE * 360,
lngSize = Math.sqrt(area) * Ox.getDegreesPerMeter(city.latitude);
2011-05-29 10:42:38 +00:00
return {
alternativeNames: [],
2011-05-29 12:22:54 +00:00
area: area,
2011-05-29 10:42:38 +00:00
countryCode: countryCode,
editable: true,
flag: countryCode,
geoname: city.name + ', ' + Ox.getCountryByCode(countryCode).name,
geonameSort: getGeonameSort(city.name + ', ' + Ox.getCountryByCode(countryCode).name),
2011-05-29 16:14:08 +00:00
id: Ox.encodeBase32(Ox.uid()),
2011-05-29 10:42:38 +00:00
markerColor: marker.color,
markerSize: marker.size,
name: city.name,
type: 'city',
lat: city.latitude,
lng: city.longitude,
south: city.latitude - latSize / 2,
west: city.longitude - lngSize / 2,
north: city.latitude + latSize / 2,
east: city.longitude + lngSize / 2
};
});
var $listmap = new Ox.ListMap({
2011-05-29 17:44:48 +00:00
addPlace: function(data, callback) {
var id = Ox.encodeBase32(Ox.uid());
Ox.print('addplace', getGeonameSort(data.place.geoname));
places.push(Ox.extend(data.place, {
geonameSort: getGeonameSort(data.place.geoname),
id: id
}));
callback({
data: {id: id},
result: {code: 200, text: 'OK'}
});
},
editPlace: function(data, callback) {
var place = Ox.getObjectById(places, data.id);
place = Ox.extend(place, data, {
geonameSort: getGeonameSort(data.geoname)
});
callback({
result: {code: 200, text: 'OK'}
});
},
height: window.innerHeight,
2011-05-29 10:42:38 +00:00
places: function(options, callback) {
// this simulates a remote API
Ox.print('OPTIONS', options)
var data = {};
// query, sort, range, area
if (Ox.isEmpty(options)) {
data = {
area: {
south: 90,
west: 180,
north: -90,
east: -180
},
items: places.length
2011-05-29 10:42:38 +00:00
};
places.forEach(function(place) {
['south', 'west', 'north', 'east'].forEach(function(v) {
if (
((v == 'south' || v == 'west') && place[v] < data[v])
|| ((v == 'north' || v == 'east') && place[v] > data[v])
) {
data[v] = place[v];
}
});
});
} else {
data.items = places;
if (options.query) {
if (options.query.conditions[0].key == 'id') {
data.items = data.items.filter(function(place) {
return place.id == options.query.conditions[0].value;
});
} else {
Ox.print('query', options)
data.items = data.items.filter(function(place) {
var ret = true;
options.query.conditions.forEach(function(condition) {
if (condition.operator == '-') {
if (
place[condition.key] < condition.value[0]
|| place[condition.key] > condition.value[1]
) {
ret = false;
}
} else { // '!-'
if (
place[condition.key] > condition.value[0]
&& place[condition.key] < condition.value[1]
) {
ret = false;
}
}
return ret;
});
return ret;
});
}
2011-05-29 10:42:38 +00:00
}
data.items.sort(function(a, b) {
if (options.sort[0].key == 'geoname') {
aValue = a.geonameSort;
bValue = b.geonameSort;
} else {
aValue = a[options.sort[0].key];
bValue = b[options.sort[0].key];
}
var ret = 0;
if (
(options.sort[0].operator == '+' && aValue < bValue)
|| (options.sort[0].operator == '-' && aValue > bValue)
) {
ret = -1;
} else if (
(options.sort[0].operator == '+' && aValue > bValue)
|| (options.sort[0].operator == '-' && aValue < bValue)
) {
ret = 1;
}
return ret;
});
2011-05-29 10:42:38 +00:00
if (options.ids) {
data.positions = {};
data.items.forEach(function(place, i) {
if (options.ids.indexOf(place.id) > -1) {
data.positions[place.id] = i;
}
});
delete data.items;
} else if (options.range) {
data.items = data.items.filter(function(place, i) {
return i >= options.range[0] && i < options.range[1];
});
}
}
Ox.print('DATA', data)
callback({
data: data,
result: {code: 200, text: 'OK'}
});
},
2011-05-29 17:44:48 +00:00
removePlace: function(data, callback) {
var i = Ox.getPositionById(places, data.id);
places.splice(i, 1);
callback();
},
width: window.innerWidth
})
.bindEvent({
geocode: function(event, data) {
2011-04-29 22:07:23 +00:00
Ox.print(event)
Ox.print(JSON.stringify(data))
}
})
.appendTo(Ox.UI.$body);
2011-05-21 17:56:15 +00:00
$(window).resize(function() {
Ox.print('RESIZE', window.innerHeight)
2011-05-29 10:42:38 +00:00
$listmap.options({
height: window.innerHeight,
width: window.innerWidth
});
});
2011-05-29 10:42:38 +00:00
window.$listmap = $listmap;
function getGeonameSort(geoname) {
var names = geoname.split(', ');
if (!Ox.getCountryByGeoname(names[names.length - 1])) {
names.push('~');
}
return names.reverse().join(', ');
}
2011-05-21 17:56:15 +00:00
});
2011-05-21 17:56:15 +00:00
});
});