-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
65 lines (60 loc) · 1.69 KB
/
chat.js
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
define(
["core/ui/content", "modules/roster", "core/xmpp"],
function (content, roster, xmpp) {
var $xml = function (snippet) {
return (new DOMParser).parseFromString(snippet, "text/xml").documentElement;
};
var showMessage = function (text) {
var output = document.createElement("p");
output.insertAdjacentText("beforeEnd", text);
document.querySelector("#chatlog").appendChild(output);
};
var submitComposer = function (jid) {
event.preventDefault();
var message = $xml("<message><body/></message>");
message.setAttribute("to", jid);
var input = document.querySelector("#chatcomposer textarea");
message.firstChild.textContent = input.value;
showMessage(input.value);
input.value = "";
xmpp.send(message);
};
var xmppMessageHandler = {
xpath: "/client:message/client:body",
xmlns: {client: "jabber:client"},
type: Object,
callback: function (message, body) {
showMessage(body.textContent);
return true;
}
};
new content({
path: /^chat\/?([^\/]*)/,
open: function (path, element) {
var jid = path.match(/^chat\/?([^\/]*)/)[1];
var name = jid in roster ? roster[jid].name : jid;
require(["core/template"], function (template) {
template({
css: "modules/chat",
source: "chat",
container: element,
data: {name: name, jid: jid}
})
.then(function () {
element.querySelector("#chatcomposer")
.addEventListener("submit", function () {
submitComposer(jid);
}, false);
xmpp.subscribe(xmppMessageHandler);
});
});
},
close: function (path, element) {
require(["core/css"], function (css) {
css.unload("modules/chat");
xmpp.unsubscribe(xmppMessageHandler);
});
}
});
return {};
});