-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.js
147 lines (124 loc) · 5.15 KB
/
Message.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { getTimeString, getDateString } from "./common.js";
const source_input = document.querySelector('.source-select');
const msg_txt_input = document.querySelector('.msg-text-input');
const msg_date_input = document.querySelector('#message-date');
const msg_time_input = document.querySelector('#message-time');
const add_msg_button = document.querySelector('.add-message-button');
const idGenerator = getNewID();
export default class Message {
constructor(chat, source, text, date_time) {
this.chat = chat;
this.id = idGenerator.next().value;
this.source = source;
this.text = text;
this.date_time = date_time;
this.element = document.createElement('div');
this.controls = document.createElement('div');
this.textarea = document.createElement('p');
this.setup()
}
toggleControls() {
this.controls.classList.toggle('active');
this.element.classList.toggle('selected');
}
edit() {
source_input.dispatchEvent(new Event('change'));
msg_date_input.value = getDateString(this.date_time);
msg_time_input.value = getTimeString(this.date_time, 24);
msg_txt_input.value = this.text;
msg_date_input.disabled = msg_time_input.disabled = true;
add_msg_button.textContent = "Save changes"
this.chat.current_editing_message = this;
}
delete() {
// Find the day of this message
const day_index = this.chat.days.findIndex(day => {
return getDateString(day.date) === getDateString(this.date_time)
})
// Delete the message
const msg_index = this.chat.days[day_index].messages.findIndex(msg => {
return msg.id === this.id
});
this.chat.days[day_index].messages.splice(msg_index, 1);
// Delete the day if empty
if (this.chat.days[day_index].messages.length === 0)
this.chat.days.splice(day_index, 1);
this.chat.readMessages();
}
update(text, source) {
const time_send = this.element.querySelector('.data-time');
this.textarea.textContent = this.text = text;
this.source = source;
this.textarea.appendChild(time_send);
this.element.classList.remove('message-self', 'message-receiver');
this.element.classList.add(`message-${source}`);
}
setup() {
this.element.classList.add('message', `message-${this.source}`, 'shadow');
this.element.setAttribute('id', this.id);
this.element.setAttribute('tabindex', 0);
this.element.dataset.date = getDateString(this.date_time);
this.element.dataset.time = getTimeString(this.date_time);
// controls section
this.controls.classList.add('controls');
// edit button
const edit_btn = document.createElement('button');
edit_btn.classList.add('message-control', 'edit-control');
edit_btn.addEventListener('click', () => {
msg_txt_input.focus();
this.edit();
// readMessages();
});
// edit icon
const edit_icon = document.createElement('span');
edit_icon.classList.add('material-icons');
edit_icon.textContent = 'edit';
// delete button
const delete_btn = document.createElement('button');
delete_btn.classList.add('message-control', 'delete-control');
const delete_icon = document.createElement('span');
delete_icon.classList.add('material-icons');
delete_icon.textContent = 'delete';
delete_btn.addEventListener('click', () => {
this.delete();
// readMessages();
});
// message text
this.textarea.classList.add('message-text');
// msg_text.setAttribute('contenteditable', 'true');
this.textarea.textContent = this.text;
// time of send
const time = document.createElement('span');
time.setAttribute('contenteditable', 'false')
time.classList.add('data-time');
time.textContent = getTimeString(this.date_time);
// double check
const check_icon = document.createElement('img');
check_icon.classList.add('double-check');
check_icon.setAttribute('src', 'resources/icons/double-check.svg');
check_icon.setAttribute('width', "16");
this.element.appendChild(this.controls);
this.controls.appendChild(edit_btn);
this.controls.appendChild(delete_btn);
edit_btn.appendChild(edit_icon);
delete_btn.appendChild(delete_icon);
this.textarea.appendChild(time);
time.appendChild(check_icon);
this.element.appendChild(this.textarea);
this.element.addEventListener('click', () => this.toggleControls.call(this));
this.element.addEventListener('blur', () => {
// dont toggle the buttons if hovering them
if (!delete_btn.matches(':hover') &&
!edit_btn.matches(':hover')) {
this.controls.classList.remove('active')
this.element.classList.remove('selected');
}
});
}
}
function* getNewID() {
let id = 0;
while (true) {
yield ++id;
}
}