Fixed deletion change (#177)

Closes #140 #194
This commit is contained in:
Nejc Zdovc
2016-11-30 09:04:28 +01:00
committed by József Farkas
parent 01214e58b5
commit f2ce7398d0
4 changed files with 61 additions and 7 deletions

View File

@@ -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
};
}
}

View File

@@ -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';

View File

@@ -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]);
}
});

View File

@@ -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;
}