Fixed tag input behavior for initial tags

This commit is contained in:
Marcin Kurczewski 2014-10-25 10:52:24 +02:00
parent 6f6d3a34a7
commit 7220dc3ffa

View file

@ -59,7 +59,11 @@ App.Controls.TagInput = function($underlyingInput) {
$suggestions.insertAfter($wrapper); $suggestions.insertAfter($wrapper);
$siblings.insertAfter($wrapper); $siblings.insertAfter($wrapper);
addTagsFromText($underlyingInput.val()); addTagsFromText(
$underlyingInput.val(), {
disableImplications: true,
disableSuggestions: true});
$underlyingInput.val(''); $underlyingInput.val('');
} }
@ -120,19 +124,19 @@ App.Controls.TagInput = function($underlyingInput) {
} }
}); });
function addTagsFromText(text) { function addTagsFromText(text, options) {
var tagNamesToAdd = text.split(/\s+/); var tagNamesToAdd = text.split(/\s+/);
_.map(tagNamesToAdd, addTag); _.map(tagNamesToAdd, function(tagName) { addTag(tagName, options); });
} }
function addTagsFromTextWithoutLast(text) { function addTagsFromTextWithoutLast(text) {
var tagNamesToAdd = text.split(/\s+/); var tagNamesToAdd = text.split(/\s+/);
var lastTagName = tagNamesToAdd.pop(); var lastTagName = tagNamesToAdd.pop();
_.map(tagNamesToAdd, addTag); _.map(tagNamesToAdd, function(tagName) { addTag(tagName, options); });
$input.val(lastTagName); $input.val(lastTagName);
} }
function addTag(tagName) { function addTag(tagName, options) {
tagName = tagName.trim(); tagName = tagName.trim();
if (tagName.length === 0) { if (tagName.length === 0) {
return; return;
@ -150,11 +154,11 @@ App.Controls.TagInput = function($underlyingInput) {
if (isTaggedWith(tagName)) { if (isTaggedWith(tagName)) {
flashTagRed(tagName); flashTagRed(tagName);
} else { } else {
beforeTagAdded(tagName); beforeTagAdded(tagName, options);
tags.push(tagName); tags.push(tagName);
var $elem = createListElement(tagName); var $elem = createListElement(tagName);
$tagList.append($elem); $tagList.append($elem);
afterTagAdded(tagName); afterTagAdded(tagName, options);
} }
} }
@ -164,16 +168,21 @@ App.Controls.TagInput = function($underlyingInput) {
} }
} }
function afterTagAdded(tagName) { function afterTagAdded(tagName, options) {
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(
@ -206,13 +215,16 @@ App.Controls.TagInput = function($underlyingInput) {
} }
function flashTagRed(tagName) { function flashTagRed(tagName) {
var $elem = getListElement(tagName); flashTag(tagName, 'rgba(255, 200, 200, 1)');
$elem.css({backgroundColor: 'rgba(255, 200, 200, 1)'});
} }
function flashTagYellow(tagName) { function flashTagYellow(tagName) {
flashTag(tagName, 'rgba(255, 255, 200, 1)');
}
function flashTag(tagName, color) {
var $elem = getListElement(tagName); var $elem = getListElement(tagName);
$elem.css({backgroundColor: 'rgba(255, 255, 200, 1)'}); $elem.css({backgroundColor: color});
} }
function getListElement(tagName) { function getListElement(tagName) {