-
Notifications
You must be signed in to change notification settings - Fork 1
/
drag_resize.js
executable file
·141 lines (105 loc) · 3.47 KB
/
drag_resize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// JavaScript Document
function enableDragAndResize(parent,createGrabber){
if(typeof parent!="element"){ parent=document; }
if(typeof createGrabber!="boolean"){ createGrabber=false; }
var elms=parent.getElementsByTagName('*');
for(var i=0;i<elms.length;i++){
if(elms[i].className.indexOf('Draggable')!==-1) makeDraggable(elms[i],createGrabber);
if(elms[i].className.indexOf('Resizable')!==-1) makeResizable(elms[i]);
}
}
function makeResizable(element){
var resizer = document.createElement("div");
resizer.className="RESIZER";
resizer.onmousedown=resizing;
element.appendChild(resizer);
}
function makeDraggable(element,createGrabber){
if(createGrabber){
var grabber = document.createElement("div");
grabber.className="GRABBER";
element.appendChild(grabber);
grabber.onmousedown=dragging;
}
else{
element.onmousedown=dragging;
element.style.cursor="move";
}
}
function dragging(e){
//IE fix
if(!e) e=window.event;
if(!e.target) e.target=e.srcElement;
if(!e.preventDefault) e.preventDefault=function(){event.returnValue=false;}
e.preventDefault();
element = e.target;
if(element.className=='GRABBER'){element=element.parentElement;}
var collision = element.className.toUpperCase().indexOf('TRAVERSABLE')===-1;
var draggable = element.className.toUpperCase().indexOf('DRAGGABLE')!==-1;
//click offset
offX = e.clientX - element.offsetLeft;
offY = e.clientY - element.offsetTop;
if(draggable)
document.onmousemove = function(e){
//IE fix
if(!e) e=window.event;
if(!e.preventDefault) e.preventDefault=function(){event.returnValue=false;}
e.preventDefault();
var x = e.clientX - offX;
var y = e.clientY - offY;
if(collision){ //obey boundiaries
var maxX=element.parentElement.clientWidth - element.clientWidth;
var maxY=element.parentElement.clientHeight - element.clientHeight;
if(x<0) x=0;
if(y<0) y=0;
if(x>maxX) x=maxX;
if(y>maxY) y=maxY;
}
element.style.top=y+"px";
element.style.left=x+"px";
}
document.onmouseup = function(e){
document.onmousemove=null;
}
}
//////////////////////////////////////////////////////////////////////
function resizing(e){
//IE fix
if(!e) e=window.event;
if(!e.target) e.target=e.srcElement;
if(!e.preventDefault) e.preventDefault=function(){event.returnValue=false;}
e.preventDefault();
resizer = e.target; //resizer element
element = resizer.parentElement; //element being resized
var collision = element.className.toUpperCase().indexOf('TRAVERSABLE')===-1;
var resizable = element.className.toUpperCase().indexOf('RESIZABLE')!==-1;
//click offset
offX = e.clientX - element.clientWidth;
offY = e.clientY - element.clientHeight;
if(resizable)
document.onmousemove = function(e){
element = resizer.parentElement; //element being resized
container = element.parentElement;
//IE fix
if(!e) e=window.event;
if(!e.preventDefault) e.preventDefault=function(){event.returnValue=false;}
e.preventDefault();
var x = e.clientX - offX - (resizer.offsetLeft-element.clientWidth);
var y = e.clientY - offY - (resizer.offsetTop-element.clientHeight);
if(collision){ //obey boundiaries
var minX=10;
var minY=10;
var maxX=container.clientWidth - element.offsetLeft;
var maxY=container.clientHeight - element.offsetTop;
if(x<minX) x=minX;
if(y<minY) y=minY;
if(x>maxX) x=maxX;
if(y>maxY) y=maxY;
}
element.style.height=y+"px";
element.style.width=x+"px";
}
document.onmouseup = function(e){
document.onmousemove=null;
}
}