Skip to content
Snippets Groups Projects
Commit 3b09148e authored by jojohoch's avatar jojohoch
Browse files

[player] Implement 'DISPLAYED' status detection for each element

parent f4b4fe69
No related branches found
No related tags found
No related merge requests found
...@@ -40,11 +40,11 @@ export class PageComponent implements OnInit { ...@@ -40,11 +40,11 @@ export class PageComponent implements OnInit {
}); });
} }
onIntersection(detectionType: 'top' | 'bottom'): void { onIntersection(detection: { detectionType: 'top' | 'bottom' | 'full', id: string }): void {
if (detectionType === 'bottom') { if (detection.detectionType === 'bottom') {
this.unitStateService.addPresentedPage(this.index); this.unitStateService.addPresentedPage(this.index);
} }
if (detectionType === 'top' || this.isLastPage) { if (detection.detectionType === 'top' || this.isLastPage) {
this.selectedIndexChange.emit(this.index); this.selectedIndexChange.emit(this.index);
} }
} }
......
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
<ng-template #staticElements> <ng-template #staticElements>
<ng-container *ngFor="let element of section.elements; let i = index"> <ng-container *ngFor="let element of section.elements; let i = index">
<app-element <app-element
appIntersectionDetection
detectionType="full"
[intersectionContainer]="document"
[id]="element.id"
(intersecting)="onIntersection($event)"
[style.display]="'block'" [style.display]="'block'"
[style.overflow]="'auto'" [style.overflow]="'auto'"
[style.width.px]="element.width" [style.width.px]="element.width"
...@@ -25,6 +30,11 @@ ...@@ -25,6 +30,11 @@
[style.grid-auto-rows]="section.autoRowSize ? 'auto' : undefined"> [style.grid-auto-rows]="section.autoRowSize ? 'auto' : undefined">
<ng-container *ngFor="let element of section.elements; let i = index"> <ng-container *ngFor="let element of section.elements; let i = index">
<app-element <app-element
appIntersectionDetection
detectionType="full"
[intersectionContainer]="document"
[id]="element.id"
(intersecting)="onIntersection($event)"
[style.min-width.px]="element.width" [style.min-width.px]="element.width"
[style.min-height.px]="element.height" [style.min-height.px]="element.height"
[style.margin-left.px]="element.marginLeft" [style.margin-left.px]="element.marginLeft"
......
import { import {
Component, Input, OnInit Component, Inject, Input, OnInit
} from '@angular/core'; } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms'; import { FormBuilder, FormGroup } from '@angular/forms';
import { DOCUMENT } from '@angular/common';
import { FormService } from '../../../../../common/form.service'; import { FormService } from '../../../../../common/form.service';
import { Section } from '../../../../../common/models/section'; import { Section } from '../../../../../common/models/section';
import { UnitStateService } from '../../services/unit-state.service';
@Component({ @Component({
selector: 'app-section', selector: 'app-section',
...@@ -17,7 +19,9 @@ export class SectionComponent implements OnInit { ...@@ -17,7 +19,9 @@ export class SectionComponent implements OnInit {
sectionForm!: FormGroup; sectionForm!: FormGroup;
constructor(private formService: FormService, constructor(private formService: FormService,
private formBuilder: FormBuilder) { private formBuilder: FormBuilder,
private unitStateService: UnitStateService,
@Inject(DOCUMENT) public document: Document) {
} }
ngOnInit(): void { ngOnInit(): void {
...@@ -31,4 +35,8 @@ export class SectionComponent implements OnInit { ...@@ -31,4 +35,8 @@ export class SectionComponent implements OnInit {
parentArrayIndex: this.parentArrayIndex parentArrayIndex: this.parentArrayIndex
}); });
} }
onIntersection(detection: { detectionType: 'top' | 'bottom' | 'full', id: string }): void {
this.unitStateService.changeElementStatus({ id: detection.id, status: 'DISPLAYED' });
}
} }
...@@ -6,15 +6,23 @@ import { ...@@ -6,15 +6,23 @@ import {
selector: '[appIntersectionDetection]' selector: '[appIntersectionDetection]'
}) })
export class IntersectionDetectionDirective implements OnInit, OnDestroy { export class IntersectionDetectionDirective implements OnInit, OnDestroy {
@Input() detectionType!: 'top' | 'bottom'; @Input() detectionType!: 'top' | 'bottom' | 'full';
@Output() intersecting = new EventEmitter<'top' | 'bottom'>(); @Input() id!: string;
@Input() intersectionContainer!: HTMLElement; @Output() intersecting = new EventEmitter<{ detectionType: 'top' | 'bottom' | 'full', id: string }>();
@Input() intersectionContainer!: HTMLElement | Document;
intersectionObserver!: IntersectionObserver; intersectionObserver!: IntersectionObserver;
private constraint!: string;
constructor(private elementRef: ElementRef) {} constructor(private elementRef: ElementRef) {}
ngOnInit(): void { ngOnInit(): void {
if (this.detectionType === 'top') {
this.constraint = '0px 0px -95% 0px';
} else {
this.constraint = this.detectionType === 'full' ? '0px 0px 0px 0px' : '-95% 0px 0px 0px';
}
this.initIntersectionObserver(); this.initIntersectionObserver();
} }
...@@ -22,11 +30,11 @@ export class IntersectionDetectionDirective implements OnInit, OnDestroy { ...@@ -22,11 +30,11 @@ export class IntersectionDetectionDirective implements OnInit, OnDestroy {
this.intersectionObserver = new IntersectionObserver( this.intersectionObserver = new IntersectionObserver(
(entries: IntersectionObserverEntry[]): void => entries.forEach(entry => { (entries: IntersectionObserverEntry[]): void => entries.forEach(entry => {
if (entry.isIntersecting) { if (entry.isIntersecting) {
this.intersecting.emit(this.detectionType); this.intersecting.emit({ detectionType: this.detectionType, id: this.id });
} }
}), { }), {
root: this.intersectionContainer, root: this.intersectionContainer,
rootMargin: this.detectionType === 'top' ? '0px 0px -95% 0px' : '-95% 0px 0px 0px' rootMargin: this.constraint
} }
); );
this.intersectionObserver.observe(this.elementRef.nativeElement); this.intersectionObserver.observe(this.elementRef.nativeElement);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment