mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
rewrote window resize event using redux
This commit is contained in:
@@ -1,5 +0,0 @@
|
|||||||
import { HeaderActions } from "./header/header.actions";
|
|
||||||
|
|
||||||
export default [
|
|
||||||
HeaderActions
|
|
||||||
];
|
|
@@ -3,10 +3,13 @@ import {
|
|||||||
ChangeDetectionStrategy,
|
ChangeDetectionStrategy,
|
||||||
ViewEncapsulation,
|
ViewEncapsulation,
|
||||||
OnDestroy,
|
OnDestroy,
|
||||||
OnInit
|
OnInit, HostListener
|
||||||
} from "@angular/core";
|
} from "@angular/core";
|
||||||
import { Router } from "@angular/router";
|
import { Router } from "@angular/router";
|
||||||
import { TranslateService } from "ng2-translate";
|
import { TranslateService } from "ng2-translate";
|
||||||
|
import { HostWindowState } from "./shared/host-window.reducer";
|
||||||
|
import { Store } from "@ngrx/store";
|
||||||
|
import { HostWindowActions } from "./shared/host-window.actions";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
changeDetection: ChangeDetectionStrategy.Default,
|
changeDetection: ChangeDetectionStrategy.Default,
|
||||||
@@ -27,7 +30,8 @@ export class AppComponent implements OnDestroy, OnInit {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private translate: TranslateService,
|
private translate: TranslateService,
|
||||||
private router: Router
|
private router: Router,
|
||||||
|
private store: Store<HostWindowState>
|
||||||
) {
|
) {
|
||||||
// this language will be used as a fallback when a translation isn't found in the current language
|
// this language will be used as a fallback when a translation isn't found in the current language
|
||||||
translate.setDefaultLang('en');
|
translate.setDefaultLang('en');
|
||||||
@@ -47,4 +51,11 @@ export class AppComponent implements OnDestroy, OnInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@HostListener('window:resize', ['$event'])
|
||||||
|
private onResize(event): void {
|
||||||
|
this.store.dispatch(
|
||||||
|
HostWindowActions.resize(event.target.innerWidth, event.target.innerHeight)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
6
src/app/app.effects.ts
Normal file
6
src/app/app.effects.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import { EffectsModule } from "@ngrx/effects";
|
||||||
|
import { HeaderEffects } from "./header/header.effects";
|
||||||
|
|
||||||
|
export default [
|
||||||
|
EffectsModule.run(HeaderEffects)
|
||||||
|
];
|
@@ -10,9 +10,8 @@ import { HeaderComponent } from './header/header.component';
|
|||||||
import { StoreModule } from "@ngrx/store";
|
import { StoreModule } from "@ngrx/store";
|
||||||
import { StoreDevtoolsModule } from "@ngrx/store-devtools";
|
import { StoreDevtoolsModule } from "@ngrx/store-devtools";
|
||||||
|
|
||||||
import reducers from './app.reducers'
|
import reducers from './app.reducers';
|
||||||
import actions from './app.actions'
|
import effects from './app.effects';
|
||||||
|
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
@@ -31,6 +30,7 @@ import actions from './app.actions'
|
|||||||
* based application.
|
* based application.
|
||||||
*/
|
*/
|
||||||
StoreModule.provideStore(reducers),
|
StoreModule.provideStore(reducers),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store devtools instrument the store retaining past versions of state
|
* Store devtools instrument the store retaining past versions of state
|
||||||
* and recalculating new states. This enables powerful time-travel
|
* and recalculating new states. This enables powerful time-travel
|
||||||
@@ -42,9 +42,10 @@ import actions from './app.actions'
|
|||||||
* See: https://github.com/zalmoxisus/redux-devtools-extension
|
* See: https://github.com/zalmoxisus/redux-devtools-extension
|
||||||
*/
|
*/
|
||||||
StoreDevtoolsModule.instrumentOnlyWithExtension(),
|
StoreDevtoolsModule.instrumentOnlyWithExtension(),
|
||||||
|
|
||||||
|
effects
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
actions
|
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class AppModule {
|
export class AppModule {
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
import { headerReducer } from './header/header.reducer';
|
import { headerReducer } from './header/header.reducer';
|
||||||
|
import { hostWindowReducer } from "./shared/host-window.reducer";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
headerReducer
|
headerReducer,
|
||||||
|
hostWindowReducer
|
||||||
}
|
}
|
||||||
|
@@ -1,24 +1,22 @@
|
|||||||
import { Injectable } from "@angular/core";
|
|
||||||
import { Action } from "@ngrx/store";
|
import { Action } from "@ngrx/store";
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class HeaderActions {
|
export class HeaderActions {
|
||||||
static COLLAPSE = 'dspace/header/COLLAPSE';
|
static COLLAPSE = 'dspace/header/COLLAPSE';
|
||||||
collapse(): Action {
|
static collapse(): Action {
|
||||||
return {
|
return {
|
||||||
type: HeaderActions.COLLAPSE
|
type: HeaderActions.COLLAPSE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static EXPAND = 'dspace/header/EXPAND';
|
static EXPAND = 'dspace/header/EXPAND';
|
||||||
expand(): Action {
|
static expand(): Action {
|
||||||
return {
|
return {
|
||||||
type: HeaderActions.EXPAND
|
type: HeaderActions.EXPAND
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static TOGGLE = 'dspace/header/TOGGLE';
|
static TOGGLE = 'dspace/header/TOGGLE';
|
||||||
toggle(): Action {
|
static toggle(): Action {
|
||||||
return {
|
return {
|
||||||
type: HeaderActions.TOGGLE
|
type: HeaderActions.TOGGLE
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,6 @@ export class HeaderComponent implements OnDestroy, OnInit {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private actions: HeaderActions,
|
|
||||||
private store: Store<HeaderState>
|
private store: Store<HeaderState>
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
@@ -37,21 +36,16 @@ export class HeaderComponent implements OnDestroy, OnInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@HostListener('window:resize', ['$event'])
|
|
||||||
private onResize(event): void {
|
|
||||||
this.collapse();
|
|
||||||
}
|
|
||||||
|
|
||||||
private collapse(): void {
|
private collapse(): void {
|
||||||
this.store.dispatch(this.actions.collapse());
|
this.store.dispatch(HeaderActions.collapse());
|
||||||
}
|
}
|
||||||
|
|
||||||
private expand(): void {
|
private expand(): void {
|
||||||
this.store.dispatch(this.actions.expand());
|
this.store.dispatch(HeaderActions.expand());
|
||||||
}
|
}
|
||||||
|
|
||||||
public toggle(): void {
|
public toggle(): void {
|
||||||
this.store.dispatch(this.actions.toggle());
|
this.store.dispatch(HeaderActions.toggle());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
16
src/app/header/header.effects.ts
Normal file
16
src/app/header/header.effects.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { Injectable } from "@angular/core";
|
||||||
|
import { Effect, Actions } from '@ngrx/effects'
|
||||||
|
import { HeaderActions } from "./header.actions";
|
||||||
|
import { HostWindowActions } from "../shared/host-window.actions";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class HeaderEffects {
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private actions$: Actions
|
||||||
|
) { }
|
||||||
|
|
||||||
|
@Effect() resize$ = this.actions$
|
||||||
|
.ofType(HostWindowActions.RESIZE)
|
||||||
|
.map(() => HeaderActions.collapse());
|
||||||
|
}
|
14
src/app/shared/host-window.actions.ts
Normal file
14
src/app/shared/host-window.actions.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { Action } from "@ngrx/store";
|
||||||
|
|
||||||
|
export class HostWindowActions {
|
||||||
|
static RESIZE = 'dspace/host-window/RESIZE';
|
||||||
|
static resize(newWidth: number, newHeight: number): Action {
|
||||||
|
return {
|
||||||
|
type: HostWindowActions.RESIZE,
|
||||||
|
payload: {
|
||||||
|
width: newWidth,
|
||||||
|
height: newHeight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
src/app/shared/host-window.reducer.ts
Normal file
25
src/app/shared/host-window.reducer.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { Action } from "@ngrx/store";
|
||||||
|
import { HostWindowActions } from "./host-window.actions";
|
||||||
|
|
||||||
|
export interface HostWindowState {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState: HostWindowState = {
|
||||||
|
width: null,
|
||||||
|
height: null
|
||||||
|
};
|
||||||
|
|
||||||
|
export const hostWindowReducer = (state = initialState, action: Action): HostWindowState => {
|
||||||
|
switch (action.type) {
|
||||||
|
|
||||||
|
case HostWindowActions.RESIZE: {
|
||||||
|
return Object.assign({}, state, action.payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
Reference in New Issue
Block a user