Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide browser globals with injection tokens #358

Merged
merged 2 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { AfterViewInit, Component, ElementRef, OnDestroy, OnInit } from '@angular/core';
import { AfterViewInit, Component, ElementRef, OnDestroy, OnInit, Inject } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import 'style-loader!./styles/styles.scss';
import { Subscription } from 'rxjs/Subscription';
import { DocsService } from './docs/docs.service';
import { Analytics } from './docs/utils/analytics.service';
import { NbWindow } from '@nebular/theme';
import { NB_WINDOW } from '@nebular/theme';

@Component({
selector: 'ngd-app-root',
Expand All @@ -24,7 +24,7 @@ export class NgdAppComponent implements AfterViewInit, OnDestroy, OnInit {
private fragmentSubscription: Subscription;


constructor(private window: NbWindow,
constructor(@Inject(NB_WINDOW) private window,
private docsService: DocsService,
private router: Router,
private route: ActivatedRoute,
Expand Down
6 changes: 3 additions & 3 deletions docs/app/docs/page/page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { Component, OnDestroy } from '@angular/core';
import { Component, OnDestroy, Inject } from '@angular/core';

import { NbMenuService, NbWindow } from '@nebular/theme';
import { NbMenuService, NB_WINDOW } from '@nebular/theme';
import { Subscription } from 'rxjs/Subscription';
import { Title } from '@angular/platform-browser';

Expand All @@ -30,7 +30,7 @@ export class NgdPageComponent implements OnDestroy {

private menuSubscription: Subscription;

constructor(private window: NbWindow,
constructor(@Inject(NB_WINDOW) private window,
private menuService: NbMenuService,
private titleService: Title) {

Expand Down
6 changes: 3 additions & 3 deletions docs/app/docs/utils/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Injectable } from '@angular/core';
import { Injectable, Inject } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { Location } from '@angular/common';

import { filter } from 'rxjs/operator/filter';
import { NbWindow } from '@nebular/theme';
import { NB_WINDOW } from '@nebular/theme';
declare const ga: any;

@Injectable()
export class Analytics {
private _enabled: boolean;

constructor(private window: NbWindow,
constructor(@Inject(NB_WINDOW) private window,
private _location: Location,
private _router: Router) {
this._enabled = this.window.location.href.indexOf('akveo.github.io') >= 0;
Expand Down
5 changes: 3 additions & 2 deletions docs/app/homepage/homepage.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
Renderer2,
ViewChild,
ViewChildren,
Inject,
} from '@angular/core';
import { Title } from '@angular/platform-browser';

import * as ImageComparison from 'image-comparison';
import 'style-loader!image-comparison/src/ImageComparison.css';
import { Subject } from 'rxjs/Rx';
import { NbSpinnerService, NbWindow } from '@nebular/theme';
import { NbSpinnerService, NB_WINDOW } from '@nebular/theme';

@Component({
selector: 'ngd-homepage',
Expand Down Expand Up @@ -95,7 +96,7 @@ export class NgdHomepageComponent implements AfterViewInit, OnInit {
currentSectionId: string = 'home';
highlightMenu$ = new Subject();

constructor(private window: NbWindow,
constructor(@Inject(NB_WINDOW) private window,
private renderer: Renderer2,
private spinnerService: NbSpinnerService,
private titleService: Title) {
Expand Down
6 changes: 3 additions & 3 deletions src/framework/theme/components/layout/layout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { takeWhile } from 'rxjs/operators/takeWhile';
import { convertToBoolProperty } from '../helpers';
import { NbThemeService } from '../../services/theme.service';
import { NbSpinnerService } from '../../services/spinner.service';
import { NbWindow, NbDocument } from '../../theme.options';
import { NB_WINDOW, NB_DOCUMENT } from '../../theme.options';

/**
* A container component which determines a content position inside of the layout.
Expand Down Expand Up @@ -293,8 +293,8 @@ export class NbLayoutComponent implements AfterViewInit, OnInit, OnDestroy {
protected elementRef: ElementRef,
protected renderer: Renderer2,
protected router: Router,
protected window: NbWindow,
protected document: NbDocument,
@Inject(NB_WINDOW) protected window,
@Inject(NB_DOCUMENT) protected document,
@Inject(PLATFORM_ID) protected platformId: Object,
) {

Expand Down
4 changes: 2 additions & 2 deletions src/framework/theme/components/menu/menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { filter } from 'rxjs/operators/filter';

import { NbMenuInternalService, NbMenuItem, NbMenuService, NbMenuBag } from './menu.service';
import { convertToBoolProperty, getElementHeight } from '../helpers';
import { NbWindow } from '../../theme.options';
import { NB_WINDOW } from '../../theme.options';

function sumSubmenuHeight(item: NbMenuItem) {
return item.expanded
Expand Down Expand Up @@ -217,7 +217,7 @@ export class NbMenuComponent implements OnInit, AfterViewInit, OnDestroy {
private alive: boolean = true;
private autoCollapseValue: boolean = false;

constructor(private window: NbWindow,
constructor(@Inject(NB_WINDOW) private window,
private menuInternalService: NbMenuInternalService,
private router: Router) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Injectable, Inject } from '@angular/core';

import { NbWindow } from '../../../theme.options';
import { NB_WINDOW } from '../../../theme.options';
import { NbPositioningHelper } from './positioning.helper';
import { NbPopoverAdjustment, NbPopoverPlacement, NbPopoverPosition } from './model';

Expand All @@ -26,8 +26,13 @@ const NB_ORDERED_PLACEMENTS = {
@Injectable()
export class NbAdjustmentHelper {

constructor(private positioningHelper: NbPositioningHelper, private window: NbWindow) {
}
private window: Window;

constructor(
private positioningHelper: NbPositioningHelper,
@Inject(NB_WINDOW) window) {
this.window = window as Window;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this changelog as operator was introduced to support .tsx. Maybe we have to use basic type cast instead: this.window = <Window> window.
@nnixaa, do you think?

}

/**
* Calculated {@link NbPopoverPosition} based on placed element, host element,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { async, inject, TestBed } from '@angular/core/testing';

import { NbAdjustmentHelper } from './adjustment.helper';
import { NbPopoverAdjustment, NbPopoverPlacement } from './model';
import { NbDocument, NbWindow } from '../../../theme.options';
import { NB_DOCUMENT, NB_WINDOW } from '../../../theme.options';
import { NbPositioningHelper } from './positioning.helper';

describe('adjustment-helper', () => {
Expand Down Expand Up @@ -61,8 +61,8 @@ describe('adjustment-helper', () => {
// Configure testbed to prepare services
TestBed.configureTestingModule({
providers: [
{ provide: NbWindow, useValue: window },
{ provide: NbDocument, useValue: document },
{ provide: NB_WINDOW, useValue: window },
{ provide: NB_DOCUMENT, useValue: document },
NbPositioningHelper,
NbAdjustmentHelper,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { Injectable } from '@angular/core';
import { Injectable, Inject } from '@angular/core';

import { NbPopoverPlacement } from './model';
import { NbWindow } from '../../../theme.options';
import { NB_WINDOW } from '../../../theme.options';

@Injectable()
export class NbPositioningHelper {

constructor(private window: NbWindow) {
constructor(@Inject(NB_WINDOW) private window) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { async, inject, TestBed } from '@angular/core/testing';

import { NbPositioningHelper } from './positioning.helper';
import { NbPopoverPlacement } from './model';
import { NbDocument, NbWindow } from '../../../theme.options';
import { NB_DOCUMENT, NB_WINDOW } from '../../../theme.options';

describe('positioning-helper', () => {
const placedRect: ClientRect = {
Expand All @@ -34,8 +34,8 @@ describe('positioning-helper', () => {
// Configure testbed to prepare services
TestBed.configureTestingModule({
providers: [
{ provide: NbWindow, useValue: window },
{ provide: NbDocument, useValue: document },
{ provide: NB_WINDOW, useValue: window },
{ provide: NB_DOCUMENT, useValue: document },
NbPositioningHelper,
],
});
Expand Down
14 changes: 7 additions & 7 deletions src/framework/theme/components/popover/helpers/trigger.helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { Injectable, Inject } from '@angular/core';
import { fromEvent as observableFromEvent } from 'rxjs/observable/fromEvent';
import { empty as observableEmpty } from 'rxjs/observable/empty';
import { filter } from 'rxjs/operators/filter';
Expand All @@ -10,7 +10,7 @@ import { repeat } from 'rxjs/operators/repeat';
import { takeUntil } from 'rxjs/operators/takeUntil';


import { NbDocument } from '../../../theme.options';
import { NB_DOCUMENT } from '../../../theme.options';
import { NbPopoverMode, NbPopoverTrigger } from './model';

/**
Expand All @@ -26,11 +26,11 @@ const NB_TRIGGERS = {
*
* @param host {HTMLElement} popover host element.
* @param getContainer {Function} popover container getter.
* @param document {NbDocument} document ref.
* @param document {Document} document ref.
*
* @return {NbPopoverTrigger} open and close events streams.
* */
[NbPopoverMode.CLICK](host: HTMLElement, getContainer: Function, document: NbDocument): NbPopoverTrigger {
[NbPopoverMode.CLICK](host: HTMLElement, getContainer: Function, document: Document): NbPopoverTrigger {
return {
open: observableEmpty(),
close: observableFromEvent<Event>(document, 'click')
Expand All @@ -50,11 +50,11 @@ const NB_TRIGGERS = {
*
* @param host {HTMLElement} popover host element.
* @param getContainer {Function} popover container getter.
* @param document {NbDocument} document ref.
* @param document {Document} document ref.
*
* @return {NbPopoverTrigger} open and close events streams.
* */
[NbPopoverMode.HOVER](host: HTMLElement, getContainer: Function, document: NbDocument): NbPopoverTrigger {
[NbPopoverMode.HOVER](host: HTMLElement, getContainer: Function, document: Document): NbPopoverTrigger {
return {
open: observableFromEvent<Event>(host, 'mouseenter')
.pipe(
Expand Down Expand Up @@ -104,7 +104,7 @@ const NB_TRIGGERS = {
@Injectable()
export class NbTriggerHelper {

constructor(private document: NbDocument) {
constructor(@Inject(NB_DOCUMENT) private document) {
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/framework/theme/services/spinner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { Injectable } from '@angular/core';
import { NbDocument } from '../theme.options';
import { Injectable, Inject } from '@angular/core';
import { NB_DOCUMENT } from '../theme.options';

/**
* Service to control the global page spinner.
Expand All @@ -15,7 +15,7 @@ export class NbSpinnerService {
private loaders: Promise<any>[] = [];
private selector: string = 'nb-global-spinner';

constructor(private document: NbDocument) {}
constructor(@Inject(NB_DOCUMENT) private document) {}

/**
* Appends new loader to the list of loader to be completed before
Expand Down
8 changes: 4 additions & 4 deletions src/framework/theme/theme.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
NbThemeOptions,
nbThemeOptionsToken,
nbJSThemesToken,
NbDocument,
NbWindow,
NB_DOCUMENT,
NB_WINDOW,
} from './theme.options';
import { NbThemeService } from './services/theme.service';
import { NbSpinnerService } from './services/spinner.service';
Expand Down Expand Up @@ -60,8 +60,8 @@ export class NbThemeModule {
{ provide: nbBuiltInJSThemesToken, useValue: BUILT_IN_THEMES },
{ provide: nbJSThemesToken, useValue: nbJSThemes || [] },
{ provide: nbMediaBreakpointsToken, useValue: nbMediaBreakpoints || DEFAULT_MEDIA_BREAKPOINTS },
{ provide: NbWindow, useFactory: nbWindowFactory },
{ provide: NbDocument, useExisting: DOCUMENT },
{ provide: NB_WINDOW, useFactory: nbWindowFactory },
{ provide: NB_DOCUMENT, useExisting: DOCUMENT },
NbJSThemesRegistry,
NbThemeService,
NbMediaBreakpointsService,
Expand Down
4 changes: 2 additions & 2 deletions src/framework/theme/theme.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export const nbJSThemesToken = new InjectionToken<NbJSThemeOptions[]>('NB_THEMES
/**
* We're providing browser apis with tokens to improve testing capabilities.
* */
export abstract class NbWindow extends Window {}
export abstract class NbDocument extends Document {}
export const NB_WINDOW = new InjectionToken<Window>('NB_WINDOW');
export const NB_DOCUMENT = new InjectionToken<Document>('NB_DOCUMENT');