From e3caec9d8a512586461de91962c0a7d295d09228 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Wed, 2 Dec 2009 21:24:41 -0500 Subject: Load notice data using javascript from the html on the page instead of writing json representations of the notices on each page Clicking on a geo link pops up a map (round 2) --- plugins/Mapstraction/MapstractionPlugin.php | 72 +---------------------------- plugins/Mapstraction/allmap.php | 61 +++++++++++++++++++++++- plugins/Mapstraction/usermap.php | 66 +++++++++++++++++++++++++- 3 files changed, 127 insertions(+), 72 deletions(-) (limited to 'plugins/Mapstraction') diff --git a/plugins/Mapstraction/MapstractionPlugin.php b/plugins/Mapstraction/MapstractionPlugin.php index c0c2c5b8e..a0dbf5204 100644 --- a/plugins/Mapstraction/MapstractionPlugin.php +++ b/plugins/Mapstraction/MapstractionPlugin.php @@ -58,12 +58,12 @@ class MapstractionPlugin extends Plugin * * The way to register new actions from a plugin. * - * @param Router $m reference to router + * @param Router &$m reference to router * * @return boolean event handler return */ - function onRouterInitialized($m) + function onRouterInitialized(&$m) { $m->connect(':nickname/all/map', array('action' => 'allmap'), @@ -110,11 +110,6 @@ class MapstractionPlugin extends Plugin function onEndShowScripts($action) { $actionName = $action->trimmed('action'); - // These are the ones that have maps on 'em - if (!in_array($actionName, - array('showstream', 'all', 'allmap', 'usermap'))) { - return true; - } switch ($this->provider) { @@ -151,39 +146,6 @@ class MapstractionPlugin extends Plugin $action->raw(sprintf('var _provider = "%s";', $this->provider)); $action->elementEnd('script'); - switch ($actionName) { - case 'usermap': - case 'showstream': - $notice = empty($action->tag) - ? $action->user->getNotices(($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1) - : $action->user->getTaggedNotices($action->tag, ($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null); - break; - case 'all': - case 'allmap': - $cur = common_current_user(); - if (!empty($cur) && $cur->id == $action->user->id) { - $notice = $action->user->noticeInbox(($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); - } else { - $notice = $action->user->noticesWithFriends(($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); - } - break; - } - - $jsonArray = array(); - - while ($notice->fetch()) { - if (!empty($notice->lat) && !empty($notice->lon)) { - $jsonNotice = $this->noticeAsJson($notice); - $jsonArray[] = $jsonNotice; - } - } - - $action->elementStart('script', array('type' => 'text/javascript')); - $action->raw('/*raw('var _notices = ' . json_encode($jsonArray)); - $action->raw('/*]]>*/'); // XHTML compat for Safari - $action->elementEnd('script'); - return true; } @@ -214,34 +176,4 @@ class MapstractionPlugin extends Plugin $action->elementEnd('div'); } - - function noticeAsJson($notice) - { - // FIXME: this code should be abstracted to a neutral third - // party, like Notice::asJson(). I'm not sure of the ethics - // of refactoring from within a plugin, so I'm just abusing - // the ApiAction method. Don't do this unless you're me! - - require_once(INSTALLDIR.'/lib/api.php'); - - $act = new ApiAction('/dev/null'); - - $arr = $act->twitterStatusArray($notice, true); - $arr['url'] = $notice->bestUrl(); - $arr['html'] = $notice->rendered; - $arr['source'] = $arr['source']; - - if (!empty($notice->reply_to)) { - $reply_to = Notice::staticGet('id', $notice->reply_to); - if (!empty($reply_to)) { - $arr['in_reply_to_status_url'] = $reply_to->bestUrl(); - } - $reply_to = null; - } - - $profile = $notice->getProfile(); - $arr['user']['profile_url'] = $profile->profileurl; - - return $arr; - } } diff --git a/plugins/Mapstraction/allmap.php b/plugins/Mapstraction/allmap.php index 6a48b141f..3a78ac0d4 100644 --- a/plugins/Mapstraction/allmap.php +++ b/plugins/Mapstraction/allmap.php @@ -122,4 +122,63 @@ class AllmapAction extends OwnerDesignAction 'class' => 'gray smallmap', 'style' => "width: 100%; height: 400px")); } -} \ No newline at end of file + + function showScripts() + { + parent::showScripts(); + $cur = common_current_user(); + if (!empty($cur) && $cur->id == $this->user->id) { + $notice = $this->user->noticeInbox(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); + } else { + $notice = $this->user->noticesWithFriends(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); + } + + $jsonArray = array(); + + while ($notice->fetch()) { + if (!empty($notice->lat) && !empty($notice->lon)) { + $jsonNotice = $this->noticeAsJson($notice); + $jsonArray[] = $jsonNotice; + } + } + + $this->elementStart('script', array('type' => 'text/javascript')); + $this->raw('/*raw('var _notices = ' . json_encode($jsonArray).'; '); + $this->raw('showMapstraction($("#map_canvas"),_notices);'); + $this->raw('/*]]>*/'); // XHTML compat for Safari + $this->elementEnd('script'); + + return true; + } + + function noticeAsJson($notice) + { + // FIXME: this code should be abstracted to a neutral third + // party, like Notice::asJson(). I'm not sure of the ethics + // of refactoring from within a plugin, so I'm just abusing + // the ApiAction method. Don't do this unless you're me! + + require_once(INSTALLDIR.'/lib/api.php'); + + $act = new ApiAction('/dev/null'); + + $arr = $act->twitterStatusArray($notice, true); + $arr['url'] = $notice->bestUrl(); + $arr['html'] = $notice->rendered; + $arr['source'] = $arr['source']; + + if (!empty($notice->reply_to)) { + $reply_to = Notice::staticGet('id', $notice->reply_to); + if (!empty($reply_to)) { + $arr['in_reply_to_status_url'] = $reply_to->bestUrl(); + } + $reply_to = null; + } + + $profile = $notice->getProfile(); + $arr['user']['profile_url'] = $profile->profileurl; + + return $arr; + } +} diff --git a/plugins/Mapstraction/usermap.php b/plugins/Mapstraction/usermap.php index fbf1469c3..afa48b162 100644 --- a/plugins/Mapstraction/usermap.php +++ b/plugins/Mapstraction/usermap.php @@ -123,4 +123,68 @@ class UsermapAction extends OwnerDesignAction 'class' => 'gray smallmap', 'style' => "width: 100%; height: 400px")); } -} \ No newline at end of file + + /** + * Hook for adding extra JavaScript + * + * @param Action $action Action object for the page + * + * @return boolean event handler return + */ + + function showScripts() + { + parent::showScripts(); + $notice = empty($this->tag) + ? $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1) + : $this->user->getTaggedNotices($this->tag, ($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null); + + $jsonArray = array(); + + while ($notice->fetch()) { + if (!empty($notice->lat) && !empty($notice->lon)) { + $jsonNotice = $this->noticeAsJson($notice); + $jsonArray[] = $jsonNotice; + } + } + + $this->elementStart('script', array('type' => 'text/javascript')); + $this->raw('/*raw('var _notices = ' . json_encode($jsonArray).'; '); + $this->raw('showMapstraction($("#map_canvas"),_notices);'); + $this->raw('/*]]>*/'); // XHTML compat for Safari + $this->elementEnd('script'); + + return true; + } + + function noticeAsJson($notice) + { + // FIXME: this code should be abstracted to a neutral third + // party, like Notice::asJson(). I'm not sure of the ethics + // of refactoring from within a plugin, so I'm just abusing + // the ApiAction method. Don't do this unless you're me! + + require_once(INSTALLDIR.'/lib/api.php'); + + $act = new ApiAction('/dev/null'); + + $arr = $act->twitterStatusArray($notice, true); + $arr['url'] = $notice->bestUrl(); + $arr['html'] = $notice->rendered; + $arr['source'] = $arr['source']; + + if (!empty($notice->reply_to)) { + $reply_to = Notice::staticGet('id', $notice->reply_to); + if (!empty($reply_to)) { + $arr['in_reply_to_status_url'] = $reply_to->bestUrl(); + } + $reply_to = null; + } + + $profile = $notice->getProfile(); + $arr['user']['profile_url'] = $profile->profileurl; + + return $arr; + } +} -- cgit v1.2.3-54-g00ecf From 43a1a355241079da6bf355a996910b9d116bf105 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Thu, 3 Dec 2009 19:43:02 -0500 Subject: refactor the common parts of usermap and allmap into a common base class --- plugins/Mapstraction/MapstractionPlugin.php | 1 + plugins/Mapstraction/allmap.php | 125 ++------------------ plugins/Mapstraction/map.php | 170 ++++++++++++++++++++++++++++ plugins/Mapstraction/usermap.php | 130 ++------------------- 4 files changed, 189 insertions(+), 237 deletions(-) create mode 100644 plugins/Mapstraction/map.php (limited to 'plugins/Mapstraction') diff --git a/plugins/Mapstraction/MapstractionPlugin.php b/plugins/Mapstraction/MapstractionPlugin.php index a0dbf5204..ca1a3bf20 100644 --- a/plugins/Mapstraction/MapstractionPlugin.php +++ b/plugins/Mapstraction/MapstractionPlugin.php @@ -90,6 +90,7 @@ class MapstractionPlugin extends Plugin { case 'AllmapAction': case 'UsermapAction': + case 'MapAction': include_once INSTALLDIR.'/plugins/Mapstraction/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; return false; default: diff --git a/plugins/Mapstraction/allmap.php b/plugins/Mapstraction/allmap.php index 3a78ac0d4..0c4f03b31 100644 --- a/plugins/Mapstraction/allmap.php +++ b/plugins/Mapstraction/allmap.php @@ -37,59 +37,26 @@ if (!defined('STATUSNET')) { * @category Mapstraction * @package StatusNet * @author Evan Prodromou + * @author Craig Andrews * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ */ -class AllmapAction extends OwnerDesignAction +class AllmapAction extends MapAction { - var $profile = null; - var $page = null; - var $notices = null; - - public $plugin = null; - function prepare($args) { - parent::prepare($args); - - $nickname_arg = $this->arg('nickname'); - $nickname = common_canonical_nickname($nickname_arg); - - // Permanent redirect on non-canonical nickname - - if ($nickname_arg != $nickname) { - $args = array('nickname' => $nickname); - if ($this->arg('page') && $this->arg('page') != 1) { - $args['page'] = $this->arg['page']; + if(parent::prepare($args)) { + $cur = common_current_user(); + if (!empty($cur) && $cur->id == $this->user->id) { + $this->notice = $this->user->noticeInbox(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); + } else { + $this->notice = $this->user->noticesWithFriends(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); } - common_redirect(common_local_url($this->trimmed('action'), $args), 301); - return false; - } - - $this->user = User::staticGet('nickname', $nickname); - - if (!$this->user) { - $this->clientError(_('No such user.'), 404); + return true; + }else{ return false; } - - $this->profile = $this->user->getProfile(); - - if (!$this->profile) { - $this->serverError(_('User has no profile.')); - return false; - } - - $page = $this->trimmed('page'); - - if (!empty($page) && Validate::number($page)) { - $this->page = $page+0; - } else { - $this->page = 1; - } - - return true; } function title() @@ -109,76 +76,4 @@ class AllmapAction extends OwnerDesignAction $this->page); } } - - function handle($args) - { - parent::handle($args); - $this->showPage(); - } - - function showContent() - { - $this->element('div', array('id' => 'map_canvas', - 'class' => 'gray smallmap', - 'style' => "width: 100%; height: 400px")); - } - - function showScripts() - { - parent::showScripts(); - $cur = common_current_user(); - if (!empty($cur) && $cur->id == $this->user->id) { - $notice = $this->user->noticeInbox(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); - } else { - $notice = $this->user->noticesWithFriends(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); - } - - $jsonArray = array(); - - while ($notice->fetch()) { - if (!empty($notice->lat) && !empty($notice->lon)) { - $jsonNotice = $this->noticeAsJson($notice); - $jsonArray[] = $jsonNotice; - } - } - - $this->elementStart('script', array('type' => 'text/javascript')); - $this->raw('/*raw('var _notices = ' . json_encode($jsonArray).'; '); - $this->raw('showMapstraction($("#map_canvas"),_notices);'); - $this->raw('/*]]>*/'); // XHTML compat for Safari - $this->elementEnd('script'); - - return true; - } - - function noticeAsJson($notice) - { - // FIXME: this code should be abstracted to a neutral third - // party, like Notice::asJson(). I'm not sure of the ethics - // of refactoring from within a plugin, so I'm just abusing - // the ApiAction method. Don't do this unless you're me! - - require_once(INSTALLDIR.'/lib/api.php'); - - $act = new ApiAction('/dev/null'); - - $arr = $act->twitterStatusArray($notice, true); - $arr['url'] = $notice->bestUrl(); - $arr['html'] = $notice->rendered; - $arr['source'] = $arr['source']; - - if (!empty($notice->reply_to)) { - $reply_to = Notice::staticGet('id', $notice->reply_to); - if (!empty($reply_to)) { - $arr['in_reply_to_status_url'] = $reply_to->bestUrl(); - } - $reply_to = null; - } - - $profile = $notice->getProfile(); - $arr['user']['profile_url'] = $profile->profileurl; - - return $arr; - } } diff --git a/plugins/Mapstraction/map.php b/plugins/Mapstraction/map.php new file mode 100644 index 000000000..89723f43c --- /dev/null +++ b/plugins/Mapstraction/map.php @@ -0,0 +1,170 @@ +. + * + * @category Mapstraction + * @package StatusNet + * @author Evan Prodromou + * @copyright 2009 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Show a map of notices + * + * @category Mapstraction + * @package StatusNet + * @author Evan Prodromou + * @author Craig Andrews + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +class MapAction extends OwnerDesignAction +{ + var $profile = null; + var $page = null; + var $notices = null; + + function prepare($args) + { + parent::prepare($args); + + $nickname_arg = $this->arg('nickname'); + $nickname = common_canonical_nickname($nickname_arg); + + // Permanent redirect on non-canonical nickname + + if ($nickname_arg != $nickname) { + $args = array('nickname' => $nickname); + if ($this->arg('page') && $this->arg('page') != 1) { + $args['page'] = $this->arg['page']; + } + common_redirect(common_local_url($this->trimmed('action'), $args), 301); + return false; + } + + $this->user = User::staticGet('nickname', $nickname); + + if (!$this->user) { + $this->clientError(_('No such user.'), 404); + return false; + } + + $this->profile = $this->user->getProfile(); + + if (!$this->profile) { + $this->serverError(_('User has no profile.')); + return false; + } + + $page = $this->trimmed('page'); + + if (!empty($page) && Validate::number($page)) { + $this->page = $page+0; + } else { + $this->page = 1; + } + + $this->notices = empty($this->tag) + ? $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1) + : $this->user->getTaggedNotices($this->tag, ($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null); + + return true; + } + + function handle($args) + { + parent::handle($args); + $this->showPage(); + } + + function showContent() + { + $this->element('div', array('id' => 'map_canvas', + 'class' => 'gray smallmap', + 'style' => "width: 100%; height: 400px")); + } + + /** + * Hook for adding extra JavaScript + * + * @param Action $action Action object for the page + * + * @return boolean event handler return + */ + + function showScripts() + { + parent::showScripts(); + $jsonArray = array(); + + while ($this->notice->fetch()) { + if (!empty($this->notice->lat) && !empty($this->notice->lon)) { + $jsonNotice = $this->noticeAsJson($this->notice); + $jsonArray[] = $jsonNotice; + } + } + + $this->elementStart('script', array('type' => 'text/javascript')); + $this->raw('/*raw('var _notices = ' . json_encode($jsonArray).'; '); + $this->raw('showMapstraction($("#map_canvas"),_notices);'); + $this->raw('/*]]>*/'); // XHTML compat for Safari + $this->elementEnd('script'); + + return true; + } + + function noticeAsJson($notice) + { + // FIXME: this code should be abstracted to a neutral third + // party, like Notice::asJson(). I'm not sure of the ethics + // of refactoring from within a plugin, so I'm just abusing + // the ApiAction method. Don't do this unless you're me! + + require_once(INSTALLDIR.'/lib/api.php'); + + $act = new ApiAction('/dev/null'); + + $arr = $act->twitterStatusArray($notice, true); + $arr['url'] = $notice->bestUrl(); + $arr['html'] = $notice->rendered; + $arr['source'] = $arr['source']; + + if (!empty($notice->reply_to)) { + $reply_to = Notice::staticGet('id', $notice->reply_to); + if (!empty($reply_to)) { + $arr['in_reply_to_status_url'] = $reply_to->bestUrl(); + } + $reply_to = null; + } + + $profile = $notice->getProfile(); + $arr['user']['profile_url'] = $profile->profileurl; + + return $arr; + } +} diff --git a/plugins/Mapstraction/usermap.php b/plugins/Mapstraction/usermap.php index afa48b162..3e517ee86 100644 --- a/plugins/Mapstraction/usermap.php +++ b/plugins/Mapstraction/usermap.php @@ -37,61 +37,24 @@ if (!defined('STATUSNET')) { * @category Mapstraction * @package StatusNet * @author Evan Prodromou + * @author Craig Andrews * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ */ -class UsermapAction extends OwnerDesignAction +class UsermapAction extends MapAction { - var $profile = null; - var $page = null; - var $notices = null; - - public $plugin = null; function prepare($args) { - parent::prepare($args); - - $nickname_arg = $this->arg('nickname'); - $nickname = common_canonical_nickname($nickname_arg); - - // Permanent redirect on non-canonical nickname - - if ($nickname_arg != $nickname) { - $args = array('nickname' => $nickname); - if ($this->arg('page') && $this->arg('page') != 1) { - $args['page'] = $this->arg['page']; - } - common_redirect(common_local_url($this->trimmed('action'), $args), 301); - return false; - } - - $this->user = User::staticGet('nickname', $nickname); - - if (!$this->user) { - $this->clientError(_('No such user.'), 404); - return false; - } - - $this->profile = $this->user->getProfile(); - - if (!$this->profile) { - $this->serverError(_('User has no profile.')); + if(parent::prepare($args)) { + $this->notice = empty($this->tag) + ? $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1) + : $this->user->getTaggedNotices($this->tag, ($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null); + return true; + }else{ return false; } - - $page = $this->trimmed('page'); - - if (!empty($page) && Validate::number($page)) { - $this->page = $page+0; - } else { - $this->page = 1; - } - - $this->notices = $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); - - return true; } function title() @@ -110,81 +73,4 @@ class UsermapAction extends OwnerDesignAction $this->page); } } - - function handle($args) - { - parent::handle($args); - $this->showPage(); - } - - function showContent() - { - $this->element('div', array('id' => 'map_canvas', - 'class' => 'gray smallmap', - 'style' => "width: 100%; height: 400px")); - } - - /** - * Hook for adding extra JavaScript - * - * @param Action $action Action object for the page - * - * @return boolean event handler return - */ - - function showScripts() - { - parent::showScripts(); - $notice = empty($this->tag) - ? $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1) - : $this->user->getTaggedNotices($this->tag, ($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null); - - $jsonArray = array(); - - while ($notice->fetch()) { - if (!empty($notice->lat) && !empty($notice->lon)) { - $jsonNotice = $this->noticeAsJson($notice); - $jsonArray[] = $jsonNotice; - } - } - - $this->elementStart('script', array('type' => 'text/javascript')); - $this->raw('/*raw('var _notices = ' . json_encode($jsonArray).'; '); - $this->raw('showMapstraction($("#map_canvas"),_notices);'); - $this->raw('/*]]>*/'); // XHTML compat for Safari - $this->elementEnd('script'); - - return true; - } - - function noticeAsJson($notice) - { - // FIXME: this code should be abstracted to a neutral third - // party, like Notice::asJson(). I'm not sure of the ethics - // of refactoring from within a plugin, so I'm just abusing - // the ApiAction method. Don't do this unless you're me! - - require_once(INSTALLDIR.'/lib/api.php'); - - $act = new ApiAction('/dev/null'); - - $arr = $act->twitterStatusArray($notice, true); - $arr['url'] = $notice->bestUrl(); - $arr['html'] = $notice->rendered; - $arr['source'] = $arr['source']; - - if (!empty($notice->reply_to)) { - $reply_to = Notice::staticGet('id', $notice->reply_to); - if (!empty($reply_to)) { - $arr['in_reply_to_status_url'] = $reply_to->bestUrl(); - } - $reply_to = null; - } - - $profile = $notice->getProfile(); - $arr['user']['profile_url'] = $profile->profileurl; - - return $arr; - } } -- cgit v1.2.3-54-g00ecf From 35ff3961e8a51e41517d8a8b9e5a1a21f826e4ca Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Thu, 3 Dec 2009 19:59:31 -0500 Subject: Remove unnecessary pass by reference indicators --- plugins/Mapstraction/MapstractionPlugin.php | 4 ++-- plugins/UserFlag/UserFlagPlugin.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins/Mapstraction') diff --git a/plugins/Mapstraction/MapstractionPlugin.php b/plugins/Mapstraction/MapstractionPlugin.php index ca1a3bf20..c4f9cd77d 100644 --- a/plugins/Mapstraction/MapstractionPlugin.php +++ b/plugins/Mapstraction/MapstractionPlugin.php @@ -58,12 +58,12 @@ class MapstractionPlugin extends Plugin * * The way to register new actions from a plugin. * - * @param Router &$m reference to router + * @param Router $m reference to router * * @return boolean event handler return */ - function onRouterInitialized(&$m) + function onRouterInitialized($m) { $m->connect(':nickname/all/map', array('action' => 'allmap'), diff --git a/plugins/UserFlag/UserFlagPlugin.php b/plugins/UserFlag/UserFlagPlugin.php index 60c0c2c0a..e13c2555a 100644 --- a/plugins/UserFlag/UserFlagPlugin.php +++ b/plugins/UserFlag/UserFlagPlugin.php @@ -68,7 +68,7 @@ class UserFlagPlugin extends Plugin return true; } - function onRouterInitialized(&$m) { + function onRouterInitialized($m) { $m->connect('main/flag/profile', array('action' => 'flagprofile')); $m->connect('admin/profile/flag', array('action' => 'adminprofileflag')); return true; -- cgit v1.2.3-54-g00ecf From a92228c713c73b4015766b45eecd4769a57ae232 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 14:31:19 -0500 Subject: Update Mapstraction to properly scrape data from notice-list pages Refactored the JavaScript for Mapstraction use so it scrapes data from the HTML of the page it's on, if possible, and otherwise generates a JSON array. --- plugins/Mapstraction/MapstractionPlugin.php | 22 +++++++- plugins/Mapstraction/map.php | 9 +-- plugins/Mapstraction/usermap.js | 87 +++++++++++++++-------------- 3 files changed, 67 insertions(+), 51 deletions(-) (limited to 'plugins/Mapstraction') diff --git a/plugins/Mapstraction/MapstractionPlugin.php b/plugins/Mapstraction/MapstractionPlugin.php index c4f9cd77d..ba7827151 100644 --- a/plugins/Mapstraction/MapstractionPlugin.php +++ b/plugins/Mapstraction/MapstractionPlugin.php @@ -112,6 +112,11 @@ class MapstractionPlugin extends Plugin { $actionName = $action->trimmed('action'); + if (!in_array($actionName, + array('showstream', 'all', 'usermap', 'allmap'))) { + return true; + } + switch ($this->provider) { case 'cloudmade': @@ -143,9 +148,20 @@ class MapstractionPlugin extends Plugin $action->script(common_path('plugins/Mapstraction/usermap.js')); - $action->elementStart('script', array('type' => 'text/javascript')); - $action->raw(sprintf('var _provider = "%s";', $this->provider)); - $action->elementEnd('script'); + $action->inlineScript(sprintf('var _provider = "%s";', $this->provider)); + + // usermap and allmap handle this themselves + + if (in_array($actionName, + array('showstream', 'all'))) { + $action->inlineScript('$(document).ready(function() { '. + ' var user = null; '. + (($actionName == 'showstream') ? ' user = scrapeUser(); ' : '') . + ' var notices = scrapeNotices(user); ' . + ' console.log(notices); ' . + ' showMapstraction($("#map_canvas"), notices); '. + '});'); + } return true; } diff --git a/plugins/Mapstraction/map.php b/plugins/Mapstraction/map.php index 89723f43c..734e48088 100644 --- a/plugins/Mapstraction/map.php +++ b/plugins/Mapstraction/map.php @@ -128,12 +128,9 @@ class MapAction extends OwnerDesignAction } } - $this->elementStart('script', array('type' => 'text/javascript')); - $this->raw('/*raw('var _notices = ' . json_encode($jsonArray).'; '); - $this->raw('showMapstraction($("#map_canvas"),_notices);'); - $this->raw('/*]]>*/'); // XHTML compat for Safari - $this->elementEnd('script'); + $this->inlineScript('$(document).ready(function() { '. + ' var _notices = ' . json_encode($jsonArray).'; ' . + 'showMapstraction($("#map_canvas"), _notices); });'); return true; } diff --git a/plugins/Mapstraction/usermap.js b/plugins/Mapstraction/usermap.js index e667dd579..4b7a6c26b 100644 --- a/plugins/Mapstraction/usermap.js +++ b/plugins/Mapstraction/usermap.js @@ -1,40 +1,32 @@ -$(document).ready(function() { +function scrapeNotices(user) +{ var notices = []; $(".notice").each(function(){ - var notice = getNoticeFromElement($(this)); - if(notice['geo']) - notices.push(notice); + var notice = getNoticeFromElement($(this)); + if (user) { + notice['user'] = user; + } else { + notice['user'] = getUserFromElement($(this)); + } + if(notice['geo']) + notices.push(notice); }); - if($("#map_canvas").length && notices.length>0) - { - showMapstraction($("#map_canvas"), notices); - } - $('.geo').click(function(){ - var noticeElement = $(this).closest(".notice"); - notice = getNoticeFromElement(noticeElement); - - $.fn.jOverlay.options = { - color : '#000', - opacity : '0.6', - zIndex : 99, - center : false, - bgClickToClose : true, - autoHide : true, - css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'} - }; - var html="
"; - html+=""; - html+=$("
").append($(this).clone()).html(); - $().jOverlay({ "html": html }); - $('#jOverlayContent').show(); - $('#jOverlayContent button').click($.closeOverlay); - - showMapstraction($("#map_canvas_popup"), notice); - - return false; - }); -}); + return notices; +} + +function scrapeUser() +{ + var avatarURL = $(".entity_profile .entity_depiction img.avatar").attr('src'); + var profileURL = $(".entity_profile .entity_nickname .url").attr('href'); + var nickname = $(".entity_profile .entity_nickname .nickname").text(); + + return { + 'profile_image_url': avatarURL, + 'profile_url': profileURL, + 'screen_name': nickname + }; +} function getMicroformatValue(element) { @@ -48,23 +40,34 @@ function getMicroformatValue(element) function getNoticeFromElement(noticeElement) { var notice = {}; - if(noticeElement.find(".geo").length){ + + if(noticeElement.find(".geo").length) { var latlon = noticeElement.find(".geo").attr('title').split(";"); notice['geo']={'coordinates': [ parseFloat(latlon[0]), parseFloat(latlon[1])] }; } - notice['user']={ - 'profile_image_url': noticeElement.find("img.avatar").attr('src'), - 'profile_url': noticeElement.find(".author a.url").attr('href'), - 'screen_name': noticeElement.find(".author .nickname").text() - }; - notice['html']=noticeElement.find(".entry-content").html(); - notice['url']=noticeElement.find("a.timestamp").attr('href'); - notice['created_at']=noticeElement.find("abbr.published").text(); + + notice['html'] = noticeElement.find(".entry-content").html(); + notice['url'] = noticeElement.find("a.timestamp").attr('href'); + notice['created_at'] = noticeElement.find("abbr.published").text(); + return notice; } +function getUserFromElement(noticeElement) +{ + var avatarURL = noticeElement.find("img.avatar").attr('src'); + var profileURL = noticeElement.find(".author a.url").attr('href'); + var nickname = noticeElement.find(".author .nickname").text(); + + return { + 'profile_image_url': avatarURL, + 'profile_url': profileURL, + 'screen_name': nickname + }; +} + function showMapstraction(element, notices) { if(element instanceof jQuery) element = element[0]; if(! $.isArray(notices)) notices = [notices]; -- cgit v1.2.3-54-g00ecf From e7e9dfceb4fd6db81bf266d71a3ec0b1933fbd02 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 4 Dec 2009 14:36:24 -0500 Subject: remove unnecessary debugging code from mapstraction output --- plugins/Mapstraction/MapstractionPlugin.php | 1 - 1 file changed, 1 deletion(-) (limited to 'plugins/Mapstraction') diff --git a/plugins/Mapstraction/MapstractionPlugin.php b/plugins/Mapstraction/MapstractionPlugin.php index ba7827151..f20e81b0e 100644 --- a/plugins/Mapstraction/MapstractionPlugin.php +++ b/plugins/Mapstraction/MapstractionPlugin.php @@ -158,7 +158,6 @@ class MapstractionPlugin extends Plugin ' var user = null; '. (($actionName == 'showstream') ? ' user = scrapeUser(); ' : '') . ' var notices = scrapeNotices(user); ' . - ' console.log(notices); ' . ' showMapstraction($("#map_canvas"), notices); '. '});'); } -- cgit v1.2.3-54-g00ecf