Page MenuHomeSealhub

konwersacja.html
No OneTemporary

Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None

konwersacja.html

<!DOCTYPE html>
<html>
<head>
<script src="/js/jquery.js"></script>
<script src="/js/buzz.js"></script>
<script src="/js/authorization.js"></script>
<meta charset="utf-8">
<title>Konwersacja</title>
<link rel="stylesheet" href="/style.css">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
</head>
<body style="max-width:25rem;">
<div class="content">
<a href="/">Menu główne</a> > <a href="/konwersacje.html">Konwersacje</a>
<center><h1>Konwersacja: <span class="insert-conversation-name"></span></h1></center>
<ul id="lista-wiadomosci"></ul>
<input type="text" id="message-box" placeholder="Nowa wiadomość"></textarea>
<button id="confirm" onclick="sendMessage()">Wyślij</button>
</div>
</body>
<script>
Auth.redirect_if_not_logged_in();
var mySound = new buzz.sound("/sounds/new_message.mp3");
var me;
var conversation_id = document.location.hash.slice(1);
var base_url = "/api/v1/chat/conversation/" + conversation_id + "/messages";
function getTitle() {
$.get("/api/v1/chat/conversation/"+conversation_id, function(data) {
$(".insert-conversation-name").text(data.title);
});
}
function getCurrentUserID(callback){
$.get("api/v1/users/me", function(user_data){
me = user_data.user_id;
callback();
});
}
function getUsername(user_id){
var username;
$.ajax({
url: "api/v1/users/"+user_id,
type: 'GET',
dataType: "json",
async: false,
contentType: "application/json; charset=utf-8",
success: function(data){
username = data.username;
}
});
return username;
}
var lastMessageId = -1;
function isScrolledIntoView(elem)
{
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
function readMessages(){
console.log("read messages");
var message_list = $("#lista-wiadomosci");
$.get(base_url, function(data) {
var checkState = false;
console.log(data);
for(var i in data) {
if (lastMessageId < data[i].id) {
var entry = data[i];
var node;
if(entry.message.trim() != ""){
var username = getUsername(entry.owner_id);
if(entry.owner_id == me){
node = $("<li class='message incoming'><div class='username'>"+username+":</div>"+entry.message+"</li>");
} else {
mySound.play();
node = $("<li class='message'><div class='username'>"+username+":</div>"+entry.message+"</li>");
}
message_list.append(node);
checkState = true;
}
lastMessageId = data[i].id;
}
}
if (isScrolledIntoView("#message-box") && checkState) {
$("html, body").animate({
scrollTop: $("#message-box").offset().top+50
}, 200);
}
});
}
function sendMessage(){
console.log("sendMessage")
var message = $("#message-box").val();
if(message.trim() != ""){
$.post("/api/v1/chat/message", {message: message, conversation_id: conversation_id}, function() {
console.log("before readNewMessages")
readMessages();
});
} else {
alert("Może najpierw coś napisz?");
}
$("#message-box").val("").focus();
$("html, body").animate({
scrollTop: $("#message-box").offset().top+50
}, 200);
}
$("#message-box").keydown(function(e){
if(e.which==13){
sendMessage();
}
});
getCurrentUserID(readMessages);
getTitle();
setInterval(readMessages, 500);
</script>
</html>

File Metadata

Mime Type
text/html
Expires
Fri, Nov 27, 20:18 (4 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1222090
Default Alt Text
konwersacja.html (3 KB)

Event Timeline