/*global _ccsg */ /**************************************************************************** Copyright (c) 2011-2012 cocos2d-x.org Copyright (c) 2013-2014 Chukong Technologies Inc. Copyright (c) 2012 James Chen http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ var capitalize = function(string) { return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); }); }; function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } /** * Enum for keyboard return types * @readonly * @enum cc.EditBox.KeyboardReturnType */ var KeyboardReturnType = cc.Enum({ /** * @property {Number} DEFAULT */ DEFAULT: 0, /** * @property {Number} DONE */ DONE: 1, /** * @property {Number} SEND */ SEND: 2, /** * @property {Number} SEARCH */ SEARCH: 3, /** * @property {Number} GO */ GO: 4 }); /** * The EditBox's InputMode defines the type of text that the user is allowed to enter * @readonly * @enum {number} * @memberof cc.EditBox.InputMode */ var InputMode = cc.Enum({ /** * @property {Number} ANY */ ANY: 0, /** * The user is allowed to enter an e-mail address. * @property {Number} EMAIL_ADDR */ EMAIL_ADDR: 1, /** * The user is allowed to enter an integer value. * @property {Number} NUMERIC */ NUMERIC: 2, /** * The user is allowed to enter a phone number. * @property {Number} PHONE_NUMBER */ PHONE_NUMBER: 3, /** * The user is allowed to enter a URL. * @property {Number} URL */ URL: 4, /** * The user is allowed to enter a real number value. * This extends kEditBoxInputModeNumeric by allowing a decimal point. * @property {Number} DECIMAL */ DECIMAL: 5, /** * The user is allowed to enter any text, except for line breaks. * @property {Number} SINGLE_LINE */ SINGLE_LINE: 6 }); /** * Enum for the EditBox's input flags * @readonly * @enum cc.EditBox.InputFlag */ var InputFlag = cc.Enum({ /** * Indicates that the text entered is confidential data that should be * obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE. * * @property {Number} PASSWORD */ PASSWORD: 0, /** * Indicates that the text entered is sensitive data that the * implementation must never store into a dictionary or table for use * in predictive, auto-completing, or other accelerated input schemes. * A credit card number is an example of sensitive data. * * @property {Number} SENSITIVE */ SENSITIVE: 1, /** * This flag is a hint to the implementation that during text editing, * the initial letter of each word should be capitalized. * * @property {Number} INITIAL_CAPS_WORD */ INITIAL_CAPS_WORD: 2, /** * This flag is a hint to the implementation that during text editing, * the initial letter of each sentence should be capitalized. * * @property {Number} INITIAL_CAPS_SENTENCE */ INITIAL_CAPS_SENTENCE: 3, /** * Capitalize all characters automatically. * * @property {Number} INITIAL_CAPS_ALL_CHARACTERS */ INITIAL_CAPS_ALL_CHARACTERS: 4 }); /** * @class * @extends cc._Class */ cc.EditBoxDelegate = cc._Class.extend({ /** * This method is called when an edit box gains focus after keyboard is shown. * @param {cc.EditBox} sender */ editBoxEditingDidBegan: function (sender) { }, /** * This method is called when an edit box loses focus after keyboard is hidden. * @param {cc.EditBox} sender */ editBoxEditingDidEnded: function (sender) { }, /** * This method is called when the edit box text was changed. * @param {cc.EditBox} sender * @param {String} text */ editBoxTextChanged: function (sender, text) { }, /** * This method is called when the return button was pressed. * @param {cc.EditBox} sender */ editBoxReturn: function (sender) { } }); var EditBoxImpl = function(editBox) { this._domInputSprite = null; this._spriteDOM = null; this._edTxt = null; this._edFontSize = 14; this._edFontName = 'Arial'; this._editBox = editBox; this._textLabel = null; this._placeholderLabel = null; }; EditBoxImpl.prototype = { constructor: EditBoxImpl, createNativeControl: function(size) { var tmpDOMSprite = this._domInputSprite = new _ccsg.Sprite(); tmpDOMSprite.draw = function () {}; //redefine draw function this._editBox.addChild(tmpDOMSprite); this._spriteDOM = tmpDOMSprite; this._createDomTextArea(); cc.DOM.convert(tmpDOMSprite); this._addDomInputControl(); this._domInputSprite.dom.showTooltipDiv = false; this._domInputSprite.dom.style.width = (size.width - 6) + 'px'; this._domInputSprite.dom.style.height = (size.height - 6) + 'px'; tmpDOMSprite.canvas.remove(); this._domInputSprite.x = 3; this._domInputSprite.y = 3; this._createLabels(); }, _createLabels: function () { var editBoxSize = this._editBox.getContentSize(); this._textLabel = new _ccsg.Label(); this._textLabel.setVisible(false); this._textLabel.setAnchorPoint(cc.p(0, 1)); this._textLabel.setOverflow(_ccsg.Label.Overflow.CLAMP); this._editBox.addChild(this._textLabel, 100); this._placeholderLabel = new _ccsg.Label(); this._placeholderLabel.setAnchorPoint(cc.p(0, 1)); this._placeholderLabel.setColor(cc.Color.GRAY); this._editBox.addChild(this._placeholderLabel, 100); this._updateLabelPosition(editBoxSize); }, _updateLabelPosition: function (editBoxSize) { this._textLabel.setContentSize(editBoxSize); this._placeholderLabel.setLineHeight(editBoxSize.height); var placeholderLabelSize = this._placeholderLabel.getContentSize(); if (this._editBox._editBoxInputMode === InputMode.ANY){ this._textLabel.setPosition(cc.p(0, editBoxSize.height)); this._placeholderLabel.setPosition(cc.p(0, editBoxSize.height)); this._placeholderLabel.setVerticalAlign(cc.VerticalTextAlignment.TOP); this._textLabel.setVerticalAlign(cc.VerticalTextAlignment.TOP); this._textLabel.enableWrapText(true); } else { this._textLabel.enableWrapText(false); this._textLabel.setPosition(cc.p(0, editBoxSize.height)); this._placeholderLabel.setPosition(cc.p(0, (editBoxSize.height + placeholderLabelSize.height) / 2)); this._placeholderLabel.setVerticalAlign(cc.VerticalTextAlignment.CENTER); this._textLabel.setVerticalAlign(cc.VerticalTextAlignment.CENTER); } }, setLineHeight: function (lineHeight) { this._textLabel.setLineHeight(lineHeight); }, _hiddenLabels: function () { this._textLabel.setVisible(false); this._placeholderLabel.setVisible(false); }, _updateEditBoxContentStyle: function() { var inputFlag = this._editBox._editBoxInputFlag; if (inputFlag === InputFlag.INITIAL_CAPS_ALL_CHARACTERS) { this._editBox._text = this._editBox._text.toUpperCase(); } else if (inputFlag === InputFlag.INITIAL_CAPS_WORD) { this._editBox._text = capitalize(this._editBox._text); } else if (inputFlag === InputFlag.INITIAL_CAPS_SENTENCE) { this._editBox._text = capitalizeFirstLetter(this._editBox._text); } }, _updateLabelString: function() { this._textLabel.setVisible(true); this._textLabel.setString(this._editBox._text); if (this._edTxt.type === 'password') { var passwordString = ''; var len = this._editBox._text.length; for (var i = 0; i < len; ++i) { passwordString += '\u25CF'; } this._textLabel.setString(passwordString); } else { this._updateEditBoxContentStyle(); this._textLabel.setString(this._editBox._text); } }, _showLabels: function () { this._hiddenLabels(); if (this._edTxt.value === '') { this._placeholderLabel.setVisible(true); this._placeholderLabel.setString(this._editBox._placeholderText); } else { this._updateLabelString(); } }, registerClickEvent: function() { var self = this; var div = self._edTxt.parentNode; div.style.pointerEvents = 'none'; div.addEventListener('click', function() { if (self._edTxt.style.display === 'none') { self._edTxt.style.display = ''; self._edTxt.focus(); self._hiddenLabels(); } }); }, show: function() { var div = this._edTxt.parentNode; div.style.pointerEvents = ''; window.setTimeout(function() { div.click(); }, 10); }, hidden: function() { this._edTxt.style.display = 'none'; var div = this._edTxt.parentNode; div.style.pointerEvents = 'none'; this._showLabels(); }, _setFont: function (fontStyle) { var res = cc.LabelTTF._fontStyleRE.exec(fontStyle); var textFontName = res[2]; var textFontSize = parseInt(res[1]); if (res) { this.setFont(textFontName, textFontSize); } }, setFont: function (fontName, fontSize) { this._edFontName = fontName || this._edFontName; this._edFontSize = fontSize || this._edFontSize; this._updateDOMFontStyle(); }, setFontName: function (fontName) { this._edFontName = fontName || this._edFontName; this._updateDOMFontStyle(); }, setFontSize: function (fontSize) { this._edFontSize = fontSize || this._edFontSize; this._updateDOMFontStyle(); }, setFontColor: function (color) { if (this._edTxt.value !== this._editBox._placeholderText) { this._edTxt.style.color = cc.colorToHex(color); } this._textLabel.setColor(color); }, setPlaceHolder: function (text) { this._placeholderLabel.setString(text); }, setMaxLength: function (maxLength) { this._edTxt.maxLength = maxLength; }, _updateDOMPlaceholderFontStyle: function () { this._placeholderLabel.setFontFileOrFamily(this._editBox._placeholderFontName); this._placeholderLabel.setFontSize(this._editBox._placeholderFontSize); }, setPlaceholderFontColor: function (color) { this._placeholderLabel.setColor(color); }, setInputFlag: function (inputFlag) { if ((this._edTxt.value !== this._placeholderText) && (inputFlag === InputFlag.PASSWORD)) this._edTxt.type = 'password'; else this._edTxt.type = 'text'; this._edTxt.style.textTransform = 'none'; if (inputFlag === InputFlag.INITIAL_CAPS_ALL_CHARACTERS) { this._edTxt.style.textTransform = 'uppercase'; } else if (inputFlag === InputFlag.INITIAL_CAPS_WORD) { this._edTxt.style.textTransform = 'capitalize'; } this._updateLabelString(); }, setInputMode : function (inputMode) { if (inputMode === InputMode.ANY) { this._removeDomInputControl(); this._createDomTextArea(); this._addDomInputControl(); } else if(this._editBox._editBoxInputMode === InputMode.ANY) { this._removeDomInputControl(); this._createDomInput(); this._addDomInputControl(); } }, /** * Set the text entered in the edit box. * @param {string} text The given text. */ setString: function (text) { if (text !== null) { this._edTxt.value = text; if (text === '') { this._edTxt.type = 'text'; this._placeholderLabel.setString(this._editBox._placeholderText); this._placeholderLabel.setColor(this._editBox._placeholderColor); this._placeholderLabel.setVisible(true); this._textLabel.setVisible(false); } else { this._edTxt.style.color = cc.colorToHex(this._editBox._textColor); this._textLabel.setColor(this._editBox._textColor); this._placeholderLabel.setVisible(false); if (this._editBox._editBoxInputFlag === InputFlag.PASSWORD) { this._edTxt.type = 'password'; } else { this._edTxt.type = 'text'; } this._updateLabelString(); } } }, _updateDOMFontStyle: function() { if (this._edTxt.value !== '') { this._edTxt.style.fontFamily = this._edFontName; this._edTxt.style.fontSize = this._edFontSize + 'px'; if (this._editBox._editBoxInputFlag === InputFlag.PASSWORD) this._edTxt.type = 'password'; else this._edTxt.type = 'text'; } this._textLabel.setFontSize(this._edFontSize); this._textLabel.setFontFileOrFamily(this._edFontName); }, setSize: function(newWidth, newHeight) { this._spriteDOM.dom.style.width = (newWidth - 6) + 'px'; this._spriteDOM.dom.style.height = (newHeight - 6) + 'px'; this._updateLabelPosition(cc.size(newWidth, newHeight)); }, _createDomInput: function () { var selfPointer = this; var tmpEdTxt = this._edTxt = document.createElement('input'); tmpEdTxt.type = 'text'; tmpEdTxt.style.fontSize = this._edFontSize + 'px'; tmpEdTxt.style.color = '#000000'; tmpEdTxt.style.border = 0; tmpEdTxt.style.background = 'transparent'; //tmpEdTxt.style.paddingLeft = '2px'; tmpEdTxt.style.width = '100%'; tmpEdTxt.style.height = '100%'; tmpEdTxt.style.active = 0; tmpEdTxt.style.outline = 'medium'; tmpEdTxt.style.padding = '0'; tmpEdTxt.style.textTransform = 'uppercase'; tmpEdTxt.style.display = 'none'; var onCanvasClick = function() { tmpEdTxt.blur();}; tmpEdTxt.addEventListener('input', function () { var editBox = selfPointer._editBox; if (editBox._delegate && editBox._delegate.editBoxTextChanged) { editBox._text = this.value; selfPointer._updateEditBoxContentStyle(); editBox._delegate.editBoxTextChanged(editBox, editBox._text); } }); tmpEdTxt.addEventListener('keypress', function (e) { var editBox = selfPointer._editBox; if (e.keyCode === cc.KEY.enter) { e.stopPropagation(); e.preventDefault(); if(this.value === '') { this.style.fontSize = editBox._placeholderFontSize + 'px'; this.style.color = cc.colorToHex(editBox._placeholderColor); } editBox._text = this.value; selfPointer._updateEditBoxContentStyle(); selfPointer.hidden(); if (editBox._delegate && editBox._delegate.editBoxReturn) { editBox._delegate.editBoxReturn(editBox); } cc._canvas.focus(); } }); tmpEdTxt.addEventListener('focus', function () { var editBox = selfPointer._editBox; this.style.fontSize = selfPointer._edFontSize + 'px'; this.style.color = cc.colorToHex(editBox._textColor); if (editBox._editBoxInputFlag === InputFlag.PASSWORD) { selfPointer._edTxt.type = 'password'; } else { selfPointer._edTxt.type = 'text'; } if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { editBox._delegate.editBoxEditingDidBegan(editBox); } cc._canvas.addEventListener('click', onCanvasClick); }); tmpEdTxt.addEventListener('blur', function () { var editBox = selfPointer._editBox; editBox._text = this.value; selfPointer._updateEditBoxContentStyle(); if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { editBox._delegate.editBoxEditingDidEnded(editBox); } cc._canvas.removeEventListener('click', onCanvasClick); if (this.value === '') { this.style.fontSize = editBox._placeholderFontSize + 'px'; this.style.color = cc.colorToHex(editBox._placeholderColor); } selfPointer.hidden(); }); return tmpEdTxt; }, _createDomTextArea: function () { var selfPointer = this; var tmpEdTxt = this._edTxt = document.createElement('textarea'); tmpEdTxt.type = 'text'; tmpEdTxt.style.fontSize = this._edFontSize + 'px'; tmpEdTxt.style.color = '#000000'; tmpEdTxt.style.border = 0; tmpEdTxt.style.background = 'transparent'; //tmpEdTxt.style.paddingLeft = '2px'; tmpEdTxt.style.width = '100%'; tmpEdTxt.style.height = '100%'; tmpEdTxt.style.active = 0; tmpEdTxt.style.outline = 'medium'; tmpEdTxt.style.padding = '0'; tmpEdTxt.style.resize = 'none'; tmpEdTxt.style.textTransform = 'uppercase'; tmpEdTxt.style.overflow_y = 'scroll'; tmpEdTxt.style.display = 'none'; var onCanvasClick = function() { tmpEdTxt.blur();}; tmpEdTxt.addEventListener('input', function () { var editBox = selfPointer._editBox; if (editBox._delegate && editBox._delegate.editBoxTextChanged) { editBox._text = this.value; selfPointer._updateEditBoxContentStyle(); editBox._delegate.editBoxTextChanged(editBox, editBox._text); } }); tmpEdTxt.addEventListener('focus', function () { var editBox = selfPointer._editBox; this.style.fontSize = selfPointer._edFontSize + 'px'; this.style.color = cc.colorToHex(editBox._textColor); if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { editBox._delegate.editBoxEditingDidBegan(editBox); } cc._canvas.addEventListener('click', onCanvasClick); }); tmpEdTxt.addEventListener('blur', function () { var editBox = selfPointer._editBox; editBox._text = this.value; selfPointer._updateEditBoxContentStyle(); if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { editBox._delegate.editBoxEditingDidEnded(editBox); } if (this.value === '') { this.style.fontSize = editBox._placeholderFontSize + 'px'; this.style.color = cc.colorToHex(editBox._placeholderColor); } selfPointer.hidden(); cc._canvas.removeEventListener('click', onCanvasClick); }); return tmpEdTxt; }, _removeDomInputControl: function() { this._domInputSprite.dom.removeChild(this._edTxt); }, _addDomInputControl: function () { this._domInputSprite.dom.appendChild(this._edTxt); }, }; /** *
cc.EditBox is a brief Class for edit box.
* You can use this widget to gather small amounts of text from the user.
* Sets the maximum input length of the edit box.
* Setting this value enables multiline input mode by default.
*