Skip to content

Commit b6bde71

Browse files
committed
Pull request #53
1 parent 210351c commit b6bde71

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

assets/examples.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $(function () {
1818

1919
callback.call(this, data);
2020
},
21-
allowRepeat: true
21+
onCaret: true
2222
});
2323

2424
$('.get-syntax-text').click(function() {

jquery.mentionsInput.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
allowRepeat : false, //Allow repeat mentions
2222
showAvatars : true, //Show the avatars
2323
elastic : true, //Grow the textarea automatically
24+
onCaret : false,
2425
classes : {
2526
autoCompleteItemActive : "active" //Classes to apply in each item
2627
},
@@ -221,6 +222,29 @@
221222
function getInputBoxValue() {
222223
return $.trim(elmInputBox.val());
223224
}
225+
226+
// This is taken straight from live (as of Sep 2012) GitHub code. The
227+
// technique is known around the web. Just google it. Github's is quite
228+
// succint though. NOTE: relies on selectionEnd, which as far as IE is concerned,
229+
// it'll only work on 9+. Good news is nothing will happen if the browser
230+
// doesn't support it.
231+
function textareaSelectionPosition($el) {
232+
var a, b, c, d, e, f, g, h, i, j, k;
233+
if (!(i = $el[0])) return;
234+
if (!$(i).is("textarea")) return;
235+
if (i.selectionEnd == null) return;
236+
g = {
237+
position: "absolute",
238+
overflow: "auto",
239+
whiteSpace: "pre-wrap",
240+
wordWrap: "break-word",
241+
boxSizing: "content-box",
242+
top: 0,
243+
left: -9999
244+
}, h = ["boxSizing", "fontFamily", "fontSize", "fontStyle", "fontVariant", "fontWeight", "height", "letterSpacing", "lineHeight", "paddingBottom", "paddingLeft", "paddingRight", "paddingTop", "textDecoration", "textIndent", "textTransform", "width", "word-spacing"];
245+
for (j = 0, k = h.length; j < k; j++) e = h[j], g[e] = $(i).css(e);
246+
return c = document.createElement("div"), $(c).css(g), $(i).after(c), b = document.createTextNode(i.value.substring(0, i.selectionEnd)), a = document.createTextNode(i.value.substring(i.selectionEnd)), d = document.createElement("span"), d.innerHTML = "&nbsp;", c.appendChild(b), c.appendChild(d), c.appendChild(a), c.scrollTop = i.scrollTop, f = $(d).position(), $(c).remove(), f
247+
}
224248

225249
//Scrolls back to the input after autocomplete if the window has scrolled past the input
226250
function scrollToInput() {
@@ -384,7 +408,7 @@
384408
'id' : utils.htmlEncode(item.id),
385409
'display' : utils.htmlEncode(item.name),
386410
'type' : utils.htmlEncode(item.type),
387-
'content' : utils.highlightTerm(utils.htmlEncode((item.name)), query)
411+
'content' : utils.highlightTerm(utils.htmlEncode((item.display ? item.display : item.name)), query)
388412
})).attr('data-uid', itemUid); //Inserts the new item to list
389413

390414
//If the index is 0
@@ -408,7 +432,10 @@
408432
});
409433

410434
elmAutocompleteList.show(); //Shows the elmAutocompleteList div
411-
elmDropDownList.show(); //Shows the elmDropDownList
435+
if (settings.onCaret) {
436+
positionAutocomplete(elmAutocompleteList, elmInputBox);
437+
}
438+
elmDropDownList.show(); //Shows the elmDropDownList
412439
}
413440

414441
//Search into data list passed as parameter
@@ -423,6 +450,14 @@
423450
hideAutoComplete(); //Hide the autocompletelist
424451
}
425452
}
453+
454+
function positionAutocomplete(elmAutocompleteList, elmInputBox) {
455+
var position = textareaSelectionPosition(elmInputBox),
456+
lineHeight = parseInt(elmInputBox.css('line-height'), 10) || 18;
457+
elmAutocompleteList.css('width', '15em'); // Sort of a guess
458+
elmAutocompleteList.css('left', position.left);
459+
elmAutocompleteList.css('top', lineHeight + position.top);
460+
}
426461

427462
//Resets the text area
428463
function resetInput() {

0 commit comments

Comments
 (0)