From da2af0edbe4bfffeb9f77d636e954bcb05bf0e9d Mon Sep 17 00:00:00 2001 From: Sebastian Moreno Date: Wed, 8 Jun 2016 16:00:27 -0400 Subject: [PATCH 1/3] before testing --- src/auto-complete-match.js | 6 ++++- src/auto-complete.js | 41 ++++++++++++++++++++++------- src/tag-item.js | 6 ++++- src/tags-input.js | 54 ++++++++++++++++++++++++++++++-------- src/util.js | 14 ++++++++++ 5 files changed, 98 insertions(+), 23 deletions(-) diff --git a/src/auto-complete-match.js b/src/auto-complete-match.js index 074a3f82..aa99dee7 100644 --- a/src/auto-complete-match.js +++ b/src/auto-complete-match.js @@ -31,7 +31,11 @@ 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){ + return tiUtil.safeToString(scope.data[options.displayProperty || options.tagsInput.displayProperty]); + } else { + return tiUtil.safeToString(scope.data); + } }; } }; diff --git a/src/auto-complete.js b/src/auto-complete.js index a5696ffc..d4d5496c 100644 --- a/src/auto-complete.js +++ b/src/auto-complete.js @@ -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) { + 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) { + 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); + }); + } }); }; @@ -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){ + items = tiUtil.makeObjectArray(items.data || items, getTagId()); + } + items = getDifference(items, tags); self.items = items.slice(0, options.maxResultsToShow); @@ -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) { + return item[options.tagsInput.keyProperty || options.tagsInput.displayProperty]; + } else { + return item; + } }; scope.getSuggestionClass = function(item, index) { diff --git a/src/tag-item.js b/src/tag-item.js index b252de1e..16bb41b2 100644 --- a/src/tag-item.js +++ b/src/tag-item.js @@ -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); diff --git a/src/tags-input.js b/src/tags-input.js index 4afc66d1..c7094dcc 100644 --- a/src/tags-input.js +++ b/src/tags-input.js @@ -59,11 +59,20 @@ 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) { @@ -71,8 +80,15 @@ tagsInput.directive('tagsInput', function($timeout, $document, $window, $q, tags 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); }; @@ -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); }; @@ -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() { @@ -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, @@ -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) { @@ -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 { @@ -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); + } } }); } diff --git a/src/util.js b/src/util.js index 6d2e2fe8..2d52497f 100644 --- a/src/util.js +++ b/src/util.js @@ -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 From a100395efb114cd7ba93d510f2961ea755740d99 Mon Sep 17 00:00:00 2001 From: Sebastian Moreno Date: Wed, 8 Jun 2016 17:15:30 -0400 Subject: [PATCH 2/3] Accept strings in the array --- src/auto-complete-match.js | 8 ++++---- src/auto-complete.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/auto-complete-match.js b/src/auto-complete-match.js index aa99dee7..874e5909 100644 --- a/src/auto-complete-match.js +++ b/src/auto-complete-match.js @@ -31,12 +31,12 @@ tagsInput.directive('tiAutocompleteMatch', function($sce, tiUtil) { return $sce.trustAsHtml(text); }; scope.$getDisplayText = function() { - if (options.tagsInput.itemIsObject){ - return tiUtil.safeToString(scope.data[options.displayProperty || options.tagsInput.displayProperty]); + if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject) { + return tiUtil.safeToString(scope.data[options.displayProperty || options.tagsInput.displayProperty]); } else { - return tiUtil.safeToString(scope.data); + return tiUtil.safeToString(scope.data); } }; } }; -}); +}); \ No newline at end of file diff --git a/src/auto-complete.js b/src/auto-complete.js index d4d5496c..2354b31d 100644 --- a/src/auto-complete.js +++ b/src/auto-complete.js @@ -37,7 +37,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags var self = {}, getDifference, lastPromise, getTagId; getTagId = function() { - if (options.tagsInput.itemIsObject) { + if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject) { return options.tagsInput.keyProperty || options.tagsInput.displayProperty; } else { return null; @@ -46,7 +46,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags getDifference = function(array1, array2) { return array1.filter(function(item) { - if (options.tagsInput.itemIsObject) { + if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject) { return !tiUtil.findInObjectArray(array2, item, getTagId(), function(a, b) { if (options.tagsInput.replaceSpacesWithDashes) { a = tiUtil.replaceSpacesWithDashes(a); @@ -95,7 +95,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags return; } - if (options.tagsInput.itemIsObject){ + if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject){ items = tiUtil.makeObjectArray(items.data || items, getTagId()); } @@ -220,7 +220,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags }; scope.track = function(item) { - if (options.tagsInput.itemIsObject) { + if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject) { return item[options.tagsInput.keyProperty || options.tagsInput.displayProperty]; } else { return item; From 86e40a9b9b762e2923c5b048036600a793146680 Mon Sep 17 00:00:00 2001 From: Sebastian Moreno Date: Wed, 8 Jun 2016 21:45:03 -0400 Subject: [PATCH 3/3] fixed error if item.itemIsObject is not defined --- src/auto-complete-match.js | 2 +- src/auto-complete.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/auto-complete-match.js b/src/auto-complete-match.js index 874e5909..09df8212 100644 --- a/src/auto-complete-match.js +++ b/src/auto-complete-match.js @@ -31,7 +31,7 @@ tagsInput.directive('tiAutocompleteMatch', function($sce, tiUtil) { return $sce.trustAsHtml(text); }; scope.$getDisplayText = function() { - if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject) { + 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); diff --git a/src/auto-complete.js b/src/auto-complete.js index 2354b31d..3e9b0e12 100644 --- a/src/auto-complete.js +++ b/src/auto-complete.js @@ -37,7 +37,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags var self = {}, getDifference, lastPromise, getTagId; getTagId = function() { - if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject) { + if (options.tagsInput.itemIsObject || !angular.isDefined(options.tagsInput.itemIsObject)) { return options.tagsInput.keyProperty || options.tagsInput.displayProperty; } else { return null; @@ -46,7 +46,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags getDifference = function(array1, array2) { return array1.filter(function(item) { - if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject) { + 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); @@ -95,7 +95,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags return; } - if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject){ + if (options.tagsInput.itemIsObject || !angular.isDefined(options.tagsInput.itemIsObject)){ items = tiUtil.makeObjectArray(items.data || items, getTagId()); } @@ -220,7 +220,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags }; scope.track = function(item) { - if (options.tagsInput.itemIsObject || !options.tagsInput.itemIsObject) { + if (options.tagsInput.itemIsObject || !angular.isDefined(options.tagsInput.itemIsObject)) { return item[options.tagsInput.keyProperty || options.tagsInput.displayProperty]; } else { return item;