Last active
November 20, 2016 01:22
-
-
Save attashe/1e2ac756eaa04b9e7b590b0bfe9bea31 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Chat</title> | |
<script | |
src="https://code.jquery.com/jquery-3.1.0.min.js" | |
integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" | |
crossorigin="anonymous"></script> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.16.1/vis.min.css" rel="stylesheet" type="text/css" /> | |
<!-- Library for work with files --> | |
<script src="http://momentjs.com/downloads/moment.min.js"></script> | |
<!-- Library for work with Parse --> | |
<script src="https://www.parsecdn.com/js/parse-1.6.14.min.js"></script> | |
</head> | |
<body> | |
<input type="text" id="messageText"> | |
<button id="sendMessage">Send!</button> | |
<div class="chat-box" id="chatBox"></div> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
function initParse(){ | |
Parse.initialize("zZUvULRC96RcuKT6vwKMOJFrrRlmv11PIEKO8rtb", "30c0FCe5xLMu6Q62isVH1iSfMi0P9rJJq6fQRSRx"); | |
} | |
function loadAllEvents(callback){ | |
var q = new Parse.Query('Chat'); | |
q.addDescending('createdAt'); | |
q.limit(1000); | |
q.find({ | |
success: function (results) { | |
callback(results); | |
}, | |
error: function (error) { | |
alert("Events load error"); | |
} | |
}) | |
} | |
function getEventItemHtml(event){ | |
img = ''; | |
if(event.get('photo') != undefined) { | |
console.log(event.get('photo').url()); | |
img = event.get('photo').url(); | |
} | |
// var s = '<div class="eventItem ui message " ><span class="ui label" >' + moment(event.createdAt).format('LLL') + '</span><span class="namePlaceholder" >' + event.get('name') +'<span class="descriptionPlaceholder" > - <span>' + event.get('description') + '</span> <a class="ui button mini basic" href="event.html?id=' + event.id + '" >посмотреть</a></div>'; | |
// var s = '<div class="flex-container"><article class="event"><header>' + event.get('title') + '</header>' + '<div class="event-wrap"><p>' + event.get('textNews') + '</p><img src="' + img + '"></div></article></div>'; | |
s = `<div class="message"> | |
<p>${event.get('message')}</p> | |
</div>` | |
return s; | |
} | |
function drawEvents(events){ | |
var s = ''; | |
for (var i in events){ | |
var e = events[i]; | |
s+= getEventItemHtml(e); | |
} | |
$('#chatBox').html(s); | |
} | |
function createMessage(message, callback){ | |
var Chat = Parse.Object.extend('Chat'); | |
var p = new Chat(); | |
p.set('message', message); | |
p.save().then(function(savedPhoto){ | |
callback(savedPhoto); | |
}); | |
} | |
function sendHandler() { | |
createMessage(messageText.value, function () { | |
chatBox.value = ""; | |
messageText.value = ""; | |
loadAllEvents(function(events){ | |
drawEvents(events); | |
}); | |
}); | |
//window.location.reload(); | |
} | |
sendMessage.addEventListener('click', sendHandler); | |
initParse(); | |
loadAllEvents(function(events){ | |
drawEvents(events); | |
}); | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment