Simplified tag input

This commit is contained in:
Marcin Kurczewski 2014-11-09 20:07:04 +01:00
parent a56bd55b50
commit 84e145609a

View file

@ -28,7 +28,7 @@ App.Controls.TagInput = function($underlyingInput) {
var $suggestions = jQuery('<div class="related-tags"><span>Suggested tags:</span><ul>'); var $suggestions = jQuery('<div class="related-tags"><span>Suggested tags:</span><ul>');
init(); init();
render(); render();
initAutocomplete(); initAutoComplete();
function init() { function init() {
if ($underlyingInput.length === 0) { if ($underlyingInput.length === 0) {
@ -59,21 +59,18 @@ App.Controls.TagInput = function($underlyingInput) {
$suggestions.insertAfter($wrapper); $suggestions.insertAfter($wrapper);
$siblings.insertAfter($wrapper); $siblings.insertAfter($wrapper);
addTagsFromText( processText($underlyingInput.val(), addTagDirectly);
$underlyingInput.val(), {
disableImplications: true,
disableSuggestions: true});
$underlyingInput.val(''); $underlyingInput.val('');
} }
function initAutocomplete() { function initAutoComplete() {
var autocomplete = new App.Controls.AutoCompleteInput($input); var autoComplete = new App.Controls.AutoCompleteInput($input);
autocomplete.onApply = function(text) { autoComplete.onApply = function(text) {
addTagsFromText(text); processText(text, addTagDirectly);
$input.val(''); $input.val('');
}; };
autocomplete.additionalFilter = function(results) { autoComplete.additionalFilter = function(results) {
return _.filter(results, function(resultItem) { return _.filter(results, function(resultItem) {
return !_.contains(getTags(), resultItem[0]); return !_.contains(getTags(), resultItem[0]);
}); });
@ -104,7 +101,7 @@ App.Controls.TagInput = function($underlyingInput) {
return; return;
} }
addTagsFromTextWithoutLast(pastedText); processTextWithoutLast(pastedText, addTag);
}); });
$input.bind('keydown', function(e) { $input.bind('keydown', function(e) {
@ -124,19 +121,19 @@ App.Controls.TagInput = function($underlyingInput) {
} }
}); });
function addTagsFromText(text, options) { function processText(text, callback) {
var tagNamesToAdd = text.split(/\s+/); var tagNamesToAdd = text.split(/\s+/);
_.map(tagNamesToAdd, function(tagName) { addTag(tagName, options); }); _.map(tagNamesToAdd, function(tagName) { callback(tagName); });
} }
function addTagsFromTextWithoutLast(text) { function processTextWithoutLast(text, callback) {
var tagNamesToAdd = text.split(/\s+/); var tagNamesToAdd = text.split(/\s+/);
var lastTagName = tagNamesToAdd.pop(); var lastTagName = tagNamesToAdd.pop();
_.map(tagNamesToAdd, function(tagName) { addTag(tagName, options); }); _.map(tagNamesToAdd, function(tagName) { callback(tagName); });
$input.val(lastTagName); $input.val(lastTagName);
} }
function addTag(tagName, options) { function addTag(tagName) {
tagName = tagName.trim(); tagName = tagName.trim();
if (tagName.length === 0) { if (tagName.length === 0) {
return; return;
@ -154,40 +151,39 @@ App.Controls.TagInput = function($underlyingInput) {
if (isTaggedWith(tagName)) { if (isTaggedWith(tagName)) {
flashTagRed(tagName); flashTagRed(tagName);
} else { } else {
beforeTagAdded(tagName, options); beforeTagAdded(tagName);
var exportedTag = getExportedTag(tagName); var exportedTag = getExportedTag(tagName);
if (!exportedTag || !exportedTag.banned) { if (!exportedTag || !exportedTag.banned) {
addTagDirectly(tagName);
}
afterTagAdded(tagName);
}
}
function addTagDirectly(tagName) {
tags.push(tagName); tags.push(tagName);
var $elem = createListElement(tagName); var $elem = createListElement(tagName);
$tagList.append($elem); $tagList.append($elem);
} }
afterTagAdded(tagName, options);
}
}
function beforeTagAdded(tagName) { function beforeTagAdded(tagName) {
if (typeof(options.beforeTagAdded) === 'function') { if (typeof(options.beforeTagAdded) === 'function') {
options.beforeTagAdded(tagName); options.beforeTagAdded(tagName);
} }
} }
function afterTagAdded(tagName, options) { function afterTagAdded(tagName) {
var tag = getExportedTag(tagName); var tag = getExportedTag(tagName);
if (tag) { if (tag) {
if (!options || !options.disableImplications) {
_.each(tag.implications, function(impliedTagName) { _.each(tag.implications, function(impliedTagName) {
addTag(impliedTagName); addTag(impliedTagName);
flashTagYellow(impliedTagName); flashTagYellow(impliedTagName);
}); });
}
if (!options || !options.disableSuggestions) {
showOrHideSuggestions(tag.suggestions); showOrHideSuggestions(tag.suggestions);
} }
} }
}
function getExportedTag(tagName) { function getExportedTag(tagName) {
return _.first(_.filter( return _.first(_.filter(