@bespunky/angular-zen
Version:
The Angular tools you always wished were there.
412 lines (398 loc) • 17.6 kB
JavaScript
import * as i0 from '@angular/core';
import { PLATFORM_ID, Injectable, Inject, Directive, NgModule } from '@angular/core';
import { isPlatformBrowser, isPlatformServer, isPlatformWorkerApp, isPlatformWorkerUi } from '@angular/common';
/**
* Provides quick access to platform information.
*
* @export
* @class UniversalService
*/
class UniversalService {
/**
* Creates an instance of UniversalService.
*
* @param {*} platformId The id of the current platform. This always equals to `PLATFORM_ID`.
*/
constructor(platformId) {
this.platformId = platformId;
this.isPlatformBrowser = isPlatformBrowser(this.platformId);
this.isPlatformServer = isPlatformServer(this.platformId);
this.isPlatformWorkerApp = isPlatformWorkerApp(this.platformId);
this.isPlatformWorkerUi = isPlatformWorkerUi(this.platformId);
}
/**
* Executes the specified function only on browser platfroms.
*
* @template T The type of value returned by the `execute()` function.
* @param {() => T} execute The function to execute only on browser platforms.
* @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
*/
onBrowser(execute) {
return this.onPlatform(this.isPlatformBrowser, execute);
}
/**
* Executes the specified function only on server platfroms.
*
* @template T The type of value returned by the `execute()` function.
* @param {() => T} execute The function to execute only on server platforms.
* @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
*/
onServer(execute) {
return this.onPlatform(this.isPlatformServer, execute);
}
/**
* Executes the specified function only on worker app platfroms.
*
* @template T The type of value returned by the `execute()` function.
* @param {() => T} execute The function to execute only on worker app platforms.
* @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
*/
onWorkerApp(execute) {
return this.onPlatform(this.isPlatformWorkerApp, execute);
}
/**
* Executes the specified function only on worker UI platfroms.
*
* @template T The type of value returned by the `execute()` function.
* @param {() => T} execute The function to execute only on worker UI platforms.
* @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
*/
onWorkerUi(execute) {
return this.onPlatform(this.isPlatformWorkerUi, execute);
}
/**
* Executes the specified function only on browser platfroms.
*
* @template T The type of value returned by the `execute()` function.
* @param {() => T} execute The function to execute only on browser platforms.
* @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
*/
onNonBrowser(execute) {
return this.onPlatform(!this.isPlatformBrowser, execute);
}
/**
* Executes the specified function only on server platfroms.
*
* @template T The type of value returned by the `execute()` function.
* @param {() => T} execute The function to execute only on server platforms.
* @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
*/
onNonServer(execute) {
return this.onPlatform(!this.isPlatformServer, execute);
}
/**
* Executes the specified function only on worker app platfroms.
*
* @template T The type of value returned by the `execute()` function.
* @param {() => T} execute The function to execute only on worker app platforms.
* @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
*/
onNonWorkerApp(execute) {
return this.onPlatform(!this.isPlatformWorkerApp, execute);
}
/**
* Executes the specified function only on worker UI platfroms.
*
* @template T The type of value returned by the `execute()` function.
* @param {() => T} execute The function to execute only on worker UI platforms.
* @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
*/
onNonWorkerUi(execute) {
return this.onPlatform(!this.isPlatformWorkerUi, execute);
}
onPlatform(isPlatform, execute) {
return isPlatform ? execute() : undefined;
}
}
UniversalService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: UniversalService, deps: [{ token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
UniversalService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: UniversalService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: UniversalService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () { return [{ type: undefined, decorators: [{
type: Inject,
args: [PLATFORM_ID]
}] }]; } });
/**
* Provides the base functionality for platform directives to render elements only on certain platforms.
*
* @export
* @abstract
* @class PlatformDirective
* @implements {OnInit}
*/
class PlatformDirective {
constructor(template, viewContainer, universal) {
this.template = template;
this.viewContainer = viewContainer;
this.universal = universal;
}
/**
* Checks whether the element should be rendered on the current platform and renders it.
*/
ngOnInit() {
this.shouldRender() ? this.viewContainer.createEmbeddedView(this.template) : this.viewContainer.clear();
}
}
PlatformDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: PlatformDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: UniversalService }], target: i0.ɵɵFactoryTarget.Directive });
PlatformDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: PlatformDirective, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: PlatformDirective, decorators: [{
type: Directive
}], ctorParameters: function () { return [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }, { type: UniversalService }]; } });
/**
* Renders the marked element only on browser platforms.
*
* @export
* @class BrowserOnlyDirective
* @extends {PlatformDirective}
*/
class BrowserOnlyDirective extends PlatformDirective {
/**
* Checks whether the element should be rendered on the current platform.
*
* @protected
* @returns {boolean}
*/
shouldRender() {
return this.universal.isPlatformBrowser;
}
}
BrowserOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: BrowserOnlyDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
BrowserOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: BrowserOnlyDirective, selector: "[browserOnly]", usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: BrowserOnlyDirective, decorators: [{
type: Directive,
args: [{
selector: '[browserOnly]'
}]
}] });
/**
* Renders the marked element only on server platforms.
*
* @export
* @class ServerOnlyDirective
* @extends {PlatformDirective}
*/
class ServerOnlyDirective extends PlatformDirective {
/**
* Checks whether the element should be rendered on the current platform.
*
* @protected
* @returns {boolean}
*/
shouldRender() {
return this.universal.isPlatformServer;
}
}
ServerOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: ServerOnlyDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
ServerOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: ServerOnlyDirective, selector: "[serverOnly]", usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: ServerOnlyDirective, decorators: [{
type: Directive,
args: [{
selector: '[serverOnly]'
}]
}] });
/**
* Renders the marked element only on worker-app platforms.
*
* @export
* @class WorkerAppOnlyDirective
* @extends {PlatformDirective}
*/
class WorkerAppOnlyDirective extends PlatformDirective {
/**
* Checks whether the element should be rendered on the current platform.
*
* @protected
* @returns {boolean}
*/
shouldRender() {
return this.universal.isPlatformWorkerApp;
}
}
WorkerAppOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: WorkerAppOnlyDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
WorkerAppOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: WorkerAppOnlyDirective, selector: "[workerAppOnly]", usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: WorkerAppOnlyDirective, decorators: [{
type: Directive,
args: [{
selector: '[workerAppOnly]'
}]
}] });
/**
* Renders the marked element only on worker-ui platforms.
*
* @export
* @class WorkerUiOnlyDirective
* @extends {PlatformDirective}
*/
class WorkerUiOnlyDirective extends PlatformDirective {
/**
* Checks whether the element should be rendered on the current platform.
*
* @protected
* @returns {boolean}
*/
shouldRender() {
return this.universal.isPlatformWorkerUi;
}
}
WorkerUiOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: WorkerUiOnlyDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
WorkerUiOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: WorkerUiOnlyDirective, selector: "[workerUiOnly]", usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: WorkerUiOnlyDirective, decorators: [{
type: Directive,
args: [{
selector: '[workerUiOnly]'
}]
}] });
/**
* Renders the marked element only on non-browser platforms.
*
* @export
* @class NonBrowserOnlyDirective
* @extends {PlatformDirective}
*/
class NonBrowserOnlyDirective extends PlatformDirective {
/**
* Checks whether the element should be rendered on the current platform.
*
* @protected
* @returns {boolean}
*/
shouldRender() {
return !this.universal.isPlatformBrowser;
}
}
NonBrowserOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: NonBrowserOnlyDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
NonBrowserOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: NonBrowserOnlyDirective, selector: "[nonBrowserOnly]", usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: NonBrowserOnlyDirective, decorators: [{
type: Directive,
args: [{
selector: '[nonBrowserOnly]'
}]
}] });
/**
* Renders the marked element only on non-server platforms.
*
* @export
* @class NonServerOnlyDirective
* @extends {PlatformDirective}
*/
class NonServerOnlyDirective extends PlatformDirective {
/**
* Checks whether the element should be rendered on the current platform.
*
* @protected
* @returns {boolean}
*/
shouldRender() {
return !this.universal.isPlatformServer;
}
}
NonServerOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: NonServerOnlyDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
NonServerOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: NonServerOnlyDirective, selector: "[nonServerOnly]", usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: NonServerOnlyDirective, decorators: [{
type: Directive,
args: [{
selector: '[nonServerOnly]'
}]
}] });
/**
* Renders the marked element only on non-worker-app platforms.
*
* @export
* @class NonWorkerAppOnlyDirective
* @extends {PlatformDirective}
*/
class NonWorkerAppOnlyDirective extends PlatformDirective {
/**
* Checks whether the element should be rendered on the current platform.
*
* @protected
* @returns {boolean}
*/
shouldRender() {
return !this.universal.isPlatformWorkerApp;
}
}
NonWorkerAppOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: NonWorkerAppOnlyDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
NonWorkerAppOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: NonWorkerAppOnlyDirective, selector: "[nonWorkerAppOnly]", usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: NonWorkerAppOnlyDirective, decorators: [{
type: Directive,
args: [{
selector: '[nonWorkerAppOnly]'
}]
}] });
/**
* Renders the marked element only on non-worker-ui platforms.
*
* @export
* @class NonWorkerUiOnlyDirective
* @extends {PlatformDirective}
*/
class NonWorkerUiOnlyDirective extends PlatformDirective {
/**
* Checks whether the element should be rendered on the current platform.
*
* @protected
* @returns {boolean}
*/
shouldRender() {
return !this.universal.isPlatformWorkerUi;
}
}
NonWorkerUiOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: NonWorkerUiOnlyDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
NonWorkerUiOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.2.12", type: NonWorkerUiOnlyDirective, selector: "[nonWorkerUiOnly]", usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: NonWorkerUiOnlyDirective, decorators: [{
type: Directive,
args: [{
selector: '[nonWorkerUiOnly]'
}]
}] });
const exported = [
BrowserOnlyDirective,
ServerOnlyDirective,
WorkerAppOnlyDirective,
WorkerUiOnlyDirective,
NonBrowserOnlyDirective,
NonServerOnlyDirective,
NonWorkerAppOnlyDirective,
NonWorkerUiOnlyDirective
];
/**
* Provides facilitating tools for work with Angular Universal.
*
* @export
* @class UniversalModule
*/
class UniversalModule {
}
UniversalModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: UniversalModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
UniversalModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.12", ngImport: i0, type: UniversalModule, declarations: [BrowserOnlyDirective,
ServerOnlyDirective,
WorkerAppOnlyDirective,
WorkerUiOnlyDirective,
NonBrowserOnlyDirective,
NonServerOnlyDirective,
NonWorkerAppOnlyDirective,
NonWorkerUiOnlyDirective], exports: [BrowserOnlyDirective,
ServerOnlyDirective,
WorkerAppOnlyDirective,
WorkerUiOnlyDirective,
NonBrowserOnlyDirective,
NonServerOnlyDirective,
NonWorkerAppOnlyDirective,
NonWorkerUiOnlyDirective] });
UniversalModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: UniversalModule });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: UniversalModule, decorators: [{
type: NgModule,
args: [{
declarations: exported,
exports: exported
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { BrowserOnlyDirective, NonBrowserOnlyDirective, NonServerOnlyDirective, NonWorkerAppOnlyDirective, NonWorkerUiOnlyDirective, ServerOnlyDirective, UniversalModule, UniversalService, WorkerAppOnlyDirective, WorkerUiOnlyDirective };
//# sourceMappingURL=bespunky-angular-zen-universal.mjs.map