From 68340abfb2a9425e04d04a34a31e6e0ff107216f Mon Sep 17 00:00:00 2001 From: Etenil Date: Mon, 7 Feb 2011 22:51:40 +0000 Subject: [PATCH] Rewritten handling of poll events. --- js/movim.js | 83 ++++++++++++++++++++++++++++++++++++ lib/Widget.php | 20 +++++---- lib/widgets/Chat/Chat.php | 22 +++++++--- lib/widgets/Chat/chat.js | 5 +++ lib/widgets/Log/Log.php | 5 ++- lib/widgets/Poller/poller.js | 30 +------------ 6 files changed, 119 insertions(+), 46 deletions(-) diff --git a/js/movim.js b/js/movim.js index 30f287c8a..c395f6f35 100644 --- a/js/movim.js +++ b/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. * diff --git a/lib/Widget.php b/lib/Widget.php index 064af4f9e..c253c0f9b 100644 --- a/lib/Widget.php +++ b/lib/Widget.php @@ -158,20 +158,22 @@ class Widget return $this->css; } + protected function cdata($text) + { + return ''; + } + /** * 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 ''; - echo ''; - echo $id; - echo ''; - echo ''; - echo '"; - echo ""; - echo ""; + echo '' . "\n"; + foreach($payload as $param_value) { + echo ' '.$param_value."\n"; + } + echo ""; } } diff --git a/lib/widgets/Chat/Chat.php b/lib/widgets/Chat/Chat.php index 4a87eeb1a..fc8a68c32 100644 --- a/lib/widgets/Chat/Chat.php +++ b/lib/widgets/Chat/Chat.php @@ -30,22 +30,30 @@ class Chat extends Widget function onIncomingMessage($data) { - $this->sendto('chatMessages', 'PREPEND', - '

' . substr($data['from'], 0, strpos($data['from'], '@')) . ': ' . $data['body'] . '

'); + $this->sendto('movim_prepend', array( + 'chatMessages', + $this->cdata('

' . substr($data['from'], 0, strpos($data['from'], '@')) . ': ' . $data['body'] . '

'), + )); } function onIncomingActive($data) { - $this->sendto('chatState', 'FILL', '

'.substr($data['from'], 0, strpos($data['from'], '@')). "'s chat is active

"); + $this->sendto('movim_fill', array( + 'chatState', + $this->cdata('

'.substr($data['from'], 0, strpos($data['from'], '@')). "'s chat is active

"), + )); } function onIncomingComposing($data) { - $this->sendto('chatState', 'FILL', '

'.substr($data['from'], 0, strpos($data['from'], '@')). " is composing

"); + $this->sendto('movim_fill', array( + 'chatState', + $this->cdata($this->cdata('

'.substr($data['from'], 0, strpos($data['from'], '@')). " is composing

")), + )); } 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
- + }"/> - " value=""/> + " value=""/> sendto('log', 'PREPEND', "" . date('H:i:s> ') . "data : " . var_export($data, true) ."
"); + $this->sendto('movim_prepend', array( + 'log', + $this->cdata("" . date('H:i:s> ') . "data : " . var_export($data, true) ."
"), + )); } } diff --git a/lib/widgets/Poller/poller.js b/lib/widgets/Poller/poller.js index 43206b8d4..bcccc7a7a 100644 --- a/lib/widgets/Poller/poller.js +++ b/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) {