Code improvements

Move shared properties down to FormWidgetModel level, reduce repetitive
parsing code.
This commit is contained in:
Denys Vuika
2016-10-27 10:37:14 +01:00
committed by Vito Albano
parent 7b23f939ea
commit 3fb0da0e0b
7 changed files with 35 additions and 68 deletions
@@ -107,7 +107,12 @@ describe('ContainerModel', () => {
}); });
expect(container.isCollapsible()).toBeFalsy(); expect(container.isCollapsible()).toBeFalsy();
container.type = FormFieldTypes.GROUP; container = new ContainerModel(new FormModel(), {
type: FormFieldTypes.GROUP,
params: {
allowCollapse: true
}
});
expect(container.isCollapsible()).toBeTruthy(); expect(container.isCollapsible()).toBeTruthy();
}); });
@@ -23,14 +23,8 @@ import { FormModel } from './form.model';
import { FormFieldModel } from './form-field.model'; import { FormFieldModel } from './form-field.model';
import { WidgetVisibilityModel } from '../../../models/widget-visibility.model'; import { WidgetVisibilityModel } from '../../../models/widget-visibility.model';
// TODO: inherit FormFieldModel
export class ContainerModel extends FormWidgetModel { export class ContainerModel extends FormWidgetModel {
fieldType: string;
id: string;
name: string;
type: string;
tab: string;
numberOfColumns: number = 1; numberOfColumns: number = 1;
params: FormFieldMetadata = {}; params: FormFieldMetadata = {};
isVisible: boolean = true; isVisible: boolean = true;
@@ -67,11 +61,6 @@ export class ContainerModel extends FormWidgetModel {
super(form, json); super(form, json);
if (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 = <number> json.numberOfColumns; this.numberOfColumns = <number> json.numberOfColumns;
this.params = <FormFieldMetadata> json.params || {}; this.params = <FormFieldMetadata> json.params || {};
this.visibilityCondition = <WidgetVisibilityModel> json.visibilityCondition; this.visibilityCondition = <WidgetVisibilityModel> json.visibilityCondition;
@@ -18,6 +18,7 @@
export class FormFieldTypes { export class FormFieldTypes {
static CONTAINER: string = 'container'; static CONTAINER: string = 'container';
static GROUP: string = 'group'; static GROUP: string = 'group';
static DYNAMIC_TABLE: string = 'dynamic-table';
static TEXT: string = 'text'; static TEXT: string = 'text';
static MULTILINE_TEXT: string = 'multi-line-text'; static MULTILINE_TEXT: string = 'multi-line-text';
static DROPDOWN: string = 'dropdown'; static DROPDOWN: string = 'dropdown';
@@ -23,25 +23,12 @@ export class FormOutcomeModel extends FormWidgetModel {
static SAVE_ACTION: string = 'Save'; // Activiti 'Save' action name static SAVE_ACTION: string = 'Save'; // Activiti 'Save' action name
static COMPLETE_ACTION: string = 'Complete'; // Activiti 'Complete' action name static COMPLETE_ACTION: string = 'Complete'; // Activiti 'Complete' action name
private _id: string;
private _name: string;
isSystem: boolean = false; isSystem: boolean = false;
get id() {
return this._id;
}
get name() {
return this._name;
}
constructor(form: FormModel, json?: any) { constructor(form: FormModel, json?: any) {
super(form, json); super(form, json);
if (json) { if (json) {
this._id = json.id;
this._name = json.name;
this.isSystem = json.isSystem ? true : false; this.isSystem = json.isSystem ? true : false;
} }
} }
@@ -19,20 +19,26 @@ import { FormModel } from './form.model';
export class FormWidgetModel { export class FormWidgetModel {
private _form: FormModel; readonly fieldType: string;
private _json: any; readonly id: string;
readonly name: string;
readonly type: string;
readonly tab: string;
get form(): FormModel { readonly form: FormModel;
return this._form; readonly json: any;
}
get json(): any {
return this._json;
}
constructor(form: FormModel, json: any) { constructor(form: FormModel, json: any) {
this._form = form; this.form = form;
this._json = json; 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;
}
} }
} }
@@ -28,28 +28,13 @@ export class FormModel {
static SAVE_OUTCOME: string = '$save'; static SAVE_OUTCOME: string = '$save';
static COMPLETE_OUTCOME: string = '$complete'; static COMPLETE_OUTCOME: string = '$complete';
private _id: string; readonly id: string;
private _name: string; readonly name: string;
private _taskId: string; readonly taskId: string;
private _taskName: string = FormModel.UNSET_TASK_NAME; readonly taskName: string = FormModel.UNSET_TASK_NAME;
private _isValid: boolean = true; 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 { get isValid(): boolean {
return this._isValid; return this._isValid;
} }
@@ -61,11 +46,7 @@ export class FormModel {
values: FormValues = {}; values: FormValues = {};
private _json: any; readonly json: any;
get json() {
return this._json;
}
hasTabs(): boolean { hasTabs(): boolean {
return this.tabs && this.tabs.length > 0; return this.tabs && this.tabs.length > 0;
@@ -82,12 +63,12 @@ export class FormModel {
constructor(json?: any, data?: FormValues, readOnly: boolean = false) { constructor(json?: any, data?: FormValues, readOnly: boolean = false) {
this.readOnly = readOnly; this.readOnly = readOnly;
if (json) { if (json) {
this._json = json; this.json = json;
this._id = json.id; this.id = json.id;
this._name = json.name; this.name = json.name;
this._taskId = json.taskId; this.taskId = json.taskId;
this._taskName = json.taskName || json.name || FormModel.UNSET_TASK_NAME; this.taskName = json.taskName || json.name || FormModel.UNSET_TASK_NAME;
let tabCache: FormWidgetModelCache<TabModel> = {}; let tabCache: FormWidgetModelCache<TabModel> = {};
@@ -22,7 +22,6 @@ import { WidgetVisibilityModel } from '../../../models/widget-visibility.model';
export class TabModel extends FormWidgetModel { export class TabModel extends FormWidgetModel {
id: string;
title: string; title: string;
isVisible: boolean = true; isVisible: boolean = true;
visibilityCondition: WidgetVisibilityModel; visibilityCondition: WidgetVisibilityModel;
@@ -37,7 +36,6 @@ export class TabModel extends FormWidgetModel {
super(form, json); super(form, json);
if (json) { if (json) {
this.id = json.id;
this.title = json.title; this.title = json.title;
this.visibilityCondition = <WidgetVisibilityModel> json.visibilityCondition; this.visibilityCondition = <WidgetVisibilityModel> json.visibilityCondition;
} }