Path: blob/main/build/lib/policies/stringEnumPolicy.ts
4772 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { BasePolicy } from './basePolicy.ts';6import type { CategoryDto, PolicyDto } from './policyDto.ts';7import { renderProfileString } from './render.ts';8import { type Category, type NlsString, PolicyType, type LanguageTranslations } from './types.ts';910export class StringEnumPolicy extends BasePolicy {1112static from(category: CategoryDto, policy: PolicyDto): StringEnumPolicy | undefined {13const { type, name, minimumVersion, enum: enumValue, localization } = policy;1415if (type !== 'string') {16return undefined;17}1819const enum_ = enumValue;2021if (!enum_) {22return undefined;23}2425if (!localization.enumDescriptions || !Array.isArray(localization.enumDescriptions) || localization.enumDescriptions.length !== enum_.length) {26throw new Error(`Invalid policy data: enumDescriptions must exist and have the same length as enum_ for policy "${name}".`);27}28const enumDescriptions = localization.enumDescriptions.map((e) => ({ nlsKey: e.key, value: e.value }));29return new StringEnumPolicy(30name,31{ moduleName: '', name: { nlsKey: category.name.key, value: category.name.value } },32minimumVersion,33{ nlsKey: localization.description.key, value: localization.description.value },34'',35enum_,36enumDescriptions37);38}3940protected enum_: string[];41protected enumDescriptions: NlsString[];4243private constructor(44name: string,45category: Category,46minimumVersion: string,47description: NlsString,48moduleName: string,49enum_: string[],50enumDescriptions: NlsString[],51) {52super(PolicyType.StringEnum, name, category, minimumVersion, description, moduleName);53this.enum_ = enum_;54this.enumDescriptions = enumDescriptions;55}5657protected renderADMXElements(): string[] {58return [59`<enum id="${this.name}" valueName="${this.name}">`,60...this.enum_.map((value, index) => ` <item displayName="$(string.${this.name}_${this.enumDescriptions[index].nlsKey.replace(/\./g, '_')})"><value><string>${value}</string></value></item>`),61`</enum>`62];63}6465renderADMLStrings(translations?: LanguageTranslations) {66return [67...super.renderADMLStrings(translations),68...this.enumDescriptions.map(e => this.renderADMLString(e, translations))69];70}7172renderADMLPresentationContents() {73return `<dropdownList refId="${this.name}" />`;74}7576renderJsonValue() {77return this.enum_[0];78}7980renderProfileValue() {81return `<string>${this.enum_[0]}</string>`;82}8384renderProfileManifestValue(translations?: LanguageTranslations): string {85return `<key>pfm_default</key>86<string>${this.enum_[0]}</string>87<key>pfm_description</key>88<string>${renderProfileString(this.name, this.moduleName, this.description, translations)}</string>89<key>pfm_name</key>90<string>${this.name}</string>91<key>pfm_title</key>92<string>${this.name}</string>93<key>pfm_type</key>94<string>string</string>95<key>pfm_range_list</key>96<array>97${this.enum_.map(e => `<string>${e}</string>`).join('\n ')}98</array>`;99}100}101102103