@akveo/nga-theme
Version:
@akveo/nga-theme
124 lines • 5.68 kB
JavaScript
/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
import { Inject, Injectable } from '@angular/core';
import { ReplaySubject } from 'rxjs/ReplaySubject';
import { Subject } from 'rxjs/Subject';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/operator/publish';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/pairwise';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/debounceTime';
import { ngaThemeOptionsToken } from '../theme.options';
import { NgaThemeConfig } from './themeConfig.service';
import { NgaMediaBreakpointsService } from './breakpoints.service';
var NgaThemeService = (function () {
function NgaThemeService(options, breakpointService, themeConfig) {
this.options = options;
this.breakpointService = breakpointService;
this.themeConfig = themeConfig;
this.themeChanges$ = new ReplaySubject(1);
this.appendToLayoutTop$ = new ReplaySubject(1);
this.createLayoutTop$ = new Subject();
this.appendLayoutClass$ = new Subject();
this.removeLayoutClass$ = new Subject();
this.changeWindowWidth$ = new Subject();
if (options && options.name) {
this.changeTheme(options.name);
}
}
NgaThemeService.prototype.changeTheme = function (name) {
this.themeChanges$.next({ name: name, previous: this.currentTheme });
this.currentTheme = name;
};
NgaThemeService.prototype.changeWindowWidth = function (width) {
this.changeWindowWidth$.next(width);
};
NgaThemeService.prototype.appendToLayoutTop = function (component) {
var observable = new BehaviorSubject(null);
this.appendToLayoutTop$.next({ component: component, listener: observable });
return observable.asObservable();
};
NgaThemeService.prototype.getConfig = function () {
var _this = this;
return this.onThemeChange().map(function (theme) {
return _this.themeConfig.get(theme.name);
});
};
NgaThemeService.prototype.clearLayoutTop = function () {
var observable = new BehaviorSubject(null);
this.createLayoutTop$.next({ listener: observable });
return observable.asObservable();
};
/**
* Triggers media query breakpoint change
* Returns a pair where the first item is previous media breakpoint and the second item is current breakpoit.
* ```
* [{ name: 'xs', width: 0 }, { name: 'md', width: 768 }] // change from `xs` to `md`
* ```
* @returns {Observable<[NgaMediaBreakpoint, NgaMediaBreakpoint]>}
*/
NgaThemeService.prototype.onMediaQueryChange = function () {
var _this = this;
return this.changeWindowWidth$
.pairwise()
.map(function (_a) {
var prevWidth = _a[0], width = _a[1];
return [
_this.breakpointService.getBreakpoint(prevWidth),
_this.breakpointService.getBreakpoint(width),
];
})
.filter(function (_a) {
var prevPoint = _a[0], point = _a[1];
return prevPoint.name !== point.name;
})
.distinctUntilChanged(null, function (params) { return params[0].name + params[1].name; });
};
NgaThemeService.prototype.onThemeChange = function () {
return this.themeChanges$.publish().refCount();
};
NgaThemeService.prototype.onAppendToTop = function () {
return this.appendToLayoutTop$.publish().refCount();
};
NgaThemeService.prototype.onClearLayoutTop = function () {
return this.createLayoutTop$.publish().refCount();
};
NgaThemeService.prototype.appendLayoutClass = function (className) {
this.appendLayoutClass$.next(className);
};
NgaThemeService.prototype.onAppendLayoutClass = function () {
return this.appendLayoutClass$.publish().refCount();
};
NgaThemeService.prototype.removeLayoutClass = function (className) {
this.removeLayoutClass$.next(className);
};
NgaThemeService.prototype.onRemoveLayoutClass = function () {
return this.removeLayoutClass$.publish().refCount();
};
return NgaThemeService;
}());
NgaThemeService = __decorate([
Injectable(),
__param(0, Inject(ngaThemeOptionsToken)),
__metadata("design:paramtypes", [Object, NgaMediaBreakpointsService,
NgaThemeConfig])
], NgaThemeService);
export { NgaThemeService };
//# sourceMappingURL=theme.service.js.map