This repository has been archived by the owner on Oct 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyperloopjs-chatapp.html
309 lines (237 loc) · 8.52 KB
/
hyperloopjs-chatapp.html
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hyperloop Chat App</title>
<!-- React and JQuery -->
<script src="https://unpkg.com/react@15/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<!-- Opal and Hyperloop -->
<script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/opal-compiler.min.js"></script>
<script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/hyperloop.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!-- script src="chat_service.js"></script -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<style type="text/css">
body {
padding-top: 100px;
padding-bottom: 100px;
}
div.alternating:nth-of-type(odd) {
background-color: #ddd;
}
div.alternating:nth-of-type(even) {
background-color: #ccc;
}
.input-box {
padding: 10px;
}
.saytext {
color: white;
font-size: 2em;
margin-left: 2em;
}
.message {
padding-top: 10px;
margin-top: 10px;
}
textarea {
width: 100%;
padding-top: 1em;
}
div.hyperloop-icon {
float: left;
width: 50px;
height: 50px;
margin-right: 8px;
background-size: contain;
background-image: url("hyperloop-logo-small-white.png");
background-repeat: no-repeat;
}
.navbar-inverse {
background-color: #149c8e;
background-image: none;
border-color: #e81176;
}
div.navbar {
padding-top: 1.5em;
padding-bottom: 2em;
}
.navbar-brand {
font-size: 2.5em;
font-weight: normal;
}
</style>
<script type="text/ruby">
</script>
<script type="text/ruby">
class ChatService
def initialize(&block)
@block = block
@messages = {"from"=>"user1", "time"=>1449089985, "message"=>"A 2 point message: \n+ point 1\n+ point 2\nGot it?"},{"from"=>"user2", "time"=>1449262785, "message"=>"message sent 8 days ago, by user 2"},{"from"=>"user3", "time"=>1449521985, "message"=>"message sent in the last week"},{"from"=>"user2", "time"=>1449608385, "message"=>"message sent **also** in the last week"},{"from"=>"user1", "time"=>1449950385, "message"=>"Was sent within the last hour!"},{"from"=>"user2", "time"=>1449952185, "message"=>"Was sent 30 minutes ago"},{"from"=>"user3", "time"=>1449953385, "message"=>"Was just sent\n\n\n\n\n\n\n\n\nwith a lot of blanks"},{"from"=>"user1", "time"=>1449953985, "message"=>"just now"}
end
def login(user_name)
@user_name = user_name
@block.call @messages
end
def id
@user_name
end
def send(data = {})
@messages << data
@block.call [data]
end
end
class ChatApp < Hyperloop::Component
before_mount do
@chat_service = ChatService.new do | messages |
mutate.messages ((state.messages || []) + messages)
puts "state messages updated. state.messages: #{state.messages}"
end
end
def render
DIV do
Nav login: method(:login).to_proc
if online?
Messages messages: state.messages, user_id: @chat_service.id
InputBox chat_service: @chat_service
end
end
end
def login(user_name)
mutate.messages nil
@chat_service.login(user_name)
end
def online?
state.messages
end
end
class Nav < Hyperloop::Component
param :login, type: Proc
before_mount do
mutate.current_user_name nil
mutate.user_name_input ""
end
def render
DIV(class: 'navbar-fixed-top navbar-inverse navbar') do
DIV(class: 'container') do
DIV(class: 'navbar-header') do
DIV(class: 'hyperloop-icon')
A(href: "#", style: {color: "white"}, class: 'navbar-brand') { "Hyperloop Chat Room " }
end
DIV(class: 'navbar-collapse collapse', id: 'navbar') do
FORM(class: 'navbar-left navbar-form', role: :search) do
DIV(class: 'form-group') do
INPUT(class: 'form-control', type: :text, value: state.user_name_input, placeholder: "Enter Your Handle"
).on(:change) do |e|
state.user_name_input! e.target.value
end.on(:key_down) do |e|
login! if valid_new_input? && e.key_code == 13
end
BUTTON(class: 'btn-default btn', type: :button) { SPAN(class: 'glyphicon glyphicon-log-in') }.on(:click) do
login!
end if valid_new_input?
end
end.on(:submit) { |e| e.prevent_default }
end
end
end
end
def valid_new_input?
state.user_name_input.present? && state.user_name_input != state.current_user_name
end
def login!
mutate.current_user_name state.user_name_input
params.login(state.user_name_input)
end
end
class Messages < Hyperloop::Component
param :messages, type: [Hash]
param :user_id
def render
DIV(class: 'container') do # add the bootstrap .container class here.
params.messages.each do |message|
Message message: message, user_id: params.user_id
end
end
end
end
class Message < Hyperloop::Component
param :message, type: Hash
param :user_id
after_mount :scroll_to_bottom
after_update :scroll_to_bottom
def render
DIV(class: 'row alternating message') do
DIV(class: 'col-sm-2') { sender }
FormattedDiv class: "col-sm-8", markdown: params.message[:message]
DIV(class: 'col-sm-2') { formatted_time }
end
end
def scroll_to_bottom
Element['html, body'].animate({scrollTop: Element[Document].height}, :slow)
end
def sender
if params.message[:from] == params.user_id
"you: "
else
"#{params.message[:from]}:"
end
end
def formatted_time
time = Time.at(params.message[:time])
if Time.now < time+1.day
time.strftime("%I:%M %p")
elsif Time.now < time+7.days
time.strftime("%A")
else
time.strftime("%D %I:%M %p")
end
end
end
class InputBox < Hyperloop::Component
param :chat_service, type: ChatService
before_mount { mutate.composition "" }
def render
DIV(class: 'row form-group input-box navbar navbar-inverse navbar-fixed-bottom') do
DIV(class: 'col-sm-1 saytext') {"Say: "}
TEXTAREA(class: 'col-sm-5', rows: rows, value: state.composition).on(:change) do |e|
state.composition! e.target.value
end.on(:key_down) do |e|
send_message if is_send_key?(e)
end
FormattedDiv class: "col-sm-5 saytext", markdown: state.composition
end
end
def is_send_key?(e)
(e.char_code == 13 || e.key_code == 13) && (e.meta_key || e.ctrl_key)
end
def send_message
params.chat_service.send(
message: state.composition!(""),
time: Time.now.to_i,
from: params.chat_service.id
)
end
def rows
[state.composition.count("\n") + 1,20].min
end
end
class FormattedDiv < Hyperloop::Component
param :markdown, type: String
collect_other_params_as :attributes
def render
DIV(params.attributes) do
DIV({dangerously_set_inner_HTML: { __html: `marked(#{params.markdown}, {sanitize: true })`}})
end
end
end
</script>
</head>
<body>
<!-- Tell inline reactive ruby to mount App here -->
<div data-hyperloop-mount="ChatApp"></div>
</body>
</html>