fuse-angular/src/@fuse/services/config.service.ts

97 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Inject, Injectable, InjectionToken } from '@angular/core';
2018-05-20 07:12:31 +00:00
import { NavigationStart, Router } from '@angular/router';
import { Platform } from '@angular/cdk/platform';
2018-05-20 07:12:31 +00:00
import { BehaviorSubject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';
import * as _ from 'lodash';
2018-05-20 07:12:31 +00:00
// Create the injection token for the custom settings
export const FUSE_CONFIG = new InjectionToken('fuseCustomConfig');
@Injectable()
export class FuseConfigService
{
2018-05-20 07:12:31 +00:00
// Private
private _configSubject: BehaviorSubject<any>;
private readonly _defaultConfig: any;
/**
* Constructor
*
2018-05-20 07:12:31 +00:00
* @param {Platform} _platform
* @param {Router} _router
* @param _config
*/
constructor(
2018-05-20 07:12:31 +00:00
private _platform: Platform,
private _router: Router,
@Inject(FUSE_CONFIG) private _config
)
{
2018-05-20 07:12:31 +00:00
// Set the default config from the user provided config (from forRoot)
this._defaultConfig = _config;
2018-05-20 07:12:31 +00:00
// Initialize the service
this._init();
}
2018-05-20 07:12:31 +00:00
// -----------------------------------------------------------------------------------------------------
// @ Accessors
// -----------------------------------------------------------------------------------------------------
2018-05-20 07:12:31 +00:00
/**
* Set and get the config
*/
set config(value)
{
// Get the value from the behavior subject
let config = this._configSubject.getValue();
2018-05-20 07:12:31 +00:00
// Merge the new config
config = _.merge({}, config, value);
2018-05-20 07:12:31 +00:00
// Notify the observers
this._configSubject.next(config);
}
2018-05-20 07:12:31 +00:00
get config(): any | Observable<any>
{
return this._configSubject.asObservable();
}
2018-05-20 07:12:31 +00:00
// -----------------------------------------------------------------------------------------------------
// @ Private methods
// -----------------------------------------------------------------------------------------------------
/**
2018-05-20 07:12:31 +00:00
* Initialize
*
2018-05-20 07:12:31 +00:00
* @private
*/
2018-05-20 07:12:31 +00:00
private _init(): void
{
2018-05-20 07:12:31 +00:00
/**
* Disable custom scrollbars if browser is mobile
*/
if ( this._platform.ANDROID || this._platform.IOS )
{
this._defaultConfig.customScrollbars = false;
}
2018-05-20 07:12:31 +00:00
// Set the settings from the default settings
this._configSubject = new BehaviorSubject(_.cloneDeep(this._defaultConfig));
2018-05-20 07:12:31 +00:00
// Reload the default settings on every navigation start if
// the current settings are different from defaults
this._router.events
.pipe(filter(event => event instanceof NavigationStart))
.subscribe(() => {
if ( !_.isEqual(this._configSubject.getValue(), this._defaultConfig) )
{
this._configSubject.next(_.cloneDeep(this._defaultConfig));
}
});
}
}