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. diff --git a/jquery.mentionsInput.js b/jquery.mentionsInput.js index 6dc6d78..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" }, @@ -28,7 +29,7 @@ autocompleteListItemAvatar : _.template(''), autocompleteListItemIcon : _.template('
'), mentionsOverlay : _.template('
'), - mentionItemSyntax : _.template('@[<%= value %>](<%= type %>:<%= id %>)'), + mentionItemSyntax : _.template('<%= triggerChar %>[<%= value %>](<%= type %>:<%= id %>)'), mentionItemHighlight : _.template('<%= value %>') } }; @@ -104,14 +105,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 +140,10 @@ function addMention(value, id, type) { var currentMessage = getInputBoxValue(); + var currentTriggerChar = elmInputBox.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 +158,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 +192,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) { @@ -290,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) })); @@ -312,11 +324,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); - }); + elmInputBox.data('triggerChar', triggerChar); + }, triggerChar); } } @@ -334,7 +347,6 @@ if (!_.isFunction(callback)) { return; } - var value = mentionsCollection.length ? elmInputBox.data('messageText') : getInputBoxValue(); callback.call(this, value); }, @@ -358,14 +370,9 @@ $.fn.mentionsInput = function (method, settings) { if (typeof method === 'object' || !method) { - settings = method; - } - - if (!settings) { - settings = {}; + settings = $.extend(true, {}, defaultSettings, method); } - settings = _.defaults(settings, defaultSettings); var outerArguments = arguments; return this.each(function () { @@ -384,4 +391,4 @@ }); }; -})(jQuery, _); \ No newline at end of file +})(jQuery, _);