From cfbaca35f136414e5622d83709d82ea034c03f09 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 17 Jan 2017 16:35:18 +0800 Subject: [PATCH 1/6] adjust editbox position when editing begans --- cocos2d/core/editbox/CCSGEditBox.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index d910d9a50e..9e562f020c 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -49,12 +49,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(); }); }; @@ -653,7 +647,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this.__autoResize = cc.view.__resizeWithBrowserSize; cc.view.resizeWithBrowserSize(false); - scrollWindowUp(editBox); + adjustEditBoxPosition(editBox); }; proto._endEditingOnMobile = function () { From 3d644d0181d192ed3ddb0cd1c6694f690e38c27f Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 17 Jan 2017 18:16:32 +0800 Subject: [PATCH 2/6] refactoring dom input life time --- cocos2d/core/editbox/CCSGEditBox.js | 22 ++++++---------------- cocos2d/core/platform/utils.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 9e562f020c..91bf24dce5 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -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)); @@ -591,17 +592,10 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; var editBox = this._edTxt; if (node.visible) { editBox.style.visibility = 'visible'; - cc.game.container.appendChild(editBox); + this._addDomInputControl(); } 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._removeDomInputControl(); } }; @@ -1176,14 +1170,10 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; proto._removeDomInputControl = 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; }; diff --git a/cocos2d/core/platform/utils.js b/cocos2d/core/platform/utils.js index c00d35095e..0e759616cb 100644 --- a/cocos2d/core/platform/utils.js +++ b/cocos2d/core/platform/utils.js @@ -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, From b2da0ce342a3111ff2d61e18474b8a54a4b539ca Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 18 Jan 2017 10:33:59 +0800 Subject: [PATCH 3/6] refactoring editbox --- cocos2d/core/editbox/CCSGEditBox.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 91bf24dce5..a5254f13f1 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -283,7 +283,7 @@ _ccsg.EditBox = _ccsg.Node.extend({ createDomElementIfNeeded: function () { if(!this._renderCmd._edTxt) { - this._renderCmd.createNativeControl(); + this._renderCmd.createDomTextArea(); } }, @@ -325,7 +325,7 @@ _ccsg.EditBox = _ccsg.Node.extend({ cleanup: function () { this._super(); - this._renderCmd._removeDomInputControl(); + this._renderCmd._removeDomFromGameContainer(); }, _onTouchBegan: function (touch) { @@ -592,10 +592,8 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; var editBox = this._edTxt; if (node.visible) { editBox.style.visibility = 'visible'; - this._addDomInputControl(); } else { editBox.style.visibility = 'hidden'; - this._removeDomInputControl(); } }; @@ -672,7 +670,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'; @@ -764,11 +763,15 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; } thisPointer.hidden(); }); + + this._addDomToGameContainer(); + return tmpEdTxt; }; proto._createDomTextArea = function () { - this._removeDomInputControl(); + this._removeDomFromGameContainer(); + var thisPointer = this; var tmpEdTxt = this._edTxt = document.createElement('textarea'); tmpEdTxt.type = 'text'; @@ -853,6 +856,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; thisPointer.hidden(); }); + this._addDomToGameContainer(); return tmpEdTxt; }; @@ -1089,14 +1093,12 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; }; proto.setInputMode = function (inputMode) { - this._removeDomInputControl(); if (inputMode === InputMode.ANY) { this._createDomTextArea(); } else { this._createDomInput(); } - this._addDomInputControl(); this._updateInputType(); var contentSize = this._node.getContentSize(); @@ -1158,16 +1160,11 @@ _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 = Utils.contains(cc.game.container, editBox); From 3250c28e5e18ca023172f9088415511ba6c68c3b Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 18 Jan 2017 11:04:04 +0800 Subject: [PATCH 4/6] refactoring editbox --- cocos2d/core/editbox/CCSGEditBox.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index a5254f13f1..e3250b0905 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -283,7 +283,7 @@ _ccsg.EditBox = _ccsg.Node.extend({ createDomElementIfNeeded: function () { if(!this._renderCmd._edTxt) { - this._renderCmd.createDomTextArea(); + this._renderCmd._createDomTextArea(); } }, @@ -588,12 +588,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'; + if (this._node.visible) { + this._edTxt.style.visibility = 'visible'; } else { - editBox.style.visibility = 'hidden'; + this._edTxt.style.visibility = 'hidden'; } }; From ffa53fd9d547c70ae9c9d9300e5d28b00c995e9c Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 18 Jan 2017 12:06:09 +0800 Subject: [PATCH 5/6] refactoring --- cocos2d/core/editbox/CCSGEditBox.js | 68 ++++++++++++++--------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index e3250b0905..2847e7ebd1 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -336,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) { @@ -501,6 +501,7 @@ _ccsg.EditBox = _ccsg.Node.extend({ setReturnType: function (returnType) { this._keyboardReturnType = returnType; + this._renderCmd._updateDomInputType(); }, initWithBackgroundColor: function (size, bgColor) { @@ -602,7 +603,8 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; } else { this._createLabels(); this._edTxt.style.display = 'none'; - this._updateLabelString(); + this._showLabels(); + this._updateLabelStringStyle(); } }; @@ -700,7 +702,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); } } @@ -717,8 +719,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); } @@ -744,7 +747,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(); @@ -759,7 +762,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this.style.fontSize = editBox._placeholderFontSize + 'px'; this.style.color = cc.colorToHex(editBox._placeholderColor); } - thisPointer.hidden(); + thisPointer._endEditing(); }); this._addDomToGameContainer(); @@ -800,7 +803,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); } } @@ -836,7 +839,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(); @@ -851,7 +854,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this.style.color = cc.colorToHex(editBox._placeholderColor); } - thisPointer.hidden(); + thisPointer._endEditing(); }); this._addDomToGameContainer(); @@ -926,7 +929,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(); @@ -939,14 +942,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; @@ -957,7 +953,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this._textLabel.setString(passwordString); } } else { - this._updateEditBoxContentStyle(); + this._updateDomTextCases(); if(this._textLabel) { this._textLabel.setString(this._editBox._text); } @@ -973,21 +969,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'; } @@ -1048,11 +1047,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'; @@ -1066,6 +1061,10 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this._edTxt.type = 'url'; } else { this._edTxt.type = 'text'; + + if(this._editBox._keyboardReturnType === KeyboardReturnType.SEARCH) { + this._edTxt.type = 'search'; + } } @@ -1077,7 +1076,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; proto.setInputFlag = function (inputFlag) { if(!this._edTxt) return; - this._updateInputType(); + this._updateDomInputType(); this._edTxt.style.textTransform = 'none'; @@ -1087,7 +1086,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; else if (inputFlag === InputFlag.INITIAL_CAPS_WORD) { this._edTxt.style.textTransform = 'capitalize'; } - this._updateLabelString(); + this._updateLabelStringStyle(); }; proto.setInputMode = function (inputMode) { @@ -1098,7 +1097,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this._createDomInput(); } - this._updateInputType(); + this._updateDomInputType(); var contentSize = this._node.getContentSize(); this.updateSize(contentSize.width, contentSize.height); }; @@ -1113,7 +1112,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) { @@ -1129,7 +1127,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this._placeholderLabel.setVisible(false); } - this._updateLabelString(); + this._updateLabelStringStyle(); } } }; From 2e2fa600aadb83924166f40402ae6a3cfb3e0c26 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 18 Jan 2017 13:48:58 +0800 Subject: [PATCH 6/6] remove unneeded code --- cocos2d/core/editbox/CCSGEditBox.js | 1 - 1 file changed, 1 deletion(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 2847e7ebd1..e3c27f3298 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -604,7 +604,6 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this._createLabels(); this._edTxt.style.display = 'none'; this._showLabels(); - this._updateLabelStringStyle(); } };