1- import Mixin from '@ember/object/mixin' ;
21import { readOnly } from '@ember/object/computed' ;
2+ import Mixin from '@ember/object/mixin' ;
3+ import ResizeService from 'dummy/services/resize' ;
34const { floor } = Math ;
45
5- export default Mixin . create ( {
6- resizeEventsEnabled : true ,
6+ // tslint:disable-next-line:variable-name
7+ const ResizeAwareMixin = Mixin . create ( {
78 resizeDebouncedEventsEnabled : true ,
9+ resizeEventsEnabled : true ,
810
9- screenWidth : readOnly ( 'resizeService.screenWidth' ) ,
1011 screenHeight : readOnly ( 'resizeService.screenHeight' ) ,
12+ screenWidth : readOnly ( 'resizeService.screenWidth' ) ,
1113
12- _oldViewWidth : null ,
13- _oldViewHeight : null ,
14- _oldViewWidthDebounced : null ,
15- _oldViewHeightDebounced : null ,
16- resizeWidthSensitive : true ,
14+ _oldViewHeight : null as number | null ,
15+ _oldViewHeightDebounced : null as number | null ,
16+ _oldViewWidth : null as number | null ,
17+ _oldViewWidthDebounced : null as number | null ,
1718 resizeHeightSensitive : true ,
19+ resizeWidthSensitive : true ,
1820
1921 didInsertElement ( ) {
2022 this . _super ( ...arguments ) ;
23+ const resizeService : ResizeService = ( this as any ) . get ( 'resizeService' ) ;
2124 if ( this . get ( 'resizeEventsEnabled' ) ) {
22- this . get ( ' resizeService' ) . on ( 'didResize' , this , this . _handleResizeEvent ) ;
25+ resizeService . on ( 'didResize' , this , this . _handleResizeEvent ) ;
2326 }
2427 if ( this . get ( 'resizeDebouncedEventsEnabled' ) ) {
25- this . get ( ' resizeService' ) . on ( 'debouncedDidResize' , this , this . _handleDebouncedResizeEvent ) ;
28+ resizeService . on ( 'debouncedDidResize' , this , this . _handleDebouncedResizeEvent ) ;
2629 }
2730 } ,
2831
2932 willDestroyElement ( ) {
3033 this . _super ( ...arguments ) ;
34+ const resizeService : ResizeService = ( this as any ) . get ( 'resizeService' ) ;
3135 if ( this . get ( 'resizeEventsEnabled' ) ) {
32- this . get ( ' resizeService' ) . off ( 'didResize' , this , this . _handleResizeEvent ) ;
36+ resizeService . off ( 'didResize' , this , this . _handleResizeEvent ) ;
3337 }
3438 if ( this . get ( 'resizeDebouncedEventsEnabled' ) ) {
35- this . get ( ' resizeService' ) . off ( 'debouncedDidResize' , this , this . _handleDebouncedResizeEvent ) ;
39+ resizeService . off ( 'debouncedDidResize' , this , this . _handleDebouncedResizeEvent ) ;
3640 }
3741 } ,
3842
39- didResize ( /*width, height, evt*/ ) { } , // Overridden in subclass
40- debouncedDidResize ( /*width, height, evt*/ ) { } , // Overridden in subclass
43+ // tslint:disable-next-line:no-empty
44+ didResize ( _width : number , _height : number , _evt : UIEvent ) { } , // Overridden in subclass
45+ // tslint:disable-next-line:no-empty
46+ debouncedDidResize ( _width : number , _height : number , _evt : UIEvent ) { } , // Overridden in subclass
4147
42- _getComponentSize ( ) {
48+ _getComponentSize ( this : any ) {
4349 return this . element . getClientRects ( ) [ 0 ] ;
4450 } ,
4551
46- _handleResizeEvent ( evt ) {
52+ _handleResizeEvent ( evt : UIEvent ) {
4753 const w = floor ( this . _getComponentSize ( ) . width ) ;
4854 const h = floor ( this . _getComponentSize ( ) . height ) ;
4955 if (
@@ -52,13 +58,13 @@ export default Mixin.create({
5258 ) {
5359 this . didResize ( w , h , evt ) ;
5460 this . setProperties ( {
61+ _oldViewHeight : h ,
5562 _oldViewWidth : w ,
56- _oldViewHeight : h
5763 } ) ;
5864 }
5965 } ,
6066
61- _handleDebouncedResizeEvent ( evt ) {
67+ _handleDebouncedResizeEvent ( evt : UIEvent ) {
6268 const w = floor ( this . _getComponentSize ( ) . width ) ;
6369 const h = floor ( this . _getComponentSize ( ) . height ) ;
6470 if (
@@ -67,9 +73,11 @@ export default Mixin.create({
6773 ) {
6874 this . debouncedDidResize ( w , h , evt ) ;
6975 this . setProperties ( {
76+ _oldViewHeightDebounced : h ,
7077 _oldViewWidthDebounced : w ,
71- _oldViewHeightDebounced : h
7278 } ) ;
7379 }
74- }
80+ } ,
7581} ) ;
82+
83+ export default ResizeAwareMixin ;
0 commit comments