UNPKG

@angular2-material/core

Version:
72 lines (70 loc) 3.84 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { Injectable } from '@angular/core'; /** * Simple utility for getting the bounds of the browser viewport. * TODO: internal */ export var ViewportRuler = (function () { function ViewportRuler() { } // TODO(jelbourn): cache the document's bounding rect and only update it when the window // is resized (debounced). /** Gets a ClientRect for the viewport's bounds. */ ViewportRuler.prototype.getViewportRect = function () { // Use the document element's bounding rect rather than the window scroll properties // (e.g. pageYOffset, scrollY) due to in issue in Chrome and IE where window scroll // properties and client coordinates (boundingClientRect, clientX/Y, etc.) are in different // conceptual viewports. Under most circumstances these viewports are equivalent, but they // can disagree when the page is pinch-zoomed (on devices that support touch). // See https://bugs.chromium.org/p/chromium/issues/detail?id=489206#c4 // We use the documentElement instead of the body because, by default (without a css reset) // browsers typically give the document body an 8px margin, which is not included in // getBoundingClientRect(). var documentRect = document.documentElement.getBoundingClientRect(); var scrollPosition = this.getViewportScrollPosition(documentRect); var height = window.innerHeight; var width = window.innerWidth; return { top: scrollPosition.top, left: scrollPosition.left, bottom: scrollPosition.top + height, right: scrollPosition.left + width, height: height, width: width, }; }; /** * Gets the (top, left) scroll position of the viewport. * @param documentRect */ ViewportRuler.prototype.getViewportScrollPosition = function (documentRect) { if (documentRect === void 0) { documentRect = document.documentElement.getBoundingClientRect(); } // The top-left-corner of the viewport is determined by the scroll position of the document // body, normally just (scrollLeft, scrollTop). However, Chrome and Firefox disagree about // whether `document.body` or `document.documentElement` is the scrolled element, so reading // `scrollTop` and `scrollLeft` is inconsistent. However, using the bounding rect of // `document.documentElement` works consistently, where the `top` and `left` values will // equal negative the scroll position. var top = documentRect.top < 0 && document.body.scrollTop == 0 ? -documentRect.top : document.body.scrollTop; var left = documentRect.left < 0 && document.body.scrollLeft == 0 ? -documentRect.left : document.body.scrollLeft; return { top: top, left: left }; }; ViewportRuler = __decorate([ Injectable(), __metadata('design:paramtypes', []) ], ViewportRuler); return ViewportRuler; }()); //# sourceMappingURL=viewport-ruler.js.map