angular2-advanced-notifications
Version:
UI and native notifications library for Angular
193 lines • 7.08 kB
JavaScript
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;
};
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import defaultGlobaAlertConfig from './alert/defaultGlobalAlertConfig';
import * as _ from 'lodash';
import { AnAlertTypes } from './alert/alertTypes.enum';
import { AnUtils } from './utils';
var AnBusService = (function () {
function AnBusService() {
this.globalAlertConfig = defaultGlobaAlertConfig;
this.showAlertSource = new Subject();
this.hideAlertSource = new Subject();
this.hideAlertsSource = new Subject();
this.hideAllAlertsSource = new Subject();
this.showNotificationSource = new Subject();
this.notificationApiPermissions = {
GRANTED: 'granted',
DENIED: 'denied',
DEFAULT: 'default'
};
this.documentVisibilityStates = {
VISIBLE: 'visible',
HIDDEN: 'hidden'
};
this.$$onClose = new Subject();
this.showAlert$ = this.showAlertSource;
this.hideAlert$ = this.hideAlertSource;
this.hideAlerts$ = this.hideAlertsSource;
this.hideAllAlerts$ = this.hideAllAlertsSource;
this.onClose$ = this.$$onClose;
this.alertTypes = AnAlertTypes;
}
/**
* @param {?} config
* @return {?}
*/
AnBusService.prototype.setGlobalConfigForAlerts = function (config) {
if (!_.isObject(config)) {
// todo: add log
return;
}
this.globalAlertConfig = _.merge(defaultGlobaAlertConfig, config);
};
/**
* @return {?}
*/
AnBusService.prototype.getGlobalConfigForAlerts = function () {
return this.globalAlertConfig;
};
/**
* @param {?} config
* @return {?}
*/
AnBusService.prototype.showAlert = function (config) {
var _this = this;
var /** @type {?} */ newConfig = _.merge(config, { id: AnUtils.generateGuid() });
this.showAlertSource.next(newConfig);
return {
id: newConfig.id,
hide: function () { _this._hideAlert(newConfig.id); }
};
};
/**
* @param {?=} alertsIds
* @return {?}
*/
AnBusService.prototype.hideAlerts = function (alertsIds) {
this.hideAlertsSource.next(alertsIds);
};
/**
* @return {?}
*/
AnBusService.prototype.hideAllAlerts = function () {
this.hideAllAlertsSource.next(null);
};
/**
* @return {?}
*/
AnBusService.prototype.requestNotificationPermission = function () {
var _this = this;
if (!_.isFunction(((window)).Notification)) {
return new Promise(function (resolve) {
resolve(false);
});
}
if (((window)).Notification.permission === this.notificationApiPermissions.GRANTED) {
return new Promise(function (resolve) {
resolve(true);
});
}
if (((window)).Notification.permission === this.notificationApiPermissions.DEFAULT) {
return new Promise(function (resolve) {
((window)).Notification.requestPermission(function (permission) {
resolve(permission === _this.notificationApiPermissions.GRANTED);
});
});
}
if (((window)).Notification.permission === this.notificationApiPermissions.DENIED) {
return new Promise(function (resolve) {
resolve(false);
});
}
return new Promise(function (resolve) {
resolve(false);
});
};
/**
* @param {?} options
* @param {?=} showIfDocumentVisible
* @return {?}
*/
AnBusService.prototype.showNotification = function (options, showIfDocumentVisible) {
var _this = this;
return new Promise(function (resolve) {
_this.requestNotificationPermission().then(function (result) {
if (!result) {
return;
}
resolve(_this._createNativeNotification(options, showIfDocumentVisible));
});
});
};
/**
* @param {?} options
* @param {?=} showIfDocumentVisible
* @return {?}
*/
AnBusService.prototype._createNativeNotification = function (options, showIfDocumentVisible) {
if (!_.isObject(options)) {
return undefined;
}
if (showIfDocumentVisible && document.visibilityState !== this.documentVisibilityStates.VISIBLE) {
return undefined;
}
if (!showIfDocumentVisible && document.visibilityState === this.documentVisibilityStates.VISIBLE) {
return undefined;
}
var /** @type {?} */ notificationInstance = new ((window)).Notification(options.title, options);
notificationInstance.onclick = options.onclick;
notificationInstance.onerror = options.onerror;
return notificationInstance;
};
/**
* @param {?} id
* @return {?}
*/
AnBusService.prototype._hideAlert = function (id) {
this.hideAlertSource.next(id);
};
return AnBusService;
}());
AnBusService = __decorate([
Injectable()
], AnBusService);
export { AnBusService };
function AnBusService_tsickle_Closure_declarations() {
/** @type {?} */
AnBusService.prototype.globalAlertConfig;
/** @type {?} */
AnBusService.prototype.showAlertSource;
/** @type {?} */
AnBusService.prototype.hideAlertSource;
/** @type {?} */
AnBusService.prototype.hideAlertsSource;
/** @type {?} */
AnBusService.prototype.hideAllAlertsSource;
/** @type {?} */
AnBusService.prototype.showNotificationSource;
/** @type {?} */
AnBusService.prototype.notificationApiPermissions;
/** @type {?} */
AnBusService.prototype.documentVisibilityStates;
/** @type {?} */
AnBusService.prototype.$$onClose;
/** @type {?} */
AnBusService.prototype.showAlert$;
/** @type {?} */
AnBusService.prototype.hideAlert$;
/** @type {?} */
AnBusService.prototype.hideAlerts$;
/** @type {?} */
AnBusService.prototype.hideAllAlerts$;
/** @type {?} */
AnBusService.prototype.onClose$;
/** @type {?} */
AnBusService.prototype.alertTypes;
}
//# sourceMappingURL=anBus.service.js.map