diff --git a/packages/zowe-explorer/src/trees/dataset/DatasetTree.ts b/packages/zowe-explorer/src/trees/dataset/DatasetTree.ts index e10fbab11..396fa8170 100644 --- a/packages/zowe-explorer/src/trees/dataset/DatasetTree.ts +++ b/packages/zowe-explorer/src/trees/dataset/DatasetTree.ts @@ -1304,7 +1304,9 @@ export class DatasetTree extends ZoweTreeProvider 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 diff --git a/packages/zowe-explorer/src/trees/dataset/DatasetUtils.ts b/packages/zowe-explorer/src/trees/dataset/DatasetUtils.ts index 3fd4c6cf3..2383386e5 100644 --- a/packages/zowe-explorer/src/trees/dataset/DatasetUtils.ts +++ b/packages/zowe-explorer/src/trees/dataset/DatasetUtils.ts @@ -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 { @@ -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(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; diff --git a/packages/zowe-explorer/src/trees/dataset/ZoweDatasetNode.ts b/packages/zowe-explorer/src/trees/dataset/ZoweDatasetNode.ts index 077d797de..2e68bde26 100644 --- a/packages/zowe-explorer/src/trees/dataset/ZoweDatasetNode.ts +++ b/packages/zowe-explorer/src/trees/dataset/ZoweDatasetNode.ts @@ -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, diff --git a/packages/zowe-explorer/src/trees/job/JobUtils.ts b/packages/zowe-explorer/src/trees/job/JobUtils.ts index 81f1aa6ce..227383bf3 100644 --- a/packages/zowe-explorer/src/trees/job/JobUtils.ts +++ b/packages/zowe-explorer/src/trees/job/JobUtils.ts @@ -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 = [ @@ -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(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, - }; - } } diff --git a/packages/zowe-explorer/src/trees/job/ZoweJobNode.ts b/packages/zowe-explorer/src/trees/job/ZoweJobNode.ts index 72bcfba04..1508e4afe 100644 --- a/packages/zowe-explorer/src/trees/job/ZoweJobNode.ts +++ b/packages/zowe-explorer/src/trees/job/ZoweJobNode.ts @@ -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, diff --git a/packages/zowe-explorer/src/trees/shared/SharedUtils.ts b/packages/zowe-explorer/src/trees/shared/SharedUtils.ts index 63c837887..5a85325c0 100644 --- a/packages/zowe-explorer/src/trees/shared/SharedUtils.ts +++ b/packages/zowe-explorer/src/trees/shared/SharedUtils.ts @@ -13,7 +13,7 @@ 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"; @@ -21,6 +21,7 @@ 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 { @@ -357,4 +358,35 @@ export class SharedUtils { timeoutId = setTimeout(() => callback(...args), delay); }; } + + private static updateSortOptionsWithDefault(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(sortOptions: string[], settingsKey: string, sortMethod: T): Sorting.NodeSort { + const sortSetting = SettingsConfig.getDirectValue(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, + }; + } }