UNPKG

@jirkasa/code-box

Version:

Showcase code samples on the web with a container that lets users select and display different samples.

482 lines (481 loc) 23.2 kB
import CodeView from "../../code-view/CodeView"; import EventSourcePoint from "../../utils/EventSourcePoint"; import TreeNode from "../../utils/TreeNode"; import CodeViewButton from "../CodeViewButton"; import FolderInfo from "./FolderInfo"; import PackageInfo from "./PackageInfo"; import ProjectCodeBoxFile from "./ProjectCodeBoxFile"; /** Manages folders and its contents in project code box. */ declare class FoldersManager { /** Container for package folders. */ private packagesContainer; /** Root (project) folder. */ private rootFolder; /** Package folders stored by package name. */ private packages; /** Default package folder. */ private defaultPackage; /** Folder path for packages. */ private packagesFolderPath; /** Determines whether folders are created for packages. */ private readonly createFoldersForPackages; /** Delimiter based on which are created folders for packages (if null, only single folder for package is created). */ private readonly foldersDelimiterForPackages; /** Mappings between folder and package for code views. */ private codeViewFolderAndPackageMappings; /** Mappings between folder and package for files. */ private fileFolderAndPackageMappings; /** Code view identifier for which are currently buttons set as active. */ private activeCodeViewIdentifier; /** Indicates whether panel is opened. */ private panelOpened; /** Path to SVG sprite. */ private readonly svgSpritePath; /** Name of folder arrow icon. */ private readonly folderArrowIconName; /** Name of folder icon. */ private readonly folderIconName; /** Name of package icon. */ private readonly packageIconName; /** Name of icon for code view buttons. */ private readonly codeFileIconName; /** Name of icon for file buttons. */ private readonly fileIconName; /** Name of download icon. */ private readonly downloadIconName; /** Name of folder for default package. */ private readonly defaultPackageName; /** Animation speed in miliseconds for open/close folder animations. */ private readonly openCloseAnimationSpeed; /** CSS easing function used for open/close folder animations. */ private readonly openCloseAnimationEasingFunction; /** * Creates new folders manager. * @param folderStructureContainer Container for folder structure. * @param packagesContainer Container for package folders. * @param rootFolderName Name of root (project) folder. * @param packagesFolderPath Folder path for packages. * @param defaultPackageName Name of folder for default package. * @param createFoldersForPackages Determines whether folders should be created for packages. * @param foldersDelimiterForPackages Delimiter based on which should be created folders for packages (if null, only single folder for package is created). * @param panelOpened Indicates whether panel is opened (needed to properly update tab navigation). * @param openCloseAnimationSpeed Animation speed in miliseconds for open/close folder animations. * @param openCloseAnimationEasingFunction CSS easing function that should be used for open/close folder animations. * @param svgSpritePath Path to SVG sprite. * @param folderArrowIconName Name of folder arrow icon. * @param projectIconName Name of icon for root (project) folder. * @param folderIconName Name of folder icon. * @param packageIconName Name of package icon. * @param codeFileIconName Name of icon for code view buttons. * @param fileIconName Name of icon for file buttons. * @param downloadIconName Name of download icon. */ constructor(folderStructureContainer: HTMLElement, packagesContainer: HTMLElement, rootFolderName: string, packagesFolderPath: string | null, defaultPackageName: string | null, createFoldersForPackages: boolean, foldersDelimiterForPackages: string | null, panelOpened: boolean, openCloseAnimationSpeed: number, openCloseAnimationEasingFunction: string, svgSpritePath?: string | null, folderArrowIconName?: string | null, projectIconName?: string | null, folderIconName?: string | null, packageIconName?: string | null, codeFileIconName?: string | null, fileIconName?: string | null, downloadIconName?: string | null); /** * Checks whether folders are created for packages. * @returns Indicates whether folders are created for packages. */ isCreateFoldersForPackagesEnabled(): boolean; /** * Returns delimiter based on which are created folders for packages. * @returns Delimiter based on which are created folders for packages (if null, only single folder for package is created). */ getFoldersDelimiterForPackages(): string | null; /** * Returns identifier based on passed parameters. * @param fileName File name. * @param folderPath Folder path. * @param usePackage Determines whether item is added to package. * @param packageName Name of package. * @returns Identifier. */ getItemIdentifier(fileName: string, folderPath: string | null, usePackage?: boolean, packageName?: string | null): string; /** * Returns normalized version of folder path (removes starting and ending slashes and so on..). * @param folderPath Folder path. * @returns Normalized folder path. */ getNormalizedFolderPath(folderPath: string): string; /** * Returns sanitized folder name. * @param folderName Folder name. * @returns Sanitized folder name. */ getSanitizedFolderName(folderName: string): string; /** * Sets name of root (project) folder. * @param name Name. */ setRootFolderName(name: string): void; /** * Creates new folder(s) (this does not create packages if folders are created in folder for packages). * @param folderPath Folder path based on which should be created new folder(s). */ addFolder(folderPath: string): void; /** * Removes folder and its contents. This might also remove packages if their folders are removed (if generation of folders for packages is enabled). * @param folderPath Folder path to folder that should be removed. * @returns Indicates whether folder has been successfully removed. */ removeFolder(folderPath: string): boolean; /** * Renames folder. This can also change (rename) folder for packages and rename packages. * @param folderPath Path to folder that should be renamed. * @param newName New name for folder. * @returns Path to renamed folder or null if renaming wasn't successfull. */ renameFolder(folderPath: string, newName: string): string | null; /** * Checks whether folder exists. * @param folderPath Path to folder. * @returns Indicates whether folder exists. */ folderExists(folderPath: string): boolean; /** * Checks whether folder is opened. * @param folderPath Path to folder. * @returns Indicates whether folder is opened (false might also be returned if folder does not exists). */ isFolderOpened(folderPath: string): boolean; /** * Returns subfolder names of folder. * @param folderPath Path to folder. * @returns Names of subfolders or null if folder does not exist. */ getSubfolderNames(folderPath: string): string[] | null; /** * Adds new package (if generation of folders for packages is enabled, folders might also be created). * @param packageName Package name. * @returns Indicates whether package could be added. Only returns false if package could not be created because of bad package name, otherwise it always returns true (if the package already exists, it also returns true). */ addPackage(packageName: string): boolean; /** * Removes package (this does not remove folders for package if folders generation is enabled). * @param packageName Package name. * @param deleteCodeViewsAndFiles Determines whether code views and files should be deleted also in folders. * @returns Indicates whether package was successfully removed. */ removePackage(packageName: string, deleteCodeViewsAndFiles?: boolean): boolean; /** * Returns folder path for package. * @param packageName Package name. * @returns Package folder path or null if package does not exists. */ getPackageFolderPath(packageName: string | null): string | null; /** * Returns folder path that can be used to delete folder to delete package or null, if folder for package cannot be deleted (for example when folders generation is disabled or there is subpackage - basically whenever there is something else that does not belong to package). * @param packageName Package name. * @returns Folder path that can be used to delete folder to delete package or null, if folder for package cannot be deleted. */ getFolderPathToRemovePackage(packageName: string): string | null; /** * Checks whether package exists. * @param packageName Package name. * @returns Indicates whether package exists. */ packageExists(packageName: string): boolean; /** * Checks whether package package is opened. * @param packageName Package name. * @returns Indicates whether package is opened (false might also be returned if folder does not exists). */ isPackageFolderOpened(packageName: string | null): boolean; /** * Checkes whether at least one package exists (default package included). * @returns Indicates whether at least one package exists (default package included). */ hasPackages(): boolean; /** * Adds code view. * @param fileName Name of code view. * @param codeView Code view. * @param showCodeViewEventSource Event source to be used to fire event when code view button is clicked. * @param folderPath Path to folder into which should be code view added (if folder does not exist, it is created). * @param usePackage Determines whether code view should be put into package. * @param packageName Name of package into which should be code view put (null for default package) (if package does not exist, it is created). * @returns Indicates whether code view has been successfully added (code view might not be added if there is already another code view the same name in the same folder or package). */ addCodeView(fileName: string, codeView: CodeView, showCodeViewEventSource: EventSourcePoint<CodeViewButton, CodeView>, folderPath: string | null, usePackage?: boolean, packageName?: string | null): boolean; /** * Finds code view by folder path. * @param folderPath Path to folder (null can be used for root folder). * @param fileName Name of code view. * @returns Found code view or null if code view wasn't found. */ getCodeViewByFolderPath(folderPath: string | null, fileName: string): CodeView | null; /** * Finds code view by identifier. * @param identifier Identifier (folder path + file name). * @returns Found code view or null if code view wasn't found. */ getCodeViewByIdentifier(identifier: string): CodeView | null; /** * Finds code view by package. * @param packageName Package name (null for default package). * @param fileName Name of code view. * @returns Found code view or null if code view wasn't found. */ getCodeViewByPackage(packageName: string | null, fileName: string): CodeView | null; /** * Returns code views in folder. * @param folderPath Path to folder. * @param traverseSubfolders Determines whether code views in subfolders should also be included. * @returns Code views. */ getCodeViewsInFolder(folderPath: string | null, traverseSubfolders?: boolean): CodeView[]; /** * Returns code views in package. * @param packageName Package name (null for default package). * @returns Code views. */ getCodeViewsInPackage(packageName: string | null): CodeView[]; /** * Removes code view by identifier. * @param identifier Identifier (folder path + file name). * @returns Indicates whether code view was successfully found and removed. */ removeCodeViewByIdentifier(identifier: string): boolean; /** * Removes code view by package. * @param packageName Package name. * @param fileName Name of code view. * @returns Indicates whether code view was successfully found and removed. */ removeCodeViewByPackage(packageName: string | null, fileName: string): boolean; /** * Changes identifier of code view. It can change folder path and name of code view but it never changes package of code view. * @param identifier Identifier (folder path + file name) of code view whose identifier should be changed. * @param newIdentifier New identifier (folder path + file name). * @param showCodeViewEventSource Event source to be used to fire event when code view button is clicked. * @returns Indicates whether identifier has been successfully changed. */ changeCodeViewIdentifier(identifier: string, newIdentifier: string, showCodeViewEventSource: EventSourcePoint<CodeViewButton, CodeView>): boolean; /** * Returns package of code view. * @param identifier Identifier (folder path + file name) of code view. * @returns Package name or null for default package. If undefined is returned, code view is not associated with package or does not exist. */ getCodeViewPackage(identifier: string): string | null | undefined; /** * Removes code view from package. * @param identifier Identifier (folder path + file name) of code view. * @returns Indicates whether code view was found and successfully removed from package. */ removeCodeViewPackage(identifier: string): boolean; /** * Adds file. * @param fileName Name of file. * @param codeBoxFile Code box file. * @param folderPath Path to folder into which should be file added (if folder does not exist, it is created). * @param usePackage Determines whether file should be put into package. * @param packageName Name of package into which should be file put (null for default package) (if package does not exist, it is created). * @returns Indicates whether file has been successfully added (file might not be added if there is already another file with the same name in the same folder or package). */ addFile(fileName: string, codeBoxFile: ProjectCodeBoxFile, folderPath: string | null, usePackage?: boolean, packageName?: string | null): boolean; /** * Finds file by folder path. * @param folderPath Path to folder (null can be used for root folder). * @param fileName Name of file. * @returns Found file or null if file wasn't found. */ getFileByFolderPath(folderPath: string | null, fileName: string): ProjectCodeBoxFile | null; /** * Finds file by identifier. * @param identifier Identifier (folder path + file name). * @returns Found file or null if file wasn't found. */ getFileByIdentifier(identifier: string): ProjectCodeBoxFile | null; /** * Finds file by package. * @param packageName Package name (null for default package). * @param fileName Name of file. * @returns Found file or null if file wasn't found. */ getFileByPackage(packageName: string | null, fileName: string): ProjectCodeBoxFile | null; /** * Returns files in folder. * @param folderPath Path to folder. * @param traverseSubfolders Determines whether files in subfolders should also be included. * @returns Files. */ getFilesInFolder(folderPath: string | null, traverseSubfolders?: boolean): ProjectCodeBoxFile[]; /** * Returns files in package. * @param packageName Package name (null for default package). * @returns Files. */ getFilesInPackage(packageName: string | null): ProjectCodeBoxFile[]; /** * Removes file by identifier. * @param identifier Identifier (folder path + file name). * @returns Indicates whether file was successfully found and removed. */ removeFileByIdentifier(identifier: string): boolean; /** * Removes file by package. * @param packageName Package name. * @param fileName Name of file. * @returns Indicates whether file was successfully found and removed. */ removeFileByPackage(packageName: string | null, fileName: string): boolean; /** * Changes identifier of file. It can change folder path and name of file but it never changes package of file. * @param identifier Identifier (folder path + file name) of file whose identifier should be changed. * @param newIdentifier New identifier (folder path + file name). * @returns Indicates whether identifier has been successfully changed. */ changeFileIdentifier(identifier: string, newIdentifier: string): boolean; /** * Changes download link of file (or sets file as non-downloadable). * @param identifier Identifier (folder path + file name) of file. * @param newDownloadLink Download link or null to set file as non-downloadable. * @returns Indicates whether file was successfully found and updated. */ changeFileDownloadLinkByIdentifier(identifier: string, newDownloadLink: string | null): boolean; /** * Returns package of file. * @param identifier Identifier (folder path + file name) of file. * @returns Package name or null for default package. If undefined is returned, file is not associated with package or does not exist. */ getFilePackage(identifier: string): string | null | undefined; /** * Removes file from package. * @param identifier Identifier (folder path + file name) of file. * @returns Indicates whether file was found and successfully removed from package. */ removeFilePackage(identifier: string): boolean; /** * Sets no code view button as active. */ setNoCodeViewButtonAsActive(): void; /** * Sets buttons of code view as active (previously active buttons are set as inactive). * @param identifier Identifier (folder path + file name) of code view which buttons should be set as active. */ setCodeViewButtonsAsActiveByIdentifier(identifier: string): void; /** * Opens folder. * @param folderPath Path to folder to open. * @param openParentFolders Determines whether parent folders of folder should open too. * @param animate Determines whether animation should be used. */ openFolder(folderPath: string, openParentFolders?: boolean, animate?: boolean): void; /** * Closes folder. * @param folderPath Path to folder to close. * @param closeSubfolders Determines whether subfolders should close too. * @param animate Determines whether animation should be used. */ closeFolder(folderPath: string, closeSubfolders?: boolean, animate?: boolean): void; /** * Opens package. * @param packageName Package name (null for default package). * @param animate Determines whether animation should be used. */ openPackage(packageName: string | null, animate?: boolean): void; /** * Closes package. * @param packageName Package name (null for default package). * @param animate Determines whether animation should be used. */ closePackage(packageName: string | null, animate?: boolean): void; /** * Updates tab navigation. * @param panelOpened Indicates whether panel is opened. */ updateTabNavigation(panelOpened: boolean): void; /** * Returns folder structure. * @returns Folder structure. */ getFolderStructure(): TreeNode<FolderInfo>[]; /** * Returns package names. * @returns Package names. */ getPackageNames(): string[]; /** * Returns package infos. * @returns Package infos. */ getPackageInfos(): PackageInfo[]; /** * Returns path to folder used for packages. * @returns Folder path for packages. */ getPackagesFolderPath(): string; /** * Sets new folder path for packages. (This method should be called only when folders manager is empty, otherwise problems may occur.) * @param newPackagesFolderPath New folder path for packages. */ setPackagesFolderPath(newPackagesFolderPath: string): void; /** * Closes folder and its subfolders. * @param folder Folder to be closed. * @param animate Determines whether animation should be used. */ private closeFolderAndSubfolders; /** * Sorts packages (default is always first, then alphabetically). */ private sortPackageFolders; /** * Returns folder based on passed path. * @param folderPath Path to folder. * @param createIfNotExist Determines whether folder should be created if it doesn't exist. * @returns Folder or null if folder wasn't found. */ private getFolder; /** * Returns package folder based on package name. * @param normalizedPackageName Package name (normalized) (null for default package). * @param createIfNotExist Determines whether package folder should be created if it doesn't exist. * @returns Package folder or null if folder wasn't found. */ private getPackageFolder; /** * Determines folder path for code view or file based on passed parameters. * @param normalizedFolderPath Folder path (normalized) (null means, folder path was not specified). * @param usePackage Determines whether package should be used. * @param normalizedPackageName Package name (normalized) (null for default package). * @returns Folder path. */ private getFolderPath; /** * Sanitizes file name (removes all slashes). * @param fileName File name. * @returns Sanitized file name. */ private sanitizeFileName; /** * Sanitizes folder name (removes all slashes). * @param folderName Folder name. * @returns Sanitized folder name. */ private sanitizeFolderName; /** * Normalizes folder path (removes unnecessery slashes and so on). * @param folderPath Folder path. * @returns Normalized folder path. */ private normalizeFolderPath; /** * Normalizes package name. * @param packageName Package name. * @returns Normalized package name. */ private normalizePackageName; /** * Parses folder path. * @param normalizedFolderPath Normalized folder path. * @returns Parsed folder path (array with folder names). */ private parseFolderPath; /** * Returns folder names that should be created in packages folder for package. * @param normalizedPackageName Normalized package name. * @returns Folder names. */ private getPackageFolderNames; } export default FoldersManager;