Class: Index

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

Instance Attribute Summary

Attributes inherited from WebPage

#css, #name, #s

Instance Method Summary collapse

Methods inherited from WebPage

#to_s

Constructor Details

#initialize(h) ⇒ Index

Returns a new instance of Index.



191
192
193
# File 'lib/h2g_ajaxchat.rb', line 191

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

Instance Method Details

#htmlObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/h2g_ajaxchat.rb', line 195

def html()
  
@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

end

#jsObject



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

def 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

#to_cssObject



213
214
215
216
217
218
219
# File 'lib/h2g_ajaxchat.rb', line 213

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

#to_htmlObject



221
222
223
224
225
226
# File 'lib/h2g_ajaxchat.rb', line 221

def to_html()
  
  b = binding

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

#to_jsObject



279
280
281
# File 'lib/h2g_ajaxchat.rb', line 279

def to_js()
  @js
end