diff --git a/web/js/chat.js b/web/js/chat.js index 022101b4..b10421fa 100644 --- a/web/js/chat.js +++ b/web/js/chat.js @@ -7,39 +7,35 @@ $.ajax({ }); componentconstructors['chat'] = function(dynmap, configuration) { - return { - dynmap: dynmap, - initialize: function() { - // Provides 'chat'-events by monitoring the world-updates. - $(dynmap).bind('worldupdate', function(event, update) { - swtch(update.type, { - chat: function() { - $(dynmap).trigger('chat', [{source: 'player', name: update.playerName, text: update.message}]); - }, - webchat: function() { - $(dynmap).trigger('chat', [{source: 'web', name: update.playerName, text: update.message}]); - } - }); - }); - - if (dynmap.options.allowwebchat) { - // Accepts 'sendchat'-events to send chat messages to the server. - $(dynmap).bind('sendchat', function(event, message) { - var data = '{"name":"'+ip+'","message":"'+message+'"}'; - $.ajax({ - type: 'POST', - url: 'up/sendmessage', - data: data, - dataType: 'json', - success: function(response) { - //handle response - if(response) { - $(dynmap).trigger('chat', [{source: 'me', name: ip, text: message}]); - } - } - }); - }); + var me = this; + // Provides 'chat'-events by monitoring the world-updates. + $(dynmap).bind('worldupdate', function(event, update) { + swtch(update.type, { + chat: function() { + $(dynmap).trigger('chat', [{source: 'player', name: update.playerName, text: update.message}]); + }, + webchat: function() { + $(dynmap).trigger('chat', [{source: 'web', name: update.playerName, text: update.message}]); } - } - }; + }); + }); + + if (dynmap.options.allowwebchat) { + // Accepts 'sendchat'-events to send chat messages to the server. + $(dynmap).bind('sendchat', function(event, message) { + var data = '{"name":"'+ip+'","message":"'+message+'"}'; + $.ajax({ + type: 'POST', + url: 'up/sendmessage', + data: data, + dataType: 'json', + success: function(response) { + //handle response + if(response) { + $(dynmap).trigger('chat', [{source: 'me', name: ip, text: message}]); + } + } + }); + }); + } }; diff --git a/web/js/chatballoon.js b/web/js/chatballoon.js index 55777e8b..2ea544d4 100644 --- a/web/js/chatballoon.js +++ b/web/js/chatballoon.js @@ -1,53 +1,47 @@ componentconstructors['chatballoon'] = function(dynmap, configuration) { - return { - dynmap: dynmap, - options: configuration, - chatpopups: {}, - initialize: function() { - var me = this; - $(dynmap).bind('chat', function(event, message) { - if (message.source != 'player') { - return; - } - var player = dynmap.players[message.name]; - var playerMarker = player && player.marker; - if (!playerMarker) { - return; - } - var popup = me.chatpopups[message.name]; - if (!popup) { - popup = { lines: [ message.text ] }; - } else { - popup.lines[popup.lines.length] = message.text; - } - - var MAX_LINES = 5; - if (popup.lines.length > MAX_LINES) { - popup.lines = popup.lines.slice(1); - } - var htmlMessage = '
' + message.name + "

"; - var line; - for (line in popup.lines) { - htmlMessage = htmlMessage + popup.lines[line] + "
"; - } - htmlMessage = htmlMessage + "
"; - if (!popup.infoWindow) { - popup.infoWindow = new google.maps.InfoWindow({ - disableAutoPan: !(me.options.focuschatballoons || false), - content: htmlMessage - }); - } else { - popup.infoWindow.setContent(htmlMessage); - } - popup.infoWindow.open(dynmap.map, playerMarker); - me.chatpopups[message.name] = popup; - if (popup.timeout) { window.clearTimeout(popup.timeout); } - popup.timeout = window.setTimeout(function() { - popup.infoWindow.close(); - popup.infoWindow = null; - delete me.chatpopups[message.name]; - }, 8000); - }); + var me = this; + me.chatpopups = {}; + $(dynmap).bind('chat', function(event, message) { + if (message.source != 'player') { + return; } - }; + var player = dynmap.players[message.name]; + var playerMarker = player && player.marker; + if (!playerMarker) { + return; + } + var popup = me.chatpopups[message.name]; + if (!popup) { + popup = { lines: [ message.text ] }; + } else { + popup.lines[popup.lines.length] = message.text; + } + + var MAX_LINES = 5; + if (popup.lines.length > MAX_LINES) { + popup.lines = popup.lines.slice(1); + } + var htmlMessage = '
' + message.name + "

"; + var line; + for (line in popup.lines) { + htmlMessage = htmlMessage + popup.lines[line] + "
"; + } + htmlMessage = htmlMessage + "
"; + if (!popup.infoWindow) { + popup.infoWindow = new google.maps.InfoWindow({ + disableAutoPan: !(me.options.focuschatballoons || false), + content: htmlMessage + }); + } else { + popup.infoWindow.setContent(htmlMessage); + } + popup.infoWindow.open(dynmap.map, playerMarker); + me.chatpopups[message.name] = popup; + if (popup.timeout) { window.clearTimeout(popup.timeout); } + popup.timeout = window.setTimeout(function() { + popup.infoWindow.close(); + popup.infoWindow = null; + delete me.chatpopups[message.name]; + }, 8000); + }); }; \ No newline at end of file diff --git a/web/js/chatbox.js b/web/js/chatbox.js index 2c32acd4..80a5846a 100644 --- a/web/js/chatbox.js +++ b/web/js/chatbox.js @@ -1,74 +1,70 @@ componentconstructors['chatbox'] = function(dynmap, configuration) { - return { - dynmap: dynmap, - initialize: function() { - var chat = $('
') - .addClass('chat') - .appendTo(dynmap.options.container); - var messagelist = $('
') - .addClass('messagelist') - .appendTo(chat); - - if (dynmap.options.allowwebchat) { - var chatinput = $('') - .addClass('chatinput') - .attr({ - id: 'chatinput', - type: 'text', - value: '' - }) - .keydown(function(event) { - if (event.keyCode == '13') { - event.preventDefault(); - if(chatinput.val() != '') { - $(dynmap).trigger('sendchat', [chatinput.val()]); - chatinput.val(''); - } - } - }) - .appendTo(chat); - } - - $(dynmap).bind('chat', function(event, message) { - var playerName = message.name; - var messageRow = $('
') - .addClass('messagerow'); - - var playerIconContainer = $('') - .addClass('messageicon'); - - if (message.source === 'player' && configuration.showplayerfaces) { - getMinecraftHead(playerName, 16, function(head) { - messageRow.icon = $(head) - .addClass('playerIcon') - .appendTo(playerIconContainer); - }); + var me = this; + var chat = $('
') + .addClass('chat') + .appendTo(dynmap.options.container); + var messagelist = $('
') + .addClass('messagelist') + .appendTo(chat); + + if (dynmap.options.allowwebchat) { + var chatinput = $('') + .addClass('chatinput') + .attr({ + id: 'chatinput', + type: 'text', + value: '' + }) + .keydown(function(event) { + if (event.keyCode == '13') { + event.preventDefault(); + if(chatinput.val() != '') { + $(dynmap).trigger('sendchat', [chatinput.val()]); + chatinput.val(''); + } } + }) + .appendTo(chat); + } + + $(dynmap).bind('chat', function(event, message) { + var playerName = message.name; + var messageRow = $('
') + .addClass('messagerow'); - if (message.source === 'player' && configuration.showworld) { - var playerWorldContainer = $('') - .addClass('messagetext') - .text('['+dynmap.players[message.name].location.world.name+']') - .appendTo(messageRow); - } + var playerIconContainer = $('') + .addClass('messageicon'); - var playerNameContainer = $('') - .addClass('messagetext') - .text(' '+message.name+': '); - - var playerMessageContainer = $('') - .addClass('messagetext') - .text(message.text); - - messageRow.append(playerIconContainer,playerNameContainer,playerMessageContainer); - //messageRow.append(playerIconContainer,playerWorldContainer,playerGroupContainer,playerNameContainer,playerMessageContainer); - setTimeout(function() { messageRow.remove(); }, (configuration.messagettl * 1000)); - messagelist.append(messageRow); - - messagelist.show(); - //var scrollHeight = jQuery(me.messagelist).attr('scrollHeight'); - messagelist.scrollTop(messagelist.scrollHeight()); + if (message.source === 'player' && configuration.showplayerfaces) { + getMinecraftHead(playerName, 16, function(head) { + messageRow.icon = $(head) + .addClass('playerIcon') + .appendTo(playerIconContainer); }); } - }; + + if (message.source === 'player' && configuration.showworld) { + var playerWorldContainer = $('') + .addClass('messagetext') + .text('['+dynmap.players[message.name].location.world.name+']') + .appendTo(messageRow); + } + + var playerNameContainer = $('') + .addClass('messagetext') + .text(' '+message.name+': '); + + var playerMessageContainer = $('') + .addClass('messagetext') + .text(message.text); + + messageRow.append(playerIconContainer,playerNameContainer,playerMessageContainer); + //messageRow.append(playerIconContainer,playerWorldContainer,playerGroupContainer,playerNameContainer,playerMessageContainer); + setTimeout(function() { messageRow.remove(); }, (configuration.messagettl * 1000)); + messagelist.append(messageRow); + + messagelist.show(); + //var scrollHeight = jQuery(me.messagelist).attr('scrollHeight'); + messagelist.scrollTop(messagelist.scrollHeight()); + }); }; \ No newline at end of file diff --git a/web/js/map.js b/web/js/map.js index ef095c36..cea2051e 100644 --- a/web/js/map.js +++ b/web/js/map.js @@ -280,10 +280,8 @@ DynMap.prototype = { me.selectMap(me.defaultworld.defaultmap); $.each(me.options.components, function(index, configuration) { - me.components.push(componentconstructors[configuration.type](me, configuration)); - }); - $.each(me.components, function(index, component) { - component.initialize(); + var componentconstructor = componentconstructors[configuration.type]; + me.components.push(new componentconstructor(me, configuration)); }); setTimeout(function() { me.update(); }, me.options.updaterate); diff --git a/web/js/playermarkers.js b/web/js/playermarkers.js index 88803220..9271e351 100644 --- a/web/js/playermarkers.js +++ b/web/js/playermarkers.js @@ -1,40 +1,37 @@ componentconstructors['playermarkers'] = function(dynmap, configuration) { - return { - initialize: function() { - $(dynmap).bind('playeradded', function(event, player) { - // Create the player-marker. - var markerPosition = dynmap.map.getProjection().fromWorldToLatLng(player.location.x, player.location.y, player.location.z); - player.marker = new CustomMarker(markerPosition, dynmap.map, function(div) { - var playerImage; - $(div) - .addClass('Marker') - .addClass('playerMarker') - .append(playerImage = $('') - .attr({ src: 'images/player.png' })) - .append($('') - .addClass('playerName') - .text(player.name)); - - if (configuration.showplayerfaces) { - getMinecraftHead(player.name, 32, function(head) { - $(head) - .addClass('playericon') - .prependTo(div); - playerImage.remove(); - }); - } + var me = this; + $(dynmap).bind('playeradded', function(event, player) { + // Create the player-marker. + var markerPosition = dynmap.map.getProjection().fromWorldToLatLng(player.location.x, player.location.y, player.location.z); + player.marker = new CustomMarker(markerPosition, dynmap.map, function(div) { + var playerImage; + $(div) + .addClass('Marker') + .addClass('playerMarker') + .append(playerImage = $('') + .attr({ src: 'images/player.png' })) + .append($('') + .addClass('playerName') + .text(player.name)); + + if (configuration.showplayerfaces) { + getMinecraftHead(player.name, 32, function(head) { + $(head) + .addClass('playericon') + .prependTo(div); + playerImage.remove(); }); - }); - $(dynmap).bind('playerremoved', function(event, player) { - // Remove the marker. - player.marker.remove(); - }); - $(dynmap).bind('playerupdated', function(event, player) { - // Update the marker. - var markerPosition = dynmap.map.getProjection().fromWorldToLatLng(player.location.x, player.location.y, player.location.z); - player.marker.toggle(dynmap.world === player.location.world); - player.marker.setPosition(markerPosition); - }); - } - }; + } + }); + }); + $(dynmap).bind('playerremoved', function(event, player) { + // Remove the marker. + player.marker.remove(); + }); + $(dynmap).bind('playerupdated', function(event, player) { + // Update the marker. + var markerPosition = dynmap.map.getProjection().fromWorldToLatLng(player.location.x, player.location.y, player.location.z); + player.marker.toggle(dynmap.world === player.location.world); + player.marker.setPosition(markerPosition); + }); }; \ No newline at end of file