-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatting.js
65 lines (52 loc) · 1.74 KB
/
chatting.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
const root = document.getElementById("root");
const Top = CreateTag("div","Top", root);
const BubbleDiv =CreateTag("div","BubbleDiv", root);
const InputDiv = CreateTag("div","InputDiv", root);
const h1 = CreateTag("h1","h1", Top);
h1.innerText = "Chatting Room";
const InputForm = CreateTag("form","InputForm", InputDiv);
const InputText = CreateTag("input","Text", InputForm);
InputText.setAttribute("type","text");
const SubmitBtn = CreateTag("button","SubmitBtn", InputForm);
SubmitBtn.innerText = "전송";
function CreateTag (tag, id, parentTag) {
const newTag = document.createElement(tag);
newTag.setAttribute("id",id);
parentTag.appendChild(newTag);
return newTag
}
function HandleSubmit (event) {
event.preventDefault();
if(InputText.value){
const Content = InputText.value;
const SpeechBubble = MakeSpeechBubble(Content);
ShowSpeechBubble(SpeechBubble);
}
}
function ShowSpeechBubble (SpeechBubble) {
BubbleDiv.appendChild(SpeechBubble);
window.scrollTo(0, document.body.scrollHeight);
CleanValue(InputText);
}
function CleanValue (target) {
target.value = '';
}
function MakeSpeechBubble (content) {
const contentDiv = CreateTag('div','contentDiv', BubbleDiv);
const SubDiv = CreateTag('div','subDiv',contentDiv);
const Bubble = CreateTag('span','bubbleSpan', SubDiv);
const time = createTimeSpan();
contentDiv.appendChild(time);
Bubble.innerText = content;
return SubDiv;
}
function createTimeSpan () {
const span = document.createElement('span');
const today = new Date();
const hours = today.getHours();
const minutes = today.getMinutes();
span.innerText = `${hours}:${minutes}`;
return span
}
InputForm.addEventListener('submit', HandleSubmit);
SubmitBtn.addEventListener('click', HandleSubmit);