2018-04-04 11:30:52 +00:00
|
|
|
import { Inject, Injectable, InjectionToken } from '@angular/core';
|
2018-04-04 11:20:45 +00:00
|
|
|
import { NavigationEnd, NavigationStart, Router } from '@angular/router';
|
2018-02-17 14:21:38 +00:00
|
|
|
import { Platform } from '@angular/cdk/platform';
|
2018-05-09 14:55:26 +00:00
|
|
|
import { BehaviorSubject } from 'rxjs';
|
|
|
|
|
2018-04-04 11:30:52 +00:00
|
|
|
import * as _ from 'lodash';
|
2018-02-17 14:21:38 +00:00
|
|
|
|
|
|
|
// Create the injection token for the custom config
|
|
|
|
export const FUSE_CONFIG = new InjectionToken('fuseCustomConfig');
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class FuseConfigService
|
|
|
|
{
|
|
|
|
config: any;
|
|
|
|
defaultConfig: any;
|
2018-04-04 11:20:45 +00:00
|
|
|
isSetConfigRan = false;
|
2018-02-17 14:21:38 +00:00
|
|
|
|
|
|
|
onConfigChanged: BehaviorSubject<any>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param router
|
|
|
|
* @param platform
|
|
|
|
* @param config
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
private router: Router,
|
|
|
|
public platform: Platform,
|
2018-04-04 11:30:52 +00:00
|
|
|
@Inject(FUSE_CONFIG) config
|
2018-02-17 14:21:38 +00:00
|
|
|
)
|
|
|
|
{
|
2018-04-04 11:30:52 +00:00
|
|
|
// Set the default config from the user provided one (forRoot)
|
|
|
|
this.defaultConfig = config;
|
2018-02-17 14:21:38 +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-04-04 11:30:52 +00:00
|
|
|
this.config = _.cloneDeep(this.defaultConfig);
|
2018-02-17 14:21:38 +00:00
|
|
|
|
|
|
|
// Reload the default settings for the
|
|
|
|
// layout on every navigation start
|
|
|
|
router.events.subscribe(
|
|
|
|
(event) => {
|
2018-04-04 11:20:45 +00:00
|
|
|
|
2018-02-17 14:21:38 +00:00
|
|
|
if ( event instanceof NavigationStart )
|
|
|
|
{
|
2018-04-04 11:20:45 +00:00
|
|
|
this.isSetConfigRan = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( event instanceof NavigationEnd )
|
|
|
|
{
|
|
|
|
if ( this.isSetConfigRan )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-17 14:21:38 +00:00
|
|
|
this.setConfig({
|
|
|
|
layout: this.defaultConfig.layout
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create the behavior subject
|
|
|
|
this.onConfigChanged = new BehaviorSubject(this.config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the new config from given object
|
|
|
|
*
|
|
|
|
* @param config
|
|
|
|
*/
|
|
|
|
setConfig(config): void
|
|
|
|
{
|
2018-04-04 11:20:45 +00:00
|
|
|
// Set the SetConfigRan true
|
|
|
|
this.isSetConfigRan = true;
|
|
|
|
|
2018-04-04 11:30:52 +00:00
|
|
|
// Merge the config
|
|
|
|
this.config = _.merge({}, this.config, config);
|
2018-02-17 14:21:38 +00:00
|
|
|
|
|
|
|
// Trigger the event
|
|
|
|
this.onConfigChanged.next(this.config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|