@@ -15,41 +15,71 @@ import NotifyConstructor from './src/pc.vue'
15
15
import '@opentiny/vue-theme/notify/index.less'
16
16
import { IconSuccess , iconError , IconPrompt , iconWarningTriangle } from '@opentiny/vue-icon'
17
17
18
+ type NotifyType = 'warning' | 'error' | 'info' | 'success'
19
+ type NotifyPosition = 'top-right' | 'bottom-right' | 'top-left' | 'bottom-left'
20
+
21
+ interface NotifyOptions {
22
+ type ?: NotifyType
23
+ duration ?: number
24
+ position ?: NotifyPosition
25
+ statusIcon ?: any
26
+ offset ?: number
27
+ verticalOffset ?: number
28
+ debounceDelay ?: number
29
+ onClose ?: ( instance : NotifyInstance ) => void
30
+ [ key : string ] : any
31
+ }
32
+
33
+ interface NotifyState {
34
+ position : NotifyPosition
35
+ verticalOffset : number
36
+ visible : boolean
37
+ verticalProperty : string
38
+ }
39
+
40
+ interface NotifyInstance {
41
+ id : string
42
+ $el : HTMLElement
43
+ dom : HTMLElement
44
+ state : NotifyState
45
+ position : NotifyPosition
46
+ getZindex : ( ) => number
47
+ close : ( id : string ) => void
48
+ }
49
+
18
50
let seed = 1
19
- let instances = [ ]
51
+ let instances : NotifyInstance [ ] = [ ]
20
52
21
- const IconMap = {
53
+ const IconMap : Record < NotifyType , any > = {
22
54
warning : iconWarningTriangle ( ) ,
23
55
error : iconError ( ) ,
24
56
info : IconPrompt ( ) ,
25
57
success : IconSuccess ( )
26
58
}
27
59
28
- const durationMap = {
60
+ const durationMap : Record < NotifyType , number > = {
29
61
info : 5000 ,
30
62
success : 5000 ,
31
63
warning : 10000 ,
32
64
error : 10000
33
65
}
34
66
35
- const positionList = [ 'top-right' , 'bottom-right' , 'top-left' , 'bottom-left' ]
67
+ const positionList : NotifyPosition [ ] = [ 'top-right' , 'bottom-right' , 'top-left' , 'bottom-left' ]
36
68
37
- const debounce = ( fn , debounceDelay ) => {
38
- let timer = null
69
+ const debounce = ( fn : ( options : NotifyOptions ) => NotifyInstance , debounceDelay : number ) => {
70
+ let timer : NodeJS . Timeout | null = null
39
71
40
- return async function ( ) {
72
+ return async function ( this : any , ... args : any [ ] ) {
41
73
if ( timer ) {
42
74
clearTimeout ( timer )
43
75
}
44
76
45
- let instance = null
77
+ let instance : NotifyInstance | null = null
46
78
47
- await new Promise ( ( resolve ) => {
79
+ await new Promise < void > ( ( resolve ) => {
48
80
timer = setTimeout ( ( ) => {
49
- // eslint-disable-next-line prefer-rest-params
50
- instance = fn . apply ( this , arguments )
81
+ instance = fn . apply ( this , args )
51
82
timer = null
52
-
53
83
resolve ( )
54
84
} , debounceDelay )
55
85
} )
@@ -58,18 +88,18 @@ const debounce = (fn, debounceDelay) => {
58
88
}
59
89
}
60
90
61
- const notify = ( options ) => {
62
- if ( ! ~ Object . keys ( IconMap ) . indexOf ( options . type ) ) {
91
+ const notify = ( options : NotifyOptions ) : NotifyInstance => {
92
+ if ( ! Object . keys ( IconMap ) . includes ( options . type as NotifyType ) ) {
63
93
options . type = 'info'
64
94
}
65
95
66
- options . duration = options . duration ? options . duration : durationMap [ options . type ]
67
- options . position = ! ~ positionList . indexOf ( options . position ) ? 'bottom-right' : options . position
68
- ! options . statusIcon && options . type && ( options . statusIcon = IconMap [ options . type ] )
96
+ options . duration = options . duration ? options . duration : durationMap [ options . type as NotifyType ]
97
+ options . position = ! positionList . includes ( options . position as NotifyPosition ) ? 'bottom-right' : options . position
98
+ ! options . statusIcon && options . type && ( options . statusIcon = IconMap [ options . type as NotifyType ] )
69
99
70
100
const id = 'notify_' + seed ++
71
101
const userOnClose = options . onClose
72
- const position = options . position
102
+ const position = options . position as NotifyPosition
73
103
74
104
options . onClose = function ( ) {
75
105
Notify . close ( id , userOnClose )
@@ -79,7 +109,7 @@ const notify = (options) => {
79
109
el : document . createElement ( 'div' ) ,
80
110
propsData : options ,
81
111
component : NotifyConstructor
82
- } )
112
+ } ) as NotifyInstance
83
113
instance . id = id
84
114
document . body . appendChild ( instance . $el )
85
115
@@ -95,7 +125,7 @@ const notify = (options) => {
95
125
instances . push ( instance )
96
126
97
127
instance . dom = instance . $el
98
- instance . dom . style . zIndex = instance . getZindex ( )
128
+ instance . dom . style . zIndex = instance . getZindex ( ) . toString ( )
99
129
instance . state . verticalOffset = verticalOffset
100
130
instance . state . visible = true
101
131
@@ -106,7 +136,7 @@ const notify = (options) => {
106
136
return instance
107
137
}
108
138
109
- const Notify = ( options ) => {
139
+ const Notify = ( options : NotifyOptions ) : NotifyInstance | ( ( options : NotifyOptions ) => Promise < NotifyInstance > ) => {
110
140
let { debounceDelay } = options
111
141
112
142
if ( debounceDelay ) {
@@ -115,10 +145,11 @@ const Notify = (options) => {
115
145
return notify ( options )
116
146
}
117
147
}
118
- Notify . close = function ( id , userOnClose ) {
148
+
149
+ Notify . close = function ( id : string , userOnClose ?: ( instance : NotifyInstance ) => void ) : void {
119
150
let index = - 1
120
151
let len = instances . length
121
- let instance
152
+ let instance : NotifyInstance | undefined
122
153
123
154
for ( let i = 0 ; i < len ; i ++ ) {
124
155
let tmp = instances [ i ]
@@ -135,7 +166,7 @@ Notify.close = function (id, userOnClose) {
135
166
136
167
typeof userOnClose === 'function' && userOnClose ( instance )
137
168
let lastHeight = instance . $el . offsetHeight
138
- instance . $el . parentNode . removeChild ( instance . $el )
169
+ instance . $el . parentNode ? .removeChild ( instance . $el )
139
170
instances . splice ( index , 1 )
140
171
141
172
if ( len <= 1 ) {
@@ -161,7 +192,7 @@ Notify.close = function (id, userOnClose) {
161
192
} )
162
193
}
163
194
164
- Notify . closeAll = function ( ) {
195
+ Notify . closeAll = function ( ) : void {
165
196
let copys = instances . slice ( 0 )
166
197
167
198
copys = copys . reverse ( )
0 commit comments