Skip to content
This repository was archived by the owner on Nov 22, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/auto-complete-match.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ tagsInput.directive('tiAutocompleteMatch', function($sce, tiUtil) {
return $sce.trustAsHtml(text);
};
scope.$getDisplayText = function() {
return tiUtil.safeToString(scope.data[options.displayProperty || options.tagsInput.displayProperty]);
if (options.tagsInput.itemIsObject || !angular.isDefined(options.tagsInput.itemIsObject)) {
return tiUtil.safeToString(scope.data[options.displayProperty || options.tagsInput.displayProperty]);
} else {
return tiUtil.safeToString(scope.data);
}
};
}
};
});
});
41 changes: 31 additions & 10 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,32 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
var self = {}, getDifference, lastPromise, getTagId;

getTagId = function() {
return options.tagsInput.keyProperty || options.tagsInput.displayProperty;
if (options.tagsInput.itemIsObject || !angular.isDefined(options.tagsInput.itemIsObject)) {
return options.tagsInput.keyProperty || options.tagsInput.displayProperty;
} else {
return null;
}
};

getDifference = function(array1, array2) {
return array1.filter(function(item) {
return !tiUtil.findInObjectArray(array2, item, getTagId(), function(a, b) {
if (options.tagsInput.replaceSpacesWithDashes) {
a = tiUtil.replaceSpacesWithDashes(a);
b = tiUtil.replaceSpacesWithDashes(b);
}
return tiUtil.defaultComparer(a, b);
});
if (options.tagsInput.itemIsObject || !angular.isDefined(options.tagsInput.itemIsObject)) {
return !tiUtil.findInObjectArray(array2, item, getTagId(), function(a, b) {
if (options.tagsInput.replaceSpacesWithDashes) {
a = tiUtil.replaceSpacesWithDashes(a);
b = tiUtil.replaceSpacesWithDashes(b);
}
return tiUtil.defaultComparer(a, b);
});
} else {
return !tiUtil.findInStringArray(array2, item, function(a, b) {
if (options.tagsInput.replaceSpacesWithDashes) {
a = tiUtil.replaceSpacesWithDashes(a);
b = tiUtil.replaceSpacesWithDashes(b);
}
return tiUtil.defaultComparer(a, b);
});
}
});
};

Expand Down Expand Up @@ -81,7 +95,10 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
return;
}

items = tiUtil.makeObjectArray(items.data || items, getTagId());
if (options.tagsInput.itemIsObject || !angular.isDefined(options.tagsInput.itemIsObject)){
items = tiUtil.makeObjectArray(items.data || items, getTagId());
}

items = getDifference(items, tags);
self.items = items.slice(0, options.maxResultsToShow);

Expand Down Expand Up @@ -203,7 +220,11 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
};

scope.track = function(item) {
return item[options.tagsInput.keyProperty || options.tagsInput.displayProperty];
if (options.tagsInput.itemIsObject || !angular.isDefined(options.tagsInput.itemIsObject)) {
return item[options.tagsInput.keyProperty || options.tagsInput.displayProperty];
} else {
return item;
}
};

scope.getSuggestionClass = function(item, index) {
Expand Down
6 changes: 5 additions & 1 deletion src/tag-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ tagsInput.directive('tiTagItem', function(tiUtil) {
scope.$$removeTagSymbol = options.removeTagSymbol;

scope.$getDisplayText = function() {
return tiUtil.safeToString(scope.data[options.displayProperty]);
if (options.itemIsObject){
return tiUtil.safeToString(scope.data[options.displayProperty]);
} else {
return tiUtil.safeToString(scope.data);
}
};
scope.$removeTag = function() {
tagsInput.removeTag(scope.$index);
Expand Down
54 changes: 43 additions & 11 deletions src/tags-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,36 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags
var self = {}, getTagText, setTagText, canAddTag, canRemoveTag;

getTagText = function(tag) {
return tiUtil.safeToString(tag[options.displayProperty]);
if (options.itemIsObject){
return tiUtil.safeToString(tag[options.displayProperty]);
} else {
return tiUtil.safeToString(tag);
}
};

setTagText = function(tag, text) {
tag[options.displayProperty] = text;
if (options.itemIsObject) {
tag[options.displayProperty] = text;
} else {
tag = text;
}
return tag;
};

canAddTag = function(tag) {
var tagText = getTagText(tag);
var valid = tagText &&
tagText.length >= options.minLength &&
tagText.length <= options.maxLength &&
options.allowedTagsPattern.test(tagText) &&
!tiUtil.findInObjectArray(self.items, tag, options.keyProperty || options.displayProperty);
options.allowedTagsPattern.test(tagText);

if (valid) {
if (options.itemIsObject) {
valid = !tiUtil.findInObjectArray(self.items, tag, options.keyProperty || options.displayProperty);
} else {
valid = !tiUtil.findInStringArray(self.items, tag);
}
}

return $q.when(valid && onTagAdding({ $tag: tag })).then(tiUtil.promisifyValue);
};
Expand All @@ -84,8 +100,11 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags
self.items = [];

self.addText = function(text) {
var tag = {};
setTagText(tag, text);
var tag;
if (options.itemIsObject){
tag = {};
}
tag = setTagText(tag, text);
return self.add(tag);
};

Expand All @@ -96,7 +115,7 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags
tagText = tiUtil.replaceSpacesWithDashes(tagText);
}

setTagText(tag, tagText);
tag = setTagText(tag, tagText);

return canAddTag(tag)
.then(function() {
Expand Down Expand Up @@ -203,7 +222,8 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags
keyProperty: [String, ''],
allowLeftoverText: [Boolean, false],
addFromAutocompleteOnly: [Boolean, false],
spellcheck: [Boolean, true]
spellcheck: [Boolean, true],
itemIsObject: [Boolean, true]
});

$scope.tagList = new TagList($scope.options, $scope.events,
Expand Down Expand Up @@ -288,7 +308,11 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags
};

scope.track = function(tag) {
return tag[options.keyProperty || options.displayProperty];
if (options.itemIsObject) {
return tag[options.keyProperty || options.displayProperty];
} else {
return tag;
}
};

scope.getTagClass = function(tag, index) {
Expand All @@ -301,7 +325,11 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags

scope.$watch('tags', function(value) {
if (value) {
tagList.items = tiUtil.makeObjectArray(value, options.displayProperty);
if (options.itemIsObject) {
tagList.items = tiUtil.makeObjectArray(value, options.displayProperty);
} else {
tagList.items = value;
}
scope.tags = tagList.items;
}
else {
Expand Down Expand Up @@ -433,7 +461,11 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags
tagList.selectPrior();
tagList.removeSelected().then(function(tag) {
if (tag) {
scope.newTag.text(tag[options.displayProperty]);
if (options.itemIsObject) {
scope.newTag.text(tag[options.displayProperty]);
} else {
scope.newTag.text(tag);
}
}
});
}
Expand Down
14 changes: 14 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ tagsInput.factory('tiUtil', function($timeout, $q) {
return item;
};

self.findInStringArray = function(array, str, comparer) {
var item = null;
comparer = comparer || self.defaultComparer;

array.some(function(element) {
if (comparer(element, str)) {
item = element;
return true;
}
});

return item;
};

self.defaultComparer = function(a, b) {
// I'm aware of the internationalization issues regarding toLowerCase()
// but I couldn't come up with a better solution right now
Expand Down