Fixed and simplified tag autocompletion

This commit is contained in:
Marcin Kurczewski 2014-02-18 21:15:00 +01:00
parent 4469767d8f
commit b86aaf90a3
8 changed files with 50 additions and 57 deletions

View file

@ -193,6 +193,25 @@ function extractLast(term)
return split(term).pop(); return split(term).pop();
} }
function retrieveTags(searchTerm, cb)
{
var options = { filter: searchTerm + ' order:popularity,desc' };
$.getJSON('/tags?json', options, function(data)
{
var tags = $.map(data.tags.slice(0, 15), function(tag)
{
var ret =
{
label: tag.name + ' (' + tag.count + ')',
value: tag.name,
};
return ret;
});
cb(tags);
});
}
$(function() $(function()
{ {
$('.autocomplete').each(function() $('.autocomplete').each(function()
@ -204,10 +223,7 @@ $(function()
{ {
var term = extractLast(request.term); var term = extractLast(request.term);
if (term != '') if (term != '')
$.get(searchInput.attr('data-autocomplete-url') + '?json', {filter: term + ' order:popularity,desc'}, function(data) retrieveTags(term, response);
{
response($.map(data.tags, function(tag) { return { label: tag.name + ' (' + tag.count + ')', value: tag.name }; }));
});
}, },
focus: function(e) focus: function(e)
{ {
@ -245,34 +261,34 @@ $(function()
}); });
}); });
function getTagItOptions() function attachTagIt(element)
{ {
return { var tagItOptions =
{
caseSensitive: false, caseSensitive: false,
autocomplete: autocomplete:
{ {
source: source:
function(request, response) function(request, response)
{ {
var term = request.term.toLowerCase(); var tagit = this;
var tags = $.map(this.options.availableTags, function(a) retrieveTags(request.term.toLowerCase(), function(tags)
{ {
return a.name; if (!tagit.options.allowDuplicates)
{
tags = $.grep(tags, function(tag)
{
return tagit.assignedTags().indexOf(tag.value) == -1;
});
}
response(tags);
}); });
var results = $.grep(tags, function(a)
{
if (term.length < 3)
return a.toLowerCase().indexOf(term) == 0;
else
return a.toLowerCase().indexOf(term) != -1;
});
results = results.slice(0, 15);
if (!this.options.allowDuplicates)
results = this._subtractArray(results, this.assignedTags());
response(results);
}, },
} }
}; };
tagItOptions.placeholderText = element.attr('placeholder');
element.tagit(tagItOptions);
} }

View file

@ -10,12 +10,6 @@ $(function()
$('.tab-content.' + className).show(); $('.tab-content.' + className).show();
}); });
var tags = [];
$.getJSON('/tags?json', {filter: 'order:popularity,desc'}, function(data)
{
tags = data['tags'];
});
$('#file-handler').on('dragenter', function(e) $('#file-handler').on('dragenter', function(e)
{ {
$(this).addClass('active'); $(this).addClass('active');
@ -237,10 +231,7 @@ $(function()
$('.posts').append(postDom); $('.posts').append(postDom);
postDom.show(); postDom.show();
var tagItOptions = getTagItOptions(); attachTagIt($('.tags input', postDom));
tagItOptions.availableTags = tags;
tagItOptions.placeholderText = $('.tags input').attr('placeholder');
$('.tags input', postDom).tagit(tagItOptions);
callback(postDom, input); callback(postDom, input);
} }

View file

@ -14,22 +14,14 @@ $(function()
var formDom = $('form.edit-post'); var formDom = $('form.edit-post');
if (formDom.find('.tagit').length == 0) if (formDom.find('.tagit').length == 0)
{ {
$.getJSON('/tags?json', {filter: 'order:popularity,desc'}, function(data) attachTagIt($('.tags input'));
aDom.removeClass('inactive');
formDom.find('input[type=text]:visible:eq(0)').focus();
formDom.find('textarea, input').bind('change keyup', function()
{ {
aDom.removeClass('inactive'); if (formDom.serialize() != formDom.data('original-data'))
var tags = data['tags']; enableExitConfirmation();
var tagItOptions = getTagItOptions();
tagItOptions.availableTags = tags;
tagItOptions.placeholderText = $('.tags input').attr('placeholder');
$('.tags input').tagit(tagItOptions);
formDom.find('input[type=text]:visible:eq(0)').focus();
formDom.find('textarea, input').bind('change keyup', function()
{
if (formDom.serialize() != formDom.data('original-data'))
enableExitConfirmation();
});
}); });
} }
else else

View file

@ -4,7 +4,6 @@ class TagSearchService extends AbstractSearchService
public static function decorate(SqlQuery $sqlQuery, $searchQuery) public static function decorate(SqlQuery $sqlQuery, $searchQuery)
{ {
$allowedSafety = PrivilegesHelper::getAllowedSafety(); $allowedSafety = PrivilegesHelper::getAllowedSafety();
$limitQuery = false;
$sqlQuery $sqlQuery
->raw(', COUNT(post_tag.post_id)') ->raw(', COUNT(post_tag.post_id)')
->as('post_count') ->as('post_count')
@ -38,7 +37,6 @@ class TagSearchService extends AbstractSearchService
} }
else else
{ {
$limitQuery = true;
if (strlen($token) >= 3) if (strlen($token) >= 3)
$token = '%' . $token; $token = '%' . $token;
$token .= '%'; $token .= '%';
@ -53,10 +51,6 @@ class TagSearchService extends AbstractSearchService
$sqlQuery->groupBy('tag.id'); $sqlQuery->groupBy('tag.id');
if ($orderToken) if ($orderToken)
self::order($sqlQuery,$orderToken); self::order($sqlQuery,$orderToken);
if ($limitQuery)
$sqlQuery->limit(15);
} }
private static function order(SqlQuery $sqlQuery, $value) private static function order(SqlQuery $sqlQuery, $value)

View file

@ -4,12 +4,12 @@
<div class="form-row"> <div class="form-row">
<label for="mass-tag-query">Search query:</label> <label for="mass-tag-query">Search query:</label>
<div class="input-wrapper"><input class="autocomplete" type="text" name="query" id="mass-tag-query" value="<?php echo isset($this->context->massTagQuery) ? $this->context->massTagQuery : '' ?>" data-autocomplete-url="<?php echo \Chibi\UrlHelper::route('tag', 'list') ?>"/></div> <div class="input-wrapper"><input class="autocomplete" type="text" name="query" id="mass-tag-query" value="<?php echo isset($this->context->massTagQuery) ? $this->context->massTagQuery : '' ?>"/></div>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="mass-tag-tag">Tag:</label> <label for="mass-tag-tag">Tag:</label>
<div class="input-wrapper"><input class="autocomplete" type="text" name="tag" id="mass-tag-tag" value="<?php echo isset($this->context->massTagTag) ? $this->context->massTagTag : '' ?>" data-autocomplete-url="<?php echo \Chibi\UrlHelper::route('tag', 'list') ?>"/></div> <div class="input-wrapper"><input class="autocomplete" type="text" name="tag" id="mass-tag-tag" value="<?php echo isset($this->context->massTagTag) ? $this->context->massTagTag : '' ?>"/></div>
</div> </div>
<input type="hidden" name="submit" value="1"/> <input type="hidden" name="submit" value="1"/>

View file

@ -4,12 +4,12 @@
<div class="form-row"> <div class="form-row">
<label for="merge-source-tag">Source tag:</label> <label for="merge-source-tag">Source tag:</label>
<div class="input-wrapper"><input class="autocomplete" type="text" name="source-tag" id="merge-source-tag" data-autocomplete-url="<?php echo \Chibi\UrlHelper::route('tag', 'list') ?>"/></div> <div class="input-wrapper"><input class="autocomplete" type="text" name="source-tag" id="merge-source-tag"/></div>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="merge-target-tag">Target tag:</label> <label for="merge-target-tag">Target tag:</label>
<div class="input-wrapper"><input class="autocomplete" type="text" name="target-tag" id="merge-target-tag" data-autocomplete-url="<?php echo \Chibi\UrlHelper::route('tag', 'list') ?>"/></div> <div class="input-wrapper"><input class="autocomplete" type="text" name="target-tag" id="merge-target-tag"/></div>
</div> </div>
<input type="hidden" name="submit" value="1"/> <input type="hidden" name="submit" value="1"/>

View file

@ -4,7 +4,7 @@
<div class="form-row"> <div class="form-row">
<label for="rename-source-tag">Source tag:</label> <label for="rename-source-tag">Source tag:</label>
<div class="input-wrapper"><input class="autocomplete" type="text" name="source-tag" id="rename-source-tag" data-autocomplete-url="<?php echo \Chibi\UrlHelper::route('tag', 'list') ?>"/></div> <div class="input-wrapper"><input class="autocomplete" type="text" name="source-tag" id="rename-source-tag"/></div>
</div> </div>
<div class="form-row"> <div class="form-row">

View file

@ -127,7 +127,7 @@
<li class="search"> <li class="search">
<form name="search" action="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>" method="get"> <form name="search" action="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>" method="get">
<input class="autocomplete" type="search" name="query" placeholder="Search&hellip;" value="<?php echo isset($this->context->transport->searchQuery) ? htmlspecialchars($this->context->transport->searchQuery) : '' ?>" data-autocomplete-url="<?php echo \Chibi\UrlHelper::route('tag', 'list') ?>"/> <input class="autocomplete" type="search" name="query" placeholder="Search&hellip;" value="<?php echo isset($this->context->transport->searchQuery) ? htmlspecialchars($this->context->transport->searchQuery) : '' ?>"/>
</form> </form>
</li> </li>
</ul> </ul>