From 215cc520cd1af68b40567363886c760f6f5f2345 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Sat, 15 Nov 2014 04:41:33 +0000 Subject: [PATCH] Fix post tags input. Closes #4378. Closes #4455. - Trim whitespace from tag names on entry. - Prevent duplicate tags in suggestion list. - Fix unhandled exception that happens when backspace is pressed in an empty tags input box. --- core/client/controllers/post-tags-input.js | 31 +++++++++++++++------- core/client/views/post-tags-input.js | 4 --- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/core/client/controllers/post-tags-input.js b/core/client/controllers/post-tags-input.js index 2098243a76..ae415623df 100644 --- a/core/client/controllers/post-tags-input.js +++ b/core/client/controllers/post-tags-input.js @@ -47,6 +47,7 @@ var PostTagsInputController = Ember.Controller.extend({ return; } + newTagText = newTagText.trim(); searchTerm = newTagText.toLowerCase(); // add existing tag if we have a match @@ -76,8 +77,10 @@ var PostTagsInputController = Ember.Controller.extend({ }, deleteTag: function (tag) { - this.get('tags').removeObject(tag); - this.get('tagEnteredOrder').removeObject(tag.get('name')); + if (tag) { + this.get('tags').removeObject(tag); + this.get('tagEnteredOrder').removeObject(tag.get('name')); + } }, deleteLastTag: function () { @@ -160,7 +163,7 @@ var PostTagsInputController = Ember.Controller.extend({ matchingTags, // Limit the suggestions number maxSuggestions = 5, - suggestions = new Ember.A(); + suggestions = Ember.A(); if (!searchTerm || Ember.isEmpty(searchTerm.trim())) { this.set('suggestions', null); @@ -182,7 +185,8 @@ var PostTagsInputController = Ember.Controller.extend({ findMatchingTags: function (searchTerm) { var matchingTags, self = this, - allTags = this.store.all('tag'); + allTags = this.store.all('tag'), + deDupe = {}; if (allTags.get('length') === 0) { return []; @@ -192,12 +196,21 @@ var PostTagsInputController = Ember.Controller.extend({ matchingTags = allTags.filter(function (tag) { var tagNameMatches, - hasAlreadyBeenAdded; + hasAlreadyBeenAdded, + tagName = tag.get('name'); - tagNameMatches = tag.get('name').toLowerCase().indexOf(searchTerm) !== -1; - hasAlreadyBeenAdded = self.hasTag(tag.get('name')); + tagNameMatches = tagName.toLowerCase().indexOf(searchTerm) !== -1; + hasAlreadyBeenAdded = self.hasTag(tagName); - return tagNameMatches && !hasAlreadyBeenAdded; + if (tagNameMatches && !hasAlreadyBeenAdded) { + if (typeof deDupe[tagName] === 'undefined') { + deDupe[tagName] = 1; + } else { + deDupe[tagName] += 1; + } + } + + return deDupe[tagName] === 1; }); return matchingTags; @@ -215,7 +228,7 @@ var PostTagsInputController = Ember.Controller.extend({ tagName = Ember.Handlebars.Utils.escapeExpression(matchingTag.get('name')), regex = new RegExp('(' + regexEscapedSearchTerm + ')', 'gi'), highlightedName, - suggestion = new Ember.Object(); + suggestion = Ember.Object.create(); highlightedName = tagName.replace(regex, '$1'); highlightedName = new Ember.Handlebars.SafeString(highlightedName); diff --git a/core/client/views/post-tags-input.js b/core/client/views/post-tags-input.js index 61c8da91b4..3585a21129 100644 --- a/core/client/views/post-tags-input.js +++ b/core/client/views/post-tags-input.js @@ -50,10 +50,6 @@ var PostTagsInputView = Ember.View.extend({ focusOut: function () { this.get('parentView').set('hasFocus', false); - - // if (!Ember.isEmpty(this.get('value'))) { - // this.get('parentView.controller').send('addNewTag'); - // } }, keyDown: function (event) {