MD.Input.Text = new Class({ Extends: MD.Input.Base ,options: { type: 'text' // [string] 'text', 'password', 'textarea' ,icon: '' // [string] 'Search', 'Filter' ,placeholderText: false ,presetValue: '' ,className: '' ,inputProperties: {} ,maxLength: -1 ,returnHTML: false ,keyupEventMinimum: 0 ,keyupEventDelay: 200 ,usePlaceholderIfEmptyValue: false ,advancedMenuTrigger: false ,ignoreEvents: false ,autoResizeTextarea: false // (only for textareas) automatically resizes textarea to content ,textareaRows: 6 ,stripHtmlTags: true ,htmlTagsToKeep: [] // ,onFocus: function(e, thisObj){} // ,onBlur: function(e, thisObj){} // ,onEmpty: function(thisObj){} // ,onPopulated: function(thisObj){} // ,onKeyup: function(key, inputValue, thisObj){} // ,onKeydown: function(key, inputValue, thisObj){} // ,onAdvancedMenuToggle: function(thisObj) {} } ,_setup: function() { this.parent(); this.placeholderTextIsVisible = true; this.keyUpEventDelayTimeout = null; this._presetValue = this.options.presetValue; if (this.options.icon !== '') { this.icon = new Element('i', {'class': 'Icon-'+this.options.icon}); } if (this.options.autoResizeTextarea === true && this.options.type == 'textarea') { this._autoResizeTextarea = true; } if (this.options.stripHtmlTags) { this.addFilter('strip tags', 'StripTags', {extraParams: [this.options.htmlTagsToKeep]}); } this.options.type = this.options.type.toLowerCase(); this.build(); } ,destroy: function() { this.html.destroy(); this.parent(); } ,_setupEventHandlers: function() { this.parent(); this.addEvent('keyup', function() { this.fireEvent('data:dirty', this); if (this._autoResizeTextarea) { this.updateTextAreaHeight(); } }.bind(this)); } ,build: function() { // This has to be set up first, and then merged with the "properties" object of the this.input element ... because IE8 doesn't like the "type" property to be changed AFTER it has been created. var typeObject = {}; if (this.options.type != 'textarea') { typeObject.type = this.options.type; } this.html = new Element('div', {'class':this._getBaseClassName() +' '+ this.options.type.capitalize() + ' ' + this.options.className + ' ' + (this._autoResizeTextarea ? 'Resize-Auto' : '')}).adopt( (this.icon ? this.icon : '') ,this.innerWrap = new Element('div.InnerWrap').adopt( this.inner = new Element('div.Inner').adopt( this.placeholderText = new Element('div.Placeholder', { html: this.options.placeholderText === false ? '' : this.options.placeholderText ,tween: {duration: 100} }) ,this.input = new Element((this.options.type == 'textarea' ? 'textarea' : 'input')+'.Input', Object.merge(this.options.inputProperties, { value: (this._presetValue !== '' ? this._presetValue : '') ,readonly: !! this.options.ignoreEvents ,maxlength: (this.options.maxLength == -1 ? '' : this.options.maxLength) ,tween: {duration: 100} ,events: { keydown: function(e){ if (this._shouldUsherEvents()) { (function(){ this.fireEvent('keydown', [e.key, this.getValue(), this, e]); }.bind(this)).delay(25); } }.bind(this) ,keyup: function(e){ if (this._shouldUsherEvents()) { clearTimeout(this.keyUpEventDelayTimeout); if (this.$events.keyup) { if (this.input.value.length >= this.options.keyupEventMinimum) { this.keyUpEventDelayTimeout = function(){ this.fireEvent('keyup', [e.key, this.input.get('value'), this, e]); }.delay(this.options.keyupEventDelay, this); } } if (this.input.get('value') === '') { this.clear(); } else { if (this.placeholderTextIsVisible) { this.placeholderText.fade('out'); this.placeholderTextIsVisible = false; } this._populated = true; this.html.addClass('Populated'); this.fireEvent('populated', this, 20); } } }.bind(this) ,focus: function(e) { if (this._shouldUsherEvents()) { this.fireEvent('focus', [e, this]); this.focused = true; } }.bind(this) ,blur: function(e) { if (this._shouldUsherEvents()) { this.fireEvent('blur', [e, this]); this.focused = false; } }.bind(this) ,click: function(e) { if (this._shouldUsherEvents()) { e.stop(); } }.bind(this) } }, typeObject)) ) ) ); if (this.options.type == 'textarea') { this.input.setProperty('rows', this.options.textareaRows); } if ( (typeof this._presetValue !== 'undefined') && this._presetValue !== false && this._presetValue !== '') { this.placeholderText.fade('hide'); this.placeholderTextIsVisible = false; this.html.addClass('Populated'); if (this.options.type == 'textarea') { this.input.set('value', this._presetValue); if (this._autoResizeTextarea) { this.updateTextAreaHeight(); } } } if (this.options.advancedMenuTrigger && this.options.type != 'textarea') { this.advancedMenuTrigger = new MD.Input.Button({ icon: 'SortDescending' ,className: 'AdvancedMenuTrigger' ,onClick: function() { if ( ! this.advancedMenuTrigger.isOpen) { this.advancedMenuTrigger.isOpen = true; this.advancedMenuTrigger.setIcon('SortAscending'); } else { this.advancedMenuTrigger.isOpen = false; this.advancedMenuTrigger.setIcon('SortDescending'); } this.fireEvent('advancedToggle', this); }.bind(this) }); this.inner.grab(this.advancedMenuTrigger.toElement()); } } ,updateTextAreaHeight: function(delay) { var update = function(){ var delay = delay || 0; var inputWidth = this.input.getSize().x; var inputClone = this.input.clone(); inputClone.set('value', inputClone.get('value')+'\n'); inputClone.addClass('Clone'); inputClone.setProperty('tabindex', -1); inputClone.setStyles({ 'height': 'auto' ,'width': inputWidth }); inputClone.inject(this.input, 'after'); var newHeight = inputClone.getScrollSize().y; inputClone.destroy(); if (newHeight > 0) { this.input.setStyle('height', newHeight); } else { this.updateTextAreaHeight(delay+200); } }.bind(this); if (delay) { update.delay(delay); } else { update(); } } ,setValue: function(string){ if (typeof string === 'undefined') { string = ''; } if (typeOf(string) == 'string') { if (string === '') { return this.clear(); } this.input.set('value', string); this.html.addClass('Populated'); // this.placeholderText.set('html', string); // this.options.placeholderText = string; this.placeholderText.fade('hide'); this.placeholderTextIsVisible = false; if (this._autoResizeTextarea) { this.updateTextAreaHeight(); } this.fireEvent('data:dirty', this); } else { console.error('Cannot set the value of this MDTextInput to anything that is not a string.'); } return this; } ,getValue: function(){ if (this.options.usePlaceholderIfEmptyValue === true && this.input && this.input.get('value') === '') { return this.parent(this.placeholderText.get('html')); } else { if (this.input) { return this.parent(this.input.get('value')); } return ""; } } ,getPresetValue: function() { return this._presetValue; } ,setPlaceholderText: function(string){ if (typeOf(string) == 'string') { this.placeholderText.set('html', string); this.options.placeholderText = string; } else { console.error('Cannot set the placeholder text of this MDTextInput to anything that is not a string.'); } return this; } ,isPopulated: function() { return this._populated; } ,isEmpty: function() { return !this._populated; } ,isFocused: function(){ return this.focused; } ,focus: function(){ if ( ! Browser.ie8) { this.input.focus(); } return this; } ,blur: function(){ this.input.blur(); return this; } ,_shouldUsherEvents: function() { return ! this._disabled && !this.options.ignoreEvents; } ,activate: function() { this.input.addClass('Activated'); this.enable(); return this; } ,deactivate: function() { this.html.removeClass('Activated'); this.html.addClass('Deactivated'); return this; } ,enable: function() { this._disabled = false; this.input.removeProperty('readonly'); this.html.removeClass('Disabled'); return this; } ,disable: function() { this._disabled = true; this.input.setProperty('readonly', 'readonly'); this.html.addClass('Disabled'); return this; } ,clear: function(){ if (!this.placeholderTextIsVisible) { this.placeholderText.fade('in'); this.placeholderTextIsVisible = true; } this.input.set('value', ''); this._populated = false; this.html.removeClass('Populated'); this.fireEvent('empty', this, 20); this.fireEvent('data:dirty', this); return this; } ,toElement: function(){ return this.html; } });