UNPKG

primeng

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primeng.svg)](https://badge.fury.io/js/primeng) [![npm downloads](https://img.shields.io/npm/dm/primeng.sv

134 lines (130 loc) 4.84 kB
import { Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, ChangeDetectorRef, Input, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { trigger, state, style, transition, animate } from '@angular/animations'; import { DomHandler } from 'primeng/dom'; class ScrollTop { constructor(el, cd) { this.el = el; this.cd = cd; this.target = "window"; this.threshold = 400; this.icon = "pi pi-chevron-up"; this.behavior = "smooth"; this.showTransitionOptions = '.15s'; this.hideTransitionOptions = '.15s'; this.visible = false; } ngOnInit() { if (this.target === 'window') this.bindDocumentScrollListener(); else if (this.target === 'parent') this.bindParentScrollListener(); } onClick() { let scrollElement = this.target === 'window' ? window : this.el.nativeElement.parentElement; scrollElement.scroll({ top: 0, behavior: this.behavior }); } onEnter() { this.el.nativeElement.children[0].style.zIndex = DomHandler.generateZIndex(); } checkVisibility(scrollY) { if (scrollY > this.threshold) this.visible = true; else this.visible = false; this.cd.markForCheck(); } bindParentScrollListener() { this.scrollListener = () => { this.checkVisibility(this.el.nativeElement.parentElement.scrollTop); }; this.el.nativeElement.parentElement.addEventListener('scroll', this.scrollListener); } bindDocumentScrollListener() { this.scrollListener = () => { this.checkVisibility(DomHandler.getWindowScrollTop()); }; window.addEventListener('scroll', this.scrollListener); } unbindParentScrollListener() { if (this.scrollListener) { this.el.nativeElement.parentElement.removeEventListener('scroll', this.scrollListener); this.scrollListener = null; } } unbindDocumentScrollListener() { if (this.scrollListener) { window.removeEventListener('scroll', this.scrollListener); this.scrollListener = null; } } containerClass() { return { 'p-scrolltop p-link p-component': true, 'p-scrolltop-sticky': this.target !== 'window' }; } ngOnDestroy() { if (this.target === 'window') this.unbindDocumentScrollListener(); else if (this.target === 'parent') this.unbindParentScrollListener(); } } ScrollTop.decorators = [ { type: Component, args: [{ selector: 'p-scrollTop', template: ` <button *ngIf="visible" [@animation]="{value: 'open', params: {showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}" (@animation.start)="onEnter()" [ngClass]="containerClass()" (click)="onClick()" [class]="styleClass" [ngStyle]="style" type="button"> <span [class]="icon" [ngClass]="'p-scrolltop-icon'"></span> </button> `, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, animations: [ trigger('animation', [ state('void', style({ opacity: 0 })), state('open', style({ opacity: 1 })), transition('void => open', animate('{{showTransitionParams}}')), transition('open => void', animate('{{hideTransitionParams}}')), ]) ], styles: [".p-scrolltop{align-items:center;bottom:20px;display:flex;justify-content:center;position:fixed;right:20px}.p-scrolltop-sticky{position:sticky}.p-scrolltop-sticky.p-link{margin-left:auto}"] },] } ]; ScrollTop.ctorParameters = () => [ { type: ElementRef }, { type: ChangeDetectorRef } ]; ScrollTop.propDecorators = { styleClass: [{ type: Input }], style: [{ type: Input }], target: [{ type: Input }], threshold: [{ type: Input }], icon: [{ type: Input }], behavior: [{ type: Input }], showTransitionOptions: [{ type: Input }], hideTransitionOptions: [{ type: Input }] }; class ScrollTopModule { } ScrollTopModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule], exports: [ScrollTop], declarations: [ScrollTop] },] } ]; /** * Generated bundle index. Do not edit. */ export { ScrollTop, ScrollTopModule }; //# sourceMappingURL=primeng-scrolltop.js.map