cleanup
This commit is contained in:
parent
361c52a16a
commit
3565a06191
1 changed files with 47 additions and 49 deletions
|
|
@ -55,28 +55,55 @@ var TextInput = function(parentNode, host) {
|
||||||
|
|
||||||
var PLACEHOLDER = "\x01\x01";
|
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 cut = false
|
||||||
var copied = false;
|
var copied = false;
|
||||||
var pasted = false;
|
var pasted = false;
|
||||||
|
|
||||||
var inCompostion = false;
|
var inCompostion = false;
|
||||||
|
|
||||||
var resetTimeout = null;
|
var resetTimeout = null;
|
||||||
|
|
||||||
var tempStyle = '';
|
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() {
|
function resetValue() {
|
||||||
if (inCompostion)
|
if (inCompostion)
|
||||||
return;
|
return;
|
||||||
|
|
@ -101,17 +128,7 @@ var TextInput = function(parentNode, host) {
|
||||||
var selectionEnd = 2;
|
var selectionEnd = 2;
|
||||||
// on firefox this throws if textarea is hidden
|
// on firefox this throws if textarea is hidden
|
||||||
try {
|
try {
|
||||||
if (text.setSelectionRange) {
|
text.setSelectionRange(selectionStart, selectionEnd);
|
||||||
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();
|
|
||||||
}
|
|
||||||
} catch(e){}
|
} catch(e){}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -124,7 +141,7 @@ var TextInput = function(parentNode, host) {
|
||||||
copied = false;
|
copied = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (text.selectionStart === 0 && text.selectionEnd === text.value.length) {
|
if (isAllSelected(text)) {
|
||||||
host.selectAll();
|
host.selectAll();
|
||||||
resetSelection();
|
resetSelection();
|
||||||
}
|
}
|
||||||
|
|
@ -214,7 +231,6 @@ var TextInput = function(parentNode, host) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var clipboardData = e.clipboardData || window.clipboardData;
|
var clipboardData = e.clipboardData || window.clipboardData;
|
||||||
|
|
||||||
if (clipboardData) {
|
if (clipboardData) {
|
||||||
// Safari 5 has clipboardData object, but does not handle setData()
|
// Safari 5 has clipboardData object, but does not handle setData()
|
||||||
var supported = clipboardData.setData("Text", data);
|
var supported = clipboardData.setData("Text", data);
|
||||||
|
|
@ -234,8 +250,6 @@ var TextInput = function(parentNode, host) {
|
||||||
host.onCopy();
|
host.onCopy();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var onPaste = function(e) {
|
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);
|
event.addListener(text, "compositionstart", onCompositionStart);
|
||||||
if (useragent.isGecko) {
|
if (useragent.isGecko) {
|
||||||
|
|
@ -315,16 +314,13 @@ var TextInput = function(parentNode, host) {
|
||||||
event.addListener(text, "blur", function() {
|
event.addListener(text, "blur", function() {
|
||||||
host.onBlur();
|
host.onBlur();
|
||||||
});
|
});
|
||||||
|
|
||||||
event.addListener(text, "focus", function() {
|
event.addListener(text, "focus", function() {
|
||||||
host.onFocus();
|
host.onFocus();
|
||||||
resetSelection();
|
resetSelection();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.focus = function() {
|
this.focus = function() {
|
||||||
text.focus();
|
text.focus();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.blur = function() {
|
this.blur = function() {
|
||||||
text.blur();
|
text.blur();
|
||||||
};
|
};
|
||||||
|
|
@ -334,6 +330,7 @@ var TextInput = function(parentNode, host) {
|
||||||
}
|
}
|
||||||
this.isFocused = isFocused;
|
this.isFocused = isFocused;
|
||||||
|
|
||||||
|
// CONTEXTMENU
|
||||||
this.getElement = function() {
|
this.getElement = function() {
|
||||||
return text;
|
return text;
|
||||||
};
|
};
|
||||||
|
|
@ -378,11 +375,12 @@ var TextInput = function(parentNode, host) {
|
||||||
this.onContextMenuClose = onContextMenuClose;
|
this.onContextMenuClose = onContextMenuClose;
|
||||||
|
|
||||||
// firefox fires contextmenu event after opening it
|
// firefox fires contextmenu event after opening it
|
||||||
if (!useragent.isGecko)
|
if (!useragent.isGecko) {
|
||||||
event.addListener(text, "contextmenu", function(e) {
|
event.addListener(text, "contextmenu", function(e) {
|
||||||
host.textInput.onContextMenu(e);
|
host.textInput.onContextMenu(e);
|
||||||
onContextMenuClose();
|
onContextMenuClose();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.TextInput = TextInput;
|
exports.TextInput = TextInput;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue