Skip to content

Commit

Permalink
Refactor into SharedUtils
Browse files Browse the repository at this point in the history
Signed-off-by: Buckminsterfullerene02 <[email protected]>
  • Loading branch information
Buckminsterfullerene02 committed Dec 22, 2024
1 parent 5066dc1 commit 9bc23cd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 57 deletions.
4 changes: 3 additions & 1 deletion packages/zowe-explorer/src/trees/dataset/DatasetTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,9 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
const isSession = SharedContext.isSession(node);

// Read default options from settings if a user hasn't selected any sort options yet
const sortOpts = node.sort ?? DatasetUtils.getDefaultSortOptions();
const sortOpts =
node.sort ??
SharedUtils.getDefaultSortOptions(DatasetUtils.DATASET_SORT_OPTS, Constants.SETTINGS_DS_DEFAULT_SORT, Sorting.DatasetSortOpts);

// Adapt menus to user based on the node that was interacted with
const specifier = isSession
Expand Down
28 changes: 1 addition & 27 deletions packages/zowe-explorer/src/trees/dataset/DatasetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
*/

import * as vscode from "vscode";
import { DS_EXTENSION_MAP, Types, Sorting } from "@zowe/zowe-explorer-api";
import { DS_EXTENSION_MAP, Types } from "@zowe/zowe-explorer-api";
import { Constants } from "../../configuration/Constants";
import { SettingsConfig } from "../../configuration/SettingsConfig";
import { ZoweLogger } from "../../tools/ZoweLogger";

export class DatasetUtils {
Expand All @@ -27,31 +26,6 @@ export class DatasetUtils {
// eslint-disable-next-line no-magic-numbers
public static readonly DATASET_FILTER_OPTS = [this.DATASET_SORT_OPTS[2], this.DATASET_SORT_OPTS[3]];

public static updateSortOptionsWithDefault(sortMethod: Sorting.DatasetSortOpts): void {
this.DATASET_SORT_OPTS = this.DATASET_SORT_OPTS.map((opt) => opt.replace(` ${vscode.l10n.t("(default)")}`, ""));
this.DATASET_SORT_OPTS = this.DATASET_SORT_OPTS.map((opt, index) => {
if (index === sortMethod) {
return `${opt} ${vscode.l10n.t("(default)")}`;
}
return opt;
});
}

public static getDefaultSortOptions(): Sorting.NodeSort {
const sortSetting = SettingsConfig.getDirectValue<Sorting.NodeSort>(Constants.SETTINGS_DS_DEFAULT_SORT);
if (typeof sortSetting.method === "string") {
sortSetting.method = Sorting.DatasetSortOpts[sortSetting.method as keyof typeof Sorting.DatasetSortOpts];
DatasetUtils.updateSortOptionsWithDefault(sortSetting.method);
}
if (typeof sortSetting.direction === "string") {
sortSetting.direction = Sorting.SortDirection[sortSetting.direction as keyof typeof Sorting.SortDirection];
}
return {
method: sortSetting?.method ?? Sorting.DatasetSortOpts.Name,
direction: sortSetting?.direction ?? Sorting.SortDirection.Ascending,
};
}

public static getProfileAndDataSetName(node: Types.IZoweNodeType): {
profileName: string;
dataSetName: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/zowe-explorer/src/trees/dataset/ZoweDatasetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ export class ZoweDatasetNode extends ZoweTreeNode implements IZoweDatasetTreeNod

if (this.getParent() == null || this.getParent().label === vscode.l10n.t("Favorites")) {
// read sort options from settings file
const sortSetting = DatasetUtils.getDefaultSortOptions();
const sortSetting = SharedUtils.getDefaultSortOptions(
DatasetUtils.DATASET_SORT_OPTS,
Constants.SETTINGS_DS_DEFAULT_SORT,
Sorting.DatasetSortOpts
);
this.sort = {
method: sortSetting.method,
direction: sortSetting.direction,
Expand Down
26 changes: 0 additions & 26 deletions packages/zowe-explorer/src/trees/job/JobUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import * as vscode from "vscode";
import * as zosjobs from "@zowe/zos-jobs-for-zowe-sdk";
import { Sorting } from "@zowe/zowe-explorer-api";
import { SettingsConfig } from "../../configuration/SettingsConfig";
import { Constants } from "../../configuration/Constants";

export class JobUtils {
public static JOB_SORT_OPTS = [
Expand All @@ -34,28 +32,4 @@ export class JobUtils {
};

public static readonly JOB_FILTER_OPTS = [vscode.l10n.t("Go to Local Filtering"), `$(clear-all) ${vscode.l10n.t("Clear filter for profile")}`];

public static updateSortOptionsWithDefault(sortMethod: Sorting.JobSortOpts): void {
this.JOB_SORT_OPTS = this.JOB_SORT_OPTS.map((opt, index) => {
if (index === sortMethod) {
return `${opt} ${vscode.l10n.t("(default)")}`;
}
return opt;
});
}

public static getDefaultSortOptions(): Sorting.NodeSort {
const sortSetting = SettingsConfig.getDirectValue<Sorting.NodeSort>(Constants.SETTINGS_JOBS_DEFAULT_SORT);
if (typeof sortSetting.method === "string") {
sortSetting.method = Sorting.JobSortOpts[sortSetting.method as keyof typeof Sorting.JobSortOpts];
JobUtils.updateSortOptionsWithDefault(sortSetting.method);
}
if (typeof sortSetting.direction === "string") {
sortSetting.direction = Sorting.SortDirection[sortSetting.direction as keyof typeof Sorting.SortDirection];
}
return {
method: sortSetting?.method ?? Sorting.JobSortOpts.Id,
direction: sortSetting?.direction ?? Sorting.SortDirection.Ascending,
};
}
}
2 changes: 1 addition & 1 deletion packages/zowe-explorer/src/trees/job/ZoweJobNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class ZoweJobNode extends ZoweTreeNode implements IZoweJobTreeNode {

if (SharedContext.isSession(this)) {
// read sort options from settings file
const sortSetting = JobUtils.getDefaultSortOptions();
const sortSetting = SharedUtils.getDefaultSortOptions(JobUtils.JOB_SORT_OPTS, Constants.SETTINGS_JOBS_DEFAULT_SORT, Sorting.JobSortOpts);
this.sort = {
method: sortSetting.method,
direction: sortSetting.direction,
Expand Down
34 changes: 33 additions & 1 deletion packages/zowe-explorer/src/trees/shared/SharedUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

import * as vscode from "vscode";
import * as path from "path";
import { Gui, IZoweTreeNode, IZoweDatasetTreeNode, IZoweUSSTreeNode, IZoweJobTreeNode, Types, ZosEncoding } from "@zowe/zowe-explorer-api";
import { Gui, IZoweTreeNode, IZoweDatasetTreeNode, IZoweUSSTreeNode, IZoweJobTreeNode, Types, ZosEncoding, Sorting } from "@zowe/zowe-explorer-api";
import { UssFSProvider } from "../uss/UssFSProvider";
import { USSUtils } from "../uss/USSUtils";
import { Constants } from "../../configuration/Constants";
import { ZoweLocalStorage } from "../../tools/ZoweLocalStorage";
import { ZoweLogger } from "../../tools/ZoweLogger";
import { SharedContext } from "./SharedContext";
import { Definitions } from "../../configuration/Definitions";
import { SettingsConfig } from "../../configuration/SettingsConfig";

export class SharedUtils {
public static async copyExternalLink(this: void, context: vscode.ExtensionContext, node: IZoweTreeNode): Promise<void> {
Expand Down Expand Up @@ -357,4 +358,35 @@ export class SharedUtils {
timeoutId = setTimeout(() => callback(...args), delay);
};
}

private static updateSortOptionsWithDefault<T>(sortMethod: T, sortOptions: string[]): void {
for (let i = 0; i < sortOptions.length; i++) {
sortOptions[i] = sortOptions[i].replace(` ${vscode.l10n.t("(default)")}`, "");
if (i === Number(sortMethod)) {
sortOptions[i] = `${sortOptions[i]} ${vscode.l10n.t("(default)")}`;
}
}
}

/**
* Gets the sort options from the settings with the default sort option marked
* @param sortOptions The list of sort options
* @param settingsKey The default sort method key
* @param sortMethod The sort method
* @returns The list of sort options with the default sort option marked
*/
public static getDefaultSortOptions<T>(sortOptions: string[], settingsKey: string, sortMethod: T): Sorting.NodeSort {
const sortSetting = SettingsConfig.getDirectValue<Sorting.NodeSort>(settingsKey);
if (typeof sortSetting.method === "string") {
sortSetting.method = sortMethod[sortSetting.method as keyof typeof sortMethod] as Sorting.JobSortOpts | Sorting.DatasetSortOpts;
SharedUtils.updateSortOptionsWithDefault(sortSetting.method, sortOptions);
}
if (typeof sortSetting.direction === "string") {
sortSetting.direction = Sorting.SortDirection[sortSetting.direction as keyof typeof Sorting.SortDirection];
}
return {
method: sortSetting?.method ?? sortMethod[Object.keys(sortMethod)[0]],
direction: sortSetting?.direction ?? Sorting.SortDirection.Ascending,
};
}
}

0 comments on commit 9bc23cd

Please sign in to comment.