Created
January 28, 2017 18:52
-
-
Save guimochila/eede4d5b9fdb82cc80444e025096d3a8 to your computer and use it in GitHub Desktop.
Code to be re-written.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var socket = io(); | |
function scrollToBottom() { | |
// Selectors | |
var messages = jQuery('#messages'); | |
var newMessage = messages.children('li:last-child'); | |
// Heights | |
var clientHeight = messages.prop('clientHeight'); | |
var scrollTop = messages.prop('scrollTop'); | |
var scrollHeight = messages.prop('scrollHeight'); | |
var newMessageHeight = newMessage.innerHeight(); | |
var lastMessageHeight = newMessage.prev().innerHeight(); | |
if (clientHeight + scrollTop + newMessageHeight + lastMessageHeight >= scrollHeight){ | |
messages.scrollTop(scrollHeight); | |
} | |
} | |
socket.on('connect', function () { | |
var params = jQuery.deparam(window.location.search); | |
socket.emit('join', params, function (err) { | |
if (err) { | |
alert(err); | |
window.location.href = '/'; | |
} else { | |
console.log('No error'); | |
} | |
}); | |
}); | |
socket.on('disconnect', function () { | |
console.log('Disconnected from server'); | |
}); | |
socket.on('updateUserList', function (users) { | |
var ol = jQuery('<ol></ol>'); | |
users.forEach(function (user) { | |
ol.append(jQuery('<li></li>').text(user)); | |
}); | |
jQuery('#users').html(ol); | |
}) | |
socket.on('newMessage', function (message) { | |
var formattedTime = moment(message.createdAt).format('h:mm a'); | |
var template = jQuery('#message-template').html(); | |
var html = Mustache.render(template, { | |
text: message.text, | |
from: message.from, | |
createdAt: formattedTime | |
}); | |
jQuery('#messages').append(html); | |
scrollToBottom(); | |
}); | |
socket.on('newLocationMessage', function (message) { | |
var formattedTime = moment(message.createdAt).format('h:mm a'); | |
var template = jQuery('#location-message-template').html(); | |
var html = Mustache.render(template, { | |
from: message.from, | |
url: message.url, | |
createdAt: formattedTime | |
}); | |
jQuery('#messages').append(html); | |
scrollToBottom(); | |
}); | |
jQuery('#message-form').on('submit', function (e) { | |
e.preventDefault(); | |
var messageTextBox = jQuery('[name=message]'); | |
socket.emit('createMessage', { | |
text: messageTextBox.val() | |
}, function () { | |
messageTextBox.val(''); | |
}); | |
}); | |
var locationButton = jQuery('#send-location'); | |
locationButton.on('click', function () { | |
if (!navigator.geolocation) { | |
return alert('Geolocation not support by your browser!'); | |
} | |
locationButton.attr('disabled', 'disabled').text('Sending location...'); | |
navigator.geolocation.getCurrentPosition(function (position) { | |
locationButton.removeAttr('disabled').text('Send location'); | |
socket.emit('createLocationMessage', { | |
latitude: position.coords.latitude, | |
longitude: position.coords.longitude | |
}); | |
}, function () { | |
locationButton.removeAttr('disabled').text('Send location'); | |
alert('Unable to fetch location'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment