From 29aab0f31f0849f3b0d8e70cc84c02fae66abc33 Mon Sep 17 00:00:00 2001 From: Shahmir Noorani Date: Mon, 4 May 2015 16:44:59 -0500 Subject: [PATCH 1/3] Allow duplicates. --- dist/js/bootstrap-tags.js | 11 ++++++++--- src/bootstrap-tags.coffee | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/dist/js/bootstrap-tags.js b/dist/js/bootstrap-tags.js index 2a4ffec..0752f71 100644 --- a/dist/js/bootstrap-tags.js +++ b/dist/js/bootstrap-tags.js @@ -31,6 +31,7 @@ this.caseInsensitive || (this.caseInsensitive = false); this.readOnlyEmptyMessage || (this.readOnlyEmptyMessage = "No tags to display..."); this.maxNumTags || (this.maxNumTags = -1); + this.allowDuplicates || (this.allowDuplicates = false); this.beforeAddingTag || (this.beforeAddingTag = function(tag) {}); this.afterAddingTag || (this.afterAddingTag = function(tag) {}); this.beforeDeletingTag || (this.beforeDeletingTag = function(tag) {}); @@ -98,11 +99,15 @@ }; }; this.hasTag = function(tag) { - return _this.tagsArray.indexOf(tag) > -1; + if (_this.allowDuplicates(true)) { + return false; + } else { + return _this.tagsArray.indexOf(tag) > -1; + } }; this.removeTagClicked = function(e) { if (e.currentTarget.tagName === "A") { - _this.removeTag($("span", e.currentTarget.parentElement).html()); + _this.removeTag($("span", e.currentTarget.parentElement).text()); $(e.currentTarget.parentNode).remove(); } return _this; @@ -190,7 +195,7 @@ e.preventDefault(); _this.pressedReturn(e); tag = e.target.value; - if (_this.suggestedIndex !== -1) { + if (_this.suggestedIndex != null && _this.suggestedIndex !== -1) { tag = _this.suggestionList[_this.suggestedIndex]; } _this.addTag(tag); diff --git a/src/bootstrap-tags.coffee b/src/bootstrap-tags.coffee index 927a618..8b9ff93 100644 --- a/src/bootstrap-tags.coffee +++ b/src/bootstrap-tags.coffee @@ -27,6 +27,7 @@ jQuery -> @caseInsensitive ||= false @readOnlyEmptyMessage ||= 'No tags to display...' @maxNumTags ||= -1 + @allowDuplicates ||= false # callbacks @beforeAddingTag ||= (tag) -> @@ -86,7 +87,7 @@ jQuery -> tag: @tagsArray[index], content: @popoverArray[index] @hasTag = (tag) => - @tagsArray.indexOf(tag) > -1 + if @allowDuplicates true then false else @tagsArray.indexOf(tag) > -1 #################### # add/remove methods From f5dfc1e4902202e7c08b3fb5dcea90d236e8d22c Mon Sep 17 00:00:00 2001 From: Shahmir Noorani Date: Mon, 4 May 2015 16:51:34 -0500 Subject: [PATCH 2/3] Fix coffee - use as variable instead of function. --- dist/js/bootstrap-tags.js | 2 +- src/bootstrap-tags.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/js/bootstrap-tags.js b/dist/js/bootstrap-tags.js index 0752f71..d126b34 100644 --- a/dist/js/bootstrap-tags.js +++ b/dist/js/bootstrap-tags.js @@ -99,7 +99,7 @@ }; }; this.hasTag = function(tag) { - if (_this.allowDuplicates(true)) { + if (_this.allowDuplicates === true) { return false; } else { return _this.tagsArray.indexOf(tag) > -1; diff --git a/src/bootstrap-tags.coffee b/src/bootstrap-tags.coffee index 8b9ff93..0ca9410 100644 --- a/src/bootstrap-tags.coffee +++ b/src/bootstrap-tags.coffee @@ -87,7 +87,7 @@ jQuery -> tag: @tagsArray[index], content: @popoverArray[index] @hasTag = (tag) => - if @allowDuplicates true then false else @tagsArray.indexOf(tag) > -1 + if @allowDuplicates == true then false else @tagsArray.indexOf(tag) > -1 #################### # add/remove methods From 8c9086025ca228f7794473fbddb18d6d8e20e8b5 Mon Sep 17 00:00:00 2001 From: Shahmir Noorani Date: Mon, 4 May 2015 17:06:08 -0500 Subject: [PATCH 3/3] allowDuplicates means option shouldn't be removed from suggestion. --- dist/js/bootstrap-tags.js | 10 +++++----- src/bootstrap-tags.coffee | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/dist/js/bootstrap-tags.js b/dist/js/bootstrap-tags.js index d126b34..37b928d 100644 --- a/dist/js/bootstrap-tags.js +++ b/dist/js/bootstrap-tags.js @@ -99,9 +99,7 @@ }; }; this.hasTag = function(tag) { - if (_this.allowDuplicates === true) { - return false; - } else { + if (!_this.allowDuplicates) { return _this.tagsArray.indexOf(tag) > -1; } }; @@ -258,8 +256,10 @@ $.each(this.suggestions, function(i, suggestion) { var suggestionVal; suggestionVal = _this.caseInsensitive ? suggestion.substring(0, str.length).toLowerCase() : suggestion.substring(0, str.length); - if (_this.tagsArray.indexOf(suggestion) < 0 && suggestionVal === str && (str.length > 0 || overrideLengthCheck)) { - return _this.suggestionList.push(suggestion); + if (_this.allowDuplicates || _this.tagsArray.indexOf(suggestion) < 0) { + if (suggestionVal === str && (str.length > 0 || overrideLengthCheck)) { + return _this.suggestionList.push(suggestion); + } } }); return this.suggestionList; diff --git a/src/bootstrap-tags.coffee b/src/bootstrap-tags.coffee index 0ca9410..9d4acb2 100644 --- a/src/bootstrap-tags.coffee +++ b/src/bootstrap-tags.coffee @@ -87,7 +87,7 @@ jQuery -> tag: @tagsArray[index], content: @popoverArray[index] @hasTag = (tag) => - if @allowDuplicates == true then false else @tagsArray.indexOf(tag) > -1 + if !@allowDuplicates then @tagsArray.indexOf(tag) > -1 #################### # add/remove methods @@ -222,8 +222,9 @@ jQuery -> str = str.toLowerCase() if @caseInsensitive $.each @suggestions, (i, suggestion) => suggestionVal = if @caseInsensitive then suggestion.substring(0, str.length).toLowerCase() else suggestion.substring(0, str.length) - if @tagsArray.indexOf(suggestion) < 0 and suggestionVal == str and (str.length > 0 or overrideLengthCheck) - @suggestionList.push suggestion + if @allowDuplicates or @tagsArray.indexOf(suggestion) < 0 + if suggestionVal == str and (str.length > 0 or overrideLengthCheck) + @suggestionList.push suggestion @suggestionList # makeSuggestions creates auto suggestions that match the value in the input