@frshaw1/angular-visibility-api
Version:
An Angular and RxJS enabled event publisher for the browser's visibility API
87 lines • 4.42 kB
JavaScript
"use strict";
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);
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var BehaviorSubject_1 = require("rxjs/BehaviorSubject");
/**
* VisibilityService
*
* An Angular service that utilizes RxJS Observables to publish events for the browsers visibility API.
* This service abstracts the differences between browsers and makes using the visibility API seamless.
*
* The service exposes a single method `onVisibilityChange()` that will return an `Observable<Boolean>` that the
* caller can subscribe to. Since the internal representation of the `Observable` is a `BehaviorSubject`, upon each
* subscription the subscribe handler will be called immediately with the current value of the visibility observable.
*/
var VisibilityService = /** @class */ (function () {
function VisibilityService() {
this.document = document;
this.legacyVisibilityHandleFn = this.legacyVisibilityHandler.bind(this);
this.initializeVisibilityParameters();
}
/**
* Generates the Observable<Boolean> that callers can subscribe to for visibility events. The subscription value
* is a {Boolean} value that indicates whether the current window IS VISIBLE.
*
* @return {Observable<Boolean>}
*/
VisibilityService.prototype.onVisibilityChange = function () {
if (!this.visibility$) {
this.setupVisibilityHandler();
}
return this.visibility$.asObservable();
};
VisibilityService.prototype.initializeVisibilityParameters = function () {
if (typeof this.document.hidden !== 'undefined') {
this.hiddenProperty = 'hidden';
this.visibilityChangeEvent = 'visibilitychange';
}
else if (typeof this.document.mozHidden !== 'undefined') {
this.hiddenProperty = 'mozHidden';
this.visibilityChangeEvent = 'mozvisibilitychange';
}
else if (typeof this.document.msHidden !== 'undefined') {
this.hiddenProperty = 'msHidden';
this.visibilityChangeEvent = 'msvisibilitychange';
}
else if (typeof this.document.webkitHidden !== 'undefined') {
this.hiddenProperty = 'webkitHidden';
this.visibilityChangeEvent = 'webkitvisibilitychange';
}
};
VisibilityService.prototype.setupVisibilityHandler = function () {
var _this = this;
if (typeof this.document.addEventListener === 'undefined' ||
typeof this.document[this.hiddenProperty] === 'undefined') {
//This is a legacy browser and does not have support for the visibility api
this.visibility$ = new BehaviorSubject_1.BehaviorSubject(this.document.hasFocus());
//Setup blur and focus event handlers for updating browser visibility.
window.onblur = this.legacyVisibilityHandleFn;
window.onfocus = this.legacyVisibilityHandleFn;
}
else {
this.visibility$ = new BehaviorSubject_1.BehaviorSubject(!this.document[this.hiddenProperty]);
this.document.addEventListener(this.visibilityChangeEvent, function () {
_this.visibility$.next(!_this.document[_this.hiddenProperty]);
});
}
};
VisibilityService.prototype.legacyVisibilityHandler = function (event) {
this.visibility$.next(event.type === 'focus');
};
VisibilityService = __decorate([
core_1.Injectable(),
__metadata("design:paramtypes", [])
], VisibilityService);
return VisibilityService;
}());
exports.VisibilityService = VisibilityService;
//# sourceMappingURL=visibility.service.js.map