From f2ce7398d048d85af909c7bdce67b7b30b89d49d Mon Sep 17 00:00:00 2001 From: Nejc Zdovc Date: Wed, 30 Nov 2016 09:04:28 +0100 Subject: [PATCH] Fixed deletion change (#177) Closes #140 #194 --- src/store/actions/keymap.ts | 9 +++++++ src/store/effects/keymap.ts | 1 + src/store/effects/macro.ts | 7 +++-- src/store/reducers/keymap.ts | 51 ++++++++++++++++++++++++++++++++---- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/store/actions/keymap.ts b/src/store/actions/keymap.ts index 330b3547..356a4bdd 100644 --- a/src/store/actions/keymap.ts +++ b/src/store/actions/keymap.ts @@ -2,6 +2,7 @@ import { Action } from '@ngrx/store'; import { KeyAction } from '../../config-serializer/config-items/key-action'; import { Keymap } from '../../config-serializer/config-items/Keymap'; +import { Macro } from '../../config-serializer/config-items/Macro'; export namespace KeymapActions { export const PREFIX = '[Keymap] '; @@ -12,6 +13,7 @@ export namespace KeymapActions { export const SAVE_KEY = KeymapActions.PREFIX + 'Save key action'; export const SET_DEFAULT = KeymapActions.PREFIX + 'Set default option'; export const REMOVE = KeymapActions.PREFIX + 'Remove keymap'; + export const CHECK_MACRO = KeymapActions.PREFIX + 'Check deleted macro'; export function addKeymap(item: Keymap): Action { return { @@ -73,4 +75,11 @@ export namespace KeymapActions { } }; } + + export function checkMacro(macro: Macro): Action { + return { + type: KeymapActions.CHECK_MACRO, + payload: macro + }; + } } diff --git a/src/store/effects/keymap.ts b/src/store/effects/keymap.ts index 40b4620f..9c5d70fa 100644 --- a/src/store/effects/keymap.ts +++ b/src/store/effects/keymap.ts @@ -5,6 +5,7 @@ import { Actions, Effect } from '@ngrx/effects'; import { Store } from '@ngrx/store'; import 'rxjs/add/operator/do'; +import 'rxjs/add/operator/map'; import 'rxjs/add/operator/withLatestFrom'; import { KeymapActions } from '../actions'; diff --git a/src/store/effects/macro.ts b/src/store/effects/macro.ts index af49de15..3be60a94 100644 --- a/src/store/effects/macro.ts +++ b/src/store/effects/macro.ts @@ -5,11 +5,12 @@ import { Actions, Effect } from '@ngrx/effects'; import { Store } from '@ngrx/store'; import 'rxjs/add/operator/do'; +import 'rxjs/add/operator/map'; import 'rxjs/add/operator/withLatestFrom'; import { Macro } from '../../config-serializer/config-items/Macro'; -import { MacroActions } from '../actions'; +import { KeymapActions, MacroActions } from '../actions'; import { AppState } from '../index'; @Injectable() @@ -17,14 +18,16 @@ export class MacroEffects { @Effect({dispatch: false}) remove$: any = this.actions$ .ofType(MacroActions.REMOVE) + .map(action => this.store.dispatch(KeymapActions.checkMacro(action.payload))) .withLatestFrom(this.store) .do((latest) => { const state: AppState = latest[1]; + const macro: Macro[] = state.macros.entities; if (state.macros.entities.length === 0) { this.router.navigate(['/macro/add']); } else { - this.router.navigate(['/macro', '0']); + this.router.navigate(['/macro', macro[0].id]); } }); diff --git a/src/store/reducers/keymap.ts b/src/store/reducers/keymap.ts index 4670e35d..eb11041e 100644 --- a/src/store/reducers/keymap.ts +++ b/src/store/reducers/keymap.ts @@ -4,7 +4,7 @@ import { Action } from '@ngrx/store'; import 'rxjs/add/operator/map'; import { Observable } from 'rxjs/Observable'; -import { Helper as KeyActionHelper } from '../../config-serializer/config-items/key-action'; +import { Helper as KeyActionHelper, KeyAction } from '../../config-serializer/config-items/key-action'; import { Keymap } from '../../config-serializer/config-items/Keymap'; import { Layer } from '../../config-serializer/config-items/Layer'; import { Module } from '../../config-serializer/config-items/Module'; @@ -17,6 +17,7 @@ const initialState: KeymapState = { export default function (state = initialState, action: Action): KeymapState { let newState: Keymap[]; + let changedKeymap: Keymap = new Keymap(); switch (action.type) { case KeymapActions.ADD: @@ -89,22 +90,29 @@ export default function (state = initialState, action: Action): KeymapState { } return true; - } - ); + }); // If deleted one is default set default keymap to the first on the list of keymaps if (isDefault && filtered.length > 0) { filtered[0].isDefault = true; } + // Check for the deleted keymap in other keymaps + newState = filtered.map((keymap: Keymap) => { + changedKeymap = new Keymap(); + Object.assign(changedKeymap, keymap); + changedKeymap.layers = checkExistence(changedKeymap.layers, 'keymapAbbreviation', action.payload); + + return changedKeymap; + }); + return { - entities: filtered + entities: newState }; case KeymapActions.SAVE_KEY: const keymap: Keymap = action.payload.keymap; - const changedKeymap: Keymap = new Keymap(); Object.assign(changedKeymap, keymap); const layerIndex: number = action.payload.layer; @@ -134,6 +142,19 @@ export default function (state = initialState, action: Action): KeymapState { entities: newState }; + case KeymapActions.CHECK_MACRO: + newState = state.entities.map((keymap: Keymap) => { + changedKeymap = new Keymap(); + Object.assign(changedKeymap, keymap); + changedKeymap.layers = checkExistence(changedKeymap.layers, '_macroId', action.payload); + + return changedKeymap; + }); + + return { + entities: newState + }; + default: { return state; } @@ -188,3 +209,23 @@ function generateName(keymaps: Keymap[], name: string) { return name; } + +function checkExistence(layers: Layer[], property: string, value: string | number) { + let newLayers = layers.map((layer) => { + let newLayer = new Layer(layer); + + newLayer.modules = layer.modules.map((module: Module) => { + module.keyActions.forEach((action: KeyAction, index: number) => { + if (action && action.hasOwnProperty(property) && action[property] === value) { + module.keyActions[index] = undefined; + } + }); + + return module; + }); + + return newLayer; + }); + + return newLayers; +}