1 // Copyright (c) 2005 Thomas Fakes (http://craz8.com)
3 // This code is substantially based on code from script.aculo.us which has the
4 // following copyright and permission notice
6 // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 var Resizeable = Class.create();
28 Resizeable.prototype = {
29 initialize: function(element) {
30 var options = Object.extend({
39 }, arguments[1] || {});
41 this.element = $(element);
42 this.handle = this.element;
44 Element.makePositioned(this.element); // fix IE
46 this.options = options;
49 this.resizing = false;
50 this.currentDirection = '';
52 this.eventMouseDown = this.startResize.bindAsEventListener(this);
53 this.eventMouseUp = this.endResize.bindAsEventListener(this);
54 this.eventMouseMove = this.update.bindAsEventListener(this);
55 this.eventCursorCheck = this.cursor.bindAsEventListener(this);
56 this.eventKeypress = this.keyPress.bindAsEventListener(this);
58 this.registerEvents();
61 Event.stopObserving(this.handle, "mousedown", this.eventMouseDown);
62 this.unregisterEvents();
64 registerEvents: function() {
65 Event.observe(document, "mouseup", this.eventMouseUp);
66 Event.observe(document, "mousemove", this.eventMouseMove);
67 Event.observe(document, "keypress", this.eventKeypress);
68 Event.observe(this.handle, "mousedown", this.eventMouseDown);
69 Event.observe(this.element, "mousemove", this.eventCursorCheck);
71 unregisterEvents: function() {
72 //if(!this.active) return;
73 //Event.stopObserving(document, "mouseup", this.eventMouseUp);
74 //Event.stopObserving(document, "mousemove", this.eventMouseMove);
75 //Event.stopObserving(document, "mousemove", this.eventCursorCheck);
76 //Event.stopObserving(document, "keypress", this.eventKeypress);
78 startResize: function(event) {
79 if(Event.isLeftClick(event)) {
81 // abort on form elements, fixes a Firefox issue
82 var src = Event.element(event);
84 src.tagName=='INPUT' ||
85 src.tagName=='SELECT' ||
86 src.tagName=='BUTTON' ||
87 src.tagName=='TEXTAREA')) return;
89 var dir = this.directions(event);
92 var offsets = Position.cumulativeOffset(this.element);
93 this.startTop = offsets[1];
94 this.startLeft = offsets[0];
95 this.startWidth = parseInt(Element.getStyle(this.element, 'width'));
96 this.startHeight = parseInt(Element.getStyle(this.element, 'height'));
97 this.startX = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
98 this.startY = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
100 this.currentDirection = dir;
105 finishResize: function(event, success) {
106 // this.unregisterEvents();
109 this.resizing = false;
111 if(this.options.zindex)
112 this.element.style.zIndex = this.originalZ;
114 if (this.options.resize) {
115 this.options.resize(this.element);
118 keyPress: function(event) {
120 if(event.keyCode==Event.KEY_ESC) {
121 this.finishResize(event, false);
126 endResize: function(event) {
127 if(this.active && this.resizing) {
128 this.finishResize(event, true);
132 this.resizing = false;
134 draw: function(event) {
135 var pointer = [Event.pointerX(event), Event.pointerY(event)];
136 var style = this.element.style;
137 if (this.currentDirection.indexOf('n') != -1) {
138 var pointerMoved = this.startY - pointer[1];
139 var margin = Element.getStyle(this.element, 'margin-top') || "0";
140 var newHeight = this.startHeight + pointerMoved;
141 if (newHeight > this.options.minHeight) {
142 style.height = newHeight + "px";
143 style.top = (this.startTop - pointerMoved - parseInt(margin)) + "px";
146 if (this.currentDirection.indexOf('w') != -1) {
147 var pointerMoved = this.startX - pointer[0];
148 var margin = Element.getStyle(this.element, 'margin-left') || "0";
149 var newWidth = this.startWidth + pointerMoved;
150 if (newWidth > this.options.minWidth) {
151 style.left = (this.startLeft - pointerMoved - parseInt(margin)) + "px";
152 style.width = newWidth + "px";
155 if (this.currentDirection.indexOf('s') != -1) {
156 var newHeight = this.startHeight + pointer[1] - this.startY;
157 if (newHeight > this.options.minHeight) {
158 style.height = newHeight + "px";
161 if (this.currentDirection.indexOf('e') != -1) {
162 var newWidth = this.startWidth + pointer[0] - this.startX;
163 if (newWidth > this.options.minWidth) {
164 style.width = newWidth + "px";
167 if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering
169 between: function(val, low, high) {
170 return (val >= low && val < high);
172 directions: function(event) {
173 var pointer = [Event.pointerX(event), Event.pointerY(event)];
174 var offsets = Position.cumulativeOffset(this.element);
177 if (this.between(pointer[1] - offsets[1], 0, this.options.top)) cursor += 'n';
178 if (this.between((offsets[1] + this.element.offsetHeight) - pointer[1], 0, this.options.bottom)) cursor += 's';
179 if (this.between(pointer[0] - offsets[0], 0, this.options.left)) cursor += 'w';
180 if (this.between((offsets[0] + this.element.offsetWidth) - pointer[0], 0, this.options.right)) cursor += 'e';
184 cursor: function(event) {
185 var cursor = this.directions(event);
186 if (cursor.length > 0) {
191 this.element.style.cursor = cursor;
193 update: function(event) {
196 var style = this.element.style;
197 this.resizing = true;
199 if(Element.getStyle(this.element,'position')=='')
200 style.position = "relative";
202 if(this.options.zindex) {
203 this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0);
204 style.zIndex = this.options.zindex;
209 // fix AppleWebKit rendering
210 if(navigator.appVersion.indexOf('AppleWebKit')>0) window.scrollBy(0,0);