Assume text inserted in eventTarget at end (cannot rely on selectionStart or selectionEnd). Reset eventTarget value when focus gained.

This commit is contained in:
Michael Jumper
2012-03-30 22:10:20 -07:00
parent 21db2579a8
commit d891b90f67

View File

@@ -508,35 +508,33 @@ GuacamoleUI.attach = function(guac) {
// Monitor whether the event target is focused // Monitor whether the event target is focused
var eventTargetFocused = false; var eventTargetFocused = false;
// Save length for calculation of changed value
var currentLength = GuacamoleUI.eventTarget.value.length;
GuacamoleUI.eventTarget.onfocus = function() { GuacamoleUI.eventTarget.onfocus = function() {
eventTargetFocused = true; eventTargetFocused = true;
GuacamoleUI.eventTarget.value = "";
currentLength = 0;
}; };
GuacamoleUI.eventTarget.onblur = function() { GuacamoleUI.eventTarget.onblur = function() {
eventTargetFocused = false; eventTargetFocused = false;
}; };
// Save length for calculation of changed value
var currentLength = GuacamoleUI.eventTarget.value.length;
// If text is input directly into event target without typing (as with // If text is input directly into event target without typing (as with
// voice input, for example), type automatically. // voice input, for example), type automatically.
GuacamoleUI.eventTarget.oninput = function(e) { GuacamoleUI.eventTarget.oninput = function(e) {
// Calculate current length and change in length // Calculate current length and change in length
var newLength = GuacamoleUI.eventTarget.value.length; var oldLength = currentLength;
var changeLength = newLength - currentLength; currentLength = GuacamoleUI.eventTarget.value.length;
currentLength = newLength;
// If deleted or replaced text, ignore // If deleted or replaced text, ignore
if (changeLength <= 0) if (currentLength <= oldLength)
return; return;
// Get changed text // Get changed text
var text = GuacamoleUI.eventTarget.value.substring( var text = GuacamoleUI.eventTarget.value.substring(oldLength);
GuacamoleUI.eventTarget.selectionStart,
GuacamoleUI.eventTarget.selectionStart + changeLength
);
// Send each character // Send each character
for (var i=0; i<text.length; i++) { for (var i=0; i<text.length; i++) {