@@ -10,7 +10,7 @@ import {Utils} from "./Utils.js";
1010// a) sets the title using pMenuItem.innerText = "xyz"
1111// b) arranges the visibility using pMenuItem.style.display = true/false
1212// 2: the callback function
13- // called when the menu item is selected: (pClickEvent ) => { ... }
13+ // called when the menu item is selected: () => { ... }
1414// all menu items are re-validated when the menu pops up
1515// when all menu items are invisible, the menu-button must be made invisible
1616// 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";
2020// the menu. when at least one item is visible, the menu is visible
2121// remember to call verifyApp() when that is potentially the case
2222
23+ // superclass for DropDownMenuRadio, DropDownMenuCheckBox and DropDownMenuCmd
24+
2325export class DropDownMenu {
2426
2527 // Creates an empty dropdown menu
@@ -87,10 +89,10 @@ export class DropDownMenu {
8789 continue ;
8890 }
8991
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 ) {
9496 chld . style . display = "none" ;
9597 continue ;
9698 }
@@ -106,6 +108,7 @@ export class DropDownMenu {
106108 itemsBeforeSeparator += 1 ;
107109 }
108110 }
111+
109112 // hide the menu when it has no visible menu-items
110113 const displayVisible = this . menuDropdown . tagName === "TD" ? "table-cell" : "inline-block" ;
111114 const displayInvisible = "none" ;
@@ -125,25 +128,49 @@ export class DropDownMenu {
125128 // function is called each time the menu opens
126129 // This allows dynamic menuitem titles (use menuitem.innerText)
127130 // or visibility (use menuitem.style.display = "none"/"inline-block")
128- addMenuItem ( pTitle , pCallBack , pValue ) {
131+ addMenuItem ( pValue , pTitle , pSystemCallBack , pUserCallBack ) {
132+
129133 const button = Utils . createDiv ( "run-command-button" , Character . HORIZONTAL_ELLIPSIS ) ;
130- if ( pValue ) {
131- button . _value = pValue ;
132- }
134+
135+ button . _value = pValue ;
136+
133137 if ( typeof pTitle === "string" ) {
134138 button . innerText = DropDownMenu . _sanitizeMenuItemTitle ( pTitle ) ;
135139 } else {
136- button . verifyCallBack = pTitle ;
140+ button . _titleCallBack = pTitle ;
137141 }
142+
138143 button . addEventListener ( "click" , ( pClickEvent ) => {
144+
145+ // hide the menu
139146 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
140151 window . setTimeout ( ( ) => {
141152 pClickEvent . target . parentElement . style . display = "" ;
142153 } , 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+
144168 pClickEvent . stopPropagation ( ) ;
145169 } ) ;
170+
146171 this . menuDropdownContent . appendChild ( button ) ;
172+
173+ // the menu might have become populated enough to get visible
147174 this . verifyAll ( ) ;
148175 return button ;
149176 }
@@ -157,11 +184,6 @@ export class DropDownMenu {
157184 this . menuDropdownContent . appendChild ( div ) ;
158185 }
159186
160- _callback ( pClickEvent , pCallBack , pValue ) {
161- this . _value = pValue ;
162- pCallBack ( pClickEvent ) ;
163- }
164-
165187 setTitle ( pTitle ) {
166188 // Setting the title implies that we are interested
167189 // in the menu values, rather than their actions.
@@ -172,18 +194,4 @@ export class DropDownMenu {
172194 pTitle += Character . GEAR ;
173195 this . menuButton . innerText = DropDownMenu . _sanitizeMenuItemTitle ( pTitle ) ;
174196 }
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- }
189197}
0 commit comments