@@ -33,11 +33,12 @@ export interface IBaseActionViewItemOptions {
3333
3434export class BaseActionViewItem extends Disposable implements IActionViewItem {
3535
36- element ?: HTMLElement ;
36+ element : HTMLElement | undefined ;
37+
3738 _context : any ;
3839 _action : IAction ;
3940
40- private _actionRunner ! : IActionRunner ;
41+ private _actionRunner : IActionRunner | undefined ;
4142
4243 constructor ( context : any , action : IAction , protected options ?: IBaseActionViewItemOptions ) {
4344 super ( ) ;
@@ -81,14 +82,18 @@ export class BaseActionViewItem extends Disposable implements IActionViewItem {
8182 }
8283 }
8384
84- set actionRunner ( actionRunner : IActionRunner ) {
85- this . _actionRunner = actionRunner ;
86- }
87-
8885 get actionRunner ( ) : IActionRunner {
86+ if ( ! this . _actionRunner ) {
87+ this . _actionRunner = this . _register ( new ActionRunner ( ) ) ;
88+ }
89+
8990 return this . _actionRunner ;
9091 }
9192
93+ set actionRunner ( actionRunner : IActionRunner ) {
94+ this . _actionRunner = actionRunner ;
95+ }
96+
9297 getAction ( ) : IAction {
9398 return this . _action ;
9499 }
@@ -102,27 +107,27 @@ export class BaseActionViewItem extends Disposable implements IActionViewItem {
102107 }
103108
104109 render ( container : HTMLElement ) : void {
105- this . element = container ;
110+ const element = this . element = container ;
106111 this . _register ( Gesture . addTarget ( container ) ) ;
107112
108113 const enableDragging = this . options && this . options . draggable ;
109114 if ( enableDragging ) {
110115 container . draggable = true ;
111116 }
112117
113- this . _register ( DOM . addDisposableListener ( this . element , EventType . Tap , e => this . onClick ( e ) ) ) ;
118+ this . _register ( DOM . addDisposableListener ( element , EventType . Tap , e => this . onClick ( e ) ) ) ;
114119
115- this . _register ( DOM . addDisposableListener ( this . element , DOM . EventType . MOUSE_DOWN , e => {
120+ this . _register ( DOM . addDisposableListener ( element , DOM . EventType . MOUSE_DOWN , e => {
116121 if ( ! enableDragging ) {
117122 DOM . EventHelper . stop ( e , true ) ; // do not run when dragging is on because that would disable it
118123 }
119124
120- if ( this . _action . enabled && e . button === 0 && this . element ) {
121- DOM . addClass ( this . element , 'active' ) ;
125+ if ( this . _action . enabled && e . button === 0 ) {
126+ DOM . addClass ( element , 'active' ) ;
122127 }
123128 } ) ) ;
124129
125- this . _register ( DOM . addDisposableListener ( this . element , DOM . EventType . CLICK , e => {
130+ this . _register ( DOM . addDisposableListener ( element , DOM . EventType . CLICK , e => {
126131 DOM . EventHelper . stop ( e , true ) ;
127132 // See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Interact_with_the_clipboard
128133 // > Writing to the clipboard
@@ -139,14 +144,14 @@ export class BaseActionViewItem extends Disposable implements IActionViewItem {
139144 }
140145 } ) ) ;
141146
142- this . _register ( DOM . addDisposableListener ( this . element , DOM . EventType . DBLCLICK , e => {
147+ this . _register ( DOM . addDisposableListener ( element , DOM . EventType . DBLCLICK , e => {
143148 DOM . EventHelper . stop ( e , true ) ;
144149 } ) ) ;
145150
146151 [ DOM . EventType . MOUSE_UP , DOM . EventType . MOUSE_OUT ] . forEach ( event => {
147- this . _register ( DOM . addDisposableListener ( this . element ! , event , e => {
152+ this . _register ( DOM . addDisposableListener ( element , event , e => {
148153 DOM . EventHelper . stop ( e ) ;
149- DOM . removeClass ( this . element ! , 'active' ) ;
154+ DOM . removeClass ( element , 'active' ) ;
150155 } ) ) ;
151156 } ) ;
152157 }
@@ -165,7 +170,7 @@ export class BaseActionViewItem extends Disposable implements IActionViewItem {
165170 }
166171 }
167172
168- this . _actionRunner . run ( this . _action , context ) ;
173+ this . actionRunner . run ( this . _action , context ) ;
169174 }
170175
171176 focus ( ) : void {
@@ -232,7 +237,7 @@ export interface IActionViewItemOptions extends IBaseActionViewItemOptions {
232237
233238export class ActionViewItem extends BaseActionViewItem {
234239
235- protected label ! : HTMLElement ;
240+ protected label : HTMLElement | undefined ;
236241 protected options : IActionViewItemOptions ;
237242
238243 private cssClass ?: string ;
@@ -252,13 +257,17 @@ export class ActionViewItem extends BaseActionViewItem {
252257 if ( this . element ) {
253258 this . label = DOM . append ( this . element , DOM . $ ( 'a.action-label' ) ) ;
254259 }
255- if ( this . _action . id === Separator . ID ) {
256- this . label . setAttribute ( 'role' , 'presentation' ) ; // A separator is a presentation item
257- } else {
258- if ( this . options . isMenu ) {
259- this . label . setAttribute ( 'role' , 'menuitem ' ) ;
260+
261+
262+ if ( this . label ) {
263+ if ( this . _action . id === Separator . ID ) {
264+ this . label . setAttribute ( 'role' , 'presentation ' ) ; // A separator is a presentation item
260265 } else {
261- this . label . setAttribute ( 'role' , 'button' ) ;
266+ if ( this . options . isMenu ) {
267+ this . label . setAttribute ( 'role' , 'menuitem' ) ;
268+ } else {
269+ this . label . setAttribute ( 'role' , 'button' ) ;
270+ }
262271 }
263272 }
264273
@@ -276,11 +285,13 @@ export class ActionViewItem extends BaseActionViewItem {
276285 focus ( ) : void {
277286 super . focus ( ) ;
278287
279- this . label . focus ( ) ;
288+ if ( this . label ) {
289+ this . label . focus ( ) ;
290+ }
280291 }
281292
282293 updateLabel ( ) : void {
283- if ( this . options . label ) {
294+ if ( this . options . label && this . label ) {
284295 this . label . textContent = this . getAction ( ) . label ;
285296 }
286297 }
@@ -299,52 +310,65 @@ export class ActionViewItem extends BaseActionViewItem {
299310 }
300311 }
301312
302- if ( title ) {
313+ if ( title && this . label ) {
303314 this . label . title = title ;
304315 }
305316 }
306317
307318 updateClass ( ) : void {
308- if ( this . cssClass ) {
319+ if ( this . cssClass && this . label ) {
309320 DOM . removeClasses ( this . label , this . cssClass ) ;
310321 }
311322
312323 if ( this . options . icon ) {
313324 this . cssClass = this . getAction ( ) . class ;
314- DOM . addClass ( this . label , 'codicon' ) ;
315- if ( this . cssClass ) {
316- DOM . addClasses ( this . label , this . cssClass ) ;
325+
326+ if ( this . label ) {
327+ DOM . addClass ( this . label , 'codicon' ) ;
328+ if ( this . cssClass ) {
329+ DOM . addClasses ( this . label , this . cssClass ) ;
330+ }
317331 }
318332
319333 this . updateEnabled ( ) ;
320334 } else {
321- DOM . removeClass ( this . label , 'codicon' ) ;
335+ if ( this . label ) {
336+ DOM . removeClass ( this . label , 'codicon' ) ;
337+ }
322338 }
323339 }
324340
325341 updateEnabled ( ) : void {
326342 if ( this . getAction ( ) . enabled ) {
327- this . label . removeAttribute ( 'aria-disabled' ) ;
343+ if ( this . label ) {
344+ this . label . removeAttribute ( 'aria-disabled' ) ;
345+ DOM . removeClass ( this . label , 'disabled' ) ;
346+ this . label . tabIndex = 0 ;
347+ }
348+
328349 if ( this . element ) {
329350 DOM . removeClass ( this . element , 'disabled' ) ;
330351 }
331- DOM . removeClass ( this . label , 'disabled' ) ;
332- this . label . tabIndex = 0 ;
333352 } else {
334- this . label . setAttribute ( 'aria-disabled' , 'true' ) ;
353+ if ( this . label ) {
354+ this . label . setAttribute ( 'aria-disabled' , 'true' ) ;
355+ DOM . addClass ( this . label , 'disabled' ) ;
356+ DOM . removeTabIndexAndUpdateFocus ( this . label ) ;
357+ }
358+
335359 if ( this . element ) {
336360 DOM . addClass ( this . element , 'disabled' ) ;
337361 }
338- DOM . addClass ( this . label , 'disabled' ) ;
339- DOM . removeTabIndexAndUpdateFocus ( this . label ) ;
340362 }
341363 }
342364
343365 updateChecked ( ) : void {
344- if ( this . getAction ( ) . checked ) {
345- DOM . addClass ( this . label , 'checked' ) ;
346- } else {
347- DOM . removeClass ( this . label , 'checked' ) ;
366+ if ( this . label ) {
367+ if ( this . getAction ( ) . checked ) {
368+ DOM . addClass ( this . label , 'checked' ) ;
369+ } else {
370+ DOM . removeClass ( this . label , 'checked' ) ;
371+ }
348372 }
349373 }
350374}
0 commit comments