-
Notifications
You must be signed in to change notification settings - Fork 11.4k
/
Copy pathchatMessages.coffee
182 lines (162 loc) · 4.45 KB
/
chatMessages.coffee
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
@ChatMessages = (->
self = {}
wrapper = {}
input = {}
editing = {}
init = ->
wrapper = $(".messages-container").find(".wrapper")
input = $(".input-message").get(0)
bindEvents()
return
resize = ->
dif = 60 + $(".messages-container").find("footer").outerHeight()
$(".messages-box").css
height: "calc(100% - #{dif}px)"
toPrevMessage = ->
msgs = wrapper.get(0).querySelectorAll(".own:not(.system)")
if msgs.length
if editing.element
if msgs[editing.index - 1]
edit msgs[editing.index - 1], editing.index - 1
else
edit msgs[msgs.length - 1], msgs.length - 1
toNextMessage = ->
if editing.element
msgs = wrapper.get(0).querySelectorAll(".own:not(.system)")
if msgs[editing.index + 1]
edit msgs[editing.index + 1], editing.index + 1
else
clearEditing()
getEditingIndex = (element) ->
msgs = wrapper.get(0).querySelectorAll(".own:not(.system)")
index = 0
for msg in msgs
if msg is element
return index
index++
return -1
edit = (element, index) ->
return if element.classList.contains("system")
clearEditing()
id = element.getAttribute("id")
message = ChatMessage.findOne { _id: id, 'u._id': Meteor.userId() }
input.value = message.msg
editing.element = element
editing.index = index or getEditingIndex(element)
editing.id = id
element.classList.add("editing")
input.classList.add("editing")
setTimeout ->
input.focus()
, 5
clearEditing = ->
if editing.element
editing.element.classList.remove("editing")
input.classList.remove("editing")
editing.id = null
editing.element = null
editing.index = null
input.value = editing.saved or ""
else
editing.saved = input.value
send = (rid, input) ->
if _.trim(input.value) isnt ''
KonchatNotification.removeRoomNotification(rid)
msg = input.value
input.value = ''
msgObject = { _id: Random.id(), rid: rid, msg: msg}
stopTyping(rid)
#Check if message starts with /command
if msg[0] is '/'
match = msg.match(/^\/([^\s]+)(?:\s+(.*))?$/m)
if(match?)
command = match[1]
param = match[2]
Meteor.call 'slashCommand', {cmd: command, params: param, msg: msgObject }
else
#Run to allow local encryption
#Meteor.call 'onClientBeforeSendMessage', {}
Meteor.call 'sendMessage', msgObject
deleteMsg = (message) ->
Meteor.call 'deleteMessage', message, (error, result) ->
if error
return Errors.throw error.reason
update = (id, rid, input) ->
if _.trim(input.value) isnt ''
msg = input.value
Meteor.call 'updateMessage', { id: id, msg: msg }
clearEditing()
stopTyping(rid)
startTyping = (rid, input) ->
if _.trim(input.value) isnt ''
MsgTyping.start(rid)
else
MsgTyping.stop(rid)
stopTyping = (rid) ->
MsgTyping.stop(rid)
bindEvents = ->
if wrapper?.length
$(".input-message").autogrow
postGrowCallback: ->
resize()
keyup = (rid, event) ->
input = event.currentTarget
k = event.which
keyCodes = [
13, # Enter
20, # Caps lock
16, # Shift
9, # Tab
27, # Escape Key
17, # Control Key
91, # Windows Command Key
19, # Pause Break
18, # Alt Key
93, # Right Click Point Key
45, # Insert Key
34, # Page Down
35, # Page Up
144, # Num Lock
145 # Scroll Lock
]
keyCodes.push i for i in [35..40] # Home, End, Arrow Keys
keyCodes.push i for i in [112..123] # F1 - F12
unless k in keyCodes
startTyping(rid, input)
keydown = (rid, event) ->
input = event.currentTarget
k = event.which
resize(input)
if k is 13 and not event.shiftKey
event.preventDefault()
event.stopPropagation()
if editing.id
update(editing.id, rid, input)
else
send(rid, input)
return
if k is 27
if editing.id
event.preventDefault()
event.stopPropagation()
clearEditing()
return
else if k is 38 or k is 40 # Arrow Up or down
if k is 38
return if input.value.slice(0, input.selectionStart).match(/[\n]/) isnt null
toPrevMessage()
else
return if input.value.slice(input.selectionEnd, input.value.length).match(/[\n]/) isnt null
toNextMessage()
event.preventDefault()
event.stopPropagation()
# ctrl (command) + shift + k -> clear room messages
else if k is 75 and ((navigator?.platform?.indexOf('Mac') isnt -1 and event.metaKey and event.shiftKey) or (navigator?.platform?.indexOf('Mac') is -1 and event.ctrlKey and event.shiftKey))
RoomHistoryManager.clear rid
keydown: keydown
keyup: keyup
deleteMsg: deleteMsg
send: send
init: init
edit: edit
)()