Refactor to tag autocompletion

This commit is contained in:
Marcin Kurczewski 2014-03-10 00:13:10 +01:00
parent e48826dd72
commit f4d0230166
4 changed files with 37 additions and 17 deletions

@ -1 +1 @@
Subproject commit a5d7a03965e7089c070defa5907798a1df66c847 Subproject commit 9b27422f780840f9814d8fc49017716cf6e7fea6

View file

@ -199,15 +199,10 @@ function split(val)
return val.split(/\s+/); return val.split(/\s+/);
} }
function extractLast(term)
{
return split(term).pop();
}
function retrieveTags(searchTerm, cb) function retrieveTags(searchTerm, cb)
{ {
var options = { filter: searchTerm + ' order:popularity,desc' }; var options = { search: searchTerm };
$.getJSON('/tags?json', options, function(data) $.getJSON('/tags-autocomplete?json', options, function(data)
{ {
var tags = $.map(data.tags.slice(0, 15), function(tag) var tags = $.map(data.tags.slice(0, 15), function(tag)
{ {
@ -232,7 +227,8 @@ $(function()
minLength: 1, minLength: 1,
source: function(request, response) source: function(request, response)
{ {
var term = extractLast(request.term); var terms = split(request.term);
var term = terms.pop();
if (term != '') if (term != '')
retrieveTags(term, response); retrieveTags(term, response);
}, },
@ -283,6 +279,7 @@ function attachTagIt(element)
function(request, response) function(request, response)
{ {
var tagit = this; var tagit = this;
//var context = tagit.element.tagit('assignedTags');
retrieveTags(request.term.toLowerCase(), function(tags) retrieveTags(request.term.toLowerCase(), function(tags)
{ {
if (!tagit.options.allowDuplicates) if (!tagit.options.allowDuplicates)

View file

@ -14,7 +14,7 @@ class TagController
$this->context->viewName = 'tag-list-wrapper'; $this->context->viewName = 'tag-list-wrapper';
PrivilegesHelper::confirmWithException(Privilege::ListTags); PrivilegesHelper::confirmWithException(Privilege::ListTags);
$suppliedFilter = $filter ?: InputHelper::get('filter') ?: 'order:alpha,asc'; $suppliedFilter = $filter ?: 'order:alpha,asc';
$page = max(1, intval($page)); $page = max(1, intval($page));
$tagsPerPage = intval($this->config->browsing->tagsPerPage); $tagsPerPage = intval($this->config->browsing->tagsPerPage);
@ -43,6 +43,29 @@ class TagController
} }
} }
/**
* @route /tags-autocomplete
*/
public function autoCompleteAction()
{
PrivilegesHelper::confirmWithException(Privilege::ListTags);
$suppliedSearch = InputHelper::get('search');
$filter = $suppliedSearch . ' order:popularity,desc';
$tags = TagSearchService::getEntitiesRows($filter, 15, 1);
$this->context->transport->tags =
array_values(array_map(
function($tag)
{
return [
'name' => $tag['name'],
'count' => $tag['post_count']
];
}, $tags));
}
/** /**
* @route /tag/merge * @route /tag/merge
*/ */

View file

@ -11,13 +11,13 @@ class TagSearchService extends AbstractSearchService
public static function getMostUsedTag() public static function getMostUsedTag()
{ {
$stmt = new Sql\SelectStatement(); $stmt = (new Sql\SelectStatement)
$stmt->setTable('post_tag'); ->setTable('post_tag')
$stmt->addColumn('tag_id'); ->addColumn('tag_id')
$stmt->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')); ->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count'))
$stmt->setGroupBy('post_tag.tag_id'); ->setGroupBy('post_tag.tag_id')
$stmt->setOrderBy('post_count', Sql\SelectStatement::ORDER_DESC); ->setOrderBy('post_count', Sql\SelectStatement::ORDER_DESC)
$stmt->setLimit(1, 0); ->setLimit(1, 0);
return Database::fetchOne($stmt); return Database::fetchOne($stmt);
} }
} }