-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathangular-resizable.js
More file actions
169 lines (153 loc) · 7.4 KB
/
angular-resizable.js
File metadata and controls
169 lines (153 loc) · 7.4 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
angular.module('angularResizable', [])
.directive('resizable', function() {
var toCall;
function throttle(fun) {
if (toCall === undefined) {
toCall = fun;
setTimeout(function() {
toCall();
toCall = undefined;
}, 100);
} else {
toCall = fun;
}
}
return {
restrict: 'AE',
scope: {
rDirections: "=",
rCenteredX: "=",
rCenteredY: "=",
rWidth: "=",
rHeight: "=",
rFlex: "=",
rGrabber: "@",
rDisabled: "@",
rGrid: "=",
rLimitResizeTo: '=?'
},
link: function(scope, element, attr) {
// register watchers on width and height attributes if they are set
scope.$watch('rWidth', function(value){
element[0].style.width = scope.rWidth + 'px';
});
scope.$watch('rHeight', function(value){
element[0].style.height = scope.rHeight + 'px';
});
element.addClass('resizable');
var style = window.getComputedStyle(element[0], null),
originalW,
originalH,
w,
h,
dir = scope.rDirections,
vx = scope.rCenteredX ? 2 : 1, // if centered double velocity
vy = scope.rCenteredY ? 2 : 1, // if centered double velocity
inner = scope.rGrabber ? scope.rGrabber : '<span></span>',
start,
dragDir,
axis,
info = {};
var updateInfo = function(e) {
info.width = false; info.height = false;
if(axis == 'x')
info.width = scope.rFlex ? parseInt(element[0].style.flexBasis) : parseInt(element[0].style.width);
else
info.height = scope.rFlex ? parseInt(element[0].style.flexBasis) : parseInt(element[0].style.height);
info.id = element[0].id;
info.evt = e;
info.originalWidth = originalW;
info.originalHeight = originalH;
};
var dragging = function(e) {
var offset = (axis == 'x') ? start - e.clientX : start - e.clientY,
gridX = scope.rGrid[0] || 1,
gridY = scope.rGrid[1] || 1,
limitResizeTo = scope.rLimitResizeTo,
futureDimension;
offset = (axis == 'x')
? Math.round(offset / gridX) * gridX
: Math.round(offset / gridY) * gridY;
switch(dragDir) {
case 'top':
futureDimension = h + (offset * vy);
if ( angular.isDefined(limitResizeTo) && futureDimension > originalH+(gridY*limitResizeTo) ) return;
if(scope.rFlex) { element[0].style.flexBasis = h + (offset * vy) + 'px'; }
else { element[0].style.height = h + (offset * vy) + 'px'; }
break;
case 'right':
futureDimension = w - (offset * vx);
if ( angular.isDefined(limitResizeTo) && futureDimension > originalW+(gridX*limitResizeTo) ) return;
if(scope.rFlex) { element[0].style.flexBasis = futureDimension + 'px'; }
else { element[0].style.width = futureDimension + 'px'; }
break;
case 'bottom':
futureDimension = h - (offset * vy);
if ( angular.isDefined(limitResizeTo) && futureDimension < originalH-(gridY*limitResizeTo) ) return;
if(scope.rFlex) { element[0].style.flexBasis = h - (offset * vy) + 'px'; }
else { element[0].style.height = h - (offset * vy) + 'px'; }
break;
case 'left':
futureDimension = w + (offset * vx);
if ( angular.isDefined(limitResizeTo) && futureDimension < originalW-(gridX*limitResizeTo) ) return;
if(scope.rFlex) { element[0].style.flexBasis = w + (offset * vx) + 'px'; }
else { element[0].style.width = w + (offset * vx) + 'px'; }
break;
}
updateInfo(e);
throttle(function() { scope.$emit("angular-resizable.resizing", info);});
};
var dragEnd = function(e) {
updateInfo(e);
scope.$emit("angular-resizable.resizeEnd", info);
scope.$apply();
document.removeEventListener('mouseup', dragEnd, false);
document.removeEventListener('mousemove', dragging, false);
element.removeClass('no-transition');
};
var dragStart = function(e, direction) {
dragDir = direction;
axis = dragDir == 'left' || dragDir == 'right' ? 'x' : 'y';
start = axis == 'x' ? e.clientX : e.clientY;
// IE returns different values using "style.getPropertyValue"
//w = parseInt(style.getPropertyValue("width"));
//h = parseInt(style.getPropertyValue("height"));
var elRect = element[0].getBoundingClientRect();
w = parseInt(elRect.width);
h = parseInt(elRect.height);
originalW = w;
originalH = h;
//prevent transition while dragging
element.addClass('no-transition');
document.addEventListener('mouseup', dragEnd, false);
document.addEventListener('mousemove', dragging, false);
// Disable highlighting while dragging
if(e.stopPropagation) e.stopPropagation();
if(e.preventDefault) e.preventDefault();
e.cancelBubble = true;
e.returnValue = false;
updateInfo(e);
scope.$emit("angular-resizable.resizeStart", info);
scope.$apply();
};
for(var i=0;i<dir.length;i++) {
(function () {
var grabber = document.createElement('div'),
direction = dir[i];
// add class for styling purposes
grabber.setAttribute('class', 'rg-' + dir[i]);
grabber.innerHTML = inner;
element[0].appendChild(grabber);
grabber.ondragstart = function() { return false }
grabber.addEventListener('mousedown', function(e) {
disabled = (scope.rDisabled == 'true');
if (!disabled && e.which == 1) {
// left mouse click
dragStart(e, direction);
}
}, false);
}())
}
}
}
});