@@ -2,27 +2,38 @@ import {
22 Directive , ElementRef , Renderer2 ,
33 Input , Output , OnInit , HostListener ,
44 EventEmitter , OnChanges , SimpleChanges ,
5- OnDestroy
5+ OnDestroy , AfterViewInit
66} from '@angular/core' ;
77
88import { ResizeHandle } from './widgets/resize-handle' ;
99import { ResizeHandleType } from './models/resize-handle-type' ;
1010import { Position } from './models/position' ;
1111import { Size } from './models/size' ;
12+ import { IResizeEvent } from './models/resize-event' ;
1213
1314@Directive ( {
1415 selector : '[ngResizable]' ,
1516 exportAs : 'ngResizable'
1617} )
17- export class AngularResizableDirective implements OnInit , OnChanges , OnDestroy {
18+ export class AngularResizableDirective implements OnInit , OnChanges , OnDestroy , AfterViewInit {
1819 private _resizable = true ;
1920 private _handles : { [ key : string ] : ResizeHandle } = { } ;
2021 private _handleType : string [ ] = [ ] ;
2122 private _handleResizing : ResizeHandle = null ;
2223 private _origMousePos : Position = null ;
24+
25+ /** Original Size and Position */
2326 private _origSize : Size = null ;
2427 private _origPos : Position = null ;
2528
29+ /** Current Size and Position */
30+ private _currSize : Size = null ;
31+ private _currPos : Position = null ;
32+
33+ /** Initial Size and Position */
34+ private _initSize : Size = null ;
35+ private _initPos : Position = null ;
36+
2637 /** Disables the resizable if set to false. */
2738 @Input ( ) set ngResizable ( v : any ) {
2839 if ( v !== undefined && v !== null && v !== '' ) {
@@ -35,11 +46,20 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy {
3546 * Which handles can be used for resizing.
3647 * @example
3748 * [rzHandles] = "'n,e,s,w,se,ne,sw,nw'"
38- * [rzHandles] = { n: 'ng-resizable-n', e: 'ng-resizable-e' }
49+ * equals to: [rzHandles] = "'all'"
3950 *
4051 * */
4152 @Input ( ) rzHandles : ResizeHandleType = 'e,s,se' ;
4253
54+ /** emitted when start resizing */
55+ @Output ( ) rzStart = new EventEmitter < IResizeEvent > ( ) ;
56+
57+ /** emitted when start resizing */
58+ @Output ( ) rzResizing = new EventEmitter < IResizeEvent > ( ) ;
59+
60+ /** emitted when stop resizing */
61+ @Output ( ) rzStop = new EventEmitter < IResizeEvent > ( ) ;
62+
4363 constructor ( private el : ElementRef , private renderer : Renderer2 ) { }
4464
4565 ngOnChanges ( changes : SimpleChanges ) {
@@ -56,6 +76,39 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy {
5676 this . removeHandles ( ) ;
5777 }
5878
79+ ngAfterViewInit ( ) {
80+ const elm = this . el . nativeElement ;
81+ this . _initSize = Size . getCurrent ( elm ) ;
82+ this . _initPos = Position . getCurrent ( elm ) ;
83+ this . _currSize = Size . copy ( this . _initSize ) ;
84+ this . _currPos = Position . copy ( this . _initPos ) ;
85+ }
86+
87+ /** A method to reset size */
88+ public resetSize ( ) {
89+ this . _currSize = Size . copy ( this . _initSize ) ;
90+ this . _currPos = Position . copy ( this . _initPos ) ;
91+ this . doResize ( ) ;
92+ }
93+
94+ /** A method to reset size */
95+ public getStatus ( ) {
96+ if ( ! this . _currPos || ! this . _currSize ) {
97+ return null ;
98+ }
99+
100+ return {
101+ size : {
102+ width : this . _currSize . width ,
103+ height : this . _currSize . height
104+ } ,
105+ position : {
106+ top : this . _currPos . y ,
107+ left : this . _currPos . x
108+ }
109+ } ;
110+ }
111+
59112 private updateResizable ( ) {
60113 const element = this . el . nativeElement ;
61114
@@ -78,7 +131,12 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy {
78131
79132 let tmpHandleTypes : string [ ] ;
80133 if ( typeof this . rzHandles === 'string' ) {
81- tmpHandleTypes = this . rzHandles . replace ( / / g, '' ) . toLowerCase ( ) . split ( ',' ) ;
134+ if ( this . rzHandles === 'all' ) {
135+ tmpHandleTypes = [ 'n' , 'e' , 's' , 'w' , 'ne' , 'se' , 'nw' , 'sw' ] ;
136+ } else {
137+ tmpHandleTypes = this . rzHandles . replace ( / / g, '' ) . toLowerCase ( ) . split ( ',' ) ;
138+ }
139+
82140 for ( let type of tmpHandleTypes ) {
83141 // default handle theme: ng-resizable-$type.
84142 let handle = this . createHandleByType ( type , `ng-resizable-${ type } ` ) ;
@@ -137,6 +195,8 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy {
137195 this . _origMousePos = Position . fromEvent ( event ) ;
138196 this . _origSize = Size . getCurrent ( elm ) ;
139197 this . _origPos = Position . getCurrent ( elm ) ; // x: left, y: top
198+ this . _currSize = Size . copy ( this . _origSize ) ;
199+ this . _currPos = Position . copy ( this . _origPos ) ;
140200 this . startResize ( handle ) ;
141201 }
142202 }
@@ -159,37 +219,68 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy {
159219 onMouseMove ( event : MouseEvent | TouchEvent ) {
160220 if ( this . _handleResizing && this . _resizable && this . _origMousePos && this . _origPos && this . _origSize ) {
161221 this . resizeTo ( Position . fromEvent ( event ) ) ;
222+ this . onResizing ( ) ;
162223 }
163224 }
164225
165- startResize ( handle : ResizeHandle ) {
226+ private startResize ( handle : ResizeHandle ) {
166227 this . _handleResizing = handle ;
228+ this . rzStart . emit ( this . getResizingEvent ( ) ) ;
167229 }
168230
169- stopResize ( ) {
231+ private stopResize ( ) {
232+ this . rzStop . emit ( this . getResizingEvent ( ) ) ;
170233 this . _handleResizing = null ;
171234 }
172235
173- resizeTo ( p : Position ) {
174- const container = this . el . nativeElement ;
236+ private onResizing ( ) {
237+ this . rzResizing . emit ( this . getResizingEvent ( ) ) ;
238+ }
239+
240+ private getResizingEvent ( ) : IResizeEvent {
241+ return {
242+ host : this . el . nativeElement ,
243+ handle : this . _handleResizing ? this . _handleResizing . el : null ,
244+ size : {
245+ width : this . _currSize . width ,
246+ height : this . _currSize . height
247+ } ,
248+ position : {
249+ top : this . _currPos . y ,
250+ left : this . _currPos . x
251+ }
252+ } ;
253+ }
254+
255+ private resizeTo ( p : Position ) {
175256 p . subtract ( this . _origMousePos ) ;
176257
177258 if ( this . _handleResizing . type . match ( / n / ) ) {
178259 // n, ne, nw
179- this . renderer . setStyle ( container , ' height' , ( this . _origSize . height - p . y ) + 'px' ) ;
180- this . renderer . setStyle ( container , 'top' , ( this . _origPos . y + p . y ) + 'px' ) ;
260+ this . _currSize . height = this . _origSize . height - p . y ;
261+ this . _currPos . y = this . _origPos . y + p . y ;
181262 } else if ( this . _handleResizing . type . match ( / s / ) ) {
182263 // s, se, sw
183- this . renderer . setStyle ( container , ' height' , ( this . _origSize . height + p . y ) + 'px' ) ;
264+ this . _currSize . height = this . _origSize . height + p . y ;
184265 }
185266
186267 if ( this . _handleResizing . type . match ( / e / ) ) {
187268 // e, ne, se
188- this . renderer . setStyle ( container , ' width' , ( this . _origSize . width + p . x ) + 'px' ) ;
269+ this . _currSize . width = this . _origSize . width + p . x ;
189270 } else if ( this . _handleResizing . type . match ( / w / ) ) {
190271 // w, nw, sw
191- this . renderer . setStyle ( container , ' width' , ( this . _origSize . width - p . x ) + 'px' ) ;
192- this . renderer . setStyle ( container , 'left' , ( this . _origPos . x + p . x ) + 'px' ) ;
272+ this . _currSize . width = this . _origSize . width - p . x ;
273+ this . _currPos . x = this . _origPos . x + p . x ;
193274 }
275+
276+ this . doResize ( ) ;
277+ }
278+
279+ private doResize ( ) {
280+ const container = this . el . nativeElement ;
281+ this . renderer . setStyle ( container , 'height' , this . _currSize . height + 'px' ) ;
282+ this . renderer . setStyle ( container , 'width' , this . _currSize . width + 'px' ) ;
283+ this . renderer . setStyle ( container , 'left' , this . _currPos . x + 'px' ) ;
284+ this . renderer . setStyle ( container , 'top' , this . _currPos . y + 'px' ) ;
194285 }
195286}
0 commit comments