From 3fb0da0e0bba3859e833a2d67ff26c53f6d33a82 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Mon, 17 Oct 2016 11:45:58 +0100 Subject: [PATCH] Code improvements Move shared properties down to FormWidgetModel level, reduce repetitive parsing code. --- .../widgets/core/container.model.spec.ts | 7 +++- .../widgets/core/container.model.ts | 11 ----- .../widgets/core/form-field-types.ts | 1 + .../widgets/core/form-outcome.model.ts | 13 ------ .../widgets/core/form-widget.model.ts | 28 ++++++++----- .../src/components/widgets/core/form.model.ts | 41 +++++-------------- .../src/components/widgets/core/tab.model.ts | 2 - 7 files changed, 35 insertions(+), 68 deletions(-) diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/container.model.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/container.model.spec.ts index 643929b392..7518e62e99 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/container.model.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/container.model.spec.ts @@ -107,7 +107,12 @@ describe('ContainerModel', () => { }); expect(container.isCollapsible()).toBeFalsy(); - container.type = FormFieldTypes.GROUP; + container = new ContainerModel(new FormModel(), { + type: FormFieldTypes.GROUP, + params: { + allowCollapse: true + } + }); expect(container.isCollapsible()).toBeTruthy(); }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/container.model.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/container.model.ts index bf6af192f4..e39acb78a0 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/container.model.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/container.model.ts @@ -23,14 +23,8 @@ import { FormModel } from './form.model'; import { FormFieldModel } from './form-field.model'; import { WidgetVisibilityModel } from '../../../models/widget-visibility.model'; -// TODO: inherit FormFieldModel export class ContainerModel extends FormWidgetModel { - fieldType: string; - id: string; - name: string; - type: string; - tab: string; numberOfColumns: number = 1; params: FormFieldMetadata = {}; isVisible: boolean = true; @@ -67,11 +61,6 @@ export class ContainerModel extends FormWidgetModel { super(form, json); if (json) { - this.fieldType = json.fieldType; - this.id = json.id; - this.name = json.name; - this.type = json.type; - this.tab = json.tab; this.numberOfColumns = json.numberOfColumns; this.params = json.params || {}; this.visibilityCondition = json.visibilityCondition; diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field-types.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field-types.ts index f673b8cea3..9d0e23f5be 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field-types.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field-types.ts @@ -18,6 +18,7 @@ export class FormFieldTypes { static CONTAINER: string = 'container'; static GROUP: string = 'group'; + static DYNAMIC_TABLE: string = 'dynamic-table'; static TEXT: string = 'text'; static MULTILINE_TEXT: string = 'multi-line-text'; static DROPDOWN: string = 'dropdown'; diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-outcome.model.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-outcome.model.ts index f0eb0abb05..219ddb7a41 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-outcome.model.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-outcome.model.ts @@ -23,25 +23,12 @@ export class FormOutcomeModel extends FormWidgetModel { static SAVE_ACTION: string = 'Save'; // Activiti 'Save' action name static COMPLETE_ACTION: string = 'Complete'; // Activiti 'Complete' action name - private _id: string; - private _name: string; - isSystem: boolean = false; - get id() { - return this._id; - } - - get name() { - return this._name; - } - constructor(form: FormModel, json?: any) { super(form, json); if (json) { - this._id = json.id; - this._name = json.name; this.isSystem = json.isSystem ? true : false; } } diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-widget.model.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-widget.model.ts index 39415c2547..f01ad71d5a 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-widget.model.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-widget.model.ts @@ -19,20 +19,26 @@ import { FormModel } from './form.model'; export class FormWidgetModel { - private _form: FormModel; - private _json: any; + readonly fieldType: string; + readonly id: string; + readonly name: string; + readonly type: string; + readonly tab: string; - get form(): FormModel { - return this._form; - } - - get json(): any { - return this._json; - } + readonly form: FormModel; + readonly json: any; constructor(form: FormModel, json: any) { - this._form = form; - this._json = json; + this.form = form; + this.json = json; + + if (json) { + this.fieldType = json.fieldType; + this.id = json.id; + this.name = json.name; + this.type = json.type; + this.tab = json.tab; + } } } diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form.model.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form.model.ts index b3079ec4d6..878da23656 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form.model.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form.model.ts @@ -28,28 +28,13 @@ export class FormModel { static SAVE_OUTCOME: string = '$save'; static COMPLETE_OUTCOME: string = '$complete'; - private _id: string; - private _name: string; - private _taskId: string; - private _taskName: string = FormModel.UNSET_TASK_NAME; + readonly id: string; + readonly name: string; + readonly taskId: string; + readonly taskName: string = FormModel.UNSET_TASK_NAME; + private _isValid: boolean = true; - get id(): string { - return this._id; - } - - get name(): string { - return this._name; - } - - get taskId(): string { - return this._taskId; - } - - get taskName(): string { - return this._taskName; - } - get isValid(): boolean { return this._isValid; } @@ -61,11 +46,7 @@ export class FormModel { values: FormValues = {}; - private _json: any; - - get json() { - return this._json; - } + readonly json: any; hasTabs(): boolean { return this.tabs && this.tabs.length > 0; @@ -82,12 +63,12 @@ export class FormModel { constructor(json?: any, data?: FormValues, readOnly: boolean = false) { this.readOnly = readOnly; if (json) { - this._json = json; + this.json = json; - this._id = json.id; - this._name = json.name; - this._taskId = json.taskId; - this._taskName = json.taskName || json.name || FormModel.UNSET_TASK_NAME; + this.id = json.id; + this.name = json.name; + this.taskId = json.taskId; + this.taskName = json.taskName || json.name || FormModel.UNSET_TASK_NAME; let tabCache: FormWidgetModelCache = {}; diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/tab.model.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/tab.model.ts index ccc0df7466..aec92bef50 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/tab.model.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/tab.model.ts @@ -22,7 +22,6 @@ import { WidgetVisibilityModel } from '../../../models/widget-visibility.model'; export class TabModel extends FormWidgetModel { - id: string; title: string; isVisible: boolean = true; visibilityCondition: WidgetVisibilityModel; @@ -37,7 +36,6 @@ export class TabModel extends FormWidgetModel { super(form, json); if (json) { - this.id = json.id; this.title = json.title; this.visibilityCondition = json.visibilityCondition; }