@@ -10,7 +10,7 @@ import {Utils} from "./Utils.js";
10
10
// a) sets the title using pMenuItem.innerText = "xyz"
11
11
// b) arranges the visibility using pMenuItem.style.display = true/false
12
12
// 2: the callback function
13
- // called when the menu item is selected: (pClickEvent ) => { ... }
13
+ // called when the menu item is selected: () => { ... }
14
14
// all menu items are re-validated when the menu pops up
15
15
// when all menu items are invisible, the menu-button must be made invisible
16
16
// since this can happen at any time, this cannot be done when the menu is shown
@@ -20,6 +20,8 @@ import {Utils} from "./Utils.js";
20
20
// the menu. when at least one item is visible, the menu is visible
21
21
// remember to call verifyApp() when that is potentially the case
22
22
23
+ // superclass for DropDownMenuRadio, DropDownMenuCheckBox and DropDownMenuCmd
24
+
23
25
export class DropDownMenu {
24
26
25
27
// Creates an empty dropdown menu
@@ -87,10 +89,10 @@ export class DropDownMenu {
87
89
continue ;
88
90
}
89
91
90
- const verifyCallBack = chld . verifyCallBack ;
91
- if ( verifyCallBack ) {
92
- const title = verifyCallBack ( chld ) ;
93
- if ( title === null ) {
92
+ const titleCallBack = chld . _titleCallBack ;
93
+ if ( titleCallBack ) {
94
+ const title = titleCallBack . bind ( this ) ( chld ) ;
95
+ if ( ! title ) {
94
96
chld . style . display = "none" ;
95
97
continue ;
96
98
}
@@ -106,6 +108,7 @@ export class DropDownMenu {
106
108
itemsBeforeSeparator += 1 ;
107
109
}
108
110
}
111
+
109
112
// hide the menu when it has no visible menu-items
110
113
const displayVisible = this . menuDropdown . tagName === "TD" ? "table-cell" : "inline-block" ;
111
114
const displayInvisible = "none" ;
@@ -125,25 +128,49 @@ export class DropDownMenu {
125
128
// function is called each time the menu opens
126
129
// This allows dynamic menuitem titles (use menuitem.innerText)
127
130
// or visibility (use menuitem.style.display = "none"/"inline-block")
128
- addMenuItem ( pTitle , pCallBack , pValue ) {
131
+ addMenuItem ( pValue , pTitle , pSystemCallBack , pUserCallBack ) {
132
+
129
133
const button = Utils . createDiv ( "run-command-button" , "..." ) ;
130
- if ( pValue ) {
131
- button . _value = pValue ;
132
- }
134
+
135
+ button . _value = pValue ;
136
+
133
137
if ( typeof pTitle === "string" ) {
134
138
button . innerText = DropDownMenu . _sanitizeMenuItemTitle ( pTitle ) ;
135
139
} else {
136
- button . verifyCallBack = pTitle ;
140
+ button . _titleCallBack = pTitle ;
137
141
}
142
+
138
143
button . addEventListener ( "click" , ( pClickEvent ) => {
144
+
145
+ // hide the menu
139
146
pClickEvent . target . parentElement . style . display = "none" ;
147
+
148
+ // "show" the menu again after a short delay
149
+ // but because the mouse is no longer hovering it,
150
+ // it will actually remain invisible
140
151
window . setTimeout ( ( ) => {
141
152
pClickEvent . target . parentElement . style . display = "" ;
142
153
} , 500 ) ;
143
- this . _callback ( pClickEvent , pCallBack , pValue ) ;
154
+
155
+ this . _value = pValue ;
156
+
157
+ if ( pSystemCallBack ) {
158
+ pSystemCallBack . bind ( this ) ( pClickEvent . target ) ;
159
+ }
160
+
161
+ if ( pUserCallBack ) {
162
+ pUserCallBack . bind ( this ) ( pClickEvent . target ) ;
163
+ }
164
+
165
+ // all menu items may have become invisible
166
+ this . verifyAll ( ) ;
167
+
144
168
pClickEvent . stopPropagation ( ) ;
145
169
} ) ;
170
+
146
171
this . menuDropdownContent . appendChild ( button ) ;
172
+
173
+ // the menu might have become populated enough to get visible
147
174
this . verifyAll ( ) ;
148
175
return button ;
149
176
}
@@ -157,11 +184,6 @@ export class DropDownMenu {
157
184
this . menuDropdownContent . appendChild ( div ) ;
158
185
}
159
186
160
- _callback ( pClickEvent , pCallBack , pValue ) {
161
- this . _value = pValue ;
162
- pCallBack ( pClickEvent ) ;
163
- }
164
-
165
187
setTitle ( pTitle ) {
166
188
// Setting the title implies that we are interested
167
189
// in the menu values, rather than their actions.
@@ -172,18 +194,4 @@ export class DropDownMenu {
172
194
pTitle += Character . GEAR ;
173
195
this . menuButton . innerText = DropDownMenu . _sanitizeMenuItemTitle ( pTitle ) ;
174
196
}
175
-
176
- __showMenu ( ) {
177
- this . menuDropdown . style . display = "inline-block" ;
178
- }
179
-
180
- __hideMenu ( ) {
181
- this . menuDropdown . style . display = "none" ;
182
- }
183
-
184
- clearMenu ( ) {
185
- while ( this . menuDropdownContent . firstChild ) {
186
- this . menuDropdownContent . removeChild ( this . menuDropdownContent . firstChild ) ;
187
- }
188
- }
189
197
}
0 commit comments