@@ -2,127 +2,110 @@ import { htmlSafe, classify } from '@ember/string';
2
2
import Component from '@ember/component' ;
3
3
import { isPresent } from '@ember/utils' ;
4
4
import { run } from '@ember/runloop' ;
5
- import { computed , set , get } from '@ember/object' ;
5
+ import { action , computed , set } from '@ember/object' ;
6
+ import { and , bool , readOnly , not } from '@ember/object/computed' ;
7
+ import { tagName } from '@ember-decorators/component' ;
6
8
import layout from '../templates/components/flash-message' ;
7
- import getWithDefault from '../utils/get-with-default' ;
8
-
9
- const {
10
- and,
11
- bool,
12
- readOnly,
13
- not
14
- } = computed ;
15
- const {
16
- next,
17
- cancel
18
- } = run ;
19
-
20
- export default Component . extend ( {
21
- layout,
22
- active : false ,
23
- messageStyle : 'bootstrap' ,
24
- classNames : [ 'flash-message' ] ,
25
- classNameBindings : [ 'alertType' , 'active' , 'exiting' ] ,
26
- attributeBindings : [ 'aria-label' , 'aria-describedby' , 'role' ] ,
27
-
28
- showProgress : readOnly ( 'flash.showProgress' ) ,
29
- notExiting : not ( 'exiting' ) ,
30
- showProgressBar : and ( 'showProgress' , 'notExiting' ) ,
31
- exiting : readOnly ( 'flash.exiting' ) ,
32
- hasBlock : bool ( 'template' ) . readOnly ( ) ,
33
-
34
- alertType : computed ( 'flash.type' , {
35
- get ( ) {
36
- const flashType = getWithDefault ( this , 'flash.type' , '' ) ;
37
- const messageStyle = getWithDefault ( this , 'messageStyle' , '' ) ;
38
- let prefix = 'alert alert-' ;
39
-
40
- if ( messageStyle === 'foundation' ) {
41
- prefix = 'alert-box ' ;
42
- }
43
-
44
- return `${ prefix } ${ flashType } ` ;
45
- }
46
- } ) ,
47
9
48
- flashType : computed ( 'flash.type' , {
49
- get ( ) {
50
- const flashType = getWithDefault ( this , 'flash.type' , '' ) ;
10
+ const { next, cancel } = run ;
51
11
52
- return classify ( flashType ) ;
53
- }
54
- } ) ,
12
+ @tagName ( '' )
13
+ export default class FlashMessage extends Component {
14
+ layout = layout ;
15
+ active = false ;
16
+ messageStyle = 'bootstrap' ;
55
17
56
- didInsertElement ( ) {
57
- this . _super ( ...arguments ) ;
58
- const pendingSet = next ( this , ( ) => {
59
- set ( this , 'active' , true ) ;
60
- } ) ;
61
- set ( this , 'pendingSet' , pendingSet ) ;
62
- this . set ( '_mouseEnterHandler' , this . _mouseEnter . bind ( this ) ) ;
63
- this . set ( '_mouseLeaveHandler' , this . _mouseLeave . bind ( this ) ) ;
64
- this . element . addEventListener ( 'mouseenter' , this . _mouseEnterHandler ) ;
65
- this . element . addEventListener ( 'mouseleave' , this . _mouseLeaveHandler ) ;
66
- } ,
67
-
68
- willDestroyElement ( ) {
69
- this . _super ( ...arguments ) ;
70
- this . element . removeEventListener ( 'mouseenter' , this . _mouseEnterHandler ) ;
71
- this . element . removeEventListener ( 'mouseleave' , this . _mouseLeaveHandler ) ;
72
- } ,
73
-
74
- progressDuration : computed ( 'flash.showProgress' , {
75
- get ( ) {
76
- if ( ! get ( this , 'flash.showProgress' ) ) {
77
- return false ;
78
- }
79
-
80
- const duration = getWithDefault ( this , 'flash.timeout' , 0 ) ;
81
-
82
- return htmlSafe ( `transition-duration: ${ duration } ms` ) ;
18
+ @readOnly ( 'flash.showProgress' )
19
+ showProgress ;
20
+
21
+ @not ( 'exiting' )
22
+ notExiting ;
23
+
24
+ @and ( 'showProgress' , 'notExiting' )
25
+ showProgressBar ;
26
+
27
+ @readOnly ( 'flash.exiting' )
28
+ exiting ;
29
+
30
+ @bool ( 'template' )
31
+ hasBlock ;
32
+
33
+ @computed ( 'flash.type' , 'messageStyle' )
34
+ get alertType ( ) {
35
+ const flashType = this . flash . type || '' ;
36
+ const messageStyle = this . messageStyle || '' ;
37
+ let prefix = 'alert alert-' ;
38
+
39
+ if ( messageStyle === 'foundation' ) {
40
+ prefix = 'alert-box ' ;
83
41
}
84
- } ) ,
85
42
86
- click ( ) {
87
- const destroyOnClick = getWithDefault ( this , 'flash.destroyOnClick' , true ) ;
43
+ return ` ${ prefix } ${ flashType } ` ;
44
+ }
88
45
89
- if ( destroyOnClick ) {
90
- this . _destroyFlashMessage ( ) ;
46
+ @computed ( 'flash.type' )
47
+ get flashType ( ) {
48
+ return classify ( this . flash . type || '' ) ;
49
+ }
50
+
51
+ @computed ( 'flash.{showProgress,timeout}' )
52
+ get progressDuration ( ) {
53
+ if ( ! this . flash ?. showProgress ) {
54
+ return false ;
91
55
}
92
- } ,
56
+ const duration = this . flash ?. timeout || 0 ;
57
+ return htmlSafe ( `transition-duration: ${ duration } ms` ) ;
58
+ }
93
59
94
60
_mouseEnter ( ) {
95
- const flash = get ( this , 'flash' ) ;
96
- if ( isPresent ( flash ) ) {
97
- flash . preventExit ( ) ;
61
+ if ( isPresent ( this . flash ) ) {
62
+ this . flash . preventExit ( ) ;
98
63
}
99
- } ,
64
+ }
100
65
101
66
_mouseLeave ( ) {
102
- const flash = get ( this , 'flash' ) ;
103
- if ( isPresent ( flash ) && ! get ( flash , 'exiting' ) ) {
104
- flash . allowExit ( ) ;
67
+ if ( isPresent ( this . flash ) && ! this . flash . exiting ) {
68
+ this . flash . allowExit ( ) ;
105
69
}
106
- } ,
107
-
108
- willDestroy ( ) {
109
- this . _super ( ...arguments ) ;
110
- this . _destroyFlashMessage ( ) ;
111
- cancel ( get ( this , 'pendingSet' ) ) ;
112
- } ,
70
+ }
113
71
114
- // private
115
72
_destroyFlashMessage ( ) {
116
- const flash = getWithDefault ( this , 'flash' , false ) ;
117
-
118
- if ( flash ) {
119
- flash . destroyMessage ( ) ;
73
+ if ( this . flash ) {
74
+ this . flash . destroyMessage ( ) ;
120
75
}
121
- } ,
76
+ }
122
77
123
- actions : {
124
- close ( ) {
78
+ @action
79
+ onClick ( ) {
80
+ const destroyOnClick = this . flash ?. destroyOnClick ?? true ;
81
+
82
+ if ( destroyOnClick ) {
125
83
this . _destroyFlashMessage ( ) ;
126
84
}
127
85
}
128
- } ) ;
86
+
87
+ @action
88
+ onClose ( ) {
89
+ this . _destroyFlashMessage ( ) ;
90
+ }
91
+
92
+ @action
93
+ onDidInsert ( element ) {
94
+ const pendingSet = next ( this , ( ) => {
95
+ set ( this , 'active' , true ) ;
96
+ } ) ;
97
+ set ( this , 'pendingSet' , pendingSet ) ;
98
+ set ( this , '_mouseEnterHandler' , this . _mouseEnter ) ;
99
+ set ( this , '_mouseLeaveHandler' , this . _mouseLeave ) ;
100
+ element . addEventListener ( 'mouseenter' , this . _mouseEnterHandler ) ;
101
+ element . addEventListener ( 'mouseleave' , this . _mouseLeaveHandler ) ;
102
+ }
103
+
104
+ @action
105
+ onWillDestroy ( element ) {
106
+ element . removeEventListener ( 'mouseenter' , this . _mouseEnterHandler ) ;
107
+ element . removeEventListener ( 'mouseleave' , this . _mouseLeaveHandler ) ;
108
+ cancel ( this . pendingSet ) ;
109
+ this . _destroyFlashMessage ( ) ;
110
+ }
111
+ }
0 commit comments