|
257 | 257 | 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 = " ", c.appendChild(b), c.appendChild(d), c.appendChild(a), c.scrollTop = i.scrollTop, f = $(d).position(), $(c).remove(), f |
258 | 258 | } |
259 | 259 |
|
| 260 | + //same as above function but return offset instead of position |
| 261 | + function textareaSelectionOffset($el) { |
| 262 | + var a, b, c, d, e, f, g, h, i, j, k; |
| 263 | + if (!(i = $el[0])) return; |
| 264 | + if (!$(i).is("textarea")) return; |
| 265 | + if (i.selectionEnd == null) return; |
| 266 | + g = { |
| 267 | + position: "absolute", |
| 268 | + overflow: "auto", |
| 269 | + whiteSpace: "pre-wrap", |
| 270 | + wordWrap: "break-word", |
| 271 | + boxSizing: "content-box", |
| 272 | + top: 0, |
| 273 | + left: -9999 |
| 274 | + }, h = ["boxSizing", "fontFamily", "fontSize", "fontStyle", "fontVariant", "fontWeight", "height", "letterSpacing", "lineHeight", "paddingBottom", "paddingLeft", "paddingRight", "paddingTop", "textDecoration", "textIndent", "textTransform", "width", "word-spacing"]; |
| 275 | + for (j = 0, k = h.length; j < k; j++) e = h[j], g[e] = $(i).css(e); |
| 276 | + 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 = " ", c.appendChild(b), c.appendChild(d), c.appendChild(a), c.scrollTop = i.scrollTop, f = $(d).offset(), $(c).remove(), f |
| 277 | + } |
| 278 | + |
260 | 279 | //Scrolls back to the input after autocomplete if the window has scrolled past the input |
261 | 280 | function scrollToInput() { |
262 | 281 | var elmDistanceFromTop = $(elmInputBox).offset().top; //input offset |
|
455 | 474 | } |
456 | 475 |
|
457 | 476 | function positionAutocomplete(elmAutocompleteList, elmInputBox) { |
458 | | - var position = textareaSelectionPosition(elmInputBox), |
459 | | - lineHeight = parseInt(elmInputBox.css('line-height'), 10) || 18; |
460 | | - elmAutocompleteList.css('width', '15em'); // Sort of a guess |
461 | | - elmAutocompleteList.css('left', position.left); |
462 | | - elmAutocompleteList.css('top', lineHeight + position.top); |
463 | | - |
464 | | - //check if the right position of auto complete is larger than the right position of the input |
465 | | - //if yes, reset the left of auto complete list to make it fit the input |
466 | | - var elmInputBoxRight = elmInputBox.offset().left + elmInputBox.width(), |
467 | | - elmAutocompleteListRight = elmAutocompleteList.offset().left + elmAutocompleteList.width(); |
468 | | - if (elmInputBoxRight <= elmAutocompleteListRight) { |
469 | | - elmAutocompleteList.css('left', Math.abs(elmAutocompleteList.position().left - (elmAutocompleteListRight - elmInputBoxRight))); |
| 477 | + var elmAutocompleteListPosition = elmAutocompleteList.css('position'); |
| 478 | + if (elmAutocompleteListPosition == 'absolute') { |
| 479 | + var position = textareaSelectionPosition(elmInputBox), |
| 480 | + lineHeight = parseInt(elmInputBox.css('line-height'), 10) || 18; |
| 481 | + elmAutocompleteList.css('width', '15em'); // Sort of a guess |
| 482 | + elmAutocompleteList.css('left', position.left); |
| 483 | + elmAutocompleteList.css('top', lineHeight + position.top); |
| 484 | + |
| 485 | + //check if the right position of auto complete is larger than the right position of the input |
| 486 | + //if yes, reset the left of auto complete list to make it fit the input |
| 487 | + var elmInputBoxRight = elmInputBox.offset().left + elmInputBox.width(), |
| 488 | + elmAutocompleteListRight = elmAutocompleteList.offset().left + elmAutocompleteList.width(); |
| 489 | + if (elmInputBoxRight <= elmAutocompleteListRight) { |
| 490 | + elmAutocompleteList.css('left', Math.abs(elmAutocompleteList.position().left - (elmAutocompleteListRight - elmInputBoxRight))); |
| 491 | + } |
| 492 | + } |
| 493 | + else if (elmAutocompleteListPosition == 'fixed') { |
| 494 | + var offset = textareaSelectionOffset(elmInputBox), |
| 495 | + lineHeight = parseInt(elmInputBox.css('line-height'), 10) || 18; |
| 496 | + elmAutocompleteList.css('width', '15em'); // Sort of a guess |
| 497 | + elmAutocompleteList.css('left', offset.left + 10000); |
| 498 | + elmAutocompleteList.css('top', lineHeight + offset.top); |
470 | 499 | } |
471 | 500 | } |
472 | 501 |
|
|
0 commit comments