This commit is contained in:
nightwing 2012-12-06 01:44:53 +04:00
commit 3565a06191

View file

@ -55,28 +55,55 @@ var TextInput = function(parentNode, host) {
var PLACEHOLDER = "\x01\x01";
resetValue();
if (isFocused())
host.onFocus();
// Somehow fixes problem with firing onpropertychange on first typed char
if (useragent.isOldIE) {
resetSelection();
resetValue();
setTimeout(resetSelection);
}
var cut = false
var copied = false;
var pasted = false;
var inCompostion = false;
var resetTimeout = null;
var tempStyle = '';
resetValue();
if (isFocused())
host.onFocus();
var isAllSelected = function(text) {
return text.selectionStart === 0 && text.selectionEnd === text.value.length;
}
// IE8 does not support setSelectionRange
if (!text.setSelectionRange && text.createTextRange) {
text.setSelectionRange = function(selectionStart, selectionEnd) {
var range = this.createTextRange();
range.collapse(true);
range.moveStart('character', selectionStart);
range.moveEnd('character', selectionEnd);
range.select();
};
isAllSelected = function(text) {
try {
var range = text.ownerDocument.selection.createRange();
}catch(e) {}
if (!range || range.parentElement() != text) return false;
return range.text == text.value;
}
}
if (useragent.isOldIE) {
event.addListener(text, "propertychange", function(e){
if (text.value != "" && text.value != PLACEHOLDER)
onInput(e);
});
var keytable = { 13:1, 27:1 };
event.addListener(text, "keyup", function (e) {
if (inCompostion && (!text.value || keytable[e.keyCode]))
setTimeout(onCompositionEnd, 0);
if ((text.value.charCodeAt(0)|0) < 129) {
return;
}
inCompostion ? onCompositionUpdate() : onCompositionStart();
});
}
function resetValue() {
if (inCompostion)
return;
@ -101,17 +128,7 @@ var TextInput = function(parentNode, host) {
var selectionEnd = 2;
// on firefox this throws if textarea is hidden
try {
if (text.setSelectionRange) {
text.setSelectionRange(selectionStart, selectionEnd);
}
// IE8 does not support setSelectionRange
else if (text.createTextRange) {
var range = text.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
text.setSelectionRange(selectionStart, selectionEnd);
} catch(e){}
};
@ -124,7 +141,7 @@ var TextInput = function(parentNode, host) {
copied = false;
return;
}
if (text.selectionStart === 0 && text.selectionEnd === text.value.length) {
if (isAllSelected(text)) {
host.selectAll();
resetSelection();
}
@ -214,7 +231,6 @@ var TextInput = function(parentNode, host) {
}
var clipboardData = e.clipboardData || window.clipboardData;
if (clipboardData) {
// Safari 5 has clipboardData object, but does not handle setData()
var supported = clipboardData.setData("Text", data);
@ -234,8 +250,6 @@ var TextInput = function(parentNode, host) {
host.onCopy();
});
}
};
var onPaste = function(e) {
@ -286,22 +300,7 @@ var TextInput = function(parentNode, host) {
});
}
if (useragent.isOldIE) {
event.addListener(text, "propertychange", function(e){
if (text.value != "" && text.value != PLACEHOLDER)
onInput(e);
});
var keytable = { 13:1, 27:1 };
event.addListener(text, "keyup", function (e) {
if (inCompostion && (!text.value || keytable[e.keyCode]))
setTimeout(onCompositionEnd, 0);
if ((text.value.charCodeAt(0)|0) < 129) {
return;
}
inCompostion ? onCompositionUpdate() : onCompositionStart();
});
}
event.addListener(text, "compositionstart", onCompositionStart);
if (useragent.isGecko) {
@ -315,16 +314,13 @@ var TextInput = function(parentNode, host) {
event.addListener(text, "blur", function() {
host.onBlur();
});
event.addListener(text, "focus", function() {
host.onFocus();
resetSelection();
});
this.focus = function() {
text.focus();
};
this.blur = function() {
text.blur();
};
@ -334,6 +330,7 @@ var TextInput = function(parentNode, host) {
}
this.isFocused = isFocused;
// CONTEXTMENU
this.getElement = function() {
return text;
};
@ -378,11 +375,12 @@ var TextInput = function(parentNode, host) {
this.onContextMenuClose = onContextMenuClose;
// firefox fires contextmenu event after opening it
if (!useragent.isGecko)
if (!useragent.isGecko) {
event.addListener(text, "contextmenu", function(e) {
host.textInput.onContextMenu(e);
onContextMenuClose();
});
}
};
exports.TextInput = TextInput;