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

#380 Use document DI token #381

Merged
merged 4 commits into from
May 9, 2024
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { AfterViewInit, Component, OnInit, inject } from '@angular/core';
import { ActivatedRoute, Router, Scroll } from '@angular/router';
import { CpsTabChangeEvent } from 'cps-ui-kit';
import { DOCUMENT } from '@angular/common';

@Component({
template: ''
})
export abstract class ViewerComponent implements OnInit, AfterViewInit {
private _route = inject(ActivatedRoute);
private _router = inject(Router);
private _document = inject(DOCUMENT);
protected selectedTabIndex = 0;

ngOnInit(): void {
Expand All @@ -31,7 +33,7 @@
}

ngAfterViewInit(): void {
this._router.events.subscribe((event: any) => {

Check warning on line 36 in projects/composition/src/app/components/viewer/viewer.component.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
if (event instanceof Scroll && event.anchor) {
setTimeout(() => {
this._scroll('#' + event.anchor);
Expand All @@ -41,9 +43,9 @@
}

private _scroll(query: string) {
const targetElement = document.querySelector(query);
const targetElement = this._document.querySelector(query);
if (!targetElement) {
window.scrollTo(0, 0);
(this._document.defaultView as Window).scrollTo(0, 0);
} else {
targetElement.scrollIntoView();
targetElement.classList.add('anchor-highlight');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Component } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {
CpsButtonToggleComponent,
CpsButtonToggleOption
} from 'projects/cps-ui-kit/src/public-api';
import { CpsButtonToggleComponent, CpsButtonToggleOption } from 'cps-ui-kit';

import ComponentData from '../../api-data/cps-button-toggle.json';
import { ComponentDocsViewerComponent } from '../../components/component-docs-viewer/component-docs-viewer.component';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { CommonModule, DOCUMENT } from '@angular/common';
import { Component, Inject, OnInit } from '@angular/core';
import {
CpsInputComponent,
CpsNotificationPosition,
Expand Down Expand Up @@ -51,10 +51,13 @@ export class ColorsPageComponent implements OnInit {
colorNameColor: { [key: string]: string } = {};

// eslint-disable-next-line no-useless-constructor
constructor(private _notificationService: CpsNotificationService) {}
constructor(
private _notificationService: CpsNotificationService,
@Inject(DOCUMENT) private document: Document
) {}

ngOnInit(): void {
const customColors = getCpsColors();
const customColors = getCpsColors(this.document);
customColors.forEach((clr) => {
const name = clr[0].replace(/^--cps-color-/, '');
const value = clr[1];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { CommonModule } from '@angular/common';
import { CommonModule, DOCUMENT } from '@angular/common';
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Inject,
Input,
OnChanges,
OnDestroy,
Expand Down Expand Up @@ -412,6 +413,7 @@ export class CpsAutocompleteComponent

constructor(
@Self() @Optional() private _control: NgControl,
@Inject(DOCUMENT) private document: Document,
private cdRef: ChangeDetectorRef,
private _labelByValue: LabelByValuePipe
) {
Expand Down Expand Up @@ -658,7 +660,7 @@ export class CpsAutocompleteComponent
isActive() {
return (
this.isOpened ||
document.activeElement === this.autocompleteInput?.nativeElement
this.document.activeElement === this.autocompleteInput?.nativeElement
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CommonModule } from '@angular/common';
import { CommonModule, DOCUMENT } from '@angular/common';
import {
Component,
EventEmitter,
Inject,
Input,
OnInit,
Optional,
Expand Down Expand Up @@ -148,6 +149,7 @@ export class CpsButtonToggleComponent implements ControlValueAccessor, OnInit {

constructor(
@Self() @Optional() private _control: NgControl,
@Inject(DOCUMENT) private document: Document,
private renderer: Renderer2
) {
if (this._control) {
Expand Down Expand Up @@ -233,7 +235,7 @@ export class CpsButtonToggleComponent implements ControlValueAccessor, OnInit {
'"Source Sans Pro", sans-serif'
);

this.renderer.appendChild(document.body, hiddenSpan);
this.renderer.appendChild(this.document.body, hiddenSpan);

this.largestButtonWidth = 0;
this.options.forEach((opt) => {
Expand All @@ -252,7 +254,7 @@ export class CpsButtonToggleComponent implements ControlValueAccessor, OnInit {
this.renderer.removeChild(hiddenSpan, text);
});

this.renderer.removeChild(document.body, hiddenSpan);
this.renderer.removeChild(this.document.body, hiddenSpan);
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CommonModule } from '@angular/common';
import { CommonModule, DOCUMENT } from '@angular/common';
import {
Component,
EventEmitter,
HostBinding,
Inject,
Input,
OnChanges,
Output
Expand Down Expand Up @@ -115,11 +116,16 @@ export class CpsButtonComponent implements OnChanges {

classesList: string[] = [];

// eslint-disable-next-line no-useless-constructor
constructor(@Inject(DOCUMENT) private document: Document) {}

ngOnChanges(): void {
this.buttonColor = getCSSColor(this.color);
this.buttonColor = getCSSColor(this.color, this.document);
this.borderRadius = convertSize(this.borderRadius);
this.textColor =
this.type === 'solid' ? getCSSColor(this.contentColor) : this.buttonColor;
this.type === 'solid'
? getCSSColor(this.contentColor, this.document)
: this.buttonColor;
this.setClasses();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CommonModule } from '@angular/common';
import { CommonModule, DOCUMENT } from '@angular/common';
import {
Component,
ElementRef,
EventEmitter,
Inject,
Input,
OnInit,
Optional,
Expand Down Expand Up @@ -106,6 +107,7 @@ export class CpsCheckboxComponent implements OnInit, ControlValueAccessor {

constructor(
@Self() @Optional() private _control: NgControl,
@Inject(DOCUMENT) private document: Document,
private _elementRef: ElementRef<HTMLElement>
) {
if (this._control) {
Expand All @@ -114,7 +116,7 @@ export class CpsCheckboxComponent implements OnInit, ControlValueAccessor {
}

ngOnInit(): void {
this.iconColor = getCSSColor(this.iconColor);
this.iconColor = getCSSColor(this.iconColor, this.document);
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Component, ViewEncapsulation, computed, input } from '@angular/core';
import {
Component,
Inject,
ViewEncapsulation,
computed,
input
} from '@angular/core';
import { getCSSColor } from '../../utils/colors-utils';
import { convertSize } from '../../utils/internal/size-utils';
import { DOCUMENT } from '@angular/common';

/**
* CpsDividerType is used to define the type of the divider.
Expand Down Expand Up @@ -49,6 +56,9 @@ export class CpsDividerComponent {
*/
thickness = input<number | string>('1px');

// eslint-disable-next-line no-useless-constructor
constructor(@Inject(DOCUMENT) private document: Document) {}

private _borderTop = computed(() => {
return this._constructBorder(!this.vertical());
});
Expand All @@ -59,7 +69,7 @@ export class CpsDividerComponent {

private _constructBorder(isVertical: boolean) {
return isVertical
? `${convertSize(this.thickness())} ${this.type()} ${getCSSColor(this.color())}`
? `${convertSize(this.thickness())} ${this.type()} ${getCSSColor(this.color(), this.document)}`
: '';
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CommonModule } from '@angular/common';
import { CommonModule, DOCUMENT } from '@angular/common';
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Inject,
Input,
OnInit,
Output,
Expand Down Expand Up @@ -139,6 +140,7 @@ export class CpsExpansionPanelComponent implements OnInit, AfterViewInit {

constructor(
private _animationBuilder: AnimationBuilder,
@Inject(DOCUMENT) private document: Document,
private _renderer: Renderer2
) {
this._contentCollapseAnimation = this._animationBuilder.build([
Expand Down Expand Up @@ -167,8 +169,8 @@ export class CpsExpansionPanelComponent implements OnInit, AfterViewInit {
}

ngOnInit(): void {
this.borderColor = getCSSColor(this.borderColor);
this.backgroundColor = getCSSColor(this.backgroundColor);
this.borderColor = getCSSColor(this.borderColor, this.document);
this.backgroundColor = getCSSColor(this.backgroundColor, this.document);
this.borderRadius = convertSize(this.borderRadius);
this.width = convertSize(this.width);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonModule } from '@angular/common';
import { Component, Input, OnChanges } from '@angular/core';
import { CommonModule, DOCUMENT } from '@angular/common';
import { Component, Inject, Input, OnChanges } from '@angular/core';
import { convertSize } from '../../utils/internal/size-utils';
import { getCSSColor } from '../../utils/colors-utils';

Expand Down Expand Up @@ -178,8 +178,11 @@ export class CpsIconComponent implements OnChanges {

classesList: string[] = ['cps-icon'];

// eslint-disable-next-line no-useless-constructor
constructor(@Inject(DOCUMENT) private document: Document) {}

ngOnChanges(): void {
this.iconColor = getCSSColor(this.color);
this.iconColor = getCSSColor(this.color, this.document);
this.setClasses();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*ngIf="!valueToDisplay"
spellcheck="false"
[type]="currentType"
autocomplete="off"
[value]="value"
(input)="updateValueEvent($event)"
[placeholder]="placeholder"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonModule } from '@angular/common';
import { Component, Input, OnInit } from '@angular/core';
import { CommonModule, DOCUMENT } from '@angular/common';
import { Component, Inject, Input, OnInit } from '@angular/core';
import { getCSSColor } from '../../utils/colors-utils';

/**
Expand Down Expand Up @@ -40,8 +40,11 @@ export class CpsLoaderComponent implements OnInit {

backgroundColor = 'rgba(0, 0, 0, 0.1)';

// eslint-disable-next-line no-useless-constructor
constructor(@Inject(DOCUMENT) private document: Document) {}

ngOnInit(): void {
this.backgroundColor = `rgba(0, 0, 0, ${this.opacity})`;
this.labelColor = getCSSColor(this.labelColor);
this.labelColor = getCSSColor(this.labelColor, this.document);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}"
(@animation.start)="onAnimationStart($event)"
(@animation.done)="onAnimationEnd($event)">
<div (mousedown)="onContentClick()" (touchstart)="onContentClick()">
<div (mousedown)="onContentClick()">
<ng-container *ngIf="items.length < 1">
<ng-content></ng-content>
</ng-container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ export class CpsMenuComponent implements AfterViewInit, OnDestroy, OnChanges {

hideReason: CpsMenuHideReason | undefined;

private window: Window;

// eslint-disable-next-line no-useless-constructor
constructor(
@Inject(DOCUMENT) private document: Document,
Expand All @@ -239,6 +241,7 @@ export class CpsMenuComponent implements AfterViewInit, OnDestroy, OnChanges {
public config: PrimeNGConfig,
public overlayService: OverlayService
) {
this.window = this.document.defaultView as Window;
this.resizeObserver = new ResizeObserver((entries) => {
if (this.target) {
entries.forEach((entry) => {
Expand Down Expand Up @@ -366,14 +369,13 @@ export class CpsMenuComponent implements AfterViewInit, OnDestroy, OnChanges {
if (isPlatformBrowser(this.platformId)) {
if (!this.documentClickListener && this.dismissable) {
this.zone.runOutsideAngular(() => {
const documentEvent = DomHandler.isIOS() ? 'touchstart' : 'mousedown';
const documentTarget: any = this.el
? this.el.nativeElement.ownerDocument
: this.document;

this.documentClickListener = this.renderer.listen(
documentTarget,
documentEvent,
'mousedown',
(event) => {
if (
!this.persistent &&
Expand Down Expand Up @@ -457,26 +459,26 @@ export class CpsMenuComponent implements AfterViewInit, OnDestroy, OnChanges {
),
left: Math.min(
targetOffset.left + target.offsetWidth,
window.innerWidth - element.offsetWidth
this.window.innerWidth - element.offsetWidth
)
};
case 'tl':
return {
top: Math.min(
targetOffset.top,
window.innerHeight - element.offsetHeight
this.window.innerHeight - element.offsetHeight
),
left: Math.max(0, targetOffset.left - element.offsetWidth)
};
case 'tr':
return {
top: Math.min(
targetOffset.top,
window.innerHeight - element.offsetHeight
this.window.innerHeight - element.offsetHeight
),
left: Math.min(
targetOffset.left + target.offsetWidth,
window.innerWidth - element.offsetWidth
this.window.innerWidth - element.offsetWidth
)
};
case 'default':
Expand Down
Loading
Loading