-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathangular-resizable.js
More file actions
161 lines (147 loc) · 7.2 KB
/
angular-resizable.js
File metadata and controls
161 lines (147 loc) · 7.2 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
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',
priority: 599, // right after ng-if. Supports transclusion
scope: {
rDirections: '=',
rCenteredX: '=',
rCenteredY: '=',
rWidth: '=',
rHeight: '=',
rFlex: '=',
rGrabber: '@',
rDisabled: '@',
rNoThrottle: '='
},
link: function(scope, element, attr) {
var flexBasis = 'flexBasis' in document.documentElement.style ? 'flexBasis' :
'webkitFlexBasis' in document.documentElement.style ? 'webkitFlexBasis' :
'msFlexPreferredSize' in document.documentElement.style ? 'msFlexPreferredSize' : 'flexBasis';
// register watchers on width and height attributes if they are set
scope.$watch('rWidth', function(value){
element[0].style[scope.rFlex ? flexBasis : 'width'] = scope.rWidth + 'px';
});
scope.$watch('rHeight', function(value){
element[0].style[scope.rFlex ? flexBasis : 'height'] = scope.rHeight + 'px';
});
element.addClass('resizable');
var style = window.getComputedStyle(element[0], null),
w,
h,
dir = scope.rDirections || ['right'],
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 = parseInt(element[0].style[scope.rFlex ? flexBasis : 'width']);
else
info.height = parseInt(element[0].style[scope.rFlex ? flexBasis : 'height']);
info.id = element[0].id;
info.evt = e;
};
var getClientX = function(e) {
return e.touches ? e.touches[0].clientX : e.clientX;
};
var getClientY = function(e) {
return e.touches ? e.touches[0].clientY : e.clientY;
};
var dragging = function(e) {
var prop, offset = axis === 'x' ? start - getClientX(e) : start - getClientY(e);
switch(dragDir) {
case 'top':
prop = scope.rFlex ? flexBasis : 'height';
element[0].style[prop] = h + (offset * vy) + 'px';
break;
case 'bottom':
prop = scope.rFlex ? flexBasis : 'height';
element[0].style[prop] = h - (offset * vy) + 'px';
break;
case 'right':
prop = scope.rFlex ? flexBasis : 'width';
element[0].style[prop] = w - (offset * vx) + 'px';
break;
case 'left':
prop = scope.rFlex ? flexBasis : 'width';
element[0].style[prop] = w + (offset * vx) + 'px';
break;
}
updateInfo(e);
function resizingEmit(){
scope.$emit('angular-resizable.resizing', info);
}
if (scope.rNoThrottle) {
resizingEmit();
} else {
throttle(resizingEmit);
}
};
var dragEnd = function(e) {
updateInfo();
scope.$emit('angular-resizable.resizeEnd', info);
scope.$apply();
document.removeEventListener('mouseup', dragEnd, false);
document.removeEventListener('mousemove', dragging, false);
document.removeEventListener('touchend', dragEnd, false);
document.removeEventListener('touchmove', dragging, false);
element.removeClass('no-transition');
};
var dragStart = function(e, direction) {
dragDir = direction;
axis = dragDir === 'left' || dragDir === 'right' ? 'x' : 'y';
start = axis === 'x' ? getClientX(e) : getClientY(e);
w = parseInt(style.getPropertyValue('width'));
h = parseInt(style.getPropertyValue('height'));
//prevent transition while dragging
element.addClass('no-transition');
document.addEventListener('mouseup', dragEnd, false);
document.addEventListener('mousemove', dragging, false);
document.addEventListener('touchend', dragEnd, false);
document.addEventListener('touchmove', 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();
};
dir.forEach(function (direction) {
var grabber = document.createElement('div');
// add class for styling purposes
grabber.setAttribute('class', 'rg-' + direction);
grabber.innerHTML = inner;
element[0].appendChild(grabber);
grabber.ondragstart = function() { return false; };
var down = function(e) {
var disabled = (scope.rDisabled === 'true');
if (!disabled && (e.which === 1 || e.touches)) {
// left mouse click or touch screen
dragStart(e, direction);
}
};
grabber.addEventListener('mousedown', down, false);
grabber.addEventListener('touchstart', down, false);
});
}
};
});