Refactored tag suggestions

This commit is contained in:
Marcin Kurczewski 2014-11-30 11:45:49 +01:00
parent c350c47195
commit 303f91e15c

View file

@ -279,15 +279,8 @@ App.Controls.TagInput = function($underlyingInput) {
function showOrHideSuggestions(tagName) { function showOrHideSuggestions(tagName) {
var tag = getExportedTag(tagName); var tag = getExportedTag(tagName);
if (tag && _.size(tag.suggestions) > 0) { var suggestions = tag ? tag.suggestions : [];
var suggestions = filterSuggestions(tag.suggestions); updateSuggestions($suggestions, suggestions);
if (suggestions.length > 0) {
attachTagsToSuggestionList($suggestions.find('ul'), suggestions);
$suggestions.slideDown('fast');
return;
}
}
$suggestions.slideUp('fast');
} }
function showOrHideSiblings(tagName) { function showOrHideSiblings(tagName) {
@ -300,48 +293,53 @@ App.Controls.TagInput = function($underlyingInput) {
promise.wait(getSiblings(tagName), promise.make(function(resolve, reject) { promise.wait(getSiblings(tagName), promise.make(function(resolve, reject) {
$siblings.slideUp('fast', resolve); $siblings.slideUp('fast', resolve);
})).then(function(siblings) { })).then(function(siblings) {
siblings = _.pluck(siblings, 'name');
$siblings.data('lastTag', tagName); $siblings.data('lastTag', tagName);
updateSuggestions($siblings, siblings);
if (!_.size(siblings)) {
return;
}
var suggestions = filterSuggestions(_.pluck(siblings, 'name'));
if (suggestions.length > 0) {
attachTagsToSuggestionList($siblings.find('ul'), suggestions);
$siblings.slideDown('fast');
}
}).fail(function() { }).fail(function() {
}); });
} }
function filterSuggestions(sourceTagNames) { function updateSuggestions($target, siblings) {
var tagNames = _.filter(sourceTagNames.slice(), function(tagName) { function filterSuggestions(sourceTagNames) {
return !isTaggedWith(tagName); if (!sourceTagNames) {
}); return [];
tagNames = tagNames.slice(0, 20); }
return tagNames; var tagNames = _.filter(sourceTagNames.slice(), function(tagName) {
} return !isTaggedWith(tagName);
function attachTagsToSuggestionList($list, tagNames) {
$list.empty();
_.each(tagNames, function(tagName) {
var $li = jQuery('<li>');
var $a = jQuery('<a href="#/posts/query=' + tagName + '">');
$a.text(tagName);
$a.click(function(e) {
e.preventDefault();
addTag(tagName);
$li.fadeOut('fast', function() {
$li.remove();
if ($list.children().length === 0) {
$list.parent('div').slideUp('fast');
}
});
}); });
$li.append($a); tagNames = tagNames.slice(0, 20);
$list.append($li); return tagNames;
}); }
function attachTagsToSuggestionList($list, tagNames) {
$list.empty();
_.each(tagNames, function(tagName) {
var $li = jQuery('<li>');
var $a = jQuery('<a href="#/posts/query=' + tagName + '">');
$a.text(tagName);
$a.click(function(e) {
e.preventDefault();
addTag(tagName);
$li.fadeOut('fast', function() {
$li.remove();
if ($list.children().length === 0) {
$list.parent('div').slideUp('fast');
}
});
});
$li.append($a);
$list.append($li);
});
}
var suggestions = filterSuggestions(siblings);
if (suggestions.length > 0) {
attachTagsToSuggestionList($target.find('ul'), suggestions);
$target.slideDown('fast');
} else {
$target.slideUp('fast');
}
} }
function getSiblings(tagName) { function getSiblings(tagName) {