1
1
import {
2
2
IStatusBar ,
3
3
IStatusBarItem ,
4
+ StatusBarEvent ,
4
5
StatusBarModel ,
5
6
} from 'mo/model/workbench/statusBar' ;
6
7
import { Component } from 'mo/react' ;
7
- import { emit } from 'mo/common/event' ;
8
8
import { container , singleton } from 'tsyringe' ;
9
9
10
- /**
11
- * The activity bar event definition
12
- */
13
- export enum StatusBarEvent {
10
+ export interface IStatusBarService extends Component < IStatusBar > {
11
+ appendLeftItem ( item : IStatusBarItem ) : void ;
12
+ appendRightItem ( item : IStatusBarItem ) : void ;
13
+ updateItem ( item : IStatusBarItem ) : void ;
14
+ findById ( id : string ) : IStatusBarItem | null ;
15
+ /**
16
+ * Remove the left item of StatusBar,
17
+ * return the removed item.
18
+ * @param id
19
+ */
20
+ removeLeftItem ( id : string ) : IStatusBarItem ;
14
21
/**
15
- * Selected an activity bar
22
+ * Remove the right item of StatusBar,
23
+ * return the removed item.
24
+ * @param id
16
25
*/
17
- onClick = 'statusBar.onClick' ,
26
+ removeRightItem ( id : string ) : IStatusBarItem ;
18
27
/**
19
- * Activity bar data changed
28
+ * Listen to the statusbar onclick event
29
+ * @param callback
20
30
*/
21
- DataChanged = 'statusBar.data' ,
31
+ onClick ( callback : ( e : MouseEvent , item : IStatusBarItem ) => void ) ;
22
32
}
23
33
24
- export interface IStatusBarService extends Component < IStatusBar > {
25
- push ( data : IStatusBarItem | IStatusBarItem [ ] ) : void ;
26
- remove ( index : number ) : void ;
34
+ function searchById ( id : string ) {
35
+ return ( item : IStatusBarItem ) => item . id === id ;
27
36
}
28
-
29
37
@singleton ( )
30
38
export class StatusBarService
31
39
extends Component < IStatusBar >
@@ -37,17 +45,46 @@ export class StatusBarService
37
45
this . state = container . resolve ( StatusBarModel ) ;
38
46
}
39
47
40
- @emit ( StatusBarEvent . DataChanged )
41
- public push ( data : IStatusBarItem | IStatusBarItem [ ] ) {
42
- let original = this . state . data ;
43
- if ( Array . isArray ( data ) ) {
44
- original = original . concat ( data ) ;
45
- } else {
46
- original . push ( data ) ;
48
+ onClick ( callback : ( e : MouseEvent , item : IStatusBarItem ) => void ) {
49
+ this . subscribe ( StatusBarEvent . onClick , callback ) ;
50
+ }
51
+
52
+ private remove ( id : string , arr : IStatusBarItem [ ] ) : IStatusBarItem {
53
+ const index = arr . findIndex ( searchById ( id ) ) ;
54
+ const result = arr . splice ( index , 1 ) ;
55
+ return result [ 0 ] ;
56
+ }
57
+
58
+ removeLeftItem ( id : string ) : IStatusBarItem {
59
+ return this . remove ( id , this . state . leftItems ) ;
60
+ }
61
+
62
+ removeRightItem ( id : string ) : IStatusBarItem {
63
+ return this . remove ( id , this . state . rightItems ) ;
64
+ }
65
+
66
+ findById ( id : string ) : IStatusBarItem {
67
+ let result ;
68
+ const { leftItems, rightItems } = this . state ;
69
+ result = leftItems . find ( searchById ( id ) ) ;
70
+ if ( ! result ) {
71
+ result = rightItems . find ( searchById ( id ) ) ;
47
72
}
73
+ return result ;
48
74
}
49
75
50
- public remove ( index : number ) {
51
- this . state . data . splice ( index , 1 ) ;
76
+ appendLeftItem ( item : IStatusBarItem ) : void {
77
+ this . state . leftItems . push ( item ) ;
78
+ }
79
+
80
+ appendRightItem ( item : IStatusBarItem ) : void {
81
+ this . state . rightItems . push ( item ) ;
82
+ }
83
+
84
+ updateItem ( item : IStatusBarItem ) : void {
85
+ const original = this . findById ( item . id ) ;
86
+ if ( original ) {
87
+ Object . assign ( original , item ) ;
88
+ }
52
89
}
53
90
}
0 commit comments