mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
Merge pull request #1445 from zilongshanren/refactoring-editbox-v1.4
Refactoring editbox v1.4
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
var SCROLLY = 40;
|
||||
var TIMER_NAME = 400;
|
||||
var LEFT_PADDING = 2;
|
||||
var Utils = require('../cocos2d/core/platform/utils');
|
||||
|
||||
function adjustEditBoxPosition (editBox) {
|
||||
var worldPos = editBox.convertToWorldSpace(cc.p(0,0));
|
||||
@@ -49,12 +50,6 @@ function adjustEditBoxPosition (editBox) {
|
||||
}, TIMER_NAME);
|
||||
}
|
||||
|
||||
function scrollWindowUp(editBox) {
|
||||
if (cc.sys.os === cc.sys.OS_IOS && cc.sys.osMainVersion === 9) {
|
||||
adjustEditBoxPosition(editBox);
|
||||
}
|
||||
}
|
||||
|
||||
var capitalize = function(string) {
|
||||
return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
|
||||
};
|
||||
@@ -288,7 +283,7 @@ _ccsg.EditBox = _ccsg.Node.extend({
|
||||
|
||||
createDomElementIfNeeded: function () {
|
||||
if(!this._renderCmd._edTxt) {
|
||||
this._renderCmd.createNativeControl();
|
||||
this._renderCmd._createDomTextArea();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -330,7 +325,7 @@ _ccsg.EditBox = _ccsg.Node.extend({
|
||||
cleanup: function () {
|
||||
this._super();
|
||||
|
||||
this._renderCmd._removeDomInputControl();
|
||||
this._renderCmd._removeDomFromGameContainer();
|
||||
},
|
||||
|
||||
_onTouchBegan: function (touch) {
|
||||
@@ -341,13 +336,13 @@ _ccsg.EditBox = _ccsg.Node.extend({
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
this._renderCmd.hidden();
|
||||
this._renderCmd._endEditing();
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
_onTouchEnded: function () {
|
||||
this._renderCmd.show();
|
||||
this._renderCmd._beginEditing();
|
||||
},
|
||||
|
||||
_updateBackgroundSpriteSize: function (width, height) {
|
||||
@@ -506,6 +501,7 @@ _ccsg.EditBox = _ccsg.Node.extend({
|
||||
|
||||
setReturnType: function (returnType) {
|
||||
this._keyboardReturnType = returnType;
|
||||
this._renderCmd._updateDomInputType();
|
||||
},
|
||||
|
||||
initWithBackgroundColor: function (size, bgColor) {
|
||||
@@ -593,21 +589,10 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
proto.updateVisibility = function () {
|
||||
if (!this._edTxt) return;
|
||||
|
||||
var node = this._node;
|
||||
var editBox = this._edTxt;
|
||||
if (node.visible) {
|
||||
editBox.style.visibility = 'visible';
|
||||
cc.game.container.appendChild(editBox);
|
||||
if (this._node.visible) {
|
||||
this._edTxt.style.visibility = 'visible';
|
||||
} else {
|
||||
editBox.style.visibility = 'hidden';
|
||||
var hasChild = false;
|
||||
if('contains' in cc.game.container) {
|
||||
hasChild = cc.game.container.contains(editBox);
|
||||
}else {
|
||||
hasChild = cc.game.container.compareDocumentPosition(editBox) % 16;
|
||||
}
|
||||
if(hasChild)
|
||||
cc.game.container.removeChild(editBox);
|
||||
this._edTxt.style.visibility = 'hidden';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -618,7 +603,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
} else {
|
||||
this._createLabels();
|
||||
this._edTxt.style.display = 'none';
|
||||
this._updateLabelString();
|
||||
this._showLabels();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -653,7 +638,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
this.__autoResize = cc.view.__resizeWithBrowserSize;
|
||||
cc.view.resizeWithBrowserSize(false);
|
||||
|
||||
scrollWindowUp(editBox);
|
||||
adjustEditBoxPosition(editBox);
|
||||
};
|
||||
|
||||
proto._endEditingOnMobile = function () {
|
||||
@@ -684,7 +669,8 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
|
||||
|
||||
proto._createDomInput = function () {
|
||||
this._removeDomInputControl();
|
||||
this._removeDomFromGameContainer();
|
||||
|
||||
var thisPointer = this;
|
||||
var tmpEdTxt = this._edTxt = document.createElement('input');
|
||||
tmpEdTxt.type = 'text';
|
||||
@@ -715,7 +701,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
if (editBox._delegate && editBox._delegate.editBoxTextChanged) {
|
||||
if (editBox._text.toLowerCase() !== this.value.toLowerCase()) {
|
||||
editBox._text = this.value;
|
||||
thisPointer._updateEditBoxContentStyle();
|
||||
thisPointer._updateDomTextCases();
|
||||
editBox._delegate.editBoxTextChanged(editBox, editBox._text);
|
||||
}
|
||||
}
|
||||
@@ -732,8 +718,9 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
}
|
||||
|
||||
editBox._text = this.value;
|
||||
thisPointer._updateEditBoxContentStyle();
|
||||
thisPointer.hidden();
|
||||
thisPointer._updateDomTextCases();
|
||||
|
||||
thisPointer._endEditing();
|
||||
if (editBox._delegate && editBox._delegate.editBoxEditingReturn) {
|
||||
editBox._delegate.editBoxEditingReturn(editBox);
|
||||
}
|
||||
@@ -759,7 +746,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
tmpEdTxt.addEventListener('blur', function () {
|
||||
var editBox = thisPointer._editBox;
|
||||
editBox._text = this.value;
|
||||
thisPointer._updateEditBoxContentStyle();
|
||||
thisPointer._updateDomTextCases();
|
||||
|
||||
if (cc.sys.isMobile) {
|
||||
thisPointer._endEditingOnMobile();
|
||||
@@ -774,13 +761,17 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
this.style.fontSize = editBox._placeholderFontSize + 'px';
|
||||
this.style.color = cc.colorToHex(editBox._placeholderColor);
|
||||
}
|
||||
thisPointer.hidden();
|
||||
thisPointer._endEditing();
|
||||
});
|
||||
|
||||
this._addDomToGameContainer();
|
||||
|
||||
return tmpEdTxt;
|
||||
};
|
||||
|
||||
proto._createDomTextArea = function () {
|
||||
this._removeDomInputControl();
|
||||
this._removeDomFromGameContainer();
|
||||
|
||||
var thisPointer = this;
|
||||
var tmpEdTxt = this._edTxt = document.createElement('textarea');
|
||||
tmpEdTxt.type = 'text';
|
||||
@@ -811,7 +802,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
if (editBox._delegate && editBox._delegate.editBoxTextChanged) {
|
||||
if(editBox._text.toLowerCase() !== this.value.toLowerCase()) {
|
||||
editBox._text = this.value;
|
||||
thisPointer._updateEditBoxContentStyle();
|
||||
thisPointer._updateDomTextCases();
|
||||
editBox._delegate.editBoxTextChanged(editBox, editBox._text);
|
||||
}
|
||||
}
|
||||
@@ -847,7 +838,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
tmpEdTxt.addEventListener('blur', function () {
|
||||
var editBox = thisPointer._editBox;
|
||||
editBox._text = this.value;
|
||||
thisPointer._updateEditBoxContentStyle();
|
||||
thisPointer._updateDomTextCases();
|
||||
|
||||
if (cc.sys.isMobile) {
|
||||
thisPointer._endEditingOnMobile();
|
||||
@@ -862,9 +853,10 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
this.style.color = cc.colorToHex(editBox._placeholderColor);
|
||||
}
|
||||
|
||||
thisPointer.hidden();
|
||||
thisPointer._endEditing();
|
||||
});
|
||||
|
||||
this._addDomToGameContainer();
|
||||
return tmpEdTxt;
|
||||
};
|
||||
|
||||
@@ -936,7 +928,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
}
|
||||
};
|
||||
|
||||
proto._updateEditBoxContentStyle = function() {
|
||||
proto._updateDomTextCases = function() {
|
||||
var inputFlag = this._editBox._editBoxInputFlag;
|
||||
if (inputFlag === InputFlag.INITIAL_CAPS_ALL_CHARACTERS) {
|
||||
this._editBox._text = this._editBox._text.toUpperCase();
|
||||
@@ -949,14 +941,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
}
|
||||
};
|
||||
|
||||
proto._updateLabelString = function() {
|
||||
this._updateInputType();
|
||||
|
||||
if(this._textLabel) {
|
||||
this._textLabel.setVisible(true);
|
||||
this._textLabel.setString(this._editBox._text);
|
||||
}
|
||||
|
||||
proto._updateLabelStringStyle = function() {
|
||||
if (this._edTxt.type === 'password') {
|
||||
var passwordString = '';
|
||||
var len = this._editBox._text.length;
|
||||
@@ -967,7 +952,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
this._textLabel.setString(passwordString);
|
||||
}
|
||||
} else {
|
||||
this._updateEditBoxContentStyle();
|
||||
this._updateDomTextCases();
|
||||
if(this._textLabel) {
|
||||
this._textLabel.setString(this._editBox._text);
|
||||
}
|
||||
@@ -983,21 +968,24 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._updateLabelString();
|
||||
if(this._textLabel) {
|
||||
this._textLabel.setVisible(true);
|
||||
this._textLabel.setString(this._editBox._text);
|
||||
}
|
||||
}
|
||||
this._updateLabelStringStyle();
|
||||
};
|
||||
|
||||
proto.show = function() {
|
||||
proto._beginEditing = function() {
|
||||
if(!this._editBox._alwaysOnTop) {
|
||||
if (this._edTxt.style.display === 'none') {
|
||||
this._edTxt.style.display = '';
|
||||
this._edTxt.focus();
|
||||
}
|
||||
}
|
||||
this._hiddenLabels();
|
||||
};
|
||||
|
||||
proto.hidden = function() {
|
||||
proto._endEditing = function() {
|
||||
if(!this._editBox._alwaysOnTop) {
|
||||
this._edTxt.style.display = 'none';
|
||||
}
|
||||
@@ -1058,11 +1046,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
this._placeholderLabel.setColor(color);
|
||||
};
|
||||
|
||||
proto._updateInputType = function () {
|
||||
if(this._editBox._keyboardReturnType === KeyboardReturnType.SEARCH) {
|
||||
this._edTxt.type = 'search';
|
||||
}
|
||||
|
||||
proto._updateDomInputType = function () {
|
||||
var inputMode = this._editBox._editBoxInputMode;
|
||||
if(inputMode === InputMode.EMAIL_ADDR) {
|
||||
this._edTxt.type = 'email';
|
||||
@@ -1076,6 +1060,10 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
this._edTxt.type = 'url';
|
||||
} else {
|
||||
this._edTxt.type = 'text';
|
||||
|
||||
if(this._editBox._keyboardReturnType === KeyboardReturnType.SEARCH) {
|
||||
this._edTxt.type = 'search';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1087,7 +1075,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
proto.setInputFlag = function (inputFlag) {
|
||||
if(!this._edTxt) return;
|
||||
|
||||
this._updateInputType();
|
||||
this._updateDomInputType();
|
||||
|
||||
this._edTxt.style.textTransform = 'none';
|
||||
|
||||
@@ -1097,20 +1085,18 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
else if (inputFlag === InputFlag.INITIAL_CAPS_WORD) {
|
||||
this._edTxt.style.textTransform = 'capitalize';
|
||||
}
|
||||
this._updateLabelString();
|
||||
this._updateLabelStringStyle();
|
||||
};
|
||||
|
||||
proto.setInputMode = function (inputMode) {
|
||||
this._removeDomInputControl();
|
||||
if (inputMode === InputMode.ANY) {
|
||||
this._createDomTextArea();
|
||||
}
|
||||
else {
|
||||
this._createDomInput();
|
||||
}
|
||||
this._addDomInputControl();
|
||||
|
||||
this._updateInputType();
|
||||
this._updateDomInputType();
|
||||
var contentSize = this._node.getContentSize();
|
||||
this.updateSize(contentSize.width, contentSize.height);
|
||||
};
|
||||
@@ -1125,7 +1111,6 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
if(this._placeholderLabel) {
|
||||
this._placeholderLabel.setString(this._editBox._placeholderText);
|
||||
this._placeholderLabel.setColor(this._editBox._placeholderColor);
|
||||
this._placeholderLabel.setVisible(true);
|
||||
}
|
||||
|
||||
if(this._textLabel) {
|
||||
@@ -1141,7 +1126,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
this._placeholderLabel.setVisible(false);
|
||||
}
|
||||
|
||||
this._updateLabelString();
|
||||
this._updateLabelStringStyle();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1170,26 +1155,17 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;
|
||||
this._updateLabelPosition(cc.size(newWidth, newHeight));
|
||||
};
|
||||
|
||||
proto.createNativeControl = function() {
|
||||
this._createDomTextArea();
|
||||
this._addDomInputControl();
|
||||
};
|
||||
|
||||
proto._addDomInputControl = function () {
|
||||
proto._addDomToGameContainer = function () {
|
||||
cc.game.container.appendChild(this._edTxt);
|
||||
};
|
||||
|
||||
proto._removeDomInputControl = function () {
|
||||
proto._removeDomFromGameContainer = function () {
|
||||
var editBox = this._edTxt;
|
||||
if(editBox){
|
||||
var hasChild = false;
|
||||
if('contains' in cc.game.container) {
|
||||
hasChild = cc.game.container.contains(editBox);
|
||||
}else {
|
||||
hasChild = cc.game.container.compareDocumentPosition(editBox) % 16;
|
||||
}
|
||||
if(hasChild)
|
||||
var hasChild = Utils.contains(cc.game.container, editBox);
|
||||
if(hasChild) {
|
||||
cc.game.container.removeChild(editBox);
|
||||
}
|
||||
}
|
||||
this._edTxt = null;
|
||||
};
|
||||
|
||||
@@ -26,6 +26,24 @@
|
||||
// TODO - merge with misc.js
|
||||
|
||||
module.exports = {
|
||||
contains: function (refNode, otherNode) {
|
||||
if(typeof refNode.contains == 'function'){
|
||||
return refNode.contains(otherNode);
|
||||
}else if(typeof refNode.compareDocumentPosition == 'function' ) {
|
||||
return !!(refNode.compareDocumentPosition(otherNode) & 16);
|
||||
}else {
|
||||
var node = otherNode.parentNode;
|
||||
do {
|
||||
if(node === refNode){
|
||||
return true;
|
||||
}else{
|
||||
node = node.parentNode;
|
||||
}
|
||||
}while(node !==null);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
isDomNode: typeof window === 'object' && (typeof Node === 'function' ?
|
||||
function (obj) {
|
||||
// If "TypeError: Right-hand side of 'instanceof' is not callback" is thrown,
|
||||
|
||||
Reference in New Issue
Block a user