@@ -25,7 +25,9 @@ angular.module('angularResizable', [])
2525 rGrabber : '@' ,
2626 rDisabled : '=' ,
2727 rNoThrottle : '=' ,
28- rParent : '='
28+ rParent : '=' ,
29+ rGrid : "=" ,
30+ rLimitResizeTo : '=?'
2931 } ,
3032 link : function ( scope , elem , attr ) {
3133 var element = elem ;
@@ -48,6 +50,8 @@ angular.module('angularResizable', [])
4850 element . addClass ( 'resizable' ) ;
4951
5052 var style = window . getComputedStyle ( element [ 0 ] , null ) ,
53+ originalW ,
54+ originalH ,
5155 w ,
5256 h ,
5357 dir = scope . rDirections || [ 'right' ] ,
@@ -63,11 +67,14 @@ angular.module('angularResizable', [])
6367 info . width = false ;
6468 info . height = false ;
6569 if ( axis === 'x' )
70+
6671 info . width = parseInt ( element [ 0 ] . style [ scope . rFlex ? flexBasis : 'width' ] ) ;
6772 else
6873 info . height = parseInt ( element [ 0 ] . style [ scope . rFlex ? flexBasis : 'height' ] ) ;
6974 info . id = element [ 0 ] . id ;
7075 info . evt = e ;
76+ info . originalWidth = originalW ;
77+ info . originalHeight = originalH ;
7178 } ;
7279
7380 var getClientX = function ( e ) {
@@ -80,24 +87,53 @@ angular.module('angularResizable', [])
8087
8188 var dragging = function ( e ) {
8289 var prop , offset = axis === 'x' ? start - getClientX ( e ) : start - getClientY ( e ) ;
90+
91+ var gridX = scope . rGrid ? scope . rGrid [ 0 ] : 1 ,
92+ gridY = scope . rGrid ? scope . rGrid [ 1 ] : 1 ,
93+ limitResizeTo = scope . rLimitResizeTo ,
94+ futureDimension ;
95+
96+ offset = ( axis == 'x' ) ? Math . round ( offset / gridX ) * gridX : Math . round ( offset / gridY ) * gridY ;
97+
8398 switch ( dragDir ) {
8499 case 'top' :
100+ futureDimension = h + ( offset * vy ) ;
101+ if ( angular . isDefined ( limitResizeTo ) && futureDimension > originalH + ( gridY * limitResizeTo ) ) {
102+ return ;
103+ }
104+
85105 prop = scope . rFlex ? flexBasis : 'height' ;
86106 element [ 0 ] . style [ prop ] = h + ( offset * vy ) + 'px' ;
87107 break ;
88108 case 'bottom' :
109+ futureDimension = h - ( offset * vy ) ;
110+ if ( angular . isDefined ( limitResizeTo ) && futureDimension < originalH - ( gridY * limitResizeTo ) ) {
111+ return ;
112+ }
113+
89114 prop = scope . rFlex ? flexBasis : 'height' ;
90115 element [ 0 ] . style [ prop ] = h - ( offset * vy ) + 'px' ;
91116 break ;
92117 case 'right' :
118+ futureDimension = w - ( offset * vx ) ;
119+ if ( angular . isDefined ( limitResizeTo ) && futureDimension > originalW + ( gridX * limitResizeTo ) ) {
120+ return ;
121+ }
122+
93123 prop = scope . rFlex ? flexBasis : 'width' ;
94- element [ 0 ] . style [ prop ] = w - ( offset * vx ) + 'px' ;
124+ element [ 0 ] . style [ prop ] = futureDimension + 'px' ;
95125 break ;
96126 case 'left' :
127+ futureDimension = w + ( offset * vx ) ;
128+ if ( angular . isDefined ( limitResizeTo ) && futureDimension < originalW - ( gridX * limitResizeTo ) ) {
129+ return ;
130+ }
131+
97132 prop = scope . rFlex ? flexBasis : 'width' ;
98133 element [ 0 ] . style [ prop ] = w + ( offset * vx ) + 'px' ;
99134 break ;
100135 }
136+
101137 updateInfo ( e ) ;
102138
103139 function resizingEmit ( ) {
@@ -111,6 +147,7 @@ angular.module('angularResizable', [])
111147 } ;
112148 var dragEnd = function ( e ) {
113149 updateInfo ( ) ;
150+
114151 scope . $emit ( 'angular-resizable.resizeEnd' , info ) ;
115152 scope . $apply ( ) ;
116153 document . removeEventListener ( 'mouseup' , dragEnd , false ) ;
@@ -123,12 +160,14 @@ angular.module('angularResizable', [])
123160 dragDir = direction ;
124161 axis = dragDir === 'left' || dragDir === 'right' ? 'x' : 'y' ;
125162 start = axis === 'x' ? getClientX ( e ) : getClientY ( e ) ;
126- w = parseInt ( style . getPropertyValue ( 'width' ) ) ;
127- h = parseInt ( style . getPropertyValue ( 'height' ) ) ;
163+ var elRect = element [ 0 ] . getBoundingClientRect ( ) ;
164+ w = parseInt ( elRect . width ) ;
165+ h = parseInt ( elRect . height ) ;
166+ originalW = w ;
167+ originalH = h ;
128168
129169 //prevent transition while dragging
130170 element . addClass ( 'no-transition' ) ;
131-
132171 document . addEventListener ( 'mouseup' , dragEnd , false ) ;
133172 document . addEventListener ( 'mousemove' , dragging , false ) ;
134173 document . addEventListener ( 'touchend' , dragEnd , false ) ;
@@ -145,7 +184,6 @@ angular.module('angularResizable', [])
145184 scope . $apply ( ) ;
146185 } ;
147186
148-
149187 scope . $watch ( 'rDisabled' , function ( disabled ) {
150188 dir . forEach ( function ( direction ) {
151189 if ( disabled ) {
@@ -174,4 +212,4 @@ angular.module('angularResizable', [])
174212 } ) ;
175213 }
176214 } ;
177- } ) ;
215+ } ) ;
0 commit comments