Fixed tag suggestions for implied tags

This commit is contained in:
Marcin Kurczewski 2014-11-30 12:28:21 +01:00
parent a3aea27a13
commit 924592675c

View file

@ -14,6 +14,14 @@ App.Controls.TagInput = function($underlyingInput) {
var tagConfirmKeys = [KEY_RETURN, KEY_SPACE]; var tagConfirmKeys = [KEY_RETURN, KEY_SPACE];
var inputConfirmKeys = [KEY_RETURN]; var inputConfirmKeys = [KEY_RETURN];
var SOURCE_INITIAL_TEXT = 1;
var SOURCE_AUTOCOMPLETION = 2;
var SOURCE_PASTE = 3;
var SOURCE_IMPLICATIONS = 4;
var SOURCE_INPUT_BLUR = 5;
var SOURCE_INPUT_ENTER = 6;
var SOURCE_SUGGESTIONS = 7;
var tags = []; var tags = [];
var options = { var options = {
beforeTagAdded: null, beforeTagAdded: null,
@ -59,7 +67,7 @@ App.Controls.TagInput = function($underlyingInput) {
$siblings.insertAfter($wrapper); $siblings.insertAfter($wrapper);
$suggestions.insertAfter($wrapper); $suggestions.insertAfter($wrapper);
processText($underlyingInput.val(), addTagDirectly); processText($underlyingInput.val(), SOURCE_INITIAL_TEXT);
$underlyingInput.val(''); $underlyingInput.val('');
} }
@ -67,7 +75,7 @@ App.Controls.TagInput = function($underlyingInput) {
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) {
processText(text, addTag); processText(text, SOURCE_AUTOCOMPLETION);
$input.val(''); $input.val('');
}; };
autoComplete.additionalFilter = function(results) { autoComplete.additionalFilter = function(results) {
@ -83,7 +91,7 @@ App.Controls.TagInput = function($underlyingInput) {
$input.bind('blur', function(e) { $input.bind('blur', function(e) {
$wrapper.removeClass('focused'); $wrapper.removeClass('focused');
var tagName = $input.val(); var tagName = $input.val();
addTag(tagName); addTag(tagName, SOURCE_INPUT_BLUR);
$input.val(''); $input.val('');
}); });
@ -101,7 +109,7 @@ App.Controls.TagInput = function($underlyingInput) {
return; return;
} }
processTextWithoutLast(pastedText, addTag); processTextWithoutLast(pastedText, SOURCE_PASTE);
}); });
$input.bind('keydown', function(e) { $input.bind('keydown', function(e) {
@ -114,7 +122,7 @@ App.Controls.TagInput = function($underlyingInput) {
var tagName = $input.val(); var tagName = $input.val();
e.preventDefault(); e.preventDefault();
$input.val(''); $input.val('');
addTag(tagName); addTag(tagName, SOURCE_INPUT_ENTER);
} else if (e.which === KEY_BACKSPACE && jQuery(this).val().length === 0) { } else if (e.which === KEY_BACKSPACE && jQuery(this).val().length === 0) {
e.preventDefault(); e.preventDefault();
removeLastTag(); removeLastTag();
@ -127,19 +135,19 @@ App.Controls.TagInput = function($underlyingInput) {
}); });
} }
function processText(text, callback) { function processText(text, source) {
var tagNamesToAdd = explodeText(text); var tagNamesToAdd = explodeText(text);
_.map(tagNamesToAdd, function(tagName) { callback(tagName); }); _.map(tagNamesToAdd, function(tagName) { addTag(tagName, source); });
} }
function processTextWithoutLast(text, callback) { function processTextWithoutLast(text, source) {
var tagNamesToAdd = explodeText(text); var tagNamesToAdd = explodeText(text);
var lastTagName = tagNamesToAdd.pop(); var lastTagName = tagNamesToAdd.pop();
_.map(tagNamesToAdd, function(tagName) { callback(tagName); }); _.map(tagNamesToAdd, function(tagName) { addTag(tagName, source); });
$input.val(lastTagName); $input.val(lastTagName);
} }
function addTag(tagName) { function addTag(tagName, source) {
tagName = tagName.trim(); tagName = tagName.trim();
if (tagName.length === 0) { if (tagName.length === 0) {
return; return;
@ -157,23 +165,19 @@ App.Controls.TagInput = function($underlyingInput) {
if (isTaggedWith(tagName)) { if (isTaggedWith(tagName)) {
flashTagRed(tagName); flashTagRed(tagName);
} else { } else {
beforeTagAdded(tagName); beforeTagAdded(tagName, source);
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, source);
}
}
function beforeTagRemoved(tagName) { function beforeTagRemoved(tagName) {
if (typeof(options.beforeTagRemoved) === 'function') { if (typeof(options.beforeTagRemoved) === 'function') {
options.beforeTagRemoved(tagName); options.beforeTagRemoved(tagName);
@ -184,21 +188,26 @@ App.Controls.TagInput = function($underlyingInput) {
refreshShownSiblings(); refreshShownSiblings();
} }
function beforeTagAdded(tagName) { function beforeTagAdded(tagName, source) {
if (typeof(options.beforeTagAdded) === 'function') { if (typeof(options.beforeTagAdded) === 'function') {
options.beforeTagAdded(tagName); options.beforeTagAdded(tagName);
} }
} }
function afterTagAdded(tagName) { function afterTagAdded(tagName, source) {
if (source === SOURCE_IMPLICATIONS) {
flashTagYellow(tagName);
}
var tag = getExportedTag(tagName); var tag = getExportedTag(tagName);
if (tag) { if (tag) {
_.each(tag.implications, function(impliedTagName) { _.each(tag.implications, function(impliedTagName) {
addTag(impliedTagName); addTag(impliedTagName, SOURCE_IMPLICATIONS);
flashTagYellow(impliedTagName);
}); });
if (source !== SOURCE_IMPLICATIONS && source !== SOURCE_SUGGESTIONS) {
showOrHideSuggestions(tagName); showOrHideSuggestions(tagName);
refreshShownSiblings(); refreshShownSiblings();
}
} else { } else {
flashTagGreen(tagName); flashTagGreen(tagName);
} }
@ -335,7 +344,7 @@ App.Controls.TagInput = function($underlyingInput) {
$a.text(tagName); $a.text(tagName);
$a.click(function(e) { $a.click(function(e) {
e.preventDefault(); e.preventDefault();
addTag(tagName); addTag(tagName, SOURCE_SUGGESTIONS);
$li.fadeOut('fast', function() { $li.fadeOut('fast', function() {
$li.remove(); $li.remove();
if ($list.children().length === 0) { if ($list.children().length === 0) {