diff options
Diffstat (limited to 'plugins/Realtime')
-rw-r--r-- | plugins/Realtime/RealtimePlugin.php | 172 | ||||
-rw-r--r-- | plugins/Realtime/icon_external.gif | bin | 0 -> 90 bytes | |||
-rw-r--r-- | plugins/Realtime/jquery.getUrlParam.js | 72 | ||||
-rw-r--r-- | plugins/Realtime/realtimeupdate.js | 94 |
4 files changed, 278 insertions, 60 deletions
diff --git a/plugins/Realtime/RealtimePlugin.php b/plugins/Realtime/RealtimePlugin.php index 82eca3d08..e30c41156 100644 --- a/plugins/Realtime/RealtimePlugin.php +++ b/plugins/Realtime/RealtimePlugin.php @@ -50,6 +50,11 @@ class RealtimePlugin extends Plugin protected $favorurl = null; protected $deleteurl = null; + /** + * When it's time to initialize the plugin, calculate and + * pass the URLs we need. + */ + function onInitializePlugin() { $this->replyurl = common_local_url('newnotice'); @@ -57,29 +62,26 @@ class RealtimePlugin extends Plugin // FIXME: need to find a better way to pass this pattern in $this->deleteurl = common_local_url('deletenotice', array('notice' => '0000000000')); + return true; } function onEndShowScripts($action) { - $path = null; + $timeline = $this->_getTimeline($action); - switch ($action->trimmed('action')) { - case 'public': - $path = array('public'); - break; - case 'tag': - $tag = $action->trimmed('tag'); - if (!empty($tag)) { - $path = array('tag', $tag); - } else { - return true; - } - break; - default: + // If there's not a timeline on this page, + // just return true + + if (empty($timeline)) { return true; } - $timeline = $this->_pathToChannel($path); + $base = $action->selfUrl(); + if (mb_strstr($base, '?')) { + $url = $base . '&realtime=1'; + } else { + $url = $base . '?realtime=1'; + } $scripts = $this->_getScripts(); @@ -95,10 +97,22 @@ class RealtimePlugin extends Plugin $user_id = 0; } + if ($action->boolean('realtime')) { + $realtimeUI = ' RealtimeUpdate.initPopupWindow();'; + } + else { + $iconurl = common_path('plugins/Realtime/icon_external.gif'); + $realtimeUI = ' RealtimeUpdate.addPopup("'.$url.'", "'.$timeline.'", "'. $iconurl .'");'; + } + $action->elementStart('script', array('type' => 'text/javascript')); - $action->raw("$(document).ready(function() { "); - $action->raw($this->_updateInitialize($timeline, $user_id)); - $action->raw(" });"); + + $script = ' $(document).ready(function() { '. + $realtimeUI. + $this->_updateInitialize($timeline, $user_id). + '}); '; + $action->raw($script); + $action->elementEnd('script'); return true; @@ -108,13 +122,23 @@ class RealtimePlugin extends Plugin { $paths = array(); - // XXX: Add other timelines; this is just for the public one + // Add to the author's timeline + + $user = User::staticGet('id', $notice->profile_id); + + if (!empty($user)) { + $paths[] = array('showstream', $user->nickname); + } + + // Add to the public timeline if ($notice->is_local || ($notice->is_local == 0 && !common_config('public', 'localonly'))) { $paths[] = array('public'); } + // Add to the tags timeline + $tags = $this->getNoticeTags($notice); if (!empty($tags)) { @@ -123,6 +147,46 @@ class RealtimePlugin extends Plugin } } + // Add to inbox timelines + // XXX: do a join + + $inbox = new Notice_inbox(); + $inbox->notice_id = $notice->id; + + if ($inbox->find()) { + while ($inbox->fetch()) { + $user = User::staticGet('id', $inbox->user_id); + $paths[] = array('all', $user->nickname); + } + } + + // Add to the replies timeline + + $reply = new Reply(); + $reply->notice_id = $notice->id; + + if ($reply->find()) { + while ($reply->fetch()) { + $user = User::staticGet('id', $reply->profile_id); + if (!empty($user)) { + $paths[] = array('replies', $user->nickname); + } + } + } + + // Add to the group timeline + // XXX: join + + $gi = new Group_inbox(); + $gi->notice_id = $notice->id; + + if ($gi->find()) { + while ($gi->fetch()) { + $ug = User_group::staticGet('id', $gi->group_id); + $paths[] = array('showgroup', $ug->nickname); + } + } + if (count($paths) > 0) { $json = $this->noticeAsJson($notice); @@ -140,6 +204,39 @@ class RealtimePlugin extends Plugin return true; } + function onStartShowBody($action) + { + $realtime = $action->boolean('realtime'); + if (!$realtime) { + return true; + } + + $action->elementStart('body', + (common_current_user()) ? array('id' => $action->trimmed('action'), + 'class' => 'user_in') + : array('id' => $action->trimmed('action'))); + + $action->elementStart('div', array('id' => 'header')); + + // XXX hack to deal with JS that tries to get the + // root url from page output + + $action->elementStart('address'); + $action->element('a', array('class' => 'url', + 'href' => common_local_url('public')), + ''); + $action->elementEnd('address'); + + if (common_logged_in()) { + $action->showNoticeForm(); + } + $action->elementEnd('div'); + + $action->showContentBlock(); + $action->elementEnd('body'); + return false; // No default processing + } + function noticeAsJson($notice) { // FIXME: this code should be abstracted to a neutral third @@ -224,4 +321,41 @@ class RealtimePlugin extends Plugin { return ''; } + + function _getTimeline($action) + { + $path = null; + $timeline = null; + + $action_name = $action->trimmed('action'); + + switch ($action_name) { + case 'public': + $path = array('public'); + break; + case 'tag': + $tag = $action->trimmed('tag'); + if (!empty($tag)) { + $path = array('tag', $tag); + } + break; + case 'showstream': + case 'all': + case 'replies': + case 'showgroup': + $nickname = common_canonical_nickname($action->trimmed('nickname')); + if (!empty($nickname)) { + $path = array($action_name, $nickname); + } + break; + default: + break; + } + + if (!empty($path)) { + $timeline = $this->_pathToChannel($path); + } + + return $timeline; + } } diff --git a/plugins/Realtime/icon_external.gif b/plugins/Realtime/icon_external.gif Binary files differnew file mode 100644 index 000000000..c4118d53b --- /dev/null +++ b/plugins/Realtime/icon_external.gif diff --git a/plugins/Realtime/jquery.getUrlParam.js b/plugins/Realtime/jquery.getUrlParam.js new file mode 100644 index 000000000..e8f73eb47 --- /dev/null +++ b/plugins/Realtime/jquery.getUrlParam.js @@ -0,0 +1,72 @@ +/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de) + * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) + * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses. + * + * Version 2.1 + * + * Thanks to + * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing. + * Tom Leonard for some improvements + * + */ +jQuery.fn.extend({ +/** +* Returns get parameters. +* +* If the desired param does not exist, null will be returned +* +* To get the document params: +* @example value = $(document).getUrlParam("paramName"); +* +* To get the params of a html-attribut (uses src attribute) +* @example value = $('#imgLink').getUrlParam("paramName"); +*/ + getUrlParam: function(strParamName){ + strParamName = escape(unescape(strParamName)); + + var returnVal = new Array(); + var qString = null; + + if ($(this).attr("nodeName")=="#document") { + //document-handler + + if (window.location.search.search(strParamName) > -1 ){ + + qString = window.location.search.substr(1,window.location.search.length).split("&"); + } + + } else if ($(this).attr("src")!="undefined") { + + var strHref = $(this).attr("src") + if ( strHref.indexOf("?") > -1 ){ + var strQueryString = strHref.substr(strHref.indexOf("?")+1); + qString = strQueryString.split("&"); + } + } else if ($(this).attr("href")!="undefined") { + + var strHref = $(this).attr("href") + if ( strHref.indexOf("?") > -1 ){ + var strQueryString = strHref.substr(strHref.indexOf("?")+1); + qString = strQueryString.split("&"); + } + } else { + return null; + } + + + if (qString==null) return null; + + + for (var i=0;i<qString.length; i++){ + if (escape(unescape(qString[i].split("=")[0])) == strParamName){ + returnVal.push(qString[i].split("=")[1]); + } + + } + + + if (returnVal.length==0) return null; + else if (returnVal.length==1) return returnVal[0]; + else return returnVal; + } +});
\ No newline at end of file diff --git a/plugins/Realtime/realtimeupdate.js b/plugins/Realtime/realtimeupdate.js index d55db5859..57fe0a843 100644 --- a/plugins/Realtime/realtimeupdate.js +++ b/plugins/Realtime/realtimeupdate.js @@ -1,8 +1,8 @@ // add a notice encoded as JSON into the current timeline // +// TODO: i18n RealtimeUpdate = { - _userid: 0, _replyurl: '', _favorurl: '', @@ -10,10 +10,10 @@ RealtimeUpdate = { init: function(userid, replyurl, favorurl, deleteurl) { - RealtimeUpdate._userid = userid; - RealtimeUpdate._replyurl = replyurl; - RealtimeUpdate._favorurl = favorurl; - RealtimeUpdate._deleteurl = deleteurl; + RealtimeUpdate._userid = userid; + RealtimeUpdate._replyurl = replyurl; + RealtimeUpdate._favorurl = favorurl; + RealtimeUpdate._deleteurl = deleteurl; }, receive: function(data) @@ -21,7 +21,7 @@ RealtimeUpdate = { id = data.id; // Don't add it if it already exists - + // if ($("#notice-"+id).length > 0) { return; } @@ -50,30 +50,19 @@ RealtimeUpdate = { "<p class=\"entry-content\">"+html+"</p>"+ "</div>"+ "<div class=\"entry-content\">"+ - "<dl class=\"timestamp\">"+ - "<dt>Published</dt>"+ - "<dd>"+ - "<a rel=\"bookmark\" href=\""+data['url']+"\" >"+ + "<a class=\"timestamp\" rel=\"bookmark\" href=\""+data['url']+"\" >"+ "<abbr class=\"published\" title=\""+data['created_at']+"\">a few seconds ago</abbr>"+ "</a> "+ - "</dd>"+ - "</dl>"+ - "<dl class=\"device\">"+ - "<dt>From</dt> "+ - "<dd>"+source+"</dd>"+ // may have a link, I think - "</dl>"; - + "<span class=\"source\">"+ + "from "+ + "<span class=\"device\">"+source+"</span>"+ // may have a link + "</span>"; if (data['in_reply_to_status_id']) { - ni = ni+" <dl class=\"response\">"+ - "<dt>To</dt>"+ - "<dd>"+ - "<a href=\""+data['in_reply_to_status_url']+"\" rel=\"in-reply-to\">in reply to</a>"+ - "</dd>"+ - "</dl>"; + ni = ni+" <a class=\"response\" href=\""+data['in_reply_to_status_url']+"\">in context</a>"; } ni = ni+"</div>"+ - "<div class=\"notice-options\">"; + "<div class=\"notice-options\">"; if (RealtimeUpdate._userid != 0) { var input = $("form#form_notice fieldset input#token"); @@ -95,12 +84,12 @@ RealtimeUpdate = { var ff; ff = "<form id=\"favor-"+id+"\" class=\"form_favor\" method=\"post\" action=\""+RealtimeUpdate._favorurl+"\">"+ - "<fieldset>"+ - "<legend>Favor this notice</legend>"+ // XXX: i18n + "<fieldset>"+ + "<legend>Favor this notice</legend>"+ "<input name=\"token-"+id+"\" type=\"hidden\" id=\"token-"+id+"\" value=\""+session_key+"\"/>"+ "<input name=\"notice\" type=\"hidden\" id=\"notice-n"+id+"\" value=\""+id+"\"/>"+ "<input type=\"submit\" id=\"favor-submit-"+id+"\" name=\"favor-submit-"+id+"\" class=\"submit\" value=\"Favor\" title=\"Favor this notice\"/>"+ - "</fieldset>"+ + "</fieldset>"+ "</form>"; return ff; }, @@ -108,28 +97,51 @@ RealtimeUpdate = { makeReplyLink: function(id, nickname) { var rl; - rl = "<dl class=\"notice_reply\">"+ - "<dt>Reply to this notice</dt>"+ - "<dd>"+ - "<a href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span>"+ - "</a>"+ - "</dd>"+ - "</dl>"; + rl = "<a class=\"notice_reply\" href=\""+RealtimeUpdate._replyurl+"?replyto="+nickname+"\" title=\"Reply to this notice\">Reply <span class=\"notice_id\">"+id+"</span></a>"; return rl; - }, + }, makeDeleteLink: function(id) { var dl, delurl; delurl = RealtimeUpdate._deleteurl.replace("0000000000", id); - dl = "<dl class=\"notice_delete\">"+ - "<dt>Delete this notice</dt>"+ - "<dd>"+ - "<a href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>"+ - "</dd>"+ - "</dl>"; + dl = "<a class=\"notice_delete\" href=\""+delurl+"\" title=\"Delete this notice\">Delete</a>"; return dl; }, + + addPopup: function(url, timeline, iconurl) + { + $('#site_nav_local_views .current a').append('<button id="realtime_timeline" title="Real-time pop window">↗</button>'); + + $('#realtime_timeline').css({ + 'margin':'2px 0 0 11px', + 'background':'transparent url('+ iconurl + ') no-repeat 45% 45%', + 'text-indent':'-9999px', + 'width':'16px', + 'height':'16px', + 'padding':'0', + 'display':'block', + 'float':'right', + 'border':'none', + 'cursor':'pointer' + }); + + $('#realtime_timeline').click(function() { + window.open(url, + timeline, + 'toolbar=no,resizable=yes,scrollbars=yes,status=yes'); + + return false; + }); + }, + + initPopupWindow: function() + { + window.resizeTo(575, 640); + $('address').hide(); + $('#content').css({'width':'92%'}); + } } + |