1 // $Id: textarea.js 144 2007-03-28 07:52:20Z thierry $
4 addLoadEvent(textAreaAutoAttach);
7 function textAreaAutoAttach(event, parent) {
8 if (typeof parent == 'undefined') {
9 // Attach to all visible textareas.
10 textareas = document.getElementsByTagName('textarea');
13 // Attach to all visible textareas inside parent.
14 textareas = parent.getElementsByTagName('textarea');
17 for (var i = 0; textarea = textareas[i]; ++i) {
18 if (hasClass(textarea, 'resizable') && !hasClass(textarea.nextSibling, 'grippie')) {
19 if (typeof dimensions(textarea).width != 'undefined' && dimensions(textarea).width != 0) {
20 new textArea(textarea);
26 function textArea(element) {
28 this.element = element;
29 this.parent = this.element.parentNode;
30 this.dimensions = dimensions(element);
33 this.wrapper = document.createElement('div');
34 this.wrapper.className = 'resizable-textarea';
35 this.parent.insertBefore(this.wrapper, this.element);
37 // Add grippie and measure it
38 this.grippie = document.createElement('div');
39 this.grippie.className = 'grippie';
40 this.wrapper.appendChild(this.grippie);
41 this.grippie.dimensions = dimensions(this.grippie);
42 this.grippie.onmousedown = function (e) { ta.beginDrag(e); };
44 // Set wrapper and textarea dimensions
45 this.wrapper.style.height = this.dimensions.height + this.grippie.dimensions.height + 1 +'px';
46 this.element.style.marginBottom = '0px';
47 this.element.style.width = '100%';
48 this.element.style.height = this.dimensions.height +'px';
51 removeNode(this.element);
52 this.wrapper.insertBefore(this.element, this.grippie);
54 // Measure difference between desired and actual textarea dimensions to account for padding/borders
55 this.widthOffset = dimensions(this.wrapper).width - this.dimensions.width;
57 // Make the grippie line up in various browsers
60 this.grippie.style.marginRight = '4px';
62 if (document.all && !window.opera) {
64 this.grippie.style.width = '100%';
65 this.grippie.style.paddingLeft = '2px';
68 this.element.style.MozBoxSizing = 'border-box';
70 this.heightOffset = absolutePosition(this.grippie).y - absolutePosition(this.element).y - this.dimensions.height;
73 textArea.prototype.beginDrag = function (event) {
74 if (document.isDragging) {
77 document.isDragging = true;
79 event = event || window.event;
82 this.oldMoveHandler = document.onmousemove;
83 document.onmousemove = function(e) { cp.handleDrag(e); };
84 this.oldUpHandler = document.onmouseup;
85 document.onmouseup = function(e) { cp.endDrag(e); };
87 // Store drag offset from grippie top
88 var pos = absolutePosition(this.grippie);
89 this.dragOffset = event.clientY - pos.y;
92 this.element.style.opacity = 0.5;
95 this.handleDrag(event);
98 textArea.prototype.handleDrag = function (event) {
99 event = event || window.event;
100 // Get coordinates relative to text area
101 var pos = absolutePosition(this.element);
102 var y = event.clientY - pos.y;
105 var height = Math.max(32, y - this.dragOffset - this.heightOffset);
106 this.wrapper.style.height = height + this.grippie.dimensions.height + 1 + 'px';
107 this.element.style.height = height + 'px';
109 // Avoid text selection
113 textArea.prototype.endDrag = function (event) {
115 document.onmousemove = this.oldMoveHandler;
116 document.onmouseup = this.oldUpHandler;
119 this.element.style.opacity = 1.0;
120 document.isDragging = false;