woltlab-compiler
Version:
A compiler for WoltLab-Package Archives and WoltLab-Package Components
333 lines • 13.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ColorNames = require("colornames");
const FileSystem = require("fs-extra");
const HexToRgba = require("hex-to-rgba");
const OS = require("os");
const Path = require("path");
const util_1 = require("util");
const Component_1 = require("../../../PackageSystem/Component");
const FileDescriptor_1 = require("../../../PackageSystem/FileDescriptor");
const ImageDirectoryDescriptor_1 = require("./ImageDirectoryDescriptor");
const SassVariableParser_1 = require("./SassVariableParser");
/**
* Represents a theme.
*/
class Theme extends Component_1.Component {
/**
* Initializes a new instance of the `Theme` class.
*
* @param instruction
* The instruction of the theme.
*/
constructor(instruction, options) {
super({
Name: options.Name,
DisplayName: options.DisplayName,
Version: options.Version,
Author: options.Author,
CreationDate: options.CreationDate,
Description: options.Description,
License: options.License
});
/**
* The thumbnail of the theme.
*/
this.thumbnail = null;
/**
* The high resolution version of the thumbnail.
*/
this.highResThumbnail = null;
/**
* The path to the default cover-photo for user-profiles.
*/
this.coverPhoto = null;
/**
* The `scss`-code of the theme.
*/
this.customSCSS = null;
/**
* The variable-overrides of special `scss`-variables.
*/
this.scssOverride = null;
/**
* The variables of the theme.
*/
this.variables = {};
/**
* The image-directory provided by the theme.
*/
this.images = null;
let variables = {};
this.instruction = instruction;
if (!util_1.isNullOrUndefined(options.Thumbnail)) {
this.Thumbnail = new FileDescriptor_1.FileDescriptor(typeof options.Thumbnail === "string" ? { Source: options.Thumbnail } : options.Thumbnail);
}
if (!util_1.isNullOrUndefined(options.HighResThumbnail)) {
this.HighResThumbnail = new FileDescriptor_1.FileDescriptor(typeof options.HighResThumbnail === "string" ? { Source: options.HighResThumbnail } : options.HighResThumbnail);
}
if (!util_1.isNullOrUndefined(options.CoverPhoto)) {
this.CoverPhoto = new FileDescriptor_1.FileDescriptor(typeof options.CoverPhoto === "string" ? { Source: options.CoverPhoto } : options.CoverPhoto);
}
if (!util_1.isNullOrUndefined(options.CustomScssFileName)) {
this.CustomScss = FileSystem.readFileSync(options.CustomScssFileName).toString();
}
if (!util_1.isNullOrUndefined(options.ScssOverrideFileName)) {
Object.assign(variables, new SassVariableParser_1.SassVariableParser(options.ScssOverrideFileName).Parse());
}
if (!util_1.isNullOrUndefined(options.VariableFileName)) {
Object.assign(variables, require(Path.join(...(Path.isAbsolute(options.VariableFileName) ? [options.VariableFileName] : [process.cwd(), options.VariableFileName]))));
}
if (!util_1.isNullOrUndefined(options.Images)) {
this.images = new ImageDirectoryDescriptor_1.ImageDirectoryDescriptor(options.Images);
}
this.ParseVariables(variables);
}
/**
* @inheritdoc
*/
get Author() {
return super.Author ||
((!util_1.isNullOrUndefined(this.Instruction) &&
!util_1.isNullOrUndefined(this.Instruction.Collection) &&
!util_1.isNullOrUndefined(this.Instruction.Collection.Package)) ? this.Instruction.Collection.Package.Author : null);
}
/**
* Gets or sets the thumbnail of the theme.
*/
get Thumbnail() {
return this.thumbnail;
}
set Thumbnail(value) {
this.thumbnail = value;
}
/**
* Gets or sets the high resolution version of the thumbnail.
*/
get HighResThumbnail() {
return this.highResThumbnail;
}
set HighResThumbnail(value) {
this.highResThumbnail = value;
}
/**
* Gets the instruction which contains this theme.
*/
get Instruction() {
return this.instruction;
}
/**
* Gets or sets the path to the default cover-photo for user-profiles.
*/
get CoverPhoto() {
return this.coverPhoto;
}
set CoverPhoto(value) {
this.coverPhoto = value;
}
/**
* Gets or sets the `scss`-code of the theme.
*/
get CustomScss() {
return this.customSCSS;
}
set CustomScss(value) {
this.customSCSS = value;
}
/**
* Gets or sets the variable-overrides of special `scss`-variables.
*/
get ScssOverride() {
return this.scssOverride;
}
set ScssOverride(value) {
this.scssOverride = value;
}
/**
* Gets the variables of the theme.
*/
get Variables() {
return this.variables;
}
/**
* Gets the image-directory of the theme.
*/
get Images() {
return this.images;
}
/**
* Parses the variables and adds them either to the override scss-code or the variables-property.
*
* @param variables
* The variables to parse.
*/
ParseVariables(variables) {
let normalVariables = {};
let specialVariables = {};
for (let name in variables) {
switch (name) {
case "wcfLayoutMinWidth":
case "wcfLayoutMaxWidth":
case "pageLogo":
case "pageLogoWidth":
case "pageLogoHeight":
case "pageLogoMobile":
case "wcfFontSizeDefault":
case "wcfFontSizeSmall":
case "wcfFontSizeHeadline":
case "wcfFontSizeSection":
case "wcfFontSizeTitle":
case "useGoogleFont":
case "wcfFontFamilyGoogle":
case "wcfFontFamilyFallback":
case "wcfHeaderBackground":
case "wcfHeaderText":
case "wcfHeaderLink":
case "wcfHeaderLinkActive":
case "wcfHeaderSearchBoxBackground":
case "wcfHeaderSearchBoxText":
case "wcfHeaderSearchBoxPlaceholder":
case "wcfHeaderSearchBoxPlaceholderActive":
case "wcfHeaderSearchBoxBackgroundActive":
case "wcfHeaderSearchBoxTextActive":
case "wcfHeaderMenuBackground":
case "wcfHeaderMenuLinkBackground":
case "wcfHeaderMenuLinkBackgroundActive":
case "wcfHeaderMenuLink":
case "wcfHeaderMenuLinkActive":
case "wcfHeaderMenuDropdownBackground":
case "wcfHeaderMenuDropdownLink":
case "wcfHeaderMenuDropdownBackgroundActive":
case "wcfHeaderMenuDropdownLinkActive":
case "wcfNavigationBackground":
case "wcfNavigationText":
case "wcfNavigationLink":
case "wcfNavigationLinkActive":
case "wcfSidebarBackground":
case "wcfSidebarText":
case "wcfSidebarLink":
case "wcfSidebarLinkActive":
case "wcfSidebarDimmedText":
case "wcfSidebarDimmedLink":
case "$wcfSidebarDimmedLinkActive":
case "wcfSidebarHeadlineText":
case "wcfSidebarHeadlineLink":
case "wcfSidebarHeadlineLinkActive":
case "wcfContentBackground":
case "wcfContentBorder":
case "wcfContentBorderInner":
case "wcfContentText":
case "wcfContentLink":
case "wcfContentLinkActive":
case "wcfContentContainerBackground":
case "wcfContentContainerBorder":
case "wcfContentDimmedText":
case "wcfContentDimmedLink":
case "wcfContentDimmedLinkActive":
case "wcfContentHeadlineBorder":
case "wcfContentHeadlineText":
case "wcfContentHeadlineLink":
case "wcfContentHeadlineLinkActive":
case "wcfTabularBoxBorderInner":
case "wcfTabularBoxHeadline":
case "wcfTabularBoxBackgroundActive":
case "wcfTabularBoxHeadlineActive":
case "wcfInputLabel":
case "wcfInputBackground":
case "wcfInputBorder":
case "wcfInputText":
case "wcfInputPlaceholder":
case "wcfInputPlaceholderActive":
case "wcfInputBackgroundActive":
case "wcfInputBorderActive":
case "wcfInputTextActive":
case "wcfInputDisabledBackground":
case "wcfInputDisabledBorder":
case "wcfInputDisabledText":
case "wcfButtonBackground":
case "wcfButtonText":
case "wcfButtonBackgroundActive":
case "wcfButtonTextActive":
case "wcfButtonPrimaryBackground":
case "wcfButtonPrimaryText":
case "wcfButtonPrimaryBackgroundActive":
case "wcfButtonPrimaryTextActive":
case "wcfButtonDisabledBackground":
case "wcfButtonDisabledText":
case "wcfEditorButtonBackground":
case "wcfEditorButtonBackgroundActive":
case "wcfEditorButtonText":
case "wcfEditorButtonTextActive":
case "wcfEditorButtonTextDisabled":
case "wcfDropdownBackground":
case "wcfDropdownBorderInner":
case "wcfDropdownText":
case "wcfDropdownLink":
case "wcfDropdownBackgroundActive":
case "wcfDropdownLinkActive":
case "wcfStatusInfoBackground":
case "wcfStatusInfoBorder":
case "wcfStatusInfoText":
case "wcfStatusInfoLink":
case "wcfStatusInfoLinkActive":
case "wcfStatusSuccessBackground":
case "wcfStatusSuccessBorder":
case "wcfStatusSuccessText":
case "wcfStatusSuccessLink":
case "wcfStatusSuccessLinkActive":
case "wcfStatusWarningBackground":
case "wcfStatusWarningBorder":
case "wcfStatusWarningText":
case "wcfStatusWarningLink":
case "wcfStatusWarningLinkActive":
case "wcfStatusErrorBackground":
case "wcfStatusErrorBorder":
case "wcfStatusErrorText":
case "wcfStatusErrorLink":
case "wcfStatusErrorLinkActive":
case "wcfFooterBoxBackground":
case "wcfFooterBoxText":
case "wcfFooterBoxLink":
case "wcfFooterBoxLinkActive":
case "wcfFooterBoxHeadlineText":
case "wcfFooterBoxHeadlineLink":
case "wcfFooterBoxHeadlineLinkActive":
case "wcfFooterBackground":
case "wcfFooterText":
case "wcfFooterLink":
case "wcfFooterLinkActive":
case "wcfFooterHeadlineText":
case "wcfFooterHeadlineLink":
case "wcfFooterHeadlineLinkActive":
case "wcfFooterCopyrightBackground":
case "wcfFooterCopyrightText":
case "wcfFooterCopyrightLink":
case "wcfFooterCopyrightLinkActive":
if (/#(([0-9a-fA-F]{3}){1,2}|([0-9a-fA-F]{4}){1,2})/.test(variables[name])) {
normalVariables[name] = HexToRgba(variables[name]);
}
else if (ColorNames.get.css(variables[name])) {
normalVariables[name] = HexToRgba(ColorNames.get.css(variables[name]).value);
}
else if (variables[name] === "transparent") {
normalVariables[name] = HexToRgba("#0000");
}
else {
normalVariables[name] = variables[name];
}
break;
default:
specialVariables[name] = variables[name];
break;
}
}
this.variables = normalVariables;
if (Object.keys(specialVariables).length > 0) {
this.ScssOverride = Object.keys(specialVariables).map((name) => {
return `$${name}: ${specialVariables[name]};`;
}).join(OS.EOL);
}
}
}
exports.Theme = Theme;
//# sourceMappingURL=Theme.js.map