From c102f3c275e3b6cf040ee3014ebccc7e63e727d2 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Mon, 20 May 2019 21:49:48 +0200 Subject: [PATCH] Add types for the webicon-module (#35563) * Add types for the webicon-module * Fix typo * Add a version-restriction * Add function-type restrictions * Fix the code according to tslint --- types/webicon/Icon/CssClassConfig.d.ts | 8 + types/webicon/Icon/CssIconSet.d.ts | 7 + types/webicon/Icon/Icon.d.ts | 18 + types/webicon/Icon/IconClassGenerator.d.ts | 9 + types/webicon/Icon/ImageIcon.d.ts | 7 + types/webicon/Icon/Options/AliasOptions.d.ts | 11 + .../Icon/Options/CssIconSetOptions.d.ts | 24 + types/webicon/Icon/Options/Downloadable.d.ts | 17 + types/webicon/Icon/Options/IconOptions.d.ts | 7 + .../webicon/Icon/Options/IconSetOptions.d.ts | 14 + .../Icon/Options/ImageIconOptions.d.ts | 8 + .../webicon/Icon/Options/SizeableOptions.d.ts | 16 + types/webicon/Icon/Options/Storeable.d.ts | 22 + .../Options/SvgCumulativeIconSetOptions.d.ts | 13 + .../webicon/Icon/Options/SvgIconOptions.d.ts | 9 + .../Icon/Options/SvgIconSetOptions.d.ts | 9 + types/webicon/Icon/Sizeable.d.ts | 9 + types/webicon/Icon/SvgCumulativeIconSet.d.ts | 16 + types/webicon/Icon/SvgIcon.d.ts | 12 + types/webicon/Icon/SvgIconSet.d.ts | 11 + types/webicon/System/Config.d.ts | 105 ++++ .../webicon/System/ConfigurationHandler.d.ts | 14 + .../System/IconLoadedEventHandler.d.ts | 8 + types/webicon/System/IconPreloader.d.ts | 9 + types/webicon/System/Identifiable.d.ts | 9 + types/webicon/System/IdentityMap.d.ts | 6 + types/webicon/System/Injector.d.ts | 20 + types/webicon/System/PublicApi.d.ts | 174 +++++++ types/webicon/Web/DynamicUrlDeclaration.d.ts | 12 + types/webicon/Web/StaticUrlDeclaration.d.ts | 8 + types/webicon/Web/UrlConfig.d.ts | 6 + types/webicon/Web/UrlDeclarationBase.d.ts | 14 + types/webicon/index.d.ts | 67 +++ types/webicon/tsconfig.json | 24 + types/webicon/tslint.json | 7 + types/webicon/webicon-tests.ts | 471 ++++++++++++++++++ 36 files changed, 1201 insertions(+) create mode 100644 types/webicon/Icon/CssClassConfig.d.ts create mode 100644 types/webicon/Icon/CssIconSet.d.ts create mode 100644 types/webicon/Icon/Icon.d.ts create mode 100644 types/webicon/Icon/IconClassGenerator.d.ts create mode 100644 types/webicon/Icon/ImageIcon.d.ts create mode 100644 types/webicon/Icon/Options/AliasOptions.d.ts create mode 100644 types/webicon/Icon/Options/CssIconSetOptions.d.ts create mode 100644 types/webicon/Icon/Options/Downloadable.d.ts create mode 100644 types/webicon/Icon/Options/IconOptions.d.ts create mode 100644 types/webicon/Icon/Options/IconSetOptions.d.ts create mode 100644 types/webicon/Icon/Options/ImageIconOptions.d.ts create mode 100644 types/webicon/Icon/Options/SizeableOptions.d.ts create mode 100644 types/webicon/Icon/Options/Storeable.d.ts create mode 100644 types/webicon/Icon/Options/SvgCumulativeIconSetOptions.d.ts create mode 100644 types/webicon/Icon/Options/SvgIconOptions.d.ts create mode 100644 types/webicon/Icon/Options/SvgIconSetOptions.d.ts create mode 100644 types/webicon/Icon/Sizeable.d.ts create mode 100644 types/webicon/Icon/SvgCumulativeIconSet.d.ts create mode 100644 types/webicon/Icon/SvgIcon.d.ts create mode 100644 types/webicon/Icon/SvgIconSet.d.ts create mode 100644 types/webicon/System/Config.d.ts create mode 100644 types/webicon/System/ConfigurationHandler.d.ts create mode 100644 types/webicon/System/IconLoadedEventHandler.d.ts create mode 100644 types/webicon/System/IconPreloader.d.ts create mode 100644 types/webicon/System/Identifiable.d.ts create mode 100644 types/webicon/System/IdentityMap.d.ts create mode 100644 types/webicon/System/Injector.d.ts create mode 100644 types/webicon/System/PublicApi.d.ts create mode 100644 types/webicon/Web/DynamicUrlDeclaration.d.ts create mode 100644 types/webicon/Web/StaticUrlDeclaration.d.ts create mode 100644 types/webicon/Web/UrlConfig.d.ts create mode 100644 types/webicon/Web/UrlDeclarationBase.d.ts create mode 100644 types/webicon/index.d.ts create mode 100644 types/webicon/tsconfig.json create mode 100644 types/webicon/tslint.json create mode 100644 types/webicon/webicon-tests.ts diff --git a/types/webicon/Icon/CssClassConfig.d.ts b/types/webicon/Icon/CssClassConfig.d.ts new file mode 100644 index 0000000000..0cf661e824 --- /dev/null +++ b/types/webicon/Icon/CssClassConfig.d.ts @@ -0,0 +1,8 @@ +import { IconClassGenerator } from "./IconClassGenerator"; + +/** + * Represents a css-class resolver. + * + * If you provide a `string`, `?` and `%` are replaced by the icon-id. + */ +export type CssClassConfig = string | IconClassGenerator; diff --git a/types/webicon/Icon/CssIconSet.d.ts b/types/webicon/Icon/CssIconSet.d.ts new file mode 100644 index 0000000000..eab6846a99 --- /dev/null +++ b/types/webicon/Icon/CssIconSet.d.ts @@ -0,0 +1,7 @@ +import { ImageIcon } from "./ImageIcon"; + +/** + * Represents a css icon-set. + */ +export interface CssIconSet extends ImageIcon { +} diff --git a/types/webicon/Icon/Icon.d.ts b/types/webicon/Icon/Icon.d.ts new file mode 100644 index 0000000000..61b5a25e9b --- /dev/null +++ b/types/webicon/Icon/Icon.d.ts @@ -0,0 +1,18 @@ +/** + * Represents an icon. + */ +export interface Icon { + /** + * Parses the input to an icon-id. + * + * @param id + * The id of the icon to get. + * + * @param params + * Additional parameters for getting the icon. + * + * @return + * The id of the icon to get. + */ + iconIdParser?(id: string, params: string[]): string; +} diff --git a/types/webicon/Icon/IconClassGenerator.d.ts b/types/webicon/Icon/IconClassGenerator.d.ts new file mode 100644 index 0000000000..475d4dee9f --- /dev/null +++ b/types/webicon/Icon/IconClassGenerator.d.ts @@ -0,0 +1,9 @@ +/** + * Provides the functionality to generate a css-class for an icon. + */ +export interface IconClassGenerator { + /** + * Generates a css-class for an icon. + */ + (id: string, params: string[]): string; +} diff --git a/types/webicon/Icon/ImageIcon.d.ts b/types/webicon/Icon/ImageIcon.d.ts new file mode 100644 index 0000000000..78cdcef8b0 --- /dev/null +++ b/types/webicon/Icon/ImageIcon.d.ts @@ -0,0 +1,7 @@ +import { Icon } from "./Icon"; + +/** + * Represents a non-vector icon. + */ +export interface ImageIcon extends Icon { +} diff --git a/types/webicon/Icon/Options/AliasOptions.d.ts b/types/webicon/Icon/Options/AliasOptions.d.ts new file mode 100644 index 0000000000..638c8a98a4 --- /dev/null +++ b/types/webicon/Icon/Options/AliasOptions.d.ts @@ -0,0 +1,11 @@ +import { Identifiable } from "../../System/Identifiable"; + +/** + * Provides options for aliases. + */ +export interface AliasOptions { + /** + * The icon-set this alias refers to. + */ + alias: string; +} diff --git a/types/webicon/Icon/Options/CssIconSetOptions.d.ts b/types/webicon/Icon/Options/CssIconSetOptions.d.ts new file mode 100644 index 0000000000..00b8236e63 --- /dev/null +++ b/types/webicon/Icon/Options/CssIconSetOptions.d.ts @@ -0,0 +1,24 @@ +import { CssClassConfig } from "../CssClassConfig"; +import { IconOptions } from "./IconOptions"; + +/** + * Provides options for css icon-sets. + */ +export interface CssIconSetOptions extends IconOptions { + /** + * Either a text-pattern or a callback which provides a css-classname. + * + * The symbols `?` and `%` in the text-pattern are replaced by the icon-id. + */ + className?: CssClassConfig; + + /** + * An alias of the `className`-property. + */ + class?: this["className"]; + + /** + * An alias of the `className`-property. + */ + cssClass?: this["className"]; +} diff --git a/types/webicon/Icon/Options/Downloadable.d.ts b/types/webicon/Icon/Options/Downloadable.d.ts new file mode 100644 index 0000000000..bc313f0afb --- /dev/null +++ b/types/webicon/Icon/Options/Downloadable.d.ts @@ -0,0 +1,17 @@ +import { UrlConfig } from "../../Web/UrlConfig"; +import { IconOptions } from "./IconOptions"; + +/** + * Represents a downloadable object. + */ +export interface Downloadable extends IconOptions { + /** + * The url to load the source from. + */ + url?: UrlConfig; + + /** + * An alias of the `url`-property. + */ + uri?: this["url"]; +} diff --git a/types/webicon/Icon/Options/IconOptions.d.ts b/types/webicon/Icon/Options/IconOptions.d.ts new file mode 100644 index 0000000000..c03921bc5d --- /dev/null +++ b/types/webicon/Icon/Options/IconOptions.d.ts @@ -0,0 +1,7 @@ +import { Icon } from "../Icon"; + +/** + * Provides options for icons. + */ +export interface IconOptions extends Icon { +} diff --git a/types/webicon/Icon/Options/IconSetOptions.d.ts b/types/webicon/Icon/Options/IconSetOptions.d.ts new file mode 100644 index 0000000000..1b0f4eacff --- /dev/null +++ b/types/webicon/Icon/Options/IconSetOptions.d.ts @@ -0,0 +1,14 @@ +import { SvgIcon } from "../SvgIcon"; +import { Downloadable } from "./Downloadable"; +import { IconOptions } from "./IconOptions"; +import { SizeableOptions } from "./SizeableOptions"; + +/** + * Provides options for svg-icon sets. + */ +export interface IconSetOptions extends IconOptions, SvgIcon, SizeableOptions, Downloadable { + /** + * A value indicating whether the icons are loaded separately. + */ + cumulative?: boolean; +} diff --git a/types/webicon/Icon/Options/ImageIconOptions.d.ts b/types/webicon/Icon/Options/ImageIconOptions.d.ts new file mode 100644 index 0000000000..87a62d5872 --- /dev/null +++ b/types/webicon/Icon/Options/ImageIconOptions.d.ts @@ -0,0 +1,8 @@ +import { IconOptions } from "./IconOptions"; +import { Storeable } from "./Storeable"; + +/** + * Provides options for non-vector icons. + */ +export interface ImageIconOptions extends IconOptions, Storeable { +} diff --git a/types/webicon/Icon/Options/SizeableOptions.d.ts b/types/webicon/Icon/Options/SizeableOptions.d.ts new file mode 100644 index 0000000000..c31fe1a66f --- /dev/null +++ b/types/webicon/Icon/Options/SizeableOptions.d.ts @@ -0,0 +1,16 @@ +import { Sizeable } from "../Sizeable"; + +/** + * Provides options for sizeable objects. + */ +export interface SizeableOptions extends Sizeable { + /** + * An alias of the `iconSize`-property. + */ + size?: this["iconSize"]; + + /** + * An alias of the `iconSize`-property. + */ + svgIconSize?: this["iconSize"]; +} diff --git a/types/webicon/Icon/Options/Storeable.d.ts b/types/webicon/Icon/Options/Storeable.d.ts new file mode 100644 index 0000000000..cbaa9c3883 --- /dev/null +++ b/types/webicon/Icon/Options/Storeable.d.ts @@ -0,0 +1,22 @@ +import { Downloadable } from "./Downloadable"; + +/** + * Represents a storeable object. + */ +export interface Storeable extends Downloadable { + /** + * A value indicating whether the source is preloadable. + */ + preloadable?: boolean; + + /** + * Resolves the id of the underlying source. + * + * @param id + * The underlying id of the source. + * + * @return + * The resolved id of the source. + */ + iconIdResolver?(id: string): string; +} diff --git a/types/webicon/Icon/Options/SvgCumulativeIconSetOptions.d.ts b/types/webicon/Icon/Options/SvgCumulativeIconSetOptions.d.ts new file mode 100644 index 0000000000..f2bbc034c6 --- /dev/null +++ b/types/webicon/Icon/Options/SvgCumulativeIconSetOptions.d.ts @@ -0,0 +1,13 @@ +import { IconSetOptions } from "./IconSetOptions"; + +/** + * Provides options for svg-sets which are loaded lazily. + */ +export interface SvgCumulativeIconSetOptions extends IconSetOptions { + cumulative: true; + + /** + * The amount of miliseconds to wait before downloading the icons. + */ + waitDuration?: number; +} diff --git a/types/webicon/Icon/Options/SvgIconOptions.d.ts b/types/webicon/Icon/Options/SvgIconOptions.d.ts new file mode 100644 index 0000000000..1ef4493456 --- /dev/null +++ b/types/webicon/Icon/Options/SvgIconOptions.d.ts @@ -0,0 +1,9 @@ +import { ImageIconOptions } from "./ImageIconOptions"; +import { SizeableOptions } from "./SizeableOptions"; +import { SvgIcon } from "../SvgIcon"; + +/** + * Provides options for svg-icons. + */ +export interface SvgIconOptions extends ImageIconOptions, SizeableOptions, SvgIcon { +} diff --git a/types/webicon/Icon/Options/SvgIconSetOptions.d.ts b/types/webicon/Icon/Options/SvgIconSetOptions.d.ts new file mode 100644 index 0000000000..65e6641386 --- /dev/null +++ b/types/webicon/Icon/Options/SvgIconSetOptions.d.ts @@ -0,0 +1,9 @@ +import { IconSetOptions } from "./IconSetOptions"; +import { Storeable } from "./Storeable"; + +/** + * Provides options for svg-icon sets. + */ +export interface SvgIconSetOptions extends IconSetOptions, Storeable { + cumulative?: false; +} diff --git a/types/webicon/Icon/Sizeable.d.ts b/types/webicon/Icon/Sizeable.d.ts new file mode 100644 index 0000000000..e8770a8ecd --- /dev/null +++ b/types/webicon/Icon/Sizeable.d.ts @@ -0,0 +1,9 @@ +/** + * Represents a sizeable object. + */ +export interface Sizeable { + /** + * The default size of the icon. + */ + iconSize?: number; +} diff --git a/types/webicon/Icon/SvgCumulativeIconSet.d.ts b/types/webicon/Icon/SvgCumulativeIconSet.d.ts new file mode 100644 index 0000000000..4f553e7cf5 --- /dev/null +++ b/types/webicon/Icon/SvgCumulativeIconSet.d.ts @@ -0,0 +1,16 @@ +import { SvgIcon } from "./SvgIcon"; + +/** + * Represents an svg icon-set which is loaded delayed. + */ +export interface SvgCumulativeIconSet extends SvgIcon { + /** + * A value indicating whether the icons are loaded separately. + */ + cumulative: true; + + /** + * The number of miliseconds to wait before downloading the icons. + */ + waitDuration?: number; +} diff --git a/types/webicon/Icon/SvgIcon.d.ts b/types/webicon/Icon/SvgIcon.d.ts new file mode 100644 index 0000000000..1106332cbc --- /dev/null +++ b/types/webicon/Icon/SvgIcon.d.ts @@ -0,0 +1,12 @@ +import { ImageIcon } from "./ImageIcon"; +import { Sizeable } from "./Sizeable"; + +/** + * Represents an svg-icon. + */ +export interface SvgIcon extends ImageIcon, Sizeable { + /** + * The default viewBox of the icon. + */ + viewBox?: string; +} diff --git a/types/webicon/Icon/SvgIconSet.d.ts b/types/webicon/Icon/SvgIconSet.d.ts new file mode 100644 index 0000000000..6eea807f4d --- /dev/null +++ b/types/webicon/Icon/SvgIconSet.d.ts @@ -0,0 +1,11 @@ +import { SvgIcon } from "./SvgIcon"; + +/** + * Represents an svg icon-set. + */ +export interface SvgIconSet extends SvgIcon { + /** + * A value indicating whether the icons are loaded separately. + */ + cumulative?: false; +} diff --git a/types/webicon/System/Config.d.ts b/types/webicon/System/Config.d.ts new file mode 100644 index 0000000000..8e3daed6e9 --- /dev/null +++ b/types/webicon/System/Config.d.ts @@ -0,0 +1,105 @@ +import { SizeableOptions } from "../Icon/Options/SizeableOptions"; +import { CssClassConfig } from "../Icon/CssClassConfig"; +import { AliasOptions } from "../Icon/Options/AliasOptions"; +import { CssIconSetOptions } from "../Icon/Options/CssIconSetOptions"; +import { ImageIconOptions } from "../Icon/Options/ImageIconOptions"; +import { SvgCumulativeIconSetOptions } from "../Icon/Options/SvgCumulativeIconSetOptions"; +import { SvgIconOptions } from "../Icon/Options/SvgIconOptions"; +import { SvgIconSetOptions } from "../Icon/Options/SvgIconSetOptions"; +import { Identifiable } from "./Identifiable"; +import { IdentityMap } from "./IdentityMap"; + +/** + * Provides settings for the `webicon`-module. + */ +export interface Config { + /** + * The icons to provide. + */ + icons?: IdentityMap; + + /** + * An alias of the `icons`-property. + */ + icon?: this["icons"]; + + /** + * The svg-sets to provide. + */ + svgSets?: IdentityMap; + + /** + * An alias of the `svgSets`-property. + */ + svgSet?: this["svgSets"]; + + /** + * An alias of the `svgSets`-property. + */ + iconSet?: this["svgSets"]; + + /** + * An alias of the `svgSets`-property. + */ + iconSets?: this["svgSets"]; + + /** + * A set of icon-fonts to provide. + */ + fonts?: IdentityMap; + + /** + * An alias of the `fonts`-property. + */ + font?: this["fonts"]; + + /** + * A set of sprite-icons to provide. + */ + sprites?: IdentityMap; + + /** + * An alias of the `sprites`-property. + */ + sprite?: this["sprites"]; + + /** + * A set of alias-names for icon-sets. + */ + alias?: IdentityMap; + + /** + * An alias of the `alias`-property. + */ + sourceAlias?: this["alias"]; + + /** + * The default icon-set to provide. + */ + defaultIconSetUrl?: string | (SvgIconSetOptions | SvgCumulativeIconSetOptions); + + /** + * An alias of the `defaultIconSetUrl`-property. + */ + defaultSvgSetUrl?: this["defaultIconSetUrl"]; + + /** + * An alias of the `defaultIconSetUrl`-property. + */ + defaultSvgIconSetUrl?: this["defaultIconSetUrl"]; + + /** + * The default icon-set to use. + */ + defaultSource?: string | Identifiable; + + /** + * An alias of the `defaultSource`-property. + */ + default?: this["defaultSource"]; + + /** + * The default size for svg-icons. + */ + defaultSvgIconSize?: number | SizeableOptions; +} diff --git a/types/webicon/System/ConfigurationHandler.d.ts b/types/webicon/System/ConfigurationHandler.d.ts new file mode 100644 index 0000000000..efef409a7f --- /dev/null +++ b/types/webicon/System/ConfigurationHandler.d.ts @@ -0,0 +1,14 @@ +import { PublicApi } from "./PublicApi"; + +/** + * Provides the functionality to handle the `configuration`-event. + */ +export interface ConfigurationHandler { + /** + * Handles the `configuration`-event. + * + * @param api + * The configuration-api. + */ + (api: PublicApi): void; +} diff --git a/types/webicon/System/IconLoadedEventHandler.d.ts b/types/webicon/System/IconLoadedEventHandler.d.ts new file mode 100644 index 0000000000..66049baefe --- /dev/null +++ b/types/webicon/System/IconLoadedEventHandler.d.ts @@ -0,0 +1,8 @@ +import { IconPreloader } from "./IconPreloader"; + +/** + * Provides the functionality to handle the `preloaded`-event. + */ +export interface IconLoadedEventHandler { + (preloader: IconPreloader): any; +} diff --git a/types/webicon/System/IconPreloader.d.ts b/types/webicon/System/IconPreloader.d.ts new file mode 100644 index 0000000000..2d89898b0d --- /dev/null +++ b/types/webicon/System/IconPreloader.d.ts @@ -0,0 +1,9 @@ +/** + * Represents an icon-preloader. + */ +export interface IconPreloader extends Promise { + /** + * The names of the icons to download. + */ + iconSets: string[]; +} diff --git a/types/webicon/System/Identifiable.d.ts b/types/webicon/System/Identifiable.d.ts new file mode 100644 index 0000000000..68c7f53235 --- /dev/null +++ b/types/webicon/System/Identifiable.d.ts @@ -0,0 +1,9 @@ +/** + * Represents an identifiable object. + */ +export interface Identifiable { + /** + * The id of the object. + */ + id: string; +} diff --git a/types/webicon/System/IdentityMap.d.ts b/types/webicon/System/IdentityMap.d.ts new file mode 100644 index 0000000000..bd4eb2a027 --- /dev/null +++ b/types/webicon/System/IdentityMap.d.ts @@ -0,0 +1,6 @@ +import { Identifiable } from "./Identifiable"; + +/** + * Represents a map of identifiable objects. + */ +export type IdentityMap = Array | { [id: string]: TSimple | TComplex }; diff --git a/types/webicon/System/Injector.d.ts b/types/webicon/System/Injector.d.ts new file mode 100644 index 0000000000..b7c9f59a05 --- /dev/null +++ b/types/webicon/System/Injector.d.ts @@ -0,0 +1,20 @@ +/** + * Provides the functionality to require components of the `webicon`-module. + */ +export interface Injector { + /** + * Requires a component of the `webicon`-module. + */ + (name: string, injector: Injector): T; + + /** + * Checks whether a component with the specified `name` exists. + * + * @param name + * The name of the component to check for existence. + * + * @return + * A value indicating whether a component with the specified `name` exists. + */ + has(name: string): boolean; +} diff --git a/types/webicon/System/PublicApi.d.ts b/types/webicon/System/PublicApi.d.ts new file mode 100644 index 0000000000..dc88cd6d2e --- /dev/null +++ b/types/webicon/System/PublicApi.d.ts @@ -0,0 +1,174 @@ +import { CssClassConfig } from "../Icon/CssClassConfig"; +import { CssIconSet } from "../Icon/CssIconSet"; +import { ImageIcon } from "../Icon/ImageIcon"; +import { SvgCumulativeIconSet } from "../Icon/SvgCumulativeIconSet"; +import { SvgIcon } from "../Icon/SvgIcon"; +import { SvgIconSet } from "../Icon/SvgIconSet"; +import { UrlConfig } from "../Web/UrlConfig"; +import { IconLoadedEventHandler } from "./IconLoadedEventHandler"; +import { Injector } from "./Injector"; + +/** + * An object for configuring the `webicon`-module. + */ +export interface PublicApi { + /** + * Adds a new icon. + * + * @param id + * The id of the icon to add. + * + * @param urlConfig + * The url to download the icon from. + * + * @param options + * Additional options for the icon. + */ + icon(id: string, urlConfig: UrlConfig, options?: ImageIcon | SvgIcon): this; + + /** + * Adds an icon-set. + * + * @param id + * The id of the icon-set to add. + * + * @param urlConfig + * The url to download the icon-set from. + * + * @param options + * Additional options for the icon-set. + */ + svgSet(id: string, urlConfig: UrlConfig, options?: SvgIconSet): this; + + /** + * Adds an icon-set. + * + * @param id + * The id of the icon-set to add. + * + * @param urlConfig + * The url to download the icon-set from. + * + * @param options + * Additional options for the icon-set. + */ + svgSet(id: string, urlConfig: UrlConfig, options?: SvgCumulativeIconSet): this; + + /** + * An alias of the `svgSet`-method. + */ + iconSet: this["svgSet"]; + + /** + * Adds a font icon-set. + * + * @param id + * The id of the icon-set to add. + * + * @param cssClassConfig + * The css-class to add to icons. + * + * @param options + * Additional options for the icon-set. + */ + font(id: string, cssClassConfig: CssClassConfig, options?: CssIconSet): this; + + /** + * Adds a sprite icon-set. + * + * @param id + * The id of the icon-set to add. + * + * @param cssClassConfig + * The css-class to add to icons. + * + * @param options + * Additional options for the icon-set. + */ + sprite(id: string, cssClassConfig: CssClassConfig, options?: CssIconSet): this; + + /** + * Adds a link to the `alias` icon-set. + * + * @param id + * The id which links to the `alias`. + * + * @param alias + * The id of the icon-set to refer to. + */ + alias(id: string, alias: string): this; + + /** + * An alias of the `alias`-method. + */ + sourceAlias: this["alias"]; + + /** + * Adds a default svg-set. + * + * @param url + * The url to the default svg-set to use. + * + * @param options + * Additional options for the svg-set. + */ + defaultIconSetUrl(url: string, options?: SvgIconSet | SvgCumulativeIconSet): this; + + /** + * An alias of the `defaultSvgSetUrl`-method. + */ + defaultSvgSetUrl: this["defaultIconSetUrl"]; + + /** + * An alias of the `defaultSvgSetUrl`-method. + */ + defaultSvgIconSetUrl: this["defaultIconSetUrl"]; + + /** + * Sets a default icon-set. + * + * @param id + * The id of the default icon-set. + */ + defaultSource(id: string): this; + + /** + * An alias of the `defaultSource`-method. + */ + default: this["defaultSource"]; + + /** + * Sets the default icon-size for svg-icons. + * + * @param size + * The default svg icon-size. + */ + defaultSvgIconSize(size: number): this; + + /** + * Preloads the icons. + * + * @param eventHandler + * A callback for handling the icon-loader `Promise`. + */ + preload(eventHandler?: IconLoadedEventHandler): this; + + /** + * Preloads the icons. + * + * @param names + * Either a value indicating whether to preload all icons or the names of the icons to preload. + * + * @param eventHandler + * A callback for handling the icon-loader `Promise`. + */ + preload(names: string | string[] | boolean, eventHandler?: IconLoadedEventHandler): this; + + /** + * Adds an event-handler to the `ready`-event. + * + * @param eventHandler + * THe event-handler for handling the `ready`-event. + */ + ready(eventHandler: (injector: Injector) => void): this; +} diff --git a/types/webicon/Web/DynamicUrlDeclaration.d.ts b/types/webicon/Web/DynamicUrlDeclaration.d.ts new file mode 100644 index 0000000000..b6a79d8530 --- /dev/null +++ b/types/webicon/Web/DynamicUrlDeclaration.d.ts @@ -0,0 +1,12 @@ +import { StaticUrlDeclaration } from "./StaticUrlDeclaration"; +import { UrlDeclarationBase } from "./UrlDeclarationBase"; + +/** + * Represents a dynamically generated url. + */ +export interface DynamicUrlDeclaration extends UrlDeclarationBase { + /** + * Either the actual url or a function for generating the url. + */ + url?: string | ((args: T) => string | StaticUrlDeclaration); +} diff --git a/types/webicon/Web/StaticUrlDeclaration.d.ts b/types/webicon/Web/StaticUrlDeclaration.d.ts new file mode 100644 index 0000000000..57f8a12fae --- /dev/null +++ b/types/webicon/Web/StaticUrlDeclaration.d.ts @@ -0,0 +1,8 @@ +import { UrlDeclarationBase } from "./UrlDeclarationBase"; + +/** + * Represents a static url. + */ +export interface StaticUrlDeclaration extends UrlDeclarationBase { + url?: string; +} diff --git a/types/webicon/Web/UrlConfig.d.ts b/types/webicon/Web/UrlConfig.d.ts new file mode 100644 index 0000000000..e873164287 --- /dev/null +++ b/types/webicon/Web/UrlConfig.d.ts @@ -0,0 +1,6 @@ +import { DynamicUrlDeclaration } from "./DynamicUrlDeclaration"; + +/** + * Represents a url. + */ +export type UrlConfig = string | DynamicUrlDeclaration; diff --git a/types/webicon/Web/UrlDeclarationBase.d.ts b/types/webicon/Web/UrlDeclarationBase.d.ts new file mode 100644 index 0000000000..fa6c236c87 --- /dev/null +++ b/types/webicon/Web/UrlDeclarationBase.d.ts @@ -0,0 +1,14 @@ +/** + * Represents an url. + */ +export interface UrlDeclarationBase { + /** + * The `GET`-variables of the url. + */ + params?: any; + + /** + * The actual url. + */ + url?: any; +} diff --git a/types/webicon/index.d.ts b/types/webicon/index.d.ts new file mode 100644 index 0000000000..8c4262fb57 --- /dev/null +++ b/types/webicon/index.d.ts @@ -0,0 +1,67 @@ +// Type definitions for webicon 0.10 +// Project: https://icons8.github.io/webicon/ +// Definitions by: Manuel Thalmann +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import { CssClassConfig } from "./Icon/CssClassConfig"; +import { CssIconSet } from "./Icon/CssIconSet"; +import { IconClassGenerator } from "./Icon/IconClassGenerator"; +import { ImageIcon } from "./Icon/ImageIcon"; +import { AliasOptions } from "./Icon/Options/AliasOptions"; +import { CssIconSetOptions } from "./Icon/Options/CssIconSetOptions"; +import { ImageIconOptions } from "./Icon/Options/ImageIconOptions"; +import { SvgCumulativeIconSetOptions } from "./Icon/Options/SvgCumulativeIconSetOptions"; +import { SvgIconOptions } from "./Icon/Options/SvgIconOptions"; +import { SvgIconSetOptions } from "./Icon/Options/SvgIconSetOptions"; +import { SvgCumulativeIconSet } from "./Icon/SvgCumulativeIconSet"; +import { SvgIcon } from "./Icon/SvgIcon"; +import { SvgIconSet } from "./Icon/SvgIconSet"; +import { Config } from "./System/Config"; +import { ConfigurationHandler } from "./System/ConfigurationHandler"; +import { IconLoadedEventHandler } from "./System/IconLoadedEventHandler"; +import { IconPreloader } from "./System/IconPreloader"; +import { Identifiable } from "./System/Identifiable"; +import { Injector } from "./System/Injector"; +import { PublicApi } from "./System/PublicApi"; +import { DynamicUrlDeclaration } from "./Web/DynamicUrlDeclaration"; +import { StaticUrlDeclaration } from "./Web/StaticUrlDeclaration"; +import { UrlConfig } from "./Web/UrlConfig"; + +export { + CssClassConfig, + CssIconSet, + IconClassGenerator, + ImageIcon, + AliasOptions, + CssIconSetOptions, + ImageIconOptions, + SvgCumulativeIconSetOptions, + SvgIconOptions, + SvgIconSetOptions, + SvgCumulativeIconSet, + SvgIcon, + SvgIconSet, + Config, + ConfigurationHandler, + IconLoadedEventHandler, + IconPreloader, + Identifiable, + Injector, + PublicApi, + DynamicUrlDeclaration, + StaticUrlDeclaration, + UrlConfig +}; + +declare global { + interface JQuery { + /** + * Initializes the web-icons. + * + * @param config + * Either the configuration for initializing web-icons or a callback for configuring the web-icons. + */ + webicons(config: Config | ConfigurationHandler): this; + } +} diff --git a/types/webicon/tsconfig.json b/types/webicon/tsconfig.json new file mode 100644 index 0000000000..116158abfe --- /dev/null +++ b/types/webicon/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "webicon-tests.ts" + ] +} diff --git a/types/webicon/tslint.json b/types/webicon/tslint.json new file mode 100644 index 0000000000..e9003e412b --- /dev/null +++ b/types/webicon/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "no-unnecessary-generics": false, + "no-empty-interface": false + } +} diff --git a/types/webicon/webicon-tests.ts b/types/webicon/webicon-tests.ts new file mode 100644 index 0000000000..5b61dbd2b0 --- /dev/null +++ b/types/webicon/webicon-tests.ts @@ -0,0 +1,471 @@ +import "jquery"; +import { + Config, + ImageIconOptions, + SvgIconOptions, + Identifiable, + SvgIconSetOptions, + SvgCumulativeIconSetOptions, + CssIconSetOptions, + ImageIcon, + DynamicUrlDeclaration, + SvgIconSet, + SvgCumulativeIconSet, + CssIconSet +} from "webicon"; + +/** + * Provides tests for the `webicon`-module. + */ +export class WebiconTests { + getUrlObject(): DynamicUrlDeclaration { + const result: DynamicUrlDeclaration = { + url: "", + params: {} + }; + + return result; + } + + getStringUrlCallback(): DynamicUrlDeclaration { + const result: DynamicUrlDeclaration = { + url: (arg) => { + // $ExpectType TUriParam + arg; + return ""; + }, + params: {} + }; + return result; + } + + getObjectUrlCallback(): DynamicUrlDeclaration { + const result: DynamicUrlDeclaration = { + url: (arg) => { + // $ExpectType TUriParam + arg; + return { + url: "", + params: {} + }; + } + }; + + return result; + } + + /** + * Tests for the `webicon`-module. + */ + Test() { + const config: Config = {}; + const IDParser: ImageIconOptions["iconIdParser"] = (id, params) => { + // $ExpectType string + id; + // $ExpectType string[] + params; + return ""; + }; + + const IDResolver: ImageIconOptions["iconIdResolver"] = (id) => { + // $ExpectType string + id; + return ""; + }; + + const ClassResolver: CssIconSetOptions["className"] = (id, params) => { + // $ExpectType string + id; + // $ExpectType string[] + params; + return ""; + }; + + const icon: ImageIcon = { + iconIdParser: IDParser + }; + + const set: SvgIconSet = { + cumulative: false, + iconIdParser: IDParser, + iconSize: 1, + viewBox: "" + }; + + const cumulativeSet: SvgCumulativeIconSet = { + cumulative: true, + iconIdParser: IDParser, + iconSize: 1, + viewBox: "", + waitDuration: 1 + }; + + const cssIconSet: CssIconSet = { + iconIdParser: IDParser + }; + + const iconOptions: SvgIconOptions = { + iconIdParser: IDParser, + iconIdResolver: IDResolver, + iconSize: 1, + svgIconSize: 1, + preloadable: true, + size: 1, + url: "", + uri: "", + viewBox: "" + }; + + const idIconOptions: Identifiable & SvgIconOptions = { + id: "clock", + iconIdParser: IDParser, + iconIdResolver: IDResolver, + iconSize: 1, + svgIconSize: 1, + preloadable: true, + size: 1, + url: "", + uri: "", + viewBox: "" + }; + + const setOptions: SvgIconSetOptions = { + cumulative: false, + iconIdParser: IDParser, + iconIdResolver: IDResolver, + iconSize: 1, + preloadable: true, + size: 1, + svgIconSize: 1, + uri: "", + url: "", + viewBox: "" + }; + + const cumulativeSetOptions: SvgCumulativeIconSetOptions = { + cumulative: true, + iconIdParser: IDParser, + iconSize: 1, + size: 1, + svgIconSize: 1, + uri: "", + url: "", + viewBox: "", + waitDuration: 0 + }; + + const idSetOptions: Identifiable & SvgIconSetOptions = { + id: "", + cumulative: false, + iconIdParser: IDParser, + iconIdResolver: IDResolver, + iconSize: 1, + preloadable: true, + size: 1, + svgIconSize: 1, + uri: "", + url: "", + viewBox: "" + }; + + const cssIconSetOptions: CssIconSetOptions = { + class: "", + className: "", + cssClass: "", + iconIdParser: IDParser + }; + + const idCssIconOptions: Identifiable & CssIconSetOptions = { + id: "", + class: "", + className: "", + cssClass: "", + iconIdParser: IDParser + }; + + /** + * Configuring `webicon` using a configuration-handler. + */ + $().webicons( + (config) => { + // $ExpectType PublicApi + config; + config.icon("", "", icon); + config.icon("", this.getUrlObject(), icon); + config.icon("", this.getStringUrlCallback(), icon); + config.icon("", this.getObjectUrlCallback(), icon); + + config.svgSet("", ""); + config.svgSet("", "", set); + config.svgSet("", "", cumulativeSet); + config.svgSet("", this.getUrlObject()); + config.svgSet("", this.getUrlObject(), set); + config.svgSet("", this.getUrlObject(), cumulativeSet); + config.svgSet("", this.getStringUrlCallback()); + config.svgSet("", this.getStringUrlCallback(), set); + config.svgSet("", this.getStringUrlCallback(), cumulativeSet); + config.svgSet("", this.getObjectUrlCallback()); + config.svgSet("", this.getObjectUrlCallback(), set); + config.svgSet("", this.getObjectUrlCallback(), cumulativeSet); + config.iconSet("", ""); + config.iconSet("", "", set); + config.iconSet("", "", cumulativeSet); + config.iconSet("", this.getUrlObject()); + config.iconSet("", this.getUrlObject(), set); + config.iconSet("", this.getUrlObject(), cumulativeSet); + config.iconSet("", this.getStringUrlCallback()); + config.iconSet("", this.getStringUrlCallback(), set); + config.iconSet("", this.getStringUrlCallback(), cumulativeSet); + config.iconSet("", this.getObjectUrlCallback()); + config.iconSet("", this.getObjectUrlCallback(), set); + config.iconSet("", this.getObjectUrlCallback(), cumulativeSet); + + config.font("", "", cssIconSet); + config.font("", ClassResolver, cssIconSet); + + config.sprite("", "", cssIconSet); + config.sprite("", ClassResolver, cssIconSet); + + config.alias("", ""); + config.sourceAlias("", ""); + + config.defaultIconSetUrl(""); + config.defaultIconSetUrl("", set); + config.defaultSvgSetUrl(""); + config.defaultSvgSetUrl("", set); + config.defaultSvgIconSetUrl(""); + config.defaultSvgIconSetUrl("", set); + + config.defaultSource(""); + config.default(""); + + config.defaultSvgIconSize(1); + + config.preload( + (loader) => { + // $ExpectType IconPreloader + loader; + }); + + config.ready( + (injector) => { + // $ExpectType Injector + injector; + }); + }); + + /** + * Declaring urls + * Using a string + */ + setOptions.url = + setOptions.uri = + iconOptions.url = + iconOptions.uri = ""; + + /** + * Using an object + */ + setOptions.url = + setOptions.uri = + iconOptions.url = + iconOptions.uri = this.getUrlObject(); + + /** + * Using a function + * Returning a string + */ + setOptions.url = + setOptions.uri = + iconOptions.url = + iconOptions.uri = this.getStringUrlCallback(); + + /** + * Returning an object + */ + setOptions.url = + setOptions.uri = + iconOptions.url = + iconOptions.uri = this.getObjectUrlCallback(); + + /** + * Declaring class-resolvers + * Using a string + */ + cssIconSetOptions.className = + cssIconSetOptions.cssClass = + cssIconSetOptions.class = ""; + + /** + * Using a function + */ + cssIconSetOptions.className = + cssIconSetOptions.cssClass = + cssIconSetOptions.class = ClassResolver; + + /** + * Setting icons + * Simple + */ + config.icons = + config.icon = { + clock: "" + }; + + /** + * Complex + */ + config.icons = + config.icon = { + clock: iconOptions + }; + + /** + * Array + */ + config.icons = + config.icon = [ + idIconOptions + ]; + + /** + * Setting svg-sets + * Simple + */ + config.svgSets = + config.svgSet = + config.iconSets = + config.iconSet = { + clock: "" + }; + + /** + * Complex + */ + config.svgSets = + config.svgSet = + config.iconSets = + config.iconSet = { + clock: setOptions, + alt: cumulativeSetOptions + }; + + /** + * Array + */ + config.svgSets = + config.svgSet = + config.iconSets = + config.iconSet = [ + idSetOptions + ]; + + /** + * Setting css-class icons + * Simple + */ + config.fonts = + config.font = + config.sprites = + config.sprite = { + clock: "" + }; + + /** + * Complex + */ + config.fonts = + config.font = + config.sprites = + config.sprite = { + clock: cssIconSetOptions + }; + + /** + * Array + */ + config.fonts = + config.font = + config.sprites = + config.sprite = [ + idCssIconOptions + ]; + + /** + * Setting aliases + * Simple + */ + config.alias = + config.sourceAlias = { + alt: "clock" + }; + + /** + * Complex + */ + config.alias = + config.sourceAlias = { + id: { + alias: "" + } + }; + + /** + * Array + */ + config.alias = + config.sourceAlias = [ + { + id: "id", + alias: "" + } + ]; + + /** + * Setting a default icon-set url + * Simple + */ + config.defaultIconSetUrl = + config.defaultSvgSetUrl = + config.defaultSvgIconSetUrl = ""; + + /** + * Complex + */ + config.defaultIconSetUrl = + config.defaultSvgSetUrl = + config.defaultSvgIconSetUrl = setOptions; + + /** + * Setting a default icon-set + * Simple + */ + config.defaultSource = + config.default = ""; + + /** + * Complex + */ + config.defaultSource = + config.default = { + id: "" + }; + + /** + * Setting a default icon-size for vector-icons + * Simple + */ + config.defaultSvgIconSize = 1; + + /** + * Complex + */ + config.defaultSvgIconSize = { + iconSize: 1, + size: 1, + svgIconSize: 1 + }; + + $().webicons(config); + } +}