Hook up generateScriptButton to lastPageValidation (#13339)

This commit is contained in:
Arvind Ranasaria
2020-11-10 13:16:25 -08:00
committed by GitHub
parent 3dd74971d8
commit d7a6b55f82
4 changed files with 51 additions and 30 deletions

View File

@@ -57,10 +57,20 @@ export class NotebookWizardModel extends ResourceTypeModel {
public onCancel(): void {
}
public async onGenerateScript(): Promise<void> {
const notebook = await this.prepareNotebookAndEnvironment();
await this.openNotebook(notebook);
/**
* Generates the notebook and returns true on successful generation
**/
public async onGenerateScript(): Promise<boolean> {
const lastPage = this.wizard.lastPage! as NotebookWizardPage;
if (lastPage.validatePage()) {
const notebook = await this.prepareNotebookAndEnvironment();
await this.openNotebook(notebook);
return true;
} else {
return false;
}
}
public async onOk(): Promise<void> {
const notebook = await this.prepareNotebookAndEnvironment();
const openedNotebook = await this.openNotebook(notebook);

View File

@@ -108,31 +108,34 @@ export class NotebookWizardPage extends ResourceTypePage {
* The first condition checks that edge case.
*/
if (pcInfo.newPage === undefined || pcInfo.newPage > pcInfo.lastPage) {
const messages: string[] = [];
this.validators.forEach((validator) => {
const result = validator();
if (!result.valid) {
messages.push(result.message);
}
});
if (messages.length > 0) {
this.wizard.wizardObject.message = {
text:
messages.length === 1
? messages[0]
: localize(
"wizardPage.ValidationError",
"There are some errors on this page, click 'Show Details' to view the errors."
),
description: messages.length === 1 ? undefined : messages.join(EOL),
level: azdata.window.MessageLevel.Error,
};
}
return messages.length === 0;
return this.validatePage();
}
return true;
});
}
public validatePage(): boolean {
const messages: string[] = [];
this.validators.forEach((validator) => {
const result = validator();
if (!result.valid) {
messages.push(result.message);
}
});
if (messages.length > 0) {
this.wizard.wizardObject.message = {
text: messages.length === 1
? messages[0]
: localize(
"wizardPage.ValidationError",
"There are some errors on this page, click 'Show Details' to view the errors."
),
description: messages.length === 1 ? undefined : messages.join(EOL),
level: azdata.window.MessageLevel.Error,
};
}
return messages.length === 0;
}
}

View File

@@ -16,6 +16,9 @@ export abstract class ResourceTypeModel extends Model {
abstract initialize(): void;
abstract async onOk(): Promise<void>;
abstract onCancel(): void;
async onGenerateScript(): Promise<void> { }
/**
* performs the script generation and returns true if script was generated successfully
**/
async onGenerateScript(): Promise<boolean> { return true; }
}

View File

@@ -47,6 +47,10 @@ export class ResourceTypeWizard {
return this._model;
}
public get lastPage(): ResourceTypePage | undefined {
return this.pages.length > 0 ? this.pages[this.pages.length - 1] : undefined;
}
constructor(
public resourceType: ResourceType,
public _kubeService: IKubeService,
@@ -93,9 +97,10 @@ export class ResourceTypeWizard {
this.dispose();
}));
this.toDispose.push(this.wizardObject.generateScriptButton.onClick(async () => {
await this._model.onGenerateScript();
this.dispose();
this.wizardObject.close(); // close the wizard. This is already hooked up into doneButton, so it is not needed for that button above.
if (await this._model.onGenerateScript()) {
this.dispose();
this.wizardObject.close(); // close the wizard. This is already hooked up into doneButton, so it is not needed for that button above.
}
}));
this.toDispose.push(this.wizardObject.cancelButton.onClick(() => {
this._model.onCancel();