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

152 lines
4.1 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();
}
/**
* Get default config
*
* @returns {any}
*/
get defaultConfig(): any
{
return this._defaultConfig;
}
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;
}
// Set the config from the default config
2018-05-20 07:12:31 +00:00
this._configSubject = new BehaviorSubject(_.cloneDeep(this._defaultConfig));
// Reload the default config on every navigation start if
// the current config is different from the default one
2018-05-20 07:12:31 +00:00
this._router.events
.pipe(filter(event => event instanceof NavigationStart))
.subscribe(() => {
if ( !_.isEqual(this._configSubject.getValue(), this._defaultConfig) )
{
this._configSubject.next(_.cloneDeep(this._defaultConfig));
}
});
}
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
2018-06-06 18:20:04 +00:00
/**
* Set config
*
* @param value
* @param {{emitEvent: boolean}} opts
*/
setConfig(value, opts = {emitEvent: true}): void
{
// Get the value from the behavior subject
let config = this._configSubject.getValue();
// Merge the new config
config = _.merge({}, config, value);
// If emitEvent option is true...
if ( opts.emitEvent === true )
{
// Notify the observers
this._configSubject.next(config);
}
}
/**
* Get config
*
* @returns {Observable<any>}
*/
getConfig(): Observable<any>
{
return this._configSubject.asObservable();
}
/**
* Reset to the default config
*/
resetToDefaults(): void
{
// Set the config from the default config
this._configSubject.next(_.cloneDeep(this._defaultConfig));
}
}