Skip to content

Commit

Permalink
Add OK/Cancel buttons to the text input fields
Browse files Browse the repository at this point in the history
This PR adds OK/Cancel buttons to the text input fields used at the creation of
a cluster namespace/project as well as at the adding of a Devfile Registry (name, URL, token)

Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
  • Loading branch information
vrubezhny committed Nov 7, 2023
1 parent 75ee1e9 commit 0785f32
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 155 deletions.
73 changes: 7 additions & 66 deletions src/openshift/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*-----------------------------------------------------------------------------------------------*/

import { KubernetesObject } from '@kubernetes/client-node';
import { ExtensionContext, InputBox, QuickInputButton, QuickInputButtons, QuickPickItem, QuickPickItemButtonEvent, ThemeIcon, Uri, commands, env, window, workspace } from 'vscode';
import { ExtensionContext, QuickInputButtons, QuickPickItem, QuickPickItemButtonEvent, ThemeIcon, Uri, commands, env, window, workspace } from 'vscode';
import { quickBtn, inputValue } from '../util/inputValue';
import { CommandText } from '../base/command';
import { CliChannel } from '../cli';
import { OpenShiftExplorer } from '../explorer';
Expand All @@ -23,10 +24,6 @@ import { OpenShiftTerminalManager } from '../webview/openshift-terminal/openShif
import OpenShiftItem, { clusterRequired } from './openshiftItem';
import fetch = require('make-fetch-happen');

class quickBtn implements QuickInputButton {
constructor(public iconPath: ThemeIcon, public tooltip: string) { }
}

export class Cluster extends OpenShiftItem {

public static extensionContext: ExtensionContext;
Expand Down Expand Up @@ -231,7 +228,7 @@ export class Cluster extends OpenShiftItem {
if (choice.label === createUrl.label) {
const prompt = 'Provide new Cluster URL to connect';
const validateInput = (value: string) => NameValidator.validateUrl('Invalid URL provided', value);
const newURL = await this.enterValue(prompt, '', false, validateInput);
const newURL = await inputValue(prompt, '', false, validateInput);

Check warning on line 231 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L231

Added line #L231 was not covered by tests
if (newURL === null) return null; // Cancel
else if (!newURL) resolve(await Cluster.showQuickPick(clusterURl)); // Back
else resolve(newURL);
Expand All @@ -245,7 +242,7 @@ export class Cluster extends OpenShiftItem {
quickPick.onDidTriggerButton((button) => {
hideDisposable.dispose();
quickPick.hide();
if (button === QuickInputButtons.Back) quickPick.hide();
if (button === QuickInputButtons.Back) resolve(undefined);

Check warning on line 245 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L245

Added line #L245 was not covered by tests
else if (button === cancelBtn) resolve(null);
});
quickPick.onDidTriggerItemButton(async (event) => {
Expand Down Expand Up @@ -539,62 +536,6 @@ export class Cluster extends OpenShiftItem {
});
}

/*
* Shows a Input Field to type in a username. Returns either:
* - username string, or
* - `null` in case of user cancelled (pressed `ESC`), or
* - `undefined` if user pressed `Back` button
* @returns string contaning user name or null if cancelled or undefined if Back is pressed
*/
private static async enterValue(prompt: string, initialValue: string, password: boolean, validate ): Promise<string | null | undefined> {
return new Promise<string | null | undefined>((resolve, reject) => {
const input: InputBox = window.createInputBox();
input.value = initialValue;
input.prompt = prompt;
input.password = password;
const enterBtn = new quickBtn(new ThemeIcon('check'), 'Enter');
const cancelBtn = new quickBtn(new ThemeIcon('close'), 'Cancel');
input.buttons = [QuickInputButtons.Back, enterBtn, cancelBtn];
const validationMessage: string = validate(input.value? input.value : '');
input.ignoreFocusOut = true;
if (validationMessage) {
input.validationMessage = validationMessage;
}
const acceptInput = async () => {
const value = input.value;
input.enabled = false;
input.busy = true;
if (!(await validate(value))) {
input.hide();
resolve(value);
}
input.enabled = true;
input.busy = false;
};
input.onDidAccept(acceptInput);
input.onDidChangeValue(async text => {
const current = validate(text);
const validating = current;
const validationMessage = await current;
if (current === validating) {
input.validationMessage = validationMessage;
}
});
input.onDidHide(() => {
input.dispose();
})
input.onDidTriggerButton(async (event) => {
if (event === QuickInputButtons.Back) resolve(undefined);
else if (event === enterBtn) await acceptInput();
else if (event === cancelBtn) {
resolve(null);
input.dispose();
}
});
input.show();
});
}

@vsCommand('openshift.explorer.login.credentialsLogin')
static async credentialsLogin(skipConfirmation = false, userClusterUrl?: string, userName?: string, userPassword?: string): Promise<string | null | undefined> {
let password: string;
Expand Down Expand Up @@ -639,7 +580,7 @@ export class Cluster extends OpenShiftItem {
if (!username) {
const prompt = 'Provide Username for basic authentication to the API server';
const validateInput = (value: string) => NameValidator.emptyName('User name cannot be empty', value);
const newUsername = await this.enterValue(prompt, '', false, validateInput);
const newUsername = await inputValue(prompt, '', false, validateInput);

Check warning on line 583 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L583

Added line #L583 was not covered by tests

if (newUsername === null) {
return null; // Cancel
Expand All @@ -657,7 +598,7 @@ export class Cluster extends OpenShiftItem {
password = await TokenStore.getItem('login', username);
const prompt = 'Provide Password for basic authentication to the API server';
const validateInput = (value: string) => NameValidator.emptyName('Password cannot be empty', value);
const newPassword = await this.enterValue(prompt, password, true, validateInput);
const newPassword = await inputValue(prompt, password, true, validateInput);

Check warning on line 601 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L601

Added line #L601 was not covered by tests

if (newPassword === null) {
return null; // Cancel
Expand Down Expand Up @@ -755,7 +696,7 @@ export class Cluster extends OpenShiftItem {
const prompt = 'Provide Bearer token for authentication to the API server';
const validateInput = (value: string) => NameValidator.emptyName('Bearer token cannot be empty', value);
const initialValue = token ? token : '';
ocToken = await this.enterValue(prompt, initialValue, true, validateInput);
ocToken = await inputValue(prompt, initialValue, true, validateInput);

Check warning on line 699 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L699

Added line #L699 was not covered by tests
if (ocToken === null) {
return null; // Cancel
} else if (!ocToken) {
Expand Down
20 changes: 7 additions & 13 deletions src/openshift/openshiftItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*-----------------------------------------------------------------------------------------------*/

import { commands, QuickPickItem, window } from 'vscode';
import { inputValue } from '../util/inputValue';
import { Oc } from '../oc/ocWrapper';
import { Odo } from '../odo/odoWrapper';
import { Project } from '../odo/project';
Expand All @@ -28,25 +29,19 @@ export default class OpenShiftItem {
protected static readonly serverlessView: ServerlessFunctionView = ServerlessFunctionView.getInstance();

static async getName(message: string, offset?: string, defaultValue = ''): Promise<string> {
return window.showInputBox({
value: defaultValue,
prompt: `Provide ${message}`,
ignoreFocusOut: true,
validateInput: (value: string) => {
return await inputValue(`Provide ${message}`, defaultValue, false,

Check warning on line 32 in src/openshift/openshiftItem.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/openshiftItem.ts#L32

Added line #L32 was not covered by tests
(value: string) => {
let validationMessage = NameValidator.emptyName(`Empty ${message}`, value.trim());
if (!validationMessage) validationMessage = NameValidator.validateMatches(`Not a valid ${message}. Please use lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character`, value);
if (!validationMessage) validationMessage = NameValidator.lengthName(`${message} should be between 2-63 characters`, value, offset ? offset.length : 0);
return validationMessage;
}
});
);
}

static async getProjectName(message: string, data: Promise<Array<Project>>, offset?: string, defaultValue = ''): Promise<string> {
return window.showInputBox({
value: defaultValue,
prompt: `Provide ${message}`,
ignoreFocusOut: true,
validateInput: async (value: string) => {
return await inputValue(`Provide ${message}`, defaultValue, false,
async (value: string) => {

Check warning on line 44 in src/openshift/openshiftItem.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/openshiftItem.ts#L43-L44

Added lines #L43 - L44 were not covered by tests
let validationMessage = NameValidator.emptyName(`Empty ${message}`, value.trim());
if (!validationMessage) validationMessage = NameValidator.validateRFC1123DNSLabel(`Not a valid ${message}. Please enter name that starts with an alphanumeric character, use lower case alphanumeric characters or '-' and end with an alphanumeric character`, value);
if (!validationMessage) validationMessage = NameValidator.lengthName(`${message} should be between 2-63 characters`, value, offset ? offset.length : 0);
Expand All @@ -61,9 +56,8 @@ export default class OpenShiftItem {
}
return validationMessage;
}
});
);
}

}

export function clusterRequired() {
Expand Down
Loading

0 comments on commit 0785f32

Please sign in to comment.