Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit ce21141

Browse files
committed
Implemented a provider to make the native document injectable through angular
We want to avoid using browser globals directly in case we ever want to run the application in a different environment. With the provider, we can just switch in an environment specific provider and this will still work correctly.
1 parent a5eb19c commit ce21141

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

source/behaviors/offClick/offClick.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Directive, Host, Input, Output, EventEmitter, OnInit, OnDestroy, Simple
33
import { services } from 'typescript-angular-utilities';
44
import __guid = services.guid;
55

6+
import { DocumentWrapper } from '../../services/document/document.provider';
7+
68
export interface IOffClickEvent extends MouseEvent {
79
rlEventIdentifier: string;
810
}
@@ -34,9 +36,12 @@ export class OffClickDirective implements OnInit, OnDestroy {
3436
};
3537

3638
identifier: string;
39+
document: Document;
3740

38-
constructor(guidService: __guid.GuidService) {
41+
constructor(guidService: __guid.GuidService
42+
, document: DocumentWrapper) {
3943
this.identifier = guidService.random();
44+
this.document = <any>document;
4045
}
4146

4247
ngOnInit() {
@@ -48,11 +53,11 @@ export class OffClickDirective implements OnInit, OnDestroy {
4853
}
4954

5055
addListener(): void {
51-
document.addEventListener('click', this.listener);
56+
this.document.addEventListener('click', this.listener);
5257
}
5358

5459
removeListener(): void {
55-
document.removeEventListener('click', this.listener);
60+
this.document.removeEventListener('click', this.listener);
5661
}
5762

5863
ngOnDestroy() {

source/componentProviders.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { MergeSort } from './components/cardContainer/sorts/mergeSort/mergeSort.
1515

1616
import { AsyncHelper } from './services/async/async.service';
1717
import { AutosaveActionService } from './services/autosaveAction/autosaveAction.service';
18+
import { DOCUMENT_PROVIDER } from './services/document/document.provider';
1819
import { DocumentService } from './services/documentWrapper/documentWrapper.service';
1920
import { FormService } from './services/form/form.service';
2021
import { JQUERY_PROVIDER } from './services/jquery/jquery.provider';
@@ -66,8 +67,9 @@ const defaultThemeNg1: FactoryProvider = {
6667
DefaultTheme,
6768
defaultThemeNg1,
6869
DialogRootService,
69-
FormService,
70+
DOCUMENT_PROVIDER,
7071
DocumentService,
72+
FormService,
7173
BreakpointService,
7274
VisibleBreakpointService,
7375
JQUERY_PROVIDER,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ValueProvider } from '@angular/core';
2+
3+
export abstract class DocumentWrapper { }
4+
5+
export const DOCUMENT_PROVIDER: ValueProvider = {
6+
provide: DocumentWrapper,
7+
useValue: document,
8+
};

0 commit comments

Comments
 (0)