-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatSample.html
122 lines (109 loc) · 3.69 KB
/
ChatSample.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<div id="chat-container">
<ul id="chat-messages"><li>Loading...</li></ul>
<textarea id="chat-input" placeholder="Enter a message and hit send..."></textarea>
<input id="chat-send" type="button" value="GO" />
</div>
<script>
var chatListTitle = "Discussion";
function loadMessages(callback) {
var loadMessagesUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('" + chatListTitle + "')/items";
loadMessagesUrl += "?$select=Title,Message,Author/Title,Author/ID,Created";
loadMessagesUrl += "&$expand=Author";
loadMessagesUrl += "&$orderby=Created";
$.ajax({
url: loadMessagesUrl,
method: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
}).then(function(data) {
if (data && data.d && data.d.results) {
var html = data.d.results.map(function(messageItem){
var createdDate = new Date(messageItem.Created);
var authorLink = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/userdisp.aspx?ID=" + messageItem.Author.ID;
var messageHtml = "<li class='chat-message'>";
messageHtml += "<div class='chat-message-text'>" + messageItem.Message + "</div>";
messageHtml += "<div class='chat-message-author'>by <a href='" + authorLink + "'>" + messageItem.Author.Title + "</a> at " + createdDate.toLocaleTimeString() + " on " + createdDate.toLocaleDateString() + "</div>";
messageHtml += "</li>";
return messageHtml;
}).join("");
var $chatMessages = $("#chat-messages");
$chatMessages.html(html);
$chatMessages.scrollTop($chatMessages.height())
callback();
}
}).fail(function(error) {
console.log(error);
alert("ERROR: loadMessages(): " + error);
});
}
function addMessage(messageText, callback) {
var addMessageUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('" + chatListTitle + "')/items";
var digestToken = $('#__REQUESTDIGEST').val();
$.ajax({
url: addMessageUrl,
type: 'POST',
data: JSON.stringify({
"__metadata": {
"type": "SP.Data." + chatListTitle + "ListItem"
},
Title: "Chat Message",
Message: messageText,
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": digestToken
}
}).then(function(result) {
loadMessages(callback);
}).fail(function(error) {
console.log(error);
alert("ERROR: addMessage(): " + error);
})
}
function onAddMessage() {
$("#chat-send").attr("disabled", "disabled");
var chatMessage = $("#chat-input").val();
addMessage(chatMessage, function() {
$("#chat-input").val("");
$("#chat-send").removeAttr("disabled");
})
}
$("#chat-send").on("click", onAddMessage);
loadMessages();
</script>
<style>
#chat-container {
width: 600px;
}
ul#chat-messages {
padding: 0;
list-style: none;
max-height: 300px;
overflow-y: scroll;
}
li.chat-message {
border: 1px solid #ddd;
padding: 8px;
margin-bottom: 5px;
}
li.chat-message .chat-message-author {
padding-left: 10px;
color: #888;
font-size: 0.9em;
}
#chat-input {
width: 85%;
box-sizing: border-box;
min-height: 40px;
}
#chat-send {
width: 13%;
box-sizing: border-box;
height: 40px;
float: right;
}
</style>