From 813714073ee895bfe2dd593c174012286dbbcbde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkas=20J=C3=B3zsef?= Date: Sat, 17 Sep 2016 16:39:45 +0200 Subject: [PATCH] Add some copy constructors --- src/config-serializer/config-items/Keymap.ts | 21 ++++++++++++--- src/config-serializer/config-items/Layer.ts | 12 ++++++++- src/config-serializer/config-items/Layers.ts | 14 +++++++--- src/config-serializer/config-items/Module.ts | 20 ++++++++++---- src/config-serializer/config-items/Modules.ts | 14 +++++++--- .../config-items/key-action/KeyActions.ts | 26 ++++++++++++------- 6 files changed, 82 insertions(+), 25 deletions(-) diff --git a/src/config-serializer/config-items/Keymap.ts b/src/config-serializer/config-items/Keymap.ts index c9578725..fa4884cc 100644 --- a/src/config-serializer/config-items/Keymap.ts +++ b/src/config-serializer/config-items/Keymap.ts @@ -1,7 +1,7 @@ -import {assertUInt8} from '../assert'; -import {Serializable} from '../Serializable'; -import {UhkBuffer} from '../UhkBuffer'; -import {Layers} from './Layers'; +import { assertUInt8 } from '../assert'; +import { Serializable } from '../Serializable'; +import { UhkBuffer } from '../UhkBuffer'; +import { Layers } from './Layers'; export class Keymap extends Serializable { @@ -18,6 +18,19 @@ export class Keymap extends Serializable { layers: Layers; + constructor(keymap?: Keymap) { + super(); + if (!keymap) { + return; + } + this.id = keymap.id; + this.name = keymap.name; + this.description = keymap.description; + this.abbreviation = keymap.abbreviation; + this.isDefault = keymap.isDefault; + this.layers = new Layers(keymap.layers); + } + _fromJsObject(jsObject: any): Keymap { this.id = jsObject.id; this.isDefault = jsObject.isDefault; diff --git a/src/config-serializer/config-items/Layer.ts b/src/config-serializer/config-items/Layer.ts index 2b7b27ae..e8d1f5c6 100644 --- a/src/config-serializer/config-items/Layer.ts +++ b/src/config-serializer/config-items/Layer.ts @@ -6,7 +6,17 @@ import { Modules } from './Modules'; export class Layer extends Serializable { modules: Modules; - animation: AnimationKeyboard = 'none'; + animation: AnimationKeyboard; + + constructor(layers?: Layer) { + super(); + if (!layers) { + this.animation = 'none'; + return; + } + this.modules = new Modules(layers.modules); + this.animation = layers.animation; + } _fromJsObject(jsObject: any): Layer { this.modules = new Modules().fromJsObject(jsObject.modules); diff --git a/src/config-serializer/config-items/Layers.ts b/src/config-serializer/config-items/Layers.ts index 35ee3ab4..24ec978c 100644 --- a/src/config-serializer/config-items/Layers.ts +++ b/src/config-serializer/config-items/Layers.ts @@ -1,9 +1,17 @@ -import {ClassArray} from '../ClassArray'; -import {UhkBuffer} from '../UhkBuffer'; -import {Layer} from './Layer'; +import { ClassArray } from '../ClassArray'; +import { UhkBuffer } from '../UhkBuffer'; +import { Layer } from './Layer'; export class Layers extends ClassArray { + constructor(layers?: Layers) { + super(); + if (!layers) { + return; + } + layers.elements.forEach(layer => this.elements.push(new Layer(layer))); + } + jsObjectToClass(jsObject: any): Layer { return new Layer().fromJsObject(jsObject); } diff --git a/src/config-serializer/config-items/Module.ts b/src/config-serializer/config-items/Module.ts index e00fec10..f76d9f64 100644 --- a/src/config-serializer/config-items/Module.ts +++ b/src/config-serializer/config-items/Module.ts @@ -1,7 +1,7 @@ -import {assertEnum, assertUInt8} from '../assert'; -import {Serializable} from '../Serializable'; -import {UhkBuffer} from '../UhkBuffer'; -import {KeyActions} from './key-action'; +import { assertEnum, assertUInt8 } from '../assert'; +import { Serializable } from '../Serializable'; +import { UhkBuffer } from '../UhkBuffer'; +import { KeyActions } from './key-action'; enum PointerRole { none, @@ -19,9 +19,19 @@ export class Module extends Serializable { @assertEnum(PointerRole) private pointerRole: PointerRole; + constructor(moduleI?: Module) { + super(); + if (!moduleI) { + return; + } + this.id = moduleI.id; + this.keyActions = new KeyActions(moduleI.keyActions); + this.pointerRole = moduleI.pointerRole; + } + _fromJsObject(jsObject: any): Module { this.id = jsObject.id; - this.pointerRole = PointerRole[ jsObject.pointerRole]; + this.pointerRole = PointerRole[jsObject.pointerRole]; this.keyActions = new KeyActions().fromJsObject(jsObject.keyActions); return this; } diff --git a/src/config-serializer/config-items/Modules.ts b/src/config-serializer/config-items/Modules.ts index 14031059..e19db206 100644 --- a/src/config-serializer/config-items/Modules.ts +++ b/src/config-serializer/config-items/Modules.ts @@ -1,9 +1,17 @@ -import {ClassArray} from '../ClassArray'; -import {UhkBuffer} from '../UhkBuffer'; -import {Module} from './Module'; +import { ClassArray } from '../ClassArray'; +import { UhkBuffer } from '../UhkBuffer'; +import { Module } from './Module'; export class Modules extends ClassArray { + constructor(modules?: Modules) { + super(); + if (!modules) { + return; + } + modules.elements.forEach(module => this.elements.push(new Module(module))); + } + jsObjectToClass(jsObject: any): Module { return new Module().fromJsObject(jsObject); } diff --git a/src/config-serializer/config-items/key-action/KeyActions.ts b/src/config-serializer/config-items/key-action/KeyActions.ts index d47f6df8..1bbcfa74 100644 --- a/src/config-serializer/config-items/key-action/KeyActions.ts +++ b/src/config-serializer/config-items/key-action/KeyActions.ts @@ -1,15 +1,23 @@ -import {ClassArray} from '../../ClassArray'; -import {UhkBuffer} from '../../UhkBuffer'; -import {KeyAction, KeyActionId, keyActionType} from './KeyAction'; -import {KeystrokeAction} from './KeystrokeAction'; -import {MouseAction} from './MouseAction'; -import {NoneAction} from './NoneAction'; -import {PlayMacroAction} from './PlayMacroAction'; -import {SwitchKeymapAction} from './SwitchKeymapAction'; -import {SwitchLayerAction} from './SwitchLayerAction'; +import { ClassArray } from '../../ClassArray'; +import { UhkBuffer } from '../../UhkBuffer'; +import { KeyAction, KeyActionId, keyActionType } from './KeyAction'; +import { KeystrokeAction } from './KeystrokeAction'; +import { MouseAction } from './MouseAction'; +import { NoneAction } from './NoneAction'; +import { PlayMacroAction } from './PlayMacroAction'; +import { SwitchKeymapAction } from './SwitchKeymapAction'; +import { SwitchLayerAction } from './SwitchLayerAction'; export class KeyActions extends ClassArray { + constructor(keyActions?: KeyActions) { + super(); + if (!keyActions) { + return; + } + keyActions.elements.forEach(keyaction => this.elements.push(this.jsObjectToClass(keyaction))); + } + jsObjectToClass(jsObject: any): KeyAction { switch (jsObject.keyActionType) { case keyActionType.NoneAction: