提交 c2b17260 编写于 作者: M Matt Bierner

Strict null check action bar

上级 4bb81b60
......@@ -22,6 +22,7 @@
"./vs/base/browser/keyboardEvent.ts",
"./vs/base/browser/mouseEvent.ts",
"./vs/base/browser/touch.ts",
"./vs/base/browser/ui/actionbar/actionbar.ts",
"./vs/base/browser/ui/aria/aria.ts",
"./vs/base/browser/ui/breadcrumbs/breadcrumbsWidget.ts",
"./vs/base/browser/ui/button/button.ts",
......
......@@ -18,7 +18,7 @@ import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview
import { Event, Emitter } from 'vs/base/common/event';
export interface IActionItem {
actionRunner: IActionRunner;
actionRunner?: IActionRunner;
setActionContext(context: any): void;
render(element: HTMLElement): void;
isEnabled(): boolean;
......@@ -34,7 +34,7 @@ export interface IBaseActionItemOptions {
export class BaseActionItem extends Disposable implements IActionItem {
element: HTMLElement;
element?: HTMLElement;
_context: any;
_action: IAction;
......@@ -118,7 +118,7 @@ export class BaseActionItem extends Disposable implements IActionItem {
DOM.EventHelper.stop(e, true); // do not run when dragging is on because that would disable it
}
if (this._action.enabled && e.button === 0) {
if (this._action.enabled && e.button === 0 && this.element) {
DOM.addClass(this.element, 'active');
}
}));
......@@ -145,9 +145,9 @@ export class BaseActionItem extends Disposable implements IActionItem {
}));
[DOM.EventType.MOUSE_UP, DOM.EventType.MOUSE_OUT].forEach(event => {
this._register(DOM.addDisposableListener(this.element, event, e => {
this._register(DOM.addDisposableListener(this.element!, event, e => {
DOM.EventHelper.stop(e);
DOM.removeClass(this.element, 'active');
DOM.removeClass(this.element!, 'active');
}));
});
}
......@@ -206,7 +206,7 @@ export class BaseActionItem extends Disposable implements IActionItem {
dispose(): void {
if (this.element) {
DOM.removeNode(this.element);
this.element = null;
this.element = undefined;
}
super.dispose();
......@@ -236,7 +236,7 @@ export class ActionItem extends BaseActionItem {
protected label: HTMLElement;
protected options: IActionItemOptions;
private cssClass: string;
private cssClass?: string;
constructor(context: any, action: IAction, options: IActionItemOptions = {}) {
super(context, action, options);
......@@ -250,7 +250,9 @@ export class ActionItem extends BaseActionItem {
render(container: HTMLElement): void {
super.render(container);
if (this.element) {
this.label = DOM.append(this.element, DOM.$('a.action-label'));
}
if (this._action.id === Separator.ID) {
this.label.setAttribute('role', 'presentation'); // A separator is a presentation item
} else {
......@@ -261,7 +263,7 @@ export class ActionItem extends BaseActionItem {
}
}
if (this.options.label && this.options.keybinding) {
if (this.options.label && this.options.keybinding && this.element) {
DOM.append(this.element, DOM.$('span.keybinding')).textContent = this.options.keybinding;
}
......@@ -324,12 +326,16 @@ export class ActionItem extends BaseActionItem {
updateEnabled(): void {
if (this.getAction().enabled) {
this.label.removeAttribute('aria-disabled');
if (this.element) {
DOM.removeClass(this.element, 'disabled');
}
DOM.removeClass(this.label, 'disabled');
this.label.tabIndex = 0;
} else {
this.label.setAttribute('aria-disabled', 'true');
if (this.element) {
DOM.addClass(this.element, 'disabled');
}
DOM.addClass(this.label, 'disabled');
DOM.removeTabIndexAndUpdateFocus(this.label);
}
......@@ -387,12 +393,12 @@ export class ActionBar extends Disposable implements IActionRunner {
options: IActionBarOptions;
private _actionRunner: IActionRunner;
private _actionRunner?: IActionRunner;
private _context: any;
// Items
items: IActionItem[];
protected focusedItem: number;
protected focusedItem?: number;
private focusTracker: DOM.IFocusTracker;
// Elements
......@@ -495,7 +501,7 @@ export class ActionBar extends Disposable implements IActionRunner {
// Run action on Enter/Space
if (this.isTriggerKeyEvent(event)) {
if (!this.options.triggerKeys.keyDown) {
if (this.options.triggerKeys && !this.options.triggerKeys.keyDown) {
this.doTrigger(event);
}
......@@ -570,11 +576,11 @@ export class ActionBar extends Disposable implements IActionRunner {
this.items.forEach(i => i.setActionContext(context));
}
get actionRunner(): IActionRunner {
get actionRunner(): IActionRunner | undefined {
return this._actionRunner;
}
set actionRunner(actionRunner: IActionRunner) {
set actionRunner(actionRunner: IActionRunner | undefined) {
if (actionRunner) {
this._actionRunner = actionRunner;
this.items.forEach(item => item.actionRunner = actionRunner);
......@@ -628,7 +634,10 @@ export class ActionBar extends Disposable implements IActionRunner {
getWidth(index: number): number {
if (index >= 0 && index < this.actionsList.children.length) {
return this.actionsList.children.item(index).clientWidth;
const item = this.actionsList.children.item(index);
if (item) {
return item.clientWidth;
}
}
return 0;
......@@ -636,7 +645,10 @@ export class ActionBar extends Disposable implements IActionRunner {
getHeight(index: number): number {
if (index >= 0 && index < this.actionsList.children.length) {
return this.actionsList.children.item(index).clientHeight;
const item = this.actionsList.children.item(index);
if (item) {
return item.clientHeight;
}
}
return 0;
......@@ -765,14 +777,15 @@ export class ActionBar extends Disposable implements IActionRunner {
}
run(action: IAction, context?: any): Thenable<void> {
if (this._actionRunner) {
return this._actionRunner.run(action, context);
}
return Promise.resolve(void 0);
}
dispose(): void {
if (this.items !== null) {
dispose(this.items);
}
this.items = null;
this.items = [];
DOM.removeNode(this.getContainer());
......@@ -786,7 +799,7 @@ export class SelectActionItem extends BaseActionItem {
constructor(ctx: any, action: IAction, options: string[], selected: number, contextViewProvider: IContextViewProvider, selectBoxOptions?: ISelectBoxOptions) {
super(ctx, action);
this.selectBox = new SelectBox(options, selected, contextViewProvider, null, selectBoxOptions);
this.selectBox = new SelectBox(options, selected, contextViewProvider, undefined, selectBoxOptions);
this._register(this.selectBox);
this.registerListeners();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册