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
/
opal-browsertest.html
250 lines (194 loc) · 6.86 KB
/
opal-browsertest.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<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="http://cdn.opalrb.org/opal/0.10.1/external/opal-browser-0.2.0.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>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script type="text/ruby">
require 'browser/socket'
class ChatService
def initialize(&block)
@block = block
@messages = []
@user_id = nil
@socket = Browser::Socket.new("wss://echo.websocket.org") do |socket|
socket.on :message do |e|
data = JSON.parse(`unescape(#{JSON.parse(e.data)[:text]})`)
@messages << data
@block.call [data] if id
end
end
end
def login(user_id)
@user_id = user_id
@block.call @messages
end
def id
@user_id
end
def send(data = {})
@socket.send({handle: id, text: `escape(#{data.to_json})`}.to_json)
end
end
</script>
<script type="text/ruby">
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.navbar.navbar_inverse.navbar_fixed_top do
div.container do
div.navbar_header do
div.hyperloop_icon
a.navbar_brand(href: "#", style: {color: "white"}) { "Hyperloop Chat Room " }
end
div.collapse.navbar_collapse(id: "navbar") do
form.navbar_form.navbar_left(role: :search) do
div.form_group do
input.form_control(type: :text, value: state.user_name_input, placeholder: "Enter Your Handle"
).on(:change) do |e|
mutate.user_name_input e.target.value
end.on(:key_down) do |e|
login! if valid_new_input? && e.key_code == 13
end
button.btn.btn_default(type: :button) { span.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.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.row.alternating.message do
div.col_sm_2 { sender }
FormattedDiv class: "col-sm-8", markdown: params.message[:message]
div.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.row.form_group.input_box.navbar.navbar_inverse.navbar_fixed_bottom do
div.col_sm_1.saytext {"Say: "}
textarea.col_sm_5(rows: rows, value: state.composition).on(:change) do |e|
mutate.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>