From 4fe4f80c904d2700efb848eca0615c7a4c10d08f Mon Sep 17 00:00:00 2001 From: Ryan Ramage Date: Fri, 16 Dec 2011 11:08:58 -0700 Subject: [PATCH 1/5] Now supports multiple trigger chars. triggerChar can now be set as just a char, or an array of chars. The trigger char is passed as the last parameter to the onDataRequest callback. All type and templates now include the type and trigger char. --- jquery.mentionsInput.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/jquery.mentionsInput.js b/jquery.mentionsInput.js index 6dc6d78..4fe218e 100644 --- a/jquery.mentionsInput.js +++ b/jquery.mentionsInput.js @@ -28,7 +28,7 @@ autocompleteListItemAvatar : _.template(''), autocompleteListItemIcon : _.template('
'), mentionsOverlay : _.template('
'), - mentionItemSyntax : _.template('@[<%= value %>](<%= type %>:<%= id %>)'), + mentionItemSyntax : _.template('<%= triggerChar %>[<%= value %>](<%= type %>:<%= id %>)'), mentionItemHighlight : _.template('<%= value %>') } }; @@ -104,14 +104,14 @@ var syntaxMessage = getInputBoxValue(); _.each(mentionsCollection, function (mention) { - var textSyntax = settings.templates.mentionItemSyntax({ value : mention.value, type : 'contact', id : mention.id }); + var textSyntax = settings.templates.mentionItemSyntax({ value : mention.value, type : mention.type, id : mention.id, triggerChar: mention.trigger }); syntaxMessage = syntaxMessage.replace(mention.value, textSyntax); }); var mentionText = utils.htmlEncode(syntaxMessage); _.each(mentionsCollection, function (mention) { - var textSyntax = settings.templates.mentionItemSyntax({ value : utils.htmlEncode(mention.value), type : 'contact', id : mention.id }); + var textSyntax = settings.templates.mentionItemSyntax({ value : utils.htmlEncode(mention.value), type : mention.type, id : mention.id, triggerChar: mention.trigger }); var textHighlight = settings.templates.mentionItemHighlight({ value : utils.htmlEncode(mention.value) }); mentionText = mentionText.replace(textSyntax, textHighlight); @@ -139,9 +139,10 @@ function addMention(value, id, type) { var currentMessage = getInputBoxValue(); + var currentTriggerChar = $(this).data('triggerChar'); // Using a regex to figure out positions - var regex = new RegExp("\\" + settings.triggerChar + currentDataQuery, "gi"); + var regex = new RegExp("\\" + currentTriggerChar + currentDataQuery, "gi"); regex.exec(currentMessage); var startCaretPosition = regex.lastIndex - currentDataQuery.length - 1; @@ -156,7 +157,8 @@ mentionsCollection.push({ id : id, type : type, - value : value + value : value, + trigger : currentTriggerChar }); // Cleaning before inserting the value, otherwise auto-complete would be triggered with "old" inputbuffer @@ -189,17 +191,26 @@ resetBuffer(); } + function checkTriggerChar(inputBuffer, triggerChar) { + var triggerCharIndex = _.lastIndexOf(inputBuffer, triggerChar); + if (triggerCharIndex > -1) { + currentDataQuery = inputBuffer.slice(triggerCharIndex + 1).join(''); + _.defer(_.bind(doSearch, this, currentDataQuery, triggerChar)); + } + } function onInputBoxInput(e) { updateValues(); updateMentionsCollection(); hideAutoComplete(); - var triggerCharIndex = _.lastIndexOf(inputBuffer, settings.triggerChar); - if (triggerCharIndex > -1) { - currentDataQuery = inputBuffer.slice(triggerCharIndex + 1).join(''); + if (_.isArray(settings.triggerChar)) { + _.each(settings.triggerChar, function (triggerChar) { + checkTriggerChar(inputBuffer, triggerChar); + }); + } else { + checkTriggerChar(inputBuffer, settings.triggerChar); + } - _.defer(_.bind(doSearch, this, currentDataQuery)); - } } function onInputBoxKeyPress(e) { @@ -312,11 +323,12 @@ elmDropDownList.show(); } - function doSearch(query) { + function doSearch(query, triggerChar) { if (query && query.length && query.length >= settings.minChars) { settings.onDataRequest.call(this, 'search', query, function (responseData) { populateDropdown(query, responseData); - }); + $(this).data('triggerChar', triggerChar); + }, triggerChar); } } @@ -334,7 +346,6 @@ if (!_.isFunction(callback)) { return; } - var value = mentionsCollection.length ? elmInputBox.data('messageText') : getInputBoxValue(); callback.call(this, value); }, From 77e9be7315518859ee30eabe381aa391d42a487b Mon Sep 17 00:00:00 2001 From: Ryan Ramage Date: Thu, 22 Dec 2011 22:20:51 -0700 Subject: [PATCH 2/5] Use elmInputBox instead of $(this) --- jquery.mentionsInput.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jquery.mentionsInput.js b/jquery.mentionsInput.js index 4fe218e..8bb442e 100644 --- a/jquery.mentionsInput.js +++ b/jquery.mentionsInput.js @@ -139,7 +139,7 @@ function addMention(value, id, type) { var currentMessage = getInputBoxValue(); - var currentTriggerChar = $(this).data('triggerChar'); + var currentTriggerChar = elmInputBox.data('triggerChar'); // Using a regex to figure out positions var regex = new RegExp("\\" + currentTriggerChar + currentDataQuery, "gi"); @@ -327,7 +327,7 @@ if (query && query.length && query.length >= settings.minChars) { settings.onDataRequest.call(this, 'search', query, function (responseData) { populateDropdown(query, responseData); - $(this).data('triggerChar', triggerChar); + elmInputBox.data('triggerChar', triggerChar); }, triggerChar); } } From 0e2d955e8b7fb76fc5ebbbd46415a0435ca0889f Mon Sep 17 00:00:00 2001 From: Kenneth Auchenberg Date: Sat, 17 Dec 2011 23:55:50 +0800 Subject: [PATCH 3/5] Added license file --- MIT-LICENSE.txt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 MIT-LICENSE.txt diff --git a/MIT-LICENSE.txt b/MIT-LICENSE.txt new file mode 100644 index 0000000..c8bb3b1 --- /dev/null +++ b/MIT-LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2011 Podio, http://podio.com/ + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. From 6a32557997d0428157c4d7c553683a0fed68dbf8 Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Fri, 23 Dec 2011 03:21:01 +0800 Subject: [PATCH 4/5] Use a deep copy for settings instead of defaults --- jquery.mentionsInput.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/jquery.mentionsInput.js b/jquery.mentionsInput.js index 8bb442e..1761840 100644 --- a/jquery.mentionsInput.js +++ b/jquery.mentionsInput.js @@ -369,14 +369,9 @@ $.fn.mentionsInput = function (method, settings) { if (typeof method === 'object' || !method) { - settings = method; + settings = $.extend(true, {}, defaultSettings, method); } - if (!settings) { - settings = {}; - } - - settings = _.defaults(settings, defaultSettings); var outerArguments = arguments; return this.each(function () { @@ -395,4 +390,4 @@ }); }; -})(jQuery, _); \ No newline at end of file +})(jQuery, _); From bc9c1ea2ce99988efb2cf842e8eb4e00b9638c10 Mon Sep 17 00:00:00 2001 From: luiztiago Date: Mon, 19 Dec 2011 22:58:43 +0800 Subject: [PATCH 5/5] support to others textarea displays names from object --- jquery.mentionsInput.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jquery.mentionsInput.js b/jquery.mentionsInput.js index 1761840..ede0c19 100644 --- a/jquery.mentionsInput.js +++ b/jquery.mentionsInput.js @@ -18,6 +18,7 @@ minChars : 2, showAvatars : true, elastic : true, + display : 'name', classes : { autoCompleteItemActive : "active" }, @@ -301,7 +302,7 @@ _.each(results, function (item) { var elmListItem = $(settings.templates.autocompleteListItem({ 'id' : utils.htmlEncode(item.id), - 'display' : utils.htmlEncode(item.name), + 'display' : utils.htmlEncode(item[settings.display]), 'type' : utils.htmlEncode(item.type), 'content' : utils.highlightTerm(utils.htmlEncode((item.name)), query) }));