From 9094216550ca319c27eebbbfb012b4b158603978 Mon Sep 17 00:00:00 2001 From: Hitesh Trivedi <htr@openerp.com> Date: Tue, 9 Sep 2014 10:32:45 +0530 Subject: [PATCH] [IMP] website_forum: tag creation now done only on enter, allowing to add whitespaces in tag name. More consistent behavior with odoo. --- .../static/src/js/website_forum.js | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/addons/website_forum/static/src/js/website_forum.js b/addons/website_forum/static/src/js/website_forum.js index 9a00eb7de231..de4e1b2b9dbd 100644 --- a/addons/website_forum/static/src/js/website_forum.js +++ b/addons/website_forum/static/src/js/website_forum.js @@ -118,21 +118,54 @@ $(document).ready(function () { function set_tags(tags) { $("input.load_tags").textext({ plugins: 'tags focus autocomplete ajax', + ext: { + autocomplete: { + onSetSuggestions : function(e, data) { + var self = this, + val = self.val(), + suggestions = self._suggestions = data.result; + if(data.showHideDropdown !== false) + self.trigger(suggestions === null || suggestions.length === 0 && val.length === 0 ? "hideDropdown" : "showDropdown"); + }, + renderSuggestions: function(suggestions) { + var self = this, + val = self.val(); + self.clearItems(); + $.each(suggestions || [], function(index, item) { + self.addSuggestion(item); + }); + var lowerCasesuggestions = $.map(suggestions, function(n,i){return n.toLowerCase();}); + if(jQuery.inArray(val.toLowerCase(), lowerCasesuggestions) ==-1) { + self.addSuggestion("Create '" + val + "'"); + } + }, + }, + tags: { + onEnterKeyPress: function(e) { + var self = this, + val = self.val(), + tag = self.itemManager().stringToItem(val); + + if(self.isTagAllowed(tag)) { + tag = tag.replace(/Create\ '|\'|'/g,''); + self.addTags([ tag ]); + // refocus the textarea just in case it lost the focus + self.core().focusInput(); + } + }, + } + }, tagsItems: tags.split(","), //Note: The following list of keyboard keys is added. All entries are default except {32 : 'whitespace!'}. keys: {8: 'backspace', 9: 'tab', 13: 'enter!', 27: 'escape!', 37: 'left', 38: 'up!', 39: 'right', - 40: 'down!', 46: 'delete', 108: 'numpadEnter', 32: 'whitespace!'}, + 40: 'down!', 46: 'delete', 108: 'numpadEnter', 32: 'whitespace'}, ajax: { url: '/forum/get_tags', dataType: 'json', cacheResults: true } }); - // Adds: create tags on space + blur - $("input.load_tags").on('whitespaceKeyDown blur', function () { - $(this).textext()[0].tags().addTags([ $(this).val() ]); - $(this).val(""); - }); + $("input.load_tags").on('isTagAllowed', function(e, data) { if (_.indexOf($(this).textext()[0].tags()._formData, data.tag) != -1) { data.result = false; -- GitLab