From 72d16756b169e1f751470670eececf76e88b2bd7 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 27 Nov 2008 05:37:20 +0000 Subject: Refactor cloud generation into a helper so that we can call it from Tags_Controller::_index(). This enables our Ajax code to reload the tags block after we submit a new tag, so update the JS to do that properly. --- modules/tag/controllers/tags.php | 3 ++- modules/tag/helpers/tag.php | 17 +++++++++++++++++ modules/tag/helpers/tag_block.php | 16 +++------------- modules/tag/js/tag.js | 26 ++++++++++++-------------- modules/tag/views/tag_block.html.php | 19 ++++--------------- modules/tag/views/tag_block_cloud.html.php | 9 +++++++++ 6 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 modules/tag/views/tag_block_cloud.html.php (limited to 'modules') diff --git a/modules/tag/controllers/tags.php b/modules/tag/controllers/tags.php index 9a5422cf..bf396a5b 100644 --- a/modules/tag/controllers/tags.php +++ b/modules/tag/controllers/tags.php @@ -43,7 +43,8 @@ class Tags_Controller extends REST_Controller { } public function _index() { - throw new Exception("@todo Tag_Controller::_index NOT IMPLEMENTED"); + // @todo: represent this in different formats + print tag::cloud(30); } public function _form_add($item_id) { diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index a1939df7..23edc5f2 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -61,4 +61,21 @@ class tag_Core { ->limit($count) ->find_all(); } + + /** + * Return a rendering of the cloud for the N most popular tags. + * + * @param integer $count the number of tags + * @return View + */ + public static function cloud($count) { + $tags = tag::popular_tags($count)->as_array(); + if ($tags) { + $cloud = new View("tag_block_cloud.html"); + $cloud->max_count = $tags[0]->count; + usort($tags, array("tag_block", "sort_by_name")); + $cloud->tags = $tags; + return $cloud; + } + } } diff --git a/modules/tag/helpers/tag_block.php b/modules/tag/helpers/tag_block.php index c735ee04..02a536ad 100644 --- a/modules/tag/helpers/tag_block.php +++ b/modules/tag/helpers/tag_block.php @@ -28,23 +28,13 @@ class tag_block_Core { $block->id = "gTag"; $block->title = _("Tags"); $block->content = new View("tag_block.html"); - - $tags = tag::popular_tags(30)->as_array(); - if ($tags) { - $block->content->max_count = $tags[0]->count; - - usort($tags, array("tag_block", "sort_by_name")); - $block->content->tags = $tags; - if ($block->content->page_type != "tag") { - $block->content->item = $theme->item(); - } - } else { - $block->content->tags = array(); - } + $block->content->cloud = tag::cloud(30); if ($theme->page_type() != "tag") { $controller = new Tags_Controller(); $block->content->form = $controller->form_add($theme->item()); + } else { + $block->content->form = ""; } return $block; diff --git a/modules/tag/js/tag.js b/modules/tag/js/tag.js index 30e9ca50..fbcbd419 100644 --- a/modules/tag/js/tag.js +++ b/modules/tag/js/tag.js @@ -1,19 +1,17 @@ $("document").ready(function() { - var options = { - target: "#gTagFormContainer", - success: function(responseText, statusText) { - $("#gAddTag").ajaxForm(options); - } - }; - $("#gAddTag").ajaxForm(options); + ajaxify_tag_form(); }); -function get_tag_block(url) { - $.post(url, function(data) { - $('#gTagFormContainer').html(data); - $("#gAddTag").submit(function(event){ - get_tag_block($("#gAddTag").attr("action")); - return false; - }); +function ajaxify_tag_form() { + $("form#gAddTag").ajaxForm({ + complete: function(xhr, statusText) { + $("form#gAddTag").replaceWith(xhr.responseText); + if (xhr.status == 201) { + $.get($("#gTagCloud").attr("src"), function(data, textStatus) { + $("#gTagCloud").html(data); + }); + } + ajaxify_tag_form(); + } }); } diff --git a/modules/tag/views/tag_block.html.php b/modules/tag/views/tag_block.html.php index 6ce67e9d..cc75e6f0 100644 --- a/modules/tag/views/tag_block.html.php +++ b/modules/tag/views/tag_block.html.php @@ -1,18 +1,7 @@ - - - - - -
- +
"> +
- + + diff --git a/modules/tag/views/tag_block_cloud.html.php b/modules/tag/views/tag_block_cloud.html.php new file mode 100644 index 00000000..6abaf953 --- /dev/null +++ b/modules/tag/views/tag_block_cloud.html.php @@ -0,0 +1,9 @@ + + -- cgit v1.2.3