-
Notifications
You must be signed in to change notification settings - Fork 38
/
MessagesView.js
72 lines (59 loc) · 1.71 KB
/
MessagesView.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
66
67
68
69
70
71
72
const { argumentsSort } = require('./../../helper');
class MessagesView extends HTMLElement {
constructor() {
super();
//console.log(this);
}
add() {
const self = this;
var { message, text } = argumentsSort(arguments, {
text: "string",
message: "object"
});
message = (message || {}).fillDefaults({
content: {
type: "text/plains",
source: text || ""
},
timestamp: new Date().getTime(),
type: "self"
});
Object.defineProperty(message, "id", {
get() {
for (var i = 0; i < self.messages.length; i++) {
if (this === self.messages[i]) {
return i;
}
}
}
});
this.messages.push(message);
this.__render(message);
return message;
}
__render(message) {
const li = document.createElement("li");
li.setAttribute("type", message.type);
li.__proto__.message = message;
const msgContent = document.createElement("message-content");
const msgDescription = document.createElement("message-description");
for (let handler of this.__contentTypeHandler) {
let handleable = message.content.type.match(handler.type);
if (handleable) {
handler.content(msgContent, message, li, this);
handler.description(msgDescription, message, li, this);
break;
}
}
li.appendChild(msgContent);
li.appendChild(msgDescription);
message.__listItem = li;
this.messagesList.appendChild(li);
}
get messagesList() {
return this.getElementsByClassName('messages')[0];
}
}
MessagesView.prototype.messages = [];
MessagesView.prototype.__contentTypeHandler = require('./contentHandler.js');
module.exports = MessagesView;