123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- define("dijit/form/_ExpandingTextAreaMixin", [
- "dojo/_base/declare", // declare
- "dojo/dom-construct", // domConstruct.create
- "dojo/_base/lang", // lang.hitch
- "dojo/_base/window" // win.body
- ], function(declare, domConstruct, lang, win){
- // module:
- // dijit/form/_ExpandingTextAreaMixin
- // summary:
- // Mixin for textarea widgets to add auto-expanding capability
- // feature detection
- var needsHelpShrinking;
- return declare("dijit.form._ExpandingTextAreaMixin", null, {
- // summary:
- // Mixin for textarea widgets to add auto-expanding capability
- _setValueAttr: function(){
- this.inherited(arguments);
- this.resize();
- },
- postCreate: function(){
- this.inherited(arguments);
- var textarea = this.textbox;
- if(needsHelpShrinking == undefined){
- var te = domConstruct.create('textarea', {rows:"5", cols:"20", value: ' ', style: {zoom:1, fontSize:"12px", height:"96px", overflow:'hidden', visibility:'hidden', position:'absolute', border:"5px solid white", margin:"0", padding:"0", boxSizing: 'border-box', MsBoxSizing: 'border-box', WebkitBoxSizing: 'border-box', MozBoxSizing: 'border-box' }}, win.body(), "last");
- needsHelpShrinking = te.scrollHeight >= te.clientHeight;
- win.body().removeChild(te);
- }
- this.connect(textarea, "onresize", "_resizeLater");
- this.connect(textarea, "onfocus", "_resizeLater");
- textarea.style.overflowY = "hidden";
- },
- startup: function(){
- this.inherited(arguments);
- this._resizeLater();
- },
- _onInput: function(e){
- this.inherited(arguments);
- this.resize();
- },
- _estimateHeight: function(){
- // summary:
- // Approximate the height when the textarea is invisible with the number of lines in the text.
- // Fails when someone calls setValue with a long wrapping line, but the layout fixes itself when the user clicks inside so . . .
- // In IE, the resize event is supposed to fire when the textarea becomes visible again and that will correct the size automatically.
- //
- var textarea = this.textbox;
- // #rows = #newlines+1
- textarea.rows = (textarea.value.match(/\n/g) || []).length + 1;
- },
- _resizeLater: function(){
- this.defer("resize");
- },
- resize: function(){
- // summary:
- // Resizes the textarea vertically (should be called after a style/value change)
- var textarea = this.textbox;
- function textareaScrollHeight(){
- var empty = false;
- if(textarea.value === ''){
- textarea.value = ' ';
- empty = true;
- }
- var sh = textarea.scrollHeight;
- if(empty){ textarea.value = ''; }
- return sh;
- }
- if(textarea.style.overflowY == "hidden"){ textarea.scrollTop = 0; }
- if(this.busyResizing){ return; }
- this.busyResizing = true;
- if(textareaScrollHeight() || textarea.offsetHeight){
- var newH = textareaScrollHeight() + Math.max(textarea.offsetHeight - textarea.clientHeight, 0);
- var newHpx = newH + "px";
- if(newHpx != textarea.style.height){
- textarea.style.height = newHpx;
- textarea.rows = 1; // rows can act like a minHeight if not cleared
- }
- if(needsHelpShrinking){
- var origScrollHeight = textareaScrollHeight(),
- newScrollHeight = origScrollHeight,
- origMinHeight = textarea.style.minHeight,
- decrement = 4, // not too fast, not too slow
- thisScrollHeight,
- origScrollTop = textarea.scrollTop;
- textarea.style.minHeight = newHpx; // maintain current height
- textarea.style.height = "auto"; // allow scrollHeight to change
- while(newH > 0){
- textarea.style.minHeight = Math.max(newH - decrement, 4) + "px";
- thisScrollHeight = textareaScrollHeight();
- var change = newScrollHeight - thisScrollHeight;
- newH -= change;
- if(change < decrement){
- break; // scrollHeight didn't shrink
- }
- newScrollHeight = thisScrollHeight;
- decrement <<= 1;
- }
- textarea.style.height = newH + "px";
- textarea.style.minHeight = origMinHeight;
- textarea.scrollTop = origScrollTop;
- }
- textarea.style.overflowY = textareaScrollHeight() > textarea.clientHeight ? "auto" : "hidden";
- if(textarea.style.overflowY == "hidden"){ textarea.scrollTop = 0; }
- }else{
- // hidden content of unknown size
- this._estimateHeight();
- }
- this.busyResizing = false;
- }
- });
- });
|