2019-08-09 19:13:18 +03:00
|
|
|
///
|
2020-02-20 10:26:43 +02:00
|
|
|
/// Copyright © 2016-2020 The Thingsboard Authors
|
2019-08-09 19:13:18 +03:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { isPlatformBrowser } from '@angular/common';
|
2020-02-10 13:10:14 +02:00
|
|
|
import { ClassProvider, FactoryProvider, InjectionToken, PLATFORM_ID, Injectable } from '@angular/core';
|
2019-08-09 19:13:18 +03:00
|
|
|
|
|
|
|
|
/* Create a new injection token for injecting the window into a component. */
|
|
|
|
|
export const WINDOW = new InjectionToken('WindowToken');
|
|
|
|
|
|
|
|
|
|
/* Define abstract class for obtaining reference to the global window object. */
|
|
|
|
|
export abstract class WindowRef {
|
|
|
|
|
|
|
|
|
|
get nativeWindow(): Window | object {
|
|
|
|
|
throw new Error('Not implemented.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Define class that implements the abstract class and returns the native window object. */
|
2020-02-10 13:10:14 +02:00
|
|
|
@Injectable()
|
2019-08-09 19:13:18 +03:00
|
|
|
export class BrowserWindowRef extends WindowRef {
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get nativeWindow(): Window | object {
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create an factory function that returns the native window object. */
|
|
|
|
|
export function windowFactory(browserWindowRef: BrowserWindowRef, platformId: object): Window | object {
|
|
|
|
|
if (isPlatformBrowser(platformId)) {
|
|
|
|
|
return browserWindowRef.nativeWindow;
|
|
|
|
|
}
|
|
|
|
|
return new Object();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create a injectable provider for the WindowRef token that uses the BrowserWindowRef class. */
|
|
|
|
|
export const browserWindowProvider: ClassProvider = {
|
|
|
|
|
provide: WindowRef,
|
|
|
|
|
useClass: BrowserWindowRef
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Create an injectable provider that uses the windowFactory function for returning the native window object. */
|
|
|
|
|
export const windowProvider: FactoryProvider = {
|
|
|
|
|
provide: WINDOW,
|
|
|
|
|
useFactory: windowFactory,
|
|
|
|
|
deps: [ WindowRef, PLATFORM_ID ]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Create an array of providers. */
|
|
|
|
|
export const WINDOW_PROVIDERS = [
|
|
|
|
|
browserWindowProvider,
|
|
|
|
|
windowProvider
|
|
|
|
|
];
|