Permalink
Browse files

Store raw mention object in mentionsCollection

  • Loading branch information...
1 parent a199cc6 commit 0ec3fb23cb18fe49ae994e3e20684794997a472e @OhaiBBQ committed Jan 16, 2012
Showing with 22 additions and 25 deletions.
  1. +22 −25 jquery.mentionsInput.js
View
@@ -28,8 +28,8 @@
autocompleteListItemAvatar : _.template('<img src="<%= avatar %>" />'),
autocompleteListItemIcon : _.template('<div class="icon <%= icon %>"></div>'),
mentionsOverlay : _.template('<div class="mentions"><div></div></div>'),
- mentionItemSyntax : _.template('@[<%= value %>](<%= type %>:<%= id %>)'),
- mentionItemHighlight : _.template('<strong><span><%= value %></span></strong>')
+ mentionItemSyntax : _.template('@[<%= name %>](<%= type %>:<%= id %>)'),
+ mentionItemHighlight : _.template('<strong><span><%= name %></span></strong>')
}
};
@@ -100,19 +100,20 @@
elmMentionsOverlay.prependTo(elmWrapperBox);
}
- function updateValues() {
+ function updateNames() {
var syntaxMessage = getInputBoxValue();
_.each(mentionsCollection, function (mention) {
- var textSyntax = settings.templates.mentionItemSyntax({ value : mention.value, type : 'contact', id : mention.id });
- syntaxMessage = syntaxMessage.replace(mention.value, textSyntax);
+ var textSyntax = settings.templates.mentionItemSyntax({ name : mention.name, type : 'contact', id : mention.id, mention: mention });
+
+ syntaxMessage = syntaxMessage.replace(mention.name, 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 textHighlight = settings.templates.mentionItemHighlight({ value : utils.htmlEncode(mention.value) });
+ var textSyntax = settings.templates.mentionItemSyntax({ name : utils.htmlEncode(mention.name), type : 'contact', id : mention.id, mention : mention });
+ var textHighlight = settings.templates.mentionItemHighlight({ name : utils.htmlEncode(mention.name), mention : mention });
mentionText = mentionText.replace(textSyntax, textHighlight);
});
@@ -132,12 +133,12 @@
var inputText = getInputBoxValue();
mentionsCollection = _.reject(mentionsCollection, function (mention, index) {
- return !mention.value || inputText.indexOf(mention.value) == -1;
+ return !mention.name || inputText.indexOf(mention.name) == -1;
});
mentionsCollection = _.compact(mentionsCollection);
}
- function addMention(value, id, type) {
+ function addMention(mention) {
var currentMessage = getInputBoxValue();
// Using a regex to figure out positions
@@ -149,15 +150,11 @@
var start = currentMessage.substr(0, startCaretPosition);
var end = currentMessage.substr(currentCaretPosition, currentMessage.length);
- var startEndIndex = (start + value).length;
+ var startEndIndex = (start + mention.name).length;
- var updatedMessageText = start + value + end;
+ var updatedMessageText = start + mention.name + end;
- mentionsCollection.push({
- id : id,
- type : type,
- value : value
- });
+ mentionsCollection.push(mention);
// Cleaning before inserting the value, otherwise auto-complete would be triggered with "old" inputbuffer
resetBuffer();
@@ -166,7 +163,7 @@
// Mentions & syntax message
elmInputBox.val(updatedMessageText);
- updateValues();
+ updateNames();
// Set correct focus and selection
elmInputBox.focus();
@@ -178,9 +175,9 @@
}
function onAutoCompleteItemClick(e) {
- var elmTarget = $(this);
+ var mention = $(this).data("mention");
- addMention(elmTarget.attr('data-display'), elmTarget.attr('data-ref-id'), elmTarget.attr('data-ref-type'));
+ addMention(mention);
return false;
}
@@ -190,7 +187,7 @@
}
function onInputBoxInput(e) {
- updateValues();
+ updateNames();
updateMentionsCollection();
hideAutoComplete();
@@ -274,9 +271,9 @@
elmAutocompleteList.show();
// Filter items that has already been mentioned
- var mentionValues = _.pluck(mentionsCollection, 'value');
+ var mentionedNames = _.pluck(mentionsCollection, 'name');
results = _.reject(results, function (item) {
- return _.include(mentionValues, item.name);
+ return _.include(mentionedNames, item.name);
});
if (!results.length) {
@@ -293,7 +290,7 @@
'display' : utils.htmlEncode(item.name),
'type' : utils.htmlEncode(item.type),
'content' : utils.highlightTerm(utils.htmlEncode((item.name)), query)
- }));
+ })).data('mention', item);
if (settings.showAvatars) {
var elmIcon;
@@ -342,7 +339,7 @@
reset : function () {
elmInputBox.val('');
mentionsCollection = [];
- updateValues();
+ updateNames();
},
getMentions : function (callback) {
@@ -384,4 +381,4 @@
});
};
-})(jQuery, _);
+})(jQuery, _);

0 comments on commit 0ec3fb2

Please sign in to comment.