ngx-scroll-lock
Version:
Angular module for Page Scroll locking
158 lines (152 loc) • 4.95 kB
JavaScript
import { isDevMode, Component, ViewEncapsulation, Input, NgModule } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxScrollLockComponent {
constructor() {
this.target = '';
this.lock = false;
this.listenerOptions = {
capture: false,
passive: false,
};
}
/**
* @return {?}
*/
ngOnInit() {
this.targetElement = this.target
? document.querySelector(this.target)
: document.body;
// Shows error message only in Development
if (!this.targetElement && isDevMode()) {
console.error(
// tslint:disable-next-line: max-line-length
`\`NgxScrollLockComponent\` need to receive "target" as an element, but it received "${this.target || 'body'}". Please check if the element is unique and it's available in your DOM.`);
}
if (this.lock && this.targetElement) {
this.disableLock(this.targetElement);
}
}
/**
* @return {?}
*/
ngOnDestroy() {
if (this.targetElement) {
this.enableLock(this.targetElement);
}
}
/**
* @param {?} changes
* @return {?}
*/
ngOnChanges(changes) {
if (changes.target &&
!changes.target.firstChange &&
changes.target.previousValue !== changes.target.currentValue) {
this.enableLock(this.targetElement);
this.targetElement = this.target
? document.querySelector(this.target)
: document.body;
if (this.lock && this.targetElement) {
this.disableLock(this.targetElement);
}
}
if (changes.lock &&
!changes.lock.firstChange &&
changes.lock.previousValue !== changes.lock.currentValue &&
this.targetElement) {
changes.lock.currentValue
? this.disableLock(this.targetElement)
: this.enableLock(this.targetElement);
}
}
/**
* @param {?} target
* @return {?}
*/
disableLock(target) {
this.targetElement.classList.add('ngx-scroll-lock');
// Mobile Safari ignores { overflow: hidden } declaration on the body.
if (this.isTouchDevice()) {
this.targetElement.addEventListener('touchmove', this.preventTouchMove, this.listenerOptions);
}
}
/**
* @param {?} target
* @return {?}
*/
enableLock(target) {
this.targetElement.classList.remove('ngx-scroll-lock');
if (this.isTouchDevice()) {
this.targetElement.removeEventListener('touchmove', this.preventTouchMove, this.listenerOptions);
}
}
/**
* @return {?}
*/
isTouchDevice() {
return !!window && ('ontouchstart' in window || navigator.maxTouchPoints);
}
/**
* @param {?} rawEvent
* @return {?}
*/
preventTouchMove(rawEvent) {
/** @type {?} */
const e = rawEvent || Object.assign({}, ((/** @type {?} */ (window))).event, { touches: [] });
// // Do not prevent if the event has more than one touch
// (usually meaning this is a single or multi touch gesture like pinch to zoom)
if (e.touches && e.touches.length >= 1) {
return true;
}
if (e.preventDefault) {
e.preventDefault();
}
return false;
}
}
NgxScrollLockComponent.decorators = [
{ type: Component, args: [{
selector: 'ngx-scroll-lock',
template: `
<style>
.ngx-scroll-lock {
box-sizing: border-box !important;
overflow: hidden !important;
position: inherit !important;
}
</style>
<ng-content></ng-content>
`,
encapsulation: ViewEncapsulation.None
}] }
];
NgxScrollLockComponent.propDecorators = {
target: [{ type: Input }],
lock: [{ type: Input }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxScrollLockModule {
}
NgxScrollLockModule.decorators = [
{ type: NgModule, args: [{
declarations: [NgxScrollLockComponent],
imports: [],
exports: [NgxScrollLockComponent]
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { NgxScrollLockComponent, NgxScrollLockModule };
//# sourceMappingURL=ngx-scroll-lock.js.map