var TextInput = Class.create({ MinHeight: 15, MinWidth: 150, GrowReserve: 15, initialize: function(input) { this.input = $(input); this.div=$('WRAP_'+input); this.dragger=$('DRAG_'+input); this.corner=$('CORNER_'+input); this.parent=this.input.parentNode; this.ID_=this.input.readAttribute('id'); this.fitWidth=(this.corner ? false : true); if (this.fitWidth) { if (this.div) this.div.setStyle({ width: '98%' }); this.input.setStyle({ width: '100%' }); if (this.dragger) this.dragger.setStyle({ width: '100%' }); } else { if (this.dragger) this.dragger.addClassName('textInputResizeDownLeft'); } var visibility = this.input.readAttribute('visibility'); if (this.input.readAttribute('htmleditor') == 'yes' && (visibility == null || visibility == 'yes')) { this.dragger.hide(); this.corner.hide(); } this.span=new Element('span', {id: this.ID_, localid: this.input.readAttribute('localid'), 'class': 'spanTextInput'}); this.span.JSControl=this; this.span.innerHTML=this.input.value; this.autoGrow = this.input.readAttribute('auto-grow'); this.input.observe('keyup', this.Grow.bind(this)); this.input.observe('mouseover', this.Grow.bind(this)); if(Prototype.Browser.IE) { Event.observe(this.input, "propertychange", this.OnInput.bindAsEventListener(this)); Event.observe(this.input, "dragend", this.OnDragEnd.bindAsEventListener(this)); Event.observe(this.input, "change", this.OnChange.bindAsEventListener(this)); } else { this.input.observe('input', this.OnInput.bind(this)); this.input.observe("change", this.OnChange.bind(this)); } if (this.autoGrow) { //this.prevHeight = this.input.offsetHeight; // this.input.setStyle({ // overflowY: 'hidden' // }); } this.ID_ = this.input.readAttribute('id'); this.input.JSControl=this; // BindKeyDown(this.input, this.OnInput.bind(this)); this.input.observe("keydown", this.OnKeyDown.bind(this)); if (this.corner) this.corner.observe('mousedown', this.OnDraggerDown.bind(this, false)); if (this.dragger) { this.dragger.observe('mousedown', this.OnDraggerDown.bind(this, true)); this.dragger.observe('drag', function() { return false; }); this.dragger.observe('dblclick', this.OnDraggerDoubleClick.bind(this)); // Event.observe(window, 'resize', this.ResizeDragger.bind(this)); this.dragUpHandler = this.OnDraggerUp.bind(this); this.dragMoveHandler = this.OnDraggerMove.bind(this); } this.SetAttribute('visibility', this.input.readAttribute('visibility')); // Не удалять!!! Нужен вызов SetAttribute // if (this.dragger) this.ResizeDragger(); //this.parent = input.parentNode; this.parentWindow = AControl.GetParentWindow(this.ID()); this.changed = false; }, ResizeDragger: function() { var w = this.input.offsetWidth - (this.dragger.offsetWidth - this.dragger.clientWidth); if (this.fitWidth) { this.dragger.setStyle({ width: w + 'px' }); } else { this.dragger.setStyle({ width: w - this.corner.offsetWidth + 'px' }); } }, ResizeParentWindow: function() { // very experimental // TODO: put calls to this function in resize routines if (this.parentWindow) this.parentWindow.OnResize(); }, OnDraggerDown: function(verticalOnly, evt) { if (!evt.isLeftClick()) return; if (!this.drag) { var oldW = this.input.clientWidth; if (oldW < this.MinWidth) this.MinWidth = oldW; $(document).observe('mouseup', this.dragUpHandler) $(document).observe('mousemove', this.dragMoveHandler); this.dragX = evt.pointerX(); this.dragY = evt.pointerY(); if( this.visibility == "yes" || this.visibility == "edit" ) { this.startY = this.input.offsetHeight; this.startX = (verticalOnly ? undefined : this.input.offsetWidth); } else { this.startY = this.span.offsetHeight; this.startX = (verticalOnly ? undefined : this.span.offsetWidth); } this.drag = true; this.autoGrow = false; // this.input.setStyle({ overflowY: '' }); } evt.stop(); }, OnDraggerDoubleClick: function(evt) { this.autoGrow = false; this.input.setStyle({ height: this.MinHeight + 'px' }); this.input.setStyle({ height: this.input.scrollHeight + 'px' }); this.UpdateSpanSize( null, this.MinHeight ); this.ResizeParentWindow(); }, OnDraggerMove: function(evt) { var newX = evt.pointerX(); var newY = evt.pointerY(); var newH = this.startY + newY - this.dragY; if (newH < this.MinHeight) newH = this.MinHeight; var newW = this.startX + newX - this.dragX; if (newW < this.MinWidth) newW = this.MinWidth; if (this.startX !== undefined) { this.input.setStyle({ height: newH + 'px' }) this.input.setStyle({ width: newW + 'px' }); this.UpdateSpanSize( newW, newH); } else { this.input.setStyle({ height: newH + 'px' }); this.UpdateSpanSize( null, newH); } // this.ResizeDragger(); this.prevHeight = newH; }, OnDraggerUp: function(evt) { if (!evt.isLeftClick()) return; this.drag = false; $(document).stopObserving('mousemove', this.dragMoveHandler); $(document).stopObserving('mouseup', this.dragUpHandler); }, OnDragEnd: function(evt) { HideTip(); this.changed = true; new AEvent("INPUT", {}, this); }, OnChange: function(evt) { this.changed = true; if (this.autoGrow) { this.Grow(); } new AEvent("CHANGE", {}, this); }, OnInput: function(evt) { this.changed = true; var fn = function () { new AEvent("INPUT", {}, this); }; fn.bind(this).defer(); }, OnKeyDown: function(evt) { if(evt.altKey || evt.ctrlKey) return; var key = evt.which || evt.keyCode; switch(key) { case Event.KEY_BACKSPACE: case Event.KEY_DELETE: case Event.KEY_DOWN: case Event.KEY_END: case Event.KEY_HOME: case Event.KEY_LEFT: case Event.KEY_PAGEDOWN: case Event.KEY_PAGEUP: case Event.KEY_RETURN: case Event.KEY_RIGHT: case Event.KEY_UP: evt.stopPropagation(); } }, Grow: function() { if (!this.autoGrow) return; this.prevHeight=this.prevHeight || this.input.offsetHeight; while (this.prevHeight < this.input.scrollHeight-2) { // -2 for chrome glitching this.prevHeight = this.input.scrollHeight + this.GrowReserve; this.input.setStyle({ height: this.prevHeight + 'px' }); this.UpdateSpanSize( null, this.prevHeight); } }, SetValue: function(value) { if (typeof value == "undefined" || value === null || (typeof value.IsNull == 'function' && value.IsNull()) ){ v=''; } else{ var v = value.Value(); } if(this.input.value != v) { this.input.value=v; } if(this.span.innerHTML != v) { this.span.innerHTML=this.input.value; } this.Grow(); }, Value: function() { if(typeof(this.input.value) == 'undefined' || this.input.value===null) return optional(undefined); if (this.input.value === "") return optional(undefined); if(this.doTrim) { var val=this.input.value.toString(); val=val.replace(/^\s*/g, ''); val=val.replace(/\s*$/g, ''); if(val==="") return optional(undefined); return optional(val); } return optional(this.input.value.toString()); //return optional(this.input.value.toString()); }, SetAttribute: function(sName, sValue) { this.input.writeAttribute(sName, sValue); this.span.writeAttribute(sName, sValue); if (sName == 'disabled') return; if (sName == 'visibility') { var old=this.visibility; if( typeof(this.visibility) == 'undefined' ) { this.SetTextInputSize(this.input, this.input); this.visibility="yes"; this.mode='input'; return; } if (sValue==old) return; if(sValue=='no') this.div.setStyle({"display": 'none'}); else this.div.setStyle({"display": ''}); if( sValue != 'yes' && sValue != 'edit' ) { this.disabled = true; this.input.writeAttribute('tabindex', -1); // this.input.writeAttribute('readonly', 'yes'); this.div.addClassName('TextInputReadonly'); this.input.blur(); // this.startY = this.input.clientHeight; // this.startX = this.input.clientWidth; this.SetTextInputSize( this.input, this.span ); if( this.mode == 'input' || typeof(this.mode) == 'undefined' ) this.parent.replaceChild(this.span, this.input); if( this.mode == 'span' ) {} this.mode='span'; } else { if( this.mode == 'span' ) { this.SetTextInputSize( this.span, this.input ); this.parent.replaceChild(this.input, this.span); } this.mode='input'; if (this.div) this.div.removeClassName('TextInputReadonly'); } this.visibility=sValue; } else if (sName == "style") { var h = (Prototype.Browser.Opera) ? this.input.style.height : this.input.getStyle("height"); var w = (Prototype.Browser.Opera) ? this.input.style.width : this.input.getStyle("width"); this.input.writeAttribute(sName, sValue); this.input.setStyle({ width: w, height: h }); if (this.htmleditor) this.htmleditor.resize(w, h); if( this.mode == 'span' ) this.SetTextInputSize( this.span, this.span ); // this.ResizeDragger(); } else if (sName == "htmleditor" && sValue == "yes") { this.Grow(); var thisinput = this.input; var ckopts = { allowedContent: true, extraAllowedContent: '*{*}', resize_dir: 'both', width: parseInt(this.input.getStyle('width')), height: parseInt(this.input.getStyle('height')), uploadUrl: '/dummy-upload' //extraPlugins: 'font' }; this.htmleditor = CKEDITOR.replace(this.input, ckopts); this.htmleditor.on('change', function(e) { thisinput.value = e.editor.getData(); }); } else if (sName == "uploadUrl") { this.Grow(); var thisinput = this.input; var ckopts = { allowedContent: true, extraAllowedContent: '*{*}', resize_dir: 'both', width: parseInt(this.input.getStyle('width')), height: parseInt(this.input.getStyle('height')), uploadUrl: sValue }; if (this.htmleditor) this.htmleditor.destroy(); this.htmleditor = CKEDITOR.replace(this.input, ckopts); this.htmleditor.on('change', function(e) { thisinput.value = e.editor.getData(); }); } else this.input.writeAttribute(sName, sValue); }, SetTabOrder: function(ntabbase) { if(this.disabled) { this.input.writeAttribute('tabindex', -1); this.input.blur(); } else { this.input.writeAttribute('tabindex', ntabbase); } return ++ntabbase; }, Focus: function() { this.input.focus(); }, ID: function() { return this.ID_; }, SetTextInputSize: function( elem_from, elem_to ) { var str_h = (Prototype.Browser.Opera) ? elem_from.style.height : elem_from.getStyle("height"); var str_w = (Prototype.Browser.Opera) ? elem_from.style.width : elem_from.getStyle("width"); var h = Math.round( str_h.substring(0, str_h.indexOf("px") ) ); var w = Math.round( str_w.substring( 0, str_w.indexOf("px") ) ); var pad_l = this.input.getStyle("padding-left"); var pad_r = this.input.getStyle("padding-right"); var pad_t = this.input.getStyle("padding-top"); var pad_b = this.input.getStyle("padding-bottom"); var d_width = parseInt( pad_l.substring(0, pad_l.indexOf("px")) ) + parseInt( pad_r.substring(0, pad_r.indexOf("px")) ); var d_height = parseInt( pad_t.substring(0, pad_t.indexOf("px")) ) + parseInt( pad_b.substring(0, pad_b.indexOf("px")) ); if( (h-d_height)>= 0 && (w - d_width)>=0 ) { elem_to.setStyle({ height: h + "px", width: w + "px" }); } }, UpdateSpanSize: function( spWidth, spHeight ) { if( spHeight != null ) this.span.setStyle({ height: spHeight + 'px' }); if( spWidth != null ) this.span.setStyle({ width: spWidth + 'px' }); } });