dynmap/web/js/chatballoon.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

componentconstructors['chatballoon'] = function(dynmap, configuration) {
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();
});
});
$(dynmap).bind('chat', function(event, message) {
if (message.source != 'player') {
return;
}
var player = dynmap.players[message.name];
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);
var popup = me.chatpopups[message.name];
if (!popup) {
2011-07-21 01:23:35 +02:00
me.chatpopups[message.name] = popup = {
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-07-21 01:23:35 +02:00
// Add line to balloon.
$('<div/>').addClass('balloonmessage').text(message.text).appendTo(popup.content);
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-07-21 01:23:35 +02:00
popup.layer.setContent(popup.content);
if (popup.timeout) { window.clearTimeout(popup.timeout); }
popup.timeout = window.setTimeout(function() {
2011-07-21 01:23:35 +02:00
popup.close();
}, 8000);
2011-07-21 01:23:35 +02:00
if (configuration.focuschatballoons) {
dynmap.panToLatLng(popupPosition);
}
});
};