Class: Index

Inherits:
WebPage show all
Defined in:
lib/h2g_ajaxchat.rb

Instance Attribute Summary

Attributes inherited from WebPage

#css, #html, #js, #s

Instance Method Summary collapse

Methods inherited from WebPage

#to_s

Constructor Details

#initialize(h) ⇒ Index

Returns a new instance of Index.



188
189
190
# File 'lib/h2g_ajaxchat.rb', line 188

def initialize(h)
  @name, @h = :index, h
end

Instance Method Details

#to_cssObject



192
193
194
195
196
197
198
# File 'lib/h2g_ajaxchat.rb', line 192

def to_css()
@css ||= '
body {font-family: Arial;}
#chatbox {overflow: scroll; height: 40%}
div p span {colour: #dde}
'
end

#to_htmlObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/h2g_ajaxchat.rb', line 200

def to_html()
  
  b = binding
  
@html ||= <<EOF
<body onload="refresh()">
<div id="wrapper">
 <div id="menu">
  <p class="welcome">Welcome, <b> <%= @h[:username] %> </b></p>
  <p class="logout"><a id="exit" href="logout">Exit Chat</a></p>
  <div style="clear:both"></div>
 </div>	
 <div id="chatbox"></div>
  <input name="usermsg" type="text" id="usermsg" size="33" onkeyup='ajaxCall1(event.keyCode, this)' autofocus='true'/>

</div>      
EOF

  ERB.new(@html).result(b)
end

#to_jsObject



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
# File 'lib/h2g_ajaxchat.rb', line 221

def to_js()

@js ||= <<EOF
  
function updateScroll(){
var element = document.getElementById("chatbox");
element.scrollTop = element.scrollHeight;
}  
// ajaxCall1();

function ajaxRequest(url, cFunction) {
  var xhttp;
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
  cFunction(this);
}
  };
  xhttp.open("GET", url, true);
  xhttp.send();
}


function ajaxCall1(keyCode, e) {
  if (keyCode==13){
ajaxRequest('chatter?msg=' + e.value, ajaxResponse1)
e.value = '';
  }  
}

function ajaxResponse1(xhttp) {
  e = document.getElementById('chatbox')
  s = xhttp.responseText;
  e.innerHTML = e.innerHTML + s;
  
  if (s.length > 1)
updateScroll();
}

function refresh() {
  setInterval(ajaxCall2,2000);
}

function ajaxCall2() {
  ajaxRequest('chatter', ajaxResponse1)
}

EOF

end