mirror of
https://github.com/microsoft/vscode.git
synced 2026-09-25 00:44:05 +01:00
selectBox restructure and select(index)
This commit is contained in:
@@ -709,6 +709,10 @@ export class SelectActionItem extends BaseActionItem {
|
||||
this.selectBox.setOptions(options, selected);
|
||||
}
|
||||
|
||||
public select(index: number): void {
|
||||
this.selectBox.select(index);
|
||||
}
|
||||
|
||||
private registerListeners(): void {
|
||||
this.toDispose.push(this.selectBox.onDidSelect(selected => {
|
||||
this.actionRunner.run(this._action, this.getActionContext(selected)).done();
|
||||
@@ -735,10 +739,6 @@ export class SelectActionItem extends BaseActionItem {
|
||||
this.selectBox.render(container);
|
||||
}
|
||||
|
||||
protected getSelected(): string {
|
||||
return this.selectBox.getSelected();
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
this.toDispose = lifecycle.dispose(this.toDispose);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import * as dom from 'vs/base/browser/dom';
|
||||
|
||||
export class SelectBox extends Widget {
|
||||
|
||||
private select: HTMLSelectElement;
|
||||
private selectElement: HTMLSelectElement;
|
||||
private options: string[];
|
||||
private selected: number;
|
||||
private container: HTMLElement;
|
||||
@@ -22,16 +22,16 @@ export class SelectBox extends Widget {
|
||||
constructor(options: string[], selected: number) {
|
||||
super();
|
||||
|
||||
this.select = document.createElement('select');
|
||||
this.select.className = 'select-box';
|
||||
this.selectElement = document.createElement('select');
|
||||
this.selectElement.className = 'select-box';
|
||||
|
||||
this.options = options;
|
||||
this.selected = selected;
|
||||
this.toDispose = [];
|
||||
this._onDidSelect = new Emitter<string>();
|
||||
|
||||
this.toDispose.push(dom.addStandardDisposableListener(this.select, 'change', (e) => {
|
||||
this.select.title = e.target.value;
|
||||
this.toDispose.push(dom.addStandardDisposableListener(this.selectElement, 'change', (e) => {
|
||||
this.selectElement.title = e.target.value;
|
||||
this._onDidSelect.fire(e.target.value);
|
||||
}));
|
||||
}
|
||||
@@ -42,58 +42,51 @@ export class SelectBox extends Widget {
|
||||
|
||||
public setOptions(options: string[], selected: number): void {
|
||||
this.options = options;
|
||||
if (selected >= 0) {
|
||||
this.selected = selected;
|
||||
} else if (this.selected < 0 || this.selected > this.options.length) {
|
||||
|
||||
this.selectElement.options.length = 0;
|
||||
this.options.forEach((option) => {
|
||||
this.selectElement.add(this.createOption(option));
|
||||
});
|
||||
this.select(selected);
|
||||
}
|
||||
|
||||
public select(index: number): void {
|
||||
if (index >= 0 && index < this.options.length) {
|
||||
this.selected = index;
|
||||
} else if (this.selected < 0) {
|
||||
this.selected = 0;
|
||||
}
|
||||
|
||||
this.doSetOptions();
|
||||
this.selectElement.selectedIndex = this.selected;
|
||||
this.selectElement.title = this.options[this.selected];
|
||||
}
|
||||
|
||||
public focus(): void {
|
||||
if (this.select) {
|
||||
this.select.focus();
|
||||
if (this.selectElement) {
|
||||
this.selectElement.focus();
|
||||
}
|
||||
}
|
||||
|
||||
public set enabled(value: boolean) {
|
||||
dom.toggleClass(this.container, 'disabled', !value);
|
||||
this.select.disabled = !value;
|
||||
this.selectElement.disabled = !value;
|
||||
}
|
||||
|
||||
public get enabled(): boolean {
|
||||
return !this.select.disabled;
|
||||
return !this.selectElement.disabled;
|
||||
}
|
||||
|
||||
public blur(): void {
|
||||
if (this.select) {
|
||||
this.select.blur();
|
||||
if (this.selectElement) {
|
||||
this.selectElement.blur();
|
||||
}
|
||||
}
|
||||
|
||||
public render(container: HTMLElement): void {
|
||||
this.container = container;
|
||||
dom.addClass(container, 'select-container');
|
||||
container.appendChild(this.select);
|
||||
this.doSetOptions();
|
||||
}
|
||||
|
||||
public getSelected(): string {
|
||||
return this.options && this.selected >= 0 && this.selected < this.options.length ? this.options[this.selected] : null;
|
||||
}
|
||||
|
||||
private doSetOptions(): void {
|
||||
this.select.options.length = 0;
|
||||
|
||||
this.options.forEach((option) => {
|
||||
this.select.add(this.createOption(option));
|
||||
});
|
||||
|
||||
if (this.selected >= 0) {
|
||||
this.select.selectedIndex = this.selected;
|
||||
this.select.title = this.options[this.selected];
|
||||
}
|
||||
container.appendChild(this.selectElement);
|
||||
this.setOptions(this.options, this.selected);
|
||||
}
|
||||
|
||||
private createOption(value: string): HTMLOptionElement {
|
||||
|
||||
Reference in New Issue
Block a user