3
3
*--------------------------------------------------------*/
4
4
5
5
import vscode = require( "vscode" ) ;
6
- import QuickPickItem = vscode . QuickPickItem ;
7
6
8
- export class CheckboxQuickPickItem {
9
- name : string ;
7
+ var confirmItemLabel : string = "$(checklist) Confirm" ;
8
+ var checkedPrefix : string = "[ $(check) ]" ;
9
+ var uncheckedPrefix : string = "[ ]" ;
10
+ var defaultPlaceHolder : string = "Select 'Confirm' to confirm or press 'Esc' key to cancel" ;
11
+
12
+ export interface CheckboxQuickPickItem {
13
+ label : string ;
14
+ description ?: string ;
10
15
isSelected : boolean ;
11
16
}
12
17
13
- export class CheckboxQuickPick {
14
- private static readonly confirm : string = "$(check)" ;
15
- private static readonly checkboxOn : string = "[ x ]" ;
16
- private static readonly checkboxOff : string = "[ ]" ;
17
- private static readonly confirmPlaceHolder : string = "Select 'Confirm' to confirm change; Press 'esc' key to cancel changes" ;
18
-
19
- public static show (
20
- checkboxQuickPickItems : CheckboxQuickPickItem [ ] ,
21
- callback : ( items : CheckboxQuickPickItem [ ] ) => void ) : void {
22
- CheckboxQuickPick . showInner ( checkboxQuickPickItems . slice ( ) , callback ) ;
23
- }
24
-
25
- private static showInner (
26
- items : CheckboxQuickPickItem [ ] ,
27
- callback : ( items : CheckboxQuickPickItem [ ] ) => void ) : void {
18
+ export interface CheckboxQuickPickOptions {
19
+ confirmPlaceHolder : string ;
20
+ }
21
+
22
+ var defaultOptions :CheckboxQuickPickOptions = { confirmPlaceHolder : defaultPlaceHolder } ;
23
+
24
+ export function showCheckboxQuickPick (
25
+ items : CheckboxQuickPickItem [ ] ,
26
+ options : CheckboxQuickPickOptions = defaultOptions ) : Thenable < CheckboxQuickPickItem [ ] > {
27
+
28
+ return showInner ( items , options ) . then (
29
+ ( selectedItem ) => {
30
+ // We're mutating the original item list so just return it for now.
31
+ // If 'selectedItem' is undefined it means the user cancelled the
32
+ // inner showQuickPick UI so pass the undefined along.
33
+ return selectedItem != undefined ? items : undefined ;
34
+ } )
35
+ }
36
+
37
+ function getQuickPickItems ( items : CheckboxQuickPickItem [ ] ) : vscode . QuickPickItem [ ] {
38
+
39
+ let quickPickItems : vscode . QuickPickItem [ ] = [ ] ;
40
+ quickPickItems . push ( { label : confirmItemLabel , description : "" } ) ;
41
+
42
+ items . forEach ( item =>
43
+ quickPickItems . push ( {
44
+ label : convertToCheckBox ( item ) ,
45
+ description : item . description
46
+ } ) ) ;
47
+
48
+ return quickPickItems ;
49
+ }
50
+
51
+ function showInner (
52
+ items : CheckboxQuickPickItem [ ] ,
53
+ options : CheckboxQuickPickOptions ) : Thenable < vscode . QuickPickItem > {
54
+
55
+ var quickPickThenable : Thenable < vscode . QuickPickItem > =
28
56
vscode . window . showQuickPick (
29
- CheckboxQuickPick . getQuickPickItems ( items ) ,
57
+ getQuickPickItems ( items ) ,
30
58
{
31
59
ignoreFocusOut : true ,
32
60
matchOnDescription : true ,
33
- placeHolder : CheckboxQuickPick . confirmPlaceHolder
34
- } ) . then ( ( selection ) => {
35
- if ( ! selection ) {
36
- return ;
37
- }
38
-
39
- if ( selection . label === CheckboxQuickPick . confirm ) {
40
- callback ( items ) ;
41
- return ;
42
- }
43
-
44
- let index : number = CheckboxQuickPick . getRuleIndex ( items , selection . description ) ;
45
- CheckboxQuickPick . toggleSelection ( items [ index ] ) ;
46
- CheckboxQuickPick . showInner ( items , callback ) ;
61
+ placeHolder : options . confirmPlaceHolder
47
62
} ) ;
48
- }
49
-
50
- private static getRuleIndex ( items : CheckboxQuickPickItem [ ] , itemLabel : string ) : number {
51
- return items . findIndex ( item => item . name === itemLabel ) ;
52
- }
53
-
54
- private static getQuickPickItems ( items : CheckboxQuickPickItem [ ] ) : QuickPickItem [ ] {
55
- let quickPickItems : QuickPickItem [ ] = [ ] ;
56
- quickPickItems . push ( { label : CheckboxQuickPick . confirm , description : "Confirm" } ) ;
57
- items . forEach ( item =>
58
- quickPickItems . push ( {
59
- label : CheckboxQuickPick . convertToCheckBox ( item . isSelected ) , description : item . name
60
- } ) ) ;
61
- return quickPickItems ;
62
- }
63
-
64
- private static toggleSelection ( item : CheckboxQuickPickItem ) : void {
65
- item . isSelected = ! item . isSelected ;
66
- }
67
-
68
- private static convertToCheckBox ( state : boolean ) : string {
69
- if ( state ) {
70
- return CheckboxQuickPick . checkboxOn ;
71
- }
72
- else {
73
- return CheckboxQuickPick . checkboxOff ;
74
- }
75
- }
63
+
64
+ return quickPickThenable . then (
65
+ ( selection ) => {
66
+ if ( ! selection ) {
67
+ //return Promise.reject<vscode.QuickPickItem>("showCheckBoxQuickPick cancelled")
68
+ return Promise . resolve < vscode . QuickPickItem > ( undefined ) ;
69
+ }
70
+
71
+ if ( selection . label === confirmItemLabel ) {
72
+ return selection ;
73
+ }
74
+
75
+ let index : number = getItemIndex ( items , selection . label ) ;
76
+
77
+ if ( index >= 0 ) {
78
+ toggleSelection ( items [ index ] ) ;
79
+ }
80
+ else {
81
+ console . log ( `Couldn't find CheckboxQuickPickItem for label '${ selection . label } '` ) ;
82
+ }
83
+
84
+ return showInner ( items , options ) ;
85
+ } ) ;
86
+ }
87
+
88
+ function getItemIndex ( items : CheckboxQuickPickItem [ ] , itemLabel : string ) : number {
89
+ var trimmedLabel = itemLabel . substr ( itemLabel . indexOf ( "]" ) + 2 ) ;
90
+ return items . findIndex ( item => item . label === trimmedLabel ) ;
91
+ }
92
+
93
+ function toggleSelection ( item : CheckboxQuickPickItem ) : void {
94
+ item . isSelected = ! item . isSelected ;
95
+ }
96
+
97
+ function convertToCheckBox ( item : CheckboxQuickPickItem ) : string {
98
+ return `${ item . isSelected ? checkedPrefix : uncheckedPrefix } ${ item . label } ` ;
76
99
}
0 commit comments