UNPKG

woltlab-compiler

Version:

A compiler for WoltLab-Package Archives and WoltLab-Package Components

137 lines 5.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("util"); const XML_1 = require("../../Serialization/XML"); const XMLEditor_1 = require("../../Serialization/XMLEditor"); const ImportFileCompiler_1 = require("../ImportFileCompiler"); /** * Provides the functionality to compile option-files. */ class OptionFileCompiler extends ImportFileCompiler_1.ImportFileCompiler { /** * Initializes a new instance of the `OptionFileCompiler<T, TCategory, TOption>` class. * * @param item * The item to compile. * * @param languageCategory * The language-category which contains translations for options. */ constructor(item) { super(item); this.LanguageCategory = `${item.RootCategory}${util_1.isNullOrUndefined(item.OptionCategory) ? "" : `.${item.OptionCategory}`}`; } /** * Gets or sets the language-category which contains translations for options. */ get LanguageCategory() { return this.languageCategory; } set LanguageCategory(value) { this.languageCategory = value; } /** * @inheritdoc */ CreateImport() { let editor = new XMLEditor_1.XMLEditor(super.CreateImport()); editor.AddElement("categories", (categories) => { for (let rootCategory of this.Item.Nodes) { for (let category of rootCategory.GetAllNodes()) { categories.Add(this.CreateCategory(category)); } } }); editor.AddElement("options", (options) => { for (let rootCategory of this.Item.Nodes) { for (let category of rootCategory.GetAllNodes()) { if (!util_1.isNullOrUndefined(category.Item)) { for (let option of category.Item.Options) { options.Add(this.CreateOption(option)); } } } } }); return editor.Element; } /** * @inheritdoc */ CreateDelete() { let editor = new XMLEditor_1.XMLEditor(super.CreateDelete()); for (let categoryToDelete of this.Item.CategoriesToDelete) { editor.AddElement("category", (category) => { category.SetAttribute("name", categoryToDelete.Name); }); } for (let optionToDelete of this.Item.OptionsToDelete) { editor.AddElement("option", (option) => { option.SetAttribute("name", optionToDelete.Name); }); } return editor.Element; } /** * Serializes a category to xml. * * @param category * The category to serialize. */ CreateCategory(category) { let document = XML_1.XML.CreateDocument("category"); let editor = new XMLEditor_1.XMLEditor(document.documentElement); editor.SetAttribute("name", category.FullName); if (!util_1.isNullOrUndefined(category.Parent)) { editor.AddTextElement("parent", category.Parent.FullName); } if (!util_1.isNullOrUndefined(category.Item) && !util_1.isNullOrUndefined(category.Item.ShowOrder)) { editor.AddTextElement("showorder", category.Item.ShowOrder.toString()); } if (!util_1.isNullOrUndefined(category.Item) && category.Item.EnableOptions.length > 0) { editor.AddTextElement("options", category.Item.EnableOptions.join(",")); } return editor.Element; } /** * Serializes an option to xml. * * @param option * The option to serialize. */ CreateOption(option) { let document = XML_1.XML.CreateDocument("option"); let editor = new XMLEditor_1.XMLEditor(document.documentElement); editor.SetAttribute("name", option.Name); editor.AddTextElement("categoryname", option.Category.Node.FullName); editor.AddTextElement("optiontype", option.Type); if (!util_1.isNullOrUndefined(option.DefaultValue)) { editor.AddTextElement("defaultvalue", `${option.DefaultValue}`); } if (!util_1.isNullOrUndefined(option.ShowOrder)) { editor.AddTextElement("showorder", option.ShowOrder.toString()); } if (!util_1.isNullOrUndefined(option.ValidationPattern)) { editor.AddTextElement("validationpattern", option.ValidationPattern.source); } if (option.Items.length > 0) { editor.AddTextElement("selectoptions", option.Items.map((optionItem) => { return `${optionItem.Value}:${this.LanguageCategory}.${option.Name}.${optionItem.Name}`; }).join("\n")); } if (option.Options.length > 0) { editor.AddTextElement("options", option.Options.join(",")); } if (option.EnableOptions.length > 0) { editor.AddTextElement("enableoptions", option.EnableOptions.join(",")); } for (let additionalProperty in option.AdditionalProperties) { editor.AddTextElement(additionalProperty, option.AdditionalProperties[additionalProperty]); } return editor.Element; } } exports.OptionFileCompiler = OptionFileCompiler; //# sourceMappingURL=OptionFileCompiler.js.map