2011-03-31 11:25:09 +02:00
|
|
|
componentconstructors['chatballoon'] = function(dynmap, configuration) {
|
2011-03-31 12:54:32 +02:00
|
|
|
var me = this;
|
|
|
|
me.chatpopups = {};
|
2011-07-21 01:23:35 +02:00
|
|
|
$(dynmap).bind('playerupdated', function(event, player) {
|
|
|
|
var popup = me.chatpopups[player.name];
|
|
|
|
if (popup) {
|
|
|
|
var markerPosition = dynmap.getProjection().fromLocationToLatLng(player.location);
|
|
|
|
popup.layer.setLatLng(markerPosition);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$(dynmap).bind('worldchanged', function() {
|
|
|
|
$.each(me.chatpopups, function(name, popup) {
|
|
|
|
popup.close();
|
|
|
|
});
|
|
|
|
});
|
2011-03-31 12:54:32 +02:00
|
|
|
$(dynmap).bind('chat', function(event, message) {
|
|
|
|
if (message.source != 'player') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var player = dynmap.players[message.name];
|
2011-08-15 15:01:59 +02:00
|
|
|
if (!player)
|
|
|
|
return;
|
2011-07-21 01:23:35 +02:00
|
|
|
if (dynmap.world !== player.location.world) {
|
2011-06-15 00:57:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-07-21 01:23:35 +02:00
|
|
|
var popupPosition = dynmap.getProjection().fromLocationToLatLng(player.location);
|
2011-03-31 12:54:32 +02:00
|
|
|
var popup = me.chatpopups[message.name];
|
|
|
|
if (!popup) {
|
2011-07-21 01:23:35 +02:00
|
|
|
me.chatpopups[message.name] = popup = {
|
2011-07-29 21:28:29 +02:00
|
|
|
layer: new L.Popup({autoPan: configuration.focuschatballoons, closeButton: false}),
|
2011-07-21 01:23:35 +02:00
|
|
|
content: $('<div/>').addClass('balloonmessages')[0]
|
|
|
|
};
|
|
|
|
popup.layer.setContent(popup.content);
|
|
|
|
|
|
|
|
popup.close = function() {
|
|
|
|
if (popup.timeout) { window.clearTimeout(popup.timeout); }
|
|
|
|
dynmap.map.removeLayer(popup.layer);
|
|
|
|
delete me.chatpopups[message.name];
|
|
|
|
};
|
|
|
|
|
|
|
|
popup.layer.setLatLng(popupPosition);
|
|
|
|
dynmap.map.addLayer(popup.layer);
|
2011-03-31 12:54:32 +02:00
|
|
|
}
|
2011-07-21 01:23:35 +02:00
|
|
|
|
|
|
|
// Add line to balloon.
|
|
|
|
$('<div/>').addClass('balloonmessage').text(message.text).appendTo(popup.content);
|
2011-03-31 12:54:32 +02:00
|
|
|
|
2011-07-21 01:23:35 +02:00
|
|
|
// Remove older lines when too many messages are shown.
|
|
|
|
var children = $(popup.content).children();
|
|
|
|
if (children.length > 5) {
|
|
|
|
$(children[0]).remove();
|
2011-03-31 12:54:32 +02:00
|
|
|
}
|
2011-07-21 01:23:35 +02:00
|
|
|
|
|
|
|
popup.layer.setContent(popup.content);
|
|
|
|
|
2011-03-31 12:54:32 +02:00
|
|
|
if (popup.timeout) { window.clearTimeout(popup.timeout); }
|
|
|
|
popup.timeout = window.setTimeout(function() {
|
2011-07-21 01:23:35 +02:00
|
|
|
popup.close();
|
2011-03-31 12:54:32 +02:00
|
|
|
}, 8000);
|
2011-07-21 01:23:35 +02:00
|
|
|
|
|
|
|
if (configuration.focuschatballoons) {
|
|
|
|
dynmap.panToLatLng(popupPosition);
|
|
|
|
}
|
2011-03-31 12:54:32 +02:00
|
|
|
});
|
2011-03-31 11:25:09 +02:00
|
|
|
};
|