Browse Source

Rewritten handling of poll events.

pull/5/head
Etenil 15 years ago
parent
commit
68340abfb2
  1. 83
      js/movim.js
  2. 20
      lib/Widget.php
  3. 22
      lib/widgets/Chat/Chat.php
  4. 5
      lib/widgets/Chat/chat.js
  5. 5
      lib/widgets/Log/Log.php
  6. 30
      lib/widgets/Poller/poller.js

83
js/movim.js

@ -10,6 +10,58 @@ var PREPEND = 4;
var movimPollHandlers = new Array();
var onloaders = new Array();
/**********************************************************
* The following functions are used as standard callbacks *
* in the widgets. *
**********************************************************/
// movim_append(div, text)
function movim_append(params)
{
if(params.length < 2) {
return;
}
target = document.getElementById(params[0]);
if(target) {
target.innerHTML += params[1];
}
}
// movim_prepend(div, text)
function movim_prepend(params)
{
if(params.length < 2) {
return;
}
target = document.getElementById(params[0]);
if(target) {
target.innerHTML = params[1] + target.innerHTML;
}
}
// movim_fill(div, text)
function movim_fill(params)
{
if(params.length < 2) {
return;
}
target = document.getElementById(params[0]);
if(target) {
target.innerHTML = params[1];
}
}
// movim_drop()
function movim_drop(params)
{
// log('movim_drop called.');
}
/**********
* Done. *
**********/
/**
* Adds a function to the onload event.
*/
@ -79,6 +131,37 @@ function log(text)
}
}
/**
* Handles returns (xmlrpc)
*/
function movim_xmlrpc(xml)
{
if(xml != null) {
var funcalls = xml.getElementsByTagName("funcall");
for(h = 0; h < funcalls.length; h++) {
var func = funcalls[h];
var funcname = func.attributes.getNamedItem("name").nodeValue;
var f = window[funcname];
var params = func.childNodes;
var aparams = new Array();
for(p = 0; p < params.length; p++) {
if(params[p].nodeName != "param")
continue;
aparams.push(params[p].textContent);
}
try {
f(aparams);
}
catch(err) {
log("Error caught: " + err.toString());
}
}
}
}
/**
* Sends data to the movim server through ajax.
*

20
lib/Widget.php

@ -158,20 +158,22 @@ class Widget
return $this->css;
}
protected function cdata($text)
{
return '<![CDATA['.$text.']]>';
}
/**
* Prints out a widget content in XML to be handled by javascript for
* ajax return.
*/
protected function sendto($id, $method, $payload)
protected function sendto($method, array $payload = null)
{
echo '<movim>';
echo '<target method="' . $method . '">';
echo $id;
echo '</target>';
echo '<payload>';
echo '<![CDATA[' . $payload . "]]>";
echo "</payload>";
echo "</movim>";
echo '<funcall name="'.$method.'">' . "\n";
foreach($payload as $param_value) {
echo ' <param>'.$param_value."</param>\n";
}
echo "</funcall>";
}
}

22
lib/widgets/Chat/Chat.php

@ -30,22 +30,30 @@ class Chat extends Widget
function onIncomingMessage($data)
{
$this->sendto('chatMessages', 'PREPEND',
'<p class="message">' . substr($data['from'], 0, strpos($data['from'], '@')) . ': ' . $data['body'] . '</p>');
$this->sendto('movim_prepend', array(
'chatMessages',
$this->cdata('<p class="message">' . substr($data['from'], 0, strpos($data['from'], '@')) . ': ' . $data['body'] . '</p>'),
));
}
function onIncomingActive($data)
{
$this->sendto('chatState', 'FILL', '<h3>'.substr($data['from'], 0, strpos($data['from'], '@')). "'s chat is active</h3>");
$this->sendto('movim_fill', array(
'chatState',
$this->cdata('<h3>'.substr($data['from'], 0, strpos($data['from'], '@')). "'s chat is active</h3>"),
));
}
function onIncomingComposing($data) {
$this->sendto('chatState', 'FILL', '<h3>'.substr($data['from'], 0, strpos($data['from'], '@')). " is composing</h3>");
$this->sendto('movim_fill', array(
'chatState',
$this->cdata($this->cdata('<h3>'.substr($data['from'], 0, strpos($data['from'], '@')). " is composing</h3>")),
));
}
function onIncomingPresence($data)
{
echo "onIncomingPresence was called. Message: $data";
// echo "onIncomingPresence was called. Message: $data";
}
function ajaxSendMessage($message, $to)
@ -62,9 +70,9 @@ class Chat extends Widget
</div>
<div id="chatMessages">
</div>
<input type="text" id="chatInput" value="Message" onfocus="myFocus(this);" onblur="myBlur(this);"/>
<input type="text" id="chatInput" value="Message" onfocus="myFocus(this);" onblur="myBlur(this);" onkeypress="if(event.keyCode == 13) {<?php $this->callAjax('ajaxSendMessage', 'movim_drop', "'test'", "getDest()", "getMessageText()");?>}"/>
<input type="text" id="chatTo" value="To" onfocus="myFocus(this);" onblur="myBlur(this);" />
<input type="button" id="chatSend" onclick="<?php $this->callAjax('ajaxSendMessage', 'DROP', "'test'", "'movim@movim.eu'", "getMessageText()");?>" value="<?php echo t('Send');?>"/>
<input type="button" id="chatSend" onclick="<?php $this->callAjax('ajaxSendMessage', 'movim_drop', "'test'", "getDest()", "getMessageText()");?>" value="<?php echo t('Send');?>"/>
</div>
<?php

5
lib/widgets/Chat/chat.js

@ -9,3 +9,8 @@ function getMessageText()
return text;
}
function getDest()
{
return document.getElementById('chatTo').value;
}

5
lib/widgets/Log/Log.php

@ -36,7 +36,10 @@ class Log extends Widget
function onEvent($data)
{
$this->sendto('log', 'PREPEND', "<span>" . date('H:i:s> ') . "data :</span> " . var_export($data, true) ."<br />");
$this->sendto('movim_prepend', array(
'log',
$this->cdata("<span>" . date('H:i:s> ') . "data :</span> " . var_export($data, true) ."<br />"),
));
}
}

30
lib/widgets/Poller/poller.js

@ -23,35 +23,7 @@ function movim_poll()
{
if(poller.status == 200) {
// Handling poll return.
var movimreturn = poller.responseXML;
try {
if(movimreturn != null) {
var movimtags = movimreturn.getElementsByTagName("movim");
for(h = 0; h < movimtags.length; h++) {
var widgetreturn = movimtags[h];
var target = widgetreturn.getElementsByTagName("target")[0].childNodes[0].textContent;
var method = widgetreturn.getElementsByTagName("target")[0].attributes.getNamedItem("method").nodeValue;
var payload = widgetreturn.getElementsByTagName("payload")[0].childNodes[0].nodeValue;
if(method == 'APPEND') {
document.getElementById(target).innerHTML += payload;
}
else if(method == 'PREPEND') {
var elt = document.getElementById(target);
elt.innerHTML = payload + elt.innerHTML;
}
else if(method == 'DROP') {
// Do nothing.
}
else { // Default is FILL.
document.getElementById(target).innerHTML = payload;
}
}
}
}
catch(err) {
log("Error caught: " + err.toString());
}
movim_xmlrpc(poller.responseXML);
}
if(poller.status > 0) {

Loading…
Cancel
Save