2018-04-04 11:30:52 +00:00
|
|
|
import { Inject, Injectable, InjectionToken } from '@angular/core';
|
2018-05-20 07:12:31 +00:00
|
|
|
import { NavigationStart, Router } from '@angular/router';
|
2018-02-17 14:21:38 +00:00
|
|
|
import { Platform } from '@angular/cdk/platform';
|
2018-05-20 07:12:31 +00:00
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
import { filter } from 'rxjs/operators';
|
2018-04-04 11:30:52 +00:00
|
|
|
import * as _ from 'lodash';
|
2018-02-17 14:21:38 +00:00
|
|
|
|
2018-05-20 07:12:31 +00:00
|
|
|
// Create the injection token for the custom settings
|
2018-02-17 14:21:38 +00:00
|
|
|
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;
|
2018-02-17 14:21:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2018-05-20 07:12:31 +00:00
|
|
|
* @param {Platform} _platform
|
|
|
|
* @param {Router} _router
|
|
|
|
* @param _config
|
2018-02-17 14:21:38 +00:00
|
|
|
*/
|
|
|
|
constructor(
|
2018-05-20 07:12:31 +00:00
|
|
|
private _platform: Platform,
|
|
|
|
private _router: Router,
|
|
|
|
@Inject(FUSE_CONFIG) private _config
|
2018-02-17 14:21:38 +00:00
|
|
|
)
|
|
|
|
{
|
2018-05-20 07:12:31 +00:00
|
|
|
// Set the default config from the user provided config (from forRoot)
|
|
|
|
this._defaultConfig = _config;
|
2018-02-17 14:21:38 +00:00
|
|
|
|
2018-05-20 07:12:31 +00:00
|
|
|
// Initialize the service
|
|
|
|
this._init();
|
|
|
|
}
|
2018-02-17 14:21:38 +00:00
|
|
|
|
2018-05-20 07:12:31 +00:00
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
// @ Accessors
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
2018-04-04 11:20:45 +00:00
|
|
|
|
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-04-04 11:20:45 +00:00
|
|
|
|
2018-05-20 07:12:31 +00:00
|
|
|
// Merge the new config
|
|
|
|
config = _.merge({}, config, value);
|
2018-04-04 11:20:45 +00:00
|
|
|
|
2018-05-20 07:12:31 +00:00
|
|
|
// Notify the observers
|
|
|
|
this._configSubject.next(config);
|
|
|
|
}
|
2018-02-17 14:21:38 +00:00
|
|
|
|
2018-05-20 07:12:31 +00:00
|
|
|
get config(): any | Observable<any>
|
|
|
|
{
|
|
|
|
return this._configSubject.asObservable();
|
2018-02-17 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 13:57:54 +00:00
|
|
|
/**
|
|
|
|
* Get default config
|
|
|
|
*
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
|
|
|
get defaultConfig(): any
|
|
|
|
{
|
|
|
|
return this._defaultConfig;
|
|
|
|
}
|
|
|
|
|
2018-05-20 07:12:31 +00:00
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
// @ Private methods
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
2018-02-17 14:21:38 +00:00
|
|
|
/**
|
2018-05-20 07:12:31 +00:00
|
|
|
* Initialize
|
2018-02-17 14:21:38 +00:00
|
|
|
*
|
2018-05-20 07:12:31 +00:00
|
|
|
* @private
|
2018-02-17 14:21:38 +00:00
|
|
|
*/
|
2018-05-20 07:12:31 +00:00
|
|
|
private _init(): void
|
2018-02-17 14:21:38 +00:00
|
|
|
{
|
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-04-04 11:20:45 +00:00
|
|
|
|
2018-05-27 13:57:54 +00:00
|
|
|
// Set the config from the default config
|
2018-05-20 07:12:31 +00:00
|
|
|
this._configSubject = new BehaviorSubject(_.cloneDeep(this._defaultConfig));
|
2018-02-17 14:21:38 +00:00
|
|
|
|
2018-05-27 13:57:54 +00:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
});
|
2018-02-17 14:21:38 +00:00
|
|
|
}
|
2018-05-27 13:57:54 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
// @ Public methods
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset to the default config
|
|
|
|
*/
|
|
|
|
resetToDefaults(): void
|
|
|
|
{
|
|
|
|
// Set the config from the default config
|
|
|
|
this._configSubject.next(_.cloneDeep(this._defaultConfig));
|
|
|
|
}
|
2018-02-17 14:21:38 +00:00
|
|
|
}
|
|
|
|
|