mirror of
https://github.com/akveo/ngx-admin.git
synced 2025-12-18 00:10:14 +01:00
kinda global scope
This commit is contained in:
parent
ef18e103d5
commit
aae6dbcc23
6 changed files with 48 additions and 38 deletions
|
|
@ -2,9 +2,8 @@ import {Component, ViewEncapsulation} from 'angular2/core';
|
||||||
import {RouteConfig, Router} from 'angular2/router';
|
import {RouteConfig, Router} from 'angular2/router';
|
||||||
import {Subscription} from 'rxjs/Subscription';
|
import {Subscription} from 'rxjs/Subscription';
|
||||||
|
|
||||||
import {SidebarStateService} from './theme/sidebar/sidebarState.service';
|
|
||||||
|
|
||||||
import {Pages} from './pages';
|
import {Pages} from './pages';
|
||||||
|
import {ThemeGlobal} from "./theme/theme.global";
|
||||||
|
|
||||||
// TODO: is it really the best place to globally require that dependency?
|
// TODO: is it really the best place to globally require that dependency?
|
||||||
require("!style!css!sass!./theme/sass/_ionicons.scss");
|
require("!style!css!sass!./theme/sass/_ionicons.scss");
|
||||||
|
|
@ -16,7 +15,7 @@ require("!style!css!sass!./theme/sass/_ionicons.scss");
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app',
|
selector: 'app',
|
||||||
pipes: [],
|
pipes: [],
|
||||||
providers: [SidebarStateService],
|
providers: [],
|
||||||
encapsulation: ViewEncapsulation.None,
|
encapsulation: ViewEncapsulation.None,
|
||||||
styles: [require('normalize.css'), require('./app.scss')],
|
styles: [require('normalize.css'), require('./app.scss')],
|
||||||
template: `
|
template: `
|
||||||
|
|
@ -37,14 +36,16 @@ export class App {
|
||||||
|
|
||||||
isMenuCollapsed:boolean = false;
|
isMenuCollapsed:boolean = false;
|
||||||
|
|
||||||
private _sidebarStateSubscription:Subscription;
|
private _themeGlobalSubscription:Subscription;
|
||||||
|
|
||||||
constructor(private _sidebarStateService:SidebarStateService) {
|
constructor(private _themeGlobal:ThemeGlobal) {
|
||||||
this._sidebarStateSubscription = this._sidebarStateService.getStateStream().subscribe((isCollapsed) => this.isMenuCollapsed = isCollapsed);
|
this._themeGlobalSubscription = this._themeGlobal.getDataStream().subscribe((data) => {
|
||||||
|
this.isMenuCollapsed = data['menu.isCollapsed'] != null ? data['menu.isCollapsed'] : this.isMenuCollapsed;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy() {
|
ngOnDestroy() {
|
||||||
// prevent memory leak when component destroyed
|
// prevent memory leak when component destroyed
|
||||||
this._sidebarStateSubscription.unsubscribe();
|
this._themeGlobalSubscription.unsubscribe();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
// App
|
// App
|
||||||
|
import {ThemeGlobal} from "./theme/theme.global";
|
||||||
export * from './app.component';
|
export * from './app.component';
|
||||||
|
|
||||||
// Application wide providers
|
// Application wide providers
|
||||||
export const APP_PROVIDERS = [
|
export const APP_PROVIDERS = [
|
||||||
|
ThemeGlobal
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
import {Component, ViewEncapsulation} from 'angular2/core';
|
import {Component, ViewEncapsulation} from 'angular2/core';
|
||||||
import {Subscription} from 'rxjs/Subscription';
|
|
||||||
|
|
||||||
import {MsgCenter} from '../msgCenter';
|
import {MsgCenter} from '../msgCenter';
|
||||||
import {ProfilePicturePipe} from '../pipes/image/profile-picture.pipe';
|
import {ProfilePicturePipe} from '../pipes/image/profile-picture.pipe';
|
||||||
import {ScrollPosition} from '../directives/scrollPosition.directive';
|
import {ScrollPosition} from '../directives/scrollPosition.directive';
|
||||||
import {SidebarStateService} from '../sidebar/sidebarState.service'
|
import {ThemeGlobal} from "../theme.global";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'page-top',
|
selector: 'page-top',
|
||||||
|
|
@ -17,15 +16,14 @@ export class PageTop {
|
||||||
isScrolled:Boolean = false;
|
isScrolled:Boolean = false;
|
||||||
isMenuCollapsed:boolean = false;
|
isMenuCollapsed:boolean = false;
|
||||||
|
|
||||||
private _sidebarStateSubscription:Subscription;
|
|
||||||
|
|
||||||
constructor(private _sidebarStateService:SidebarStateService) {
|
constructor(private _themeGlobal:ThemeGlobal) {
|
||||||
this._sidebarStateSubscription = this._sidebarStateService.getStateStream().subscribe((isCollapsed) => this.isMenuCollapsed = isCollapsed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleMenu() {
|
toggleMenu() {
|
||||||
this.isMenuCollapsed = !this.isMenuCollapsed;
|
this.isMenuCollapsed = !this.isMenuCollapsed;
|
||||||
this._sidebarStateService.stateChanged(this.isMenuCollapsed);
|
this._themeGlobal.setData('menu.isCollapsed', this.isMenuCollapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
scrolledChanged(isScrolled) {
|
scrolledChanged(isScrolled) {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import {Router} from 'angular2/router';
|
||||||
|
|
||||||
import {layoutSizes} from '../theme.constants';
|
import {layoutSizes} from '../theme.constants';
|
||||||
import {SidebarService} from './sidebar.service';
|
import {SidebarService} from './sidebar.service';
|
||||||
import {SidebarStateService} from './sidebarState.service';
|
import {ThemeGlobal} from "../theme.global";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'sidebar',
|
selector: 'sidebar',
|
||||||
|
|
@ -31,7 +31,7 @@ export class Sidebar {
|
||||||
constructor(private _elementRef:ElementRef,
|
constructor(private _elementRef:ElementRef,
|
||||||
private _router:Router,
|
private _router:Router,
|
||||||
private _sidebarService:SidebarService,
|
private _sidebarService:SidebarService,
|
||||||
private _sidebarStateService:SidebarStateService) {
|
private _themeGlobal:ThemeGlobal) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,7 +68,7 @@ export class Sidebar {
|
||||||
|
|
||||||
menuCollapseStateChange(isCollapsed) {
|
menuCollapseStateChange(isCollapsed) {
|
||||||
this.isMenuCollapsed = isCollapsed;
|
this.isMenuCollapsed = isCollapsed;
|
||||||
this._sidebarStateService.stateChanged(this.isMenuCollapsed);
|
this._themeGlobal.setData('menu.isCollapsed', this.isMenuCollapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
hoverItem($event) {
|
hoverItem($event) {
|
||||||
|
|
@ -100,18 +100,25 @@ export class Sidebar {
|
||||||
}
|
}
|
||||||
|
|
||||||
private selectMenuItem() {
|
private selectMenuItem() {
|
||||||
|
let currentMenu;
|
||||||
|
|
||||||
let isCurrent = (instruction) => (instruction ? this._router.isRouteActive(this._router.generate([instruction])) : false);
|
let isCurrent = (instruction) => (instruction ? this._router.isRouteActive(this._router.generate([instruction])) : false);
|
||||||
|
let assignCurrent = (menu) => (menu.selected ? currentMenu = menu : null);
|
||||||
|
|
||||||
this.menuItems.forEach(function (menu: any) {
|
this.menuItems.forEach(function (menu: any) {
|
||||||
|
|
||||||
menu.selected = isCurrent(menu.name);
|
menu.selected = isCurrent(menu.name);
|
||||||
menu.expanded = menu.expanded || menu.selected;
|
menu.expanded = menu.expanded || menu.selected;
|
||||||
|
assignCurrent(menu);
|
||||||
|
|
||||||
if (menu.subMenu) {
|
if (menu.subMenu) {
|
||||||
menu.subMenu.forEach(function (subMenu) {
|
menu.subMenu.forEach(function (subMenu) {
|
||||||
subMenu.selected = isCurrent(subMenu.name) && !subMenu.disabled;
|
subMenu.selected = isCurrent(subMenu.name) && !subMenu.disabled;
|
||||||
|
assignCurrent(menu);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// notifies all subscribers
|
||||||
|
this._themeGlobal.setData('menu.activeLink', currentMenu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
import {Injectable} from 'angular2/core'
|
|
||||||
import {Subject} from 'rxjs/Subject';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class SidebarStateService {
|
|
||||||
|
|
||||||
// Observable string sources
|
|
||||||
private _isCollapsed = new Subject<boolean>();
|
|
||||||
|
|
||||||
// Observable string streams
|
|
||||||
isCollapsedStream$ = this._isCollapsed.asObservable();
|
|
||||||
|
|
||||||
// Service message commands
|
|
||||||
stateChanged(isCollapsed:boolean) {
|
|
||||||
this._isCollapsed.next(isCollapsed)
|
|
||||||
}
|
|
||||||
|
|
||||||
getStateStream() {
|
|
||||||
return this.isCollapsedStream$;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
24
src/app/theme/theme.global.ts
Normal file
24
src/app/theme/theme.global.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import {Injectable} from 'angular2/core'
|
||||||
|
import {Subject} from 'rxjs/Subject';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ThemeGlobal {
|
||||||
|
private _data = new Subject<Object>();
|
||||||
|
|
||||||
|
dataStream$ = this._data.asObservable();
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
setData(key, value) {
|
||||||
|
let current = this._data[key];
|
||||||
|
if (current != value) {
|
||||||
|
this._data[key] = value;
|
||||||
|
this._data.next(this._data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getDataStream() {
|
||||||
|
return this.dataStream$;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue