Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / source / widgets / form / TextField.js
diff --git a/source/widgets/form/TextField.js b/source/widgets/form/TextField.js
deleted file mode 100644 (file)
index 6788e5e..0000000
+++ /dev/null
@@ -1,405 +0,0 @@
-/*\r
- * Ext JS Library 2.2.1\r
- * Copyright(c) 2006-2009, Ext JS, LLC.\r
- * licensing@extjs.com\r
- * \r
- * http://extjs.com/license\r
- */\r
-\r
-/**\r
- * @class Ext.form.TextField\r
- * @extends Ext.form.Field\r
- * Basic text field.  Can be used as a direct replacement for traditional text inputs, or as the base\r
- * class for more sophisticated input controls (like {@link Ext.form.TextArea} and {@link Ext.form.ComboBox}).\r
- * @constructor\r
- * Creates a new TextField\r
- * @param {Object} config Configuration options\r
- */\r
-Ext.form.TextField = Ext.extend(Ext.form.Field,  {\r
-    /**\r
-     * @cfg {String} vtypeText A custom error message to display in place of the default message provided\r
-     * for the {@link #vtype} currently set for this field (defaults to '').  Only applies if vtype is set, else ignored.\r
-     */\r
-    /**\r
-     * @cfg {RegExp} stripCharsRe A JavaScript RegExp object used to strip unwanted content from the value before validation (defaults to null).\r
-     */\r
-    /**\r
-     * @cfg {Boolean} grow True if this field should automatically grow and shrink to its content\r
-     */\r
-    grow : false,\r
-    /**\r
-     * @cfg {Number} growMin The minimum width to allow when grow = true (defaults to 30)\r
-     */\r
-    growMin : 30,\r
-    /**\r
-     * @cfg {Number} growMax The maximum width to allow when grow = true (defaults to 800)\r
-     */\r
-    growMax : 800,\r
-    /**\r
-     * @cfg {String} vtype A validation type name as defined in {@link Ext.form.VTypes} (defaults to null)\r
-     */\r
-    vtype : null,\r
-    /**\r
-     * @cfg {RegExp} maskRe An input mask regular expression that will be used to filter keystrokes that don't match\r
-     * (defaults to null)\r
-     */\r
-    maskRe : null,\r
-    /**\r
-     * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)\r
-     */\r
-    disableKeyFilter : false,\r
-    /**\r
-     * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)\r
-     */\r
-    allowBlank : true,\r
-    /**\r
-     * @cfg {Number} minLength Minimum input field length required (defaults to 0)\r
-     */\r
-    minLength : 0,\r
-    /**\r
-     * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)\r
-     */\r
-    maxLength : Number.MAX_VALUE,\r
-    /**\r
-     * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to\r
-     * "The minimum length for this field is {minLength}")\r
-     */\r
-    minLengthText : "The minimum length for this field is {0}",\r
-    /**\r
-     * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to\r
-     * "The maximum length for this field is {maxLength}")\r
-     */\r
-    maxLengthText : "The maximum length for this field is {0}",\r
-    /**\r
-     * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives\r
-     * input focus (defaults to false)\r
-     */\r
-    selectOnFocus : false,\r
-    /**\r
-     * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")\r
-     */\r
-    blankText : "This field is required",\r
-    /**\r
-     * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).\r
-     * If specified, this function will be called only after the built-in validations ({@link #allowBlank}, {@link #minLength},\r
-     * {@link #maxLength}) and any configured {@link #vtype} all return true. This function will be passed the current field\r
-     * value and expected to return boolean true if the value is valid or a string error message if invalid.\r
-     */\r
-    validator : null,\r
-    /**\r
-     * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).\r
-     * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the\r
-     * current field value.  If the test fails, the field will be marked invalid using {@link #regexText}.\r
-     */\r
-    regex : null,\r
-    /**\r
-     * @cfg {String} regexText The error text to display if {@link #regex} is used and the test fails during\r
-     * validation (defaults to "")\r
-     */\r
-    regexText : "",\r
-    /**\r
-     * @cfg {String} emptyText The default text to place into an empty field (defaults to null). Note that this\r
-     * value will be submitted to the server if this field is enabled and configured with a {@link #name}.\r
-     */\r
-    emptyText : null,\r
-    /**\r
-     * @cfg {String} emptyClass The CSS class to apply to an empty field to style the {@link #emptyText} (defaults to\r
-     * 'x-form-empty-field').  This class is automatically added and removed as needed depending on the current field value.\r
-     */\r
-    emptyClass : 'x-form-empty-field',\r
-\r
-    /**\r
-     * @cfg {Boolean} enableKeyEvents True to enable the proxying of key events for the HTML input field (defaults to false)\r
-     */\r
-\r
-    initComponent : function(){\r
-        Ext.form.TextField.superclass.initComponent.call(this);\r
-        this.addEvents(\r
-            /**\r
-             * @event autosize\r
-             * Fires when the autosize function is triggered.  The field may or may not have actually changed size\r
-             * according to the default logic, but this event provides a hook for the developer to apply additional\r
-             * logic at runtime to resize the field if needed.\r
-             * @param {Ext.form.Field} this This text field\r
-             * @param {Number} width The new field width\r
-             */\r
-            'autosize',\r
-\r
-            /**\r
-             * @event keydown\r
-             * Keydown input field event. This event only fires if enableKeyEvents is set to true.\r
-             * @param {Ext.form.TextField} this This text field\r
-             * @param {Ext.EventObject} e\r
-             */\r
-            'keydown',\r
-            /**\r
-             * @event keyup\r
-             * Keyup input field event. This event only fires if enableKeyEvents is set to true.\r
-             * @param {Ext.form.TextField} this This text field\r
-             * @param {Ext.EventObject} e\r
-             */\r
-            'keyup',\r
-            /**\r
-             * @event keypress\r
-             * Keypress input field event. This event only fires if enableKeyEvents is set to true.\r
-             * @param {Ext.form.TextField} this This text field\r
-             * @param {Ext.EventObject} e\r
-             */\r
-            'keypress'\r
-        );\r
-    },\r
-\r
-    // private\r
-    initEvents : function(){\r
-        Ext.form.TextField.superclass.initEvents.call(this);\r
-        if(this.validationEvent == 'keyup'){\r
-            this.validationTask = new Ext.util.DelayedTask(this.validate, this);\r
-            this.el.on('keyup', this.filterValidation, this);\r
-        }\r
-        else if(this.validationEvent !== false){\r
-            this.el.on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});\r
-        }\r
-        if(this.selectOnFocus || this.emptyText){\r
-            this.on("focus", this.preFocus, this);\r
-            this.el.on('mousedown', function(){\r
-                if(!this.hasFocus){\r
-                    this.el.on('mouseup', function(e){\r
-                        e.preventDefault();\r
-                    }, this, {single:true});\r
-                }\r
-            }, this);\r
-            if(this.emptyText){\r
-                this.on('blur', this.postBlur, this);\r
-                this.applyEmptyText();\r
-            }\r
-        }\r
-        if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Ext.form.VTypes[this.vtype+'Mask']))){\r
-            this.el.on("keypress", this.filterKeys, this);\r
-        }\r
-        if(this.grow){\r
-            this.el.on("keyup", this.onKeyUpBuffered,  this, {buffer:50});\r
-            this.el.on("click", this.autoSize,  this);\r
-        }\r
-\r
-        if(this.enableKeyEvents){\r
-            this.el.on("keyup", this.onKeyUp, this);\r
-            this.el.on("keydown", this.onKeyDown, this);\r
-            this.el.on("keypress", this.onKeyPress, this);\r
-        }\r
-    },\r
-\r
-    processValue : function(value){\r
-        if(this.stripCharsRe){\r
-            var newValue = value.replace(this.stripCharsRe, '');\r
-            if(newValue !== value){\r
-                this.setRawValue(newValue);\r
-                return newValue;\r
-            }\r
-        }\r
-        return value;\r
-    },\r
-\r
-    filterValidation : function(e){\r
-        if(!e.isNavKeyPress()){\r
-            this.validationTask.delay(this.validationDelay);\r
-        }\r
-    },\r
-    \r
-    //private\r
-    onDisable: function(){\r
-        Ext.form.TextField.superclass.onDisable.call(this);\r
-        if(Ext.isIE){\r
-            this.el.dom.unselectable = 'on';\r
-        }\r
-    },\r
-    \r
-    //private\r
-    onEnable: function(){\r
-        Ext.form.TextField.superclass.onEnable.call(this);\r
-        if(Ext.isIE){\r
-            this.el.dom.unselectable = '';\r
-        }\r
-    },\r
-\r
-    // private\r
-    onKeyUpBuffered : function(e){\r
-        if(!e.isNavKeyPress()){\r
-            this.autoSize();\r
-        }\r
-    },\r
-\r
-    // private\r
-    onKeyUp : function(e){\r
-        this.fireEvent('keyup', this, e);\r
-    },\r
-\r
-    // private\r
-    onKeyDown : function(e){\r
-        this.fireEvent('keydown', this, e);\r
-    },\r
-\r
-    // private\r
-    onKeyPress : function(e){\r
-        this.fireEvent('keypress', this, e);\r
-    },\r
-\r
-    /**\r
-     * Resets the current field value to the originally-loaded value and clears any validation messages.\r
-     * Also adds emptyText and emptyClass if the original value was blank.\r
-     */\r
-    reset : function(){\r
-        Ext.form.TextField.superclass.reset.call(this);\r
-        this.applyEmptyText();\r
-    },\r
-\r
-    applyEmptyText : function(){\r
-        if(this.rendered && this.emptyText && this.getRawValue().length < 1 && !this.hasFocus){\r
-            this.setRawValue(this.emptyText);\r
-            this.el.addClass(this.emptyClass);\r
-        }\r
-    },\r
-\r
-    // private\r
-    preFocus : function(){\r
-        if(this.emptyText){\r
-            if(this.el.dom.value == this.emptyText){\r
-                this.setRawValue('');\r
-            }\r
-            this.el.removeClass(this.emptyClass);\r
-        }\r
-        if(this.selectOnFocus){\r
-            this.el.dom.select();\r
-        }\r
-    },\r
-\r
-    // private\r
-    postBlur : function(){\r
-        this.applyEmptyText();\r
-    },\r
-\r
-    // private\r
-    filterKeys : function(e){\r
-        if(e.ctrlKey){\r
-            return;\r
-        }\r
-        var k = e.getKey();\r
-        if(Ext.isGecko && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){\r
-            return;\r
-        }\r
-        var c = e.getCharCode(), cc = String.fromCharCode(c);\r
-        if(!Ext.isGecko && e.isSpecialKey() && !cc){\r
-            return;\r
-        }\r
-        if(!this.maskRe.test(cc)){\r
-            e.stopEvent();\r
-        }\r
-    },\r
-\r
-    setValue : function(v){\r
-        if(this.emptyText && this.el && v !== undefined && v !== null && v !== ''){\r
-            this.el.removeClass(this.emptyClass);\r
-        }\r
-        Ext.form.TextField.superclass.setValue.apply(this, arguments);\r
-        this.applyEmptyText();\r
-        this.autoSize();\r
-    },\r
-\r
-    /**\r
-     * Validates a value according to the field's validation rules and marks the field as invalid\r
-     * if the validation fails\r
-     * @param {Mixed} value The value to validate\r
-     * @return {Boolean} True if the value is valid, else false\r
-     */\r
-    validateValue : function(value){\r
-        if(value.length < 1 || value === this.emptyText){ // if it's blank\r
-             if(this.allowBlank){\r
-                 this.clearInvalid();\r
-                 return true;\r
-             }else{\r
-                 this.markInvalid(this.blankText);\r
-                 return false;\r
-             }\r
-        }\r
-        if(value.length < this.minLength){\r
-            this.markInvalid(String.format(this.minLengthText, this.minLength));\r
-            return false;\r
-        }\r
-        if(value.length > this.maxLength){\r
-            this.markInvalid(String.format(this.maxLengthText, this.maxLength));\r
-            return false;\r
-        }\r
-        if(this.vtype){\r
-            var vt = Ext.form.VTypes;\r
-            if(!vt[this.vtype](value, this)){\r
-                this.markInvalid(this.vtypeText || vt[this.vtype +'Text']);\r
-                return false;\r
-            }\r
-        }\r
-        if(typeof this.validator == "function"){\r
-            var msg = this.validator(value);\r
-            if(msg !== true){\r
-                this.markInvalid(msg);\r
-                return false;\r
-            }\r
-        }\r
-        if(this.regex && !this.regex.test(value)){\r
-            this.markInvalid(this.regexText);\r
-            return false;\r
-        }\r
-        return true;\r
-    },\r
-\r
-    /**\r
-     * Selects text in this field\r
-     * @param {Number} start (optional) The index where the selection should start (defaults to 0)\r
-     * @param {Number} end (optional) The index where the selection should end (defaults to the text length)\r
-     */\r
-    selectText : function(start, end){\r
-        var v = this.getRawValue();\r
-        var doFocus = false;\r
-        if(v.length > 0){\r
-            start = start === undefined ? 0 : start;\r
-            end = end === undefined ? v.length : end;\r
-            var d = this.el.dom;\r
-            if(d.setSelectionRange){\r
-                d.setSelectionRange(start, end);\r
-            }else if(d.createTextRange){\r
-                var range = d.createTextRange();\r
-                range.moveStart("character", start);\r
-                range.moveEnd("character", end-v.length);\r
-                range.select();\r
-            }\r
-            doFocus = Ext.isGecko || Ext.isOpera;\r
-        }else{\r
-            doFocus = true;\r
-        }\r
-        if(doFocus){\r
-            this.focus();\r
-        }\r
-    },\r
-\r
-    /**\r
-     * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed.\r
-     * This only takes effect if grow = true, and fires the {@link #autosize} event.\r
-     */\r
-    autoSize : function(){\r
-        if(!this.grow || !this.rendered){\r
-            return;\r
-        }\r
-        if(!this.metrics){\r
-            this.metrics = Ext.util.TextMetrics.createInstance(this.el);\r
-        }\r
-        var el = this.el;\r
-        var v = el.dom.value;\r
-        var d = document.createElement('div');\r
-        d.appendChild(document.createTextNode(v));\r
-        v = d.innerHTML;\r
-        Ext.removeNode(d);\r
-        d = null;\r
-        v += "&#160;";\r
-        var w = Math.min(this.growMax, Math.max(this.metrics.getWidth(v) + /* add extra padding */ 10, this.growMin));\r
-        this.el.setWidth(w);\r
-        this.fireEvent("autosize", this, w);\r
-    }\r
-});\r
-Ext.reg('textfield', Ext.form.TextField);\r