1 define("dijit/form/_ExpandingTextAreaMixin", [
2 "dojo/_base/declare", // declare
3 "dojo/dom-construct", // domConstruct.create
5 "dojo/_base/lang", // lang.hitch
7 "dojo/_base/window", // win.body
9 ], function(declare, domConstruct, has, lang, on, win, Viewport){
12 // dijit/form/_ExpandingTextAreaMixin
14 // feature detection, true for mozilla and webkit
15 has.add("textarea-needs-help-shrinking", function(){
16 var body = win.body(), // note: if multiple documents exist, doesn't matter which one we use
17 te = domConstruct.create('textarea', {
21 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' }
23 var needsHelpShrinking = te.scrollHeight >= te.clientHeight;
25 return needsHelpShrinking;
28 return declare("dijit.form._ExpandingTextAreaMixin", null, {
30 // Mixin for textarea widgets to add auto-expanding capability
32 _setValueAttr: function(){
33 this.inherited(arguments);
37 postCreate: function(){
38 this.inherited(arguments);
39 var textarea = this.textbox;
40 textarea.style.overflowY = "hidden";
41 this.own(on(textarea, "focus, resize", lang.hitch(this, "_resizeLater")));
45 this.inherited(arguments);
46 this.own(Viewport.on("resize", lang.hitch(this, "_resizeLater")));
50 _onInput: function(e){
51 this.inherited(arguments);
55 _estimateHeight: function(){
57 // Approximate the height when the textarea is invisible with the number of lines in the text.
58 // Fails when someone calls setValue with a long wrapping line, but the layout fixes itself when the user clicks inside so . . .
59 // In IE, the resize event is supposed to fire when the textarea becomes visible again and that will correct the size automatically.
61 var textarea = this.textbox;
62 // #rows = #newlines+1
63 textarea.rows = (textarea.value.match(/\n/g) || []).length + 1;
66 _resizeLater: function(){
72 // Resizes the textarea vertically (should be called after a style/value change)
74 var textarea = this.textbox;
76 function textareaScrollHeight(){
78 if(textarea.value === ''){
82 var sh = textarea.scrollHeight;
83 if(empty){ textarea.value = ''; }
87 if(textarea.style.overflowY == "hidden"){ textarea.scrollTop = 0; }
88 if(this.busyResizing){ return; }
89 this.busyResizing = true;
90 if(textareaScrollHeight() || textarea.offsetHeight){
91 var newH = textareaScrollHeight() + Math.max(textarea.offsetHeight - textarea.clientHeight, 0);
92 var newHpx = newH + "px";
93 if(newHpx != textarea.style.height){
94 textarea.style.height = newHpx;
95 textarea.rows = 1; // rows can act like a minHeight if not cleared
97 if(has("textarea-needs-help-shrinking")){
98 var origScrollHeight = textareaScrollHeight(),
99 newScrollHeight = origScrollHeight,
100 origMinHeight = textarea.style.minHeight,
101 decrement = 4, // not too fast, not too slow
103 origScrollTop = textarea.scrollTop;
104 textarea.style.minHeight = newHpx; // maintain current height
105 textarea.style.height = "auto"; // allow scrollHeight to change
107 textarea.style.minHeight = Math.max(newH - decrement, 4) + "px";
108 thisScrollHeight = textareaScrollHeight();
109 var change = newScrollHeight - thisScrollHeight;
111 if(change < decrement){
112 break; // scrollHeight didn't shrink
114 newScrollHeight = thisScrollHeight;
117 textarea.style.height = newH + "px";
118 textarea.style.minHeight = origMinHeight;
119 textarea.scrollTop = origScrollTop;
121 textarea.style.overflowY = textareaScrollHeight() > textarea.clientHeight ? "auto" : "hidden";
122 if(textarea.style.overflowY == "hidden"){ textarea.scrollTop = 0; }
124 // hidden content of unknown size
125 this._estimateHeight();
127 this.busyResizing = false;