Skip to content

Commit 21a9afd

Browse files
committed
Update CCEditBox.js
1 parent 9f65531 commit 21a9afd

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

extensions/editbox/CCEditBox.js

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ cc.EditBox = cc.ControlButton.extend({
245245
var tmpDOMSprite = this._domInputSprite = new cc.Sprite();
246246
tmpDOMSprite.draw = function () {}; //redefine draw function
247247
this.addChild(tmpDOMSprite);
248-
var selfPointer = this;
249248
var tmpEdTxt = this._edTxt = document.createElement("input");
250249
tmpEdTxt.type = "text";
251250
tmpEdTxt.style.fontSize = this._edFontSize + "px";
@@ -258,47 +257,56 @@ cc.EditBox = cc.ControlButton.extend({
258257
tmpEdTxt.style.active = 0;
259258
tmpEdTxt.style.outline = "medium";
260259
tmpEdTxt.style.padding = "0";
261-
var onCanvasClick = function() { tmpEdTxt.blur();};
260+
var onCanvasClick = function() { this._edTxt.blur();};
261+
this._onCanvasClick = onCanvasClick.bind(this);
262262

263-
// TODO the event listener will be remove when EditBox removes from parent.
264-
tmpEdTxt.addEventListener("input", function () {
265-
if (selfPointer._delegate && selfPointer._delegate.editBoxTextChanged)
266-
selfPointer._delegate.editBoxTextChanged(selfPointer, this.value);
267-
});
268-
tmpEdTxt.addEventListener("keypress", function (e) {
263+
var inputEvent = function () {
264+
if (this._delegate && this._delegate.editBoxTextChanged)
265+
this._delegate.editBoxTextChanged(this, this._edTxt.value);
266+
};
267+
this._inputEvent = inputEvent.bind(this);
268+
var keypressEvent = function ( e ) {
269269
if (e.keyCode === cc.KEY.enter) {
270270
e.stopPropagation();
271271
e.preventDefault();
272-
if (selfPointer._delegate && selfPointer._delegate.editBoxReturn)
273-
selfPointer._delegate.editBoxReturn(selfPointer);
272+
if (this._delegate && this._delegate.editBoxReturn)
273+
this._delegate.editBoxReturn(this);
274274
cc._canvas.focus();
275275
}
276-
});
277-
tmpEdTxt.addEventListener("focus", function () {
278-
if (this.value === selfPointer._placeholderText) {
279-
this.value = "";
280-
this.style.fontSize = selfPointer._edFontSize + "px";
281-
this.style.color = cc.colorToHex(selfPointer._textColor);
282-
if (selfPointer._editBoxInputFlag === cc.EDITBOX_INPUT_FLAG_PASSWORD)
283-
selfPointer._edTxt.type = "password";
276+
};
277+
this._keyPressEvent = keypressEvent.bind(this);
278+
var focusEvent = function () {
279+
if (this._edTxt.value === this._placeholderText) {
280+
this._edTxt.value = "";
281+
this._edTxt.style.fontSize = this._edFontSize + "px";
282+
this._edTxt.style.color = cc.colorToHex(this._textColor);
283+
if (this._editBoxInputFlag === cc.EDITBOX_INPUT_FLAG_PASSWORD)
284+
this._edTxt.type = "password";
284285
else
285-
selfPointer._edTxt.type = "text";
286+
this._edTxt.type = "text";
286287
}
287-
if (selfPointer._delegate && selfPointer._delegate.editBoxEditingDidBegin)
288-
selfPointer._delegate.editBoxEditingDidBegin(selfPointer);
289-
cc._canvas.addEventListener("click", onCanvasClick);
290-
});
291-
tmpEdTxt.addEventListener("blur", function () {
292-
if (this.value === "") {
293-
this.value = selfPointer._placeholderText;
294-
this.style.fontSize = selfPointer._placeholderFontSize + "px";
295-
this.style.color = cc.colorToHex(selfPointer._placeholderColor);
296-
selfPointer._edTxt.type = "text";
288+
if (this._delegate && this._delegate.editBoxEditingDidBegin)
289+
this._delegate.editBoxEditingDidBegin(this);
290+
cc._canvas.addEventListener("click", this._onCanvasClick);
291+
};
292+
this._focusEvent = focusEvent.bind(this);
293+
var blurEvent = function () {
294+
if (this._edTxt.value === "") {
295+
this._edTxt.value = this._placeholderText;
296+
this._edTxt.style.fontSize = this._placeholderFontSize + "px";
297+
this._edTxt.style.color = cc.colorToHex(this._placeholderColor);
298+
this._edTxt.type = "text";
297299
}
298-
if (selfPointer._delegate && selfPointer._delegate.editBoxEditingDidEnd)
299-
selfPointer._delegate.editBoxEditingDidEnd(selfPointer);
300-
cc._canvas.removeEventListener('click', onCanvasClick);
301-
});
300+
if (this._delegate && this._delegate.editBoxEditingDidEnd)
301+
this._delegate.editBoxEditingDidEnd(this);
302+
cc._canvas.removeEventListener('click', this._onCanvasClick);
303+
};
304+
this._blurEvent = blurEvent.bind(this);
305+
306+
tmpEdTxt.addEventListener("input", this._inputEvent);
307+
tmpEdTxt.addEventListener("keypress", this._keyPressEvent);
308+
tmpEdTxt.addEventListener("focus", this._focusEvent);
309+
tmpEdTxt.addEventListener("blur", this._blurEvent);
302310

303311
cc.DOM.convert(tmpDOMSprite);
304312
tmpDOMSprite.dom.appendChild(tmpEdTxt);
@@ -632,7 +640,23 @@ cc.EditBox = cc.ControlButton.extend({
632640
this._edHeight = size.height;
633641
this.dom.style.height = this._edHeight.toString() + "px";
634642
this.dom.style.backgroundColor = cc.colorToHex(bgColor);
635-
}
643+
},
644+
645+
cleanup : function () {
646+
this._edTxt.removeEventListener("input", this._inputEvent);
647+
this._edTxt.removeEventListener("keypress", this._keyPressEvent);
648+
this._edTxt.removeEventListener("focus", this._focusEvent);
649+
this._edTxt.removeEventListener("blur", this._blurEvent);
650+
cc._canvas.removeEventListener('click', this._onCanvasClick);
651+
652+
this._super();
653+
},
654+
655+
_onCanvasClick : null,
656+
_inputEvent : null,
657+
_keyPressEvent : null,
658+
_focusEvent : null,
659+
_blurEvent : null
636660
});
637661

638662
var _p = cc.EditBox.prototype;

0 commit comments

Comments
 (0)