From ce50e9a3d343ddb185d192d73dc606937fcad000 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Mon, 2 Sep 2019 11:20:54 +0100 Subject: [PATCH] [ACTIVITI-3720] form variables enhancements (#5028) * support form variables on the model level * take 'variables.name' convention into account * move API to a proper place * unit tests and code fixes * unit tests for form field model * process variable interface --- .../src/app/components/form/demo-form.ts | 1737 ----------------- .../form-field/form-field.component.ts | 2 +- .../widgets/core/form-field-metadata.ts | 6 + .../widgets/core/form-field.model.spec.ts | 70 + .../widgets/core/form-field.model.ts | 60 +- .../widgets/core/form-variable.model.ts | 23 + .../widgets/core/form.model.spec.ts | 132 +- .../components/widgets/core/form.model.ts | 71 +- .../form/components/widgets/core/index.ts | 2 + .../widgets/core/process-variable.model.ts | 36 + .../services/widget-visibility.service.ts | 34 +- lib/core/mock/form/demo-form.mock.ts | 77 +- 12 files changed, 422 insertions(+), 1828 deletions(-) delete mode 100644 demo-shell/src/app/components/form/demo-form.ts create mode 100644 lib/core/form/components/widgets/core/form-variable.model.ts create mode 100644 lib/core/form/components/widgets/core/process-variable.model.ts diff --git a/demo-shell/src/app/components/form/demo-form.ts b/demo-shell/src/app/components/form/demo-form.ts deleted file mode 100644 index 69bf225336..0000000000 --- a/demo-shell/src/app/components/form/demo-form.ts +++ /dev/null @@ -1,1737 +0,0 @@ -/*! - * @license - * Copyright 2019 Alfresco Software, Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*! - * @license - * Copyright 2019 Alfresco Software, Ltd. - * - * Licensed under the Apache License, Version 2.0 (the 'License'); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an 'AS IS' BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -export class DemoForm { - - static getEasyForm(): any { - return { - 'id': 1001, - 'name': 'ISSUE_FORM', - 'tabs': [], - 'fields': [ - { - 'fieldType': 'ContainerRepresentation', - 'id': '1498212398417', - 'name': 'Label', - 'type': 'container', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': false, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': null, - 'className': null, - 'dateDisplayFormat': null, - 'sizeX': 2, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'numberOfColumns': 2, - 'fields': { - '1': [ - { - 'fieldType': 'RestFieldRepresentation', - 'id': 'label', - 'name': 'Label', - 'type': 'dropdown', - 'value': 'Choose one...', - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': true, - 'options': [ - { - 'id': 'empty', - 'name': 'Choose one...' - }, - { - 'id': 'option_1', - 'name': 'test1' - }, - { - 'id': 'option_2', - 'name': 'test2' - }, - { - 'id': 'option_3', - 'name': 'test3' - } - ], - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': null, - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'endpoint': null, - 'requestHeaders': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'Date', - 'name': 'Date', - 'type': 'date', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label5', - 'name': 'Label5', - 'type': 'boolean', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label6', - 'name': 'Label6', - 'type': 'boolean', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label4', - 'name': 'Label4', - 'type': 'integer', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'RestFieldRepresentation', - 'id': 'label12', - 'name': 'Label12', - 'type': 'radio-buttons', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': [ - { - 'id': 'option_1', - 'name': 'Option 1' - }, - { - 'id': 'option_2', - 'name': 'Option 2' - } - ], - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'endpoint': null, - 'requestHeaders': null - } - ] - } - } - ], - 'outcomes': [], - 'javascriptEvents': [], - 'className': '', - 'style': '', - 'customFieldTemplates': {}, - 'metadata': {}, - 'variables': [], - 'customFieldsValueInfo': {}, - 'gridsterForm': false, - 'globalDateFormat': 'D-M-YYYY' - }; - } - - static getDefinition(): any { - return { - 'id': 3003, - 'name': 'demo-01', - 'taskId': '7501', - 'taskName': 'Demo Form 01', - 'tabs': [ - { - 'id': 'tab1', - 'title': 'Text', - 'visibilityCondition': null - }, - { - 'id': 'tab2', - 'title': 'Misc', - 'visibilityCondition': null - } - ], - 'fields': [ - { - 'fieldType': 'ContainerRepresentation', - 'id': '1488274019966', - 'name': 'Label', - 'type': 'container', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': null, - 'className': null, - 'dateDisplayFormat': null, - 'layout': null, - 'sizeX': 2, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'numberOfColumns': 2, - 'fields': { - '1': [], - '2': [] - } - }, - { - 'fieldType': 'ContainerRepresentation', - 'id': 'section4', - 'name': 'Section 4', - 'type': 'group', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 2 - }, - 'sizeX': 2, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'numberOfColumns': 2, - 'fields': { - '1': [ - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label8', - 'name': 'Label8', - 'type': 'people', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label13', - 'name': 'Label13', - 'type': 'functional-group', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label18', - 'name': 'Label18', - 'type': 'readonly', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label19', - 'name': 'Label19', - 'type': 'readonly-text', - 'value': 'Display text as part of the form', - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - } - ], - '2': [ - { - 'fieldType': 'HyperlinkRepresentation', - 'id': 'label15', - 'name': 'Label15', - 'type': 'hyperlink', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'hyperlinkUrl': 'www.google.com', - 'displayText': null - }, - { - 'fieldType': 'AttachFileFieldRepresentation', - 'id': 'label16', - 'name': 'Label16', - 'type': 'upload', - 'value': [], - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1, - 'fileSource': { - 'serviceId': 'all-file-sources', - 'name': 'All file sources' - } - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'metaDataColumnDefinitions': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label17', - 'name': 'Label17', - 'type': 'select-folder', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1, - 'folderSource': { - 'serviceId': 'alfresco-1', - 'name': 'Alfresco 5.2 Local', - 'metaDataAllowed': true - } - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - } - ] - } - }, - { - 'fieldType': 'DynamicTableRepresentation', - 'id': 'label14', - 'name': 'Label14', - 'type': 'dynamic-table', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab2', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 2 - }, - 'sizeX': 2, - 'sizeY': 2, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'columnDefinitions': [ - { - 'id': 'id', - 'name': 'id', - 'type': 'String', - 'value': null, - 'optionType': null, - 'options': null, - 'restResponsePath': null, - 'restUrl': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'amountCurrency': null, - 'amountEnableFractions': false, - 'required': true, - 'editable': true, - 'sortable': true, - 'visible': true, - 'endpoint': null, - 'requestHeaders': null - }, - { - 'id': 'name', - 'name': 'name', - 'type': 'String', - 'value': null, - 'optionType': null, - 'options': null, - 'restResponsePath': null, - 'restUrl': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'amountCurrency': null, - 'amountEnableFractions': false, - 'required': true, - 'editable': true, - 'sortable': true, - 'visible': true, - 'endpoint': null, - 'requestHeaders': null - } - ] - }, - { - 'fieldType': 'ContainerRepresentation', - 'id': 'section1', - 'name': 'Section 1', - 'type': 'group', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 2 - }, - 'sizeX': 2, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'numberOfColumns': 2, - 'fields': { - '1': [ - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label1', - 'name': 'Label1', - 'type': 'text', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label3', - 'name': 'Label3', - 'type': 'text', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - } - ], - '2': [ - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label2', - 'name': 'Label2', - 'type': 'multi-line-text', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 2, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - } - ] - } - }, - { - 'fieldType': 'ContainerRepresentation', - 'id': 'section2', - 'name': 'Section 2', - 'type': 'group', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 2 - }, - 'sizeX': 2, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'numberOfColumns': 2, - 'fields': { - '1': [ - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label4', - 'name': 'Label4', - 'type': 'integer', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label7', - 'name': 'Label7', - 'type': 'date', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - } - ], - '2': [ - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label5', - 'name': 'Label5', - 'type': 'boolean', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'FormFieldRepresentation', - 'id': 'label6', - 'name': 'Label6', - 'type': 'boolean', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null - }, - { - 'fieldType': 'AmountFieldRepresentation', - 'id': 'label11', - 'name': 'Label11', - 'type': 'amount', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': '10', - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'enableFractions': false, - 'currency': null - } - ] - } - }, - { - 'fieldType': 'ContainerRepresentation', - 'id': 'section3', - 'name': 'Section 3', - 'type': 'group', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 2 - }, - 'sizeX': 2, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'numberOfColumns': 2, - 'fields': { - '1': [ - { - 'fieldType': 'RestFieldRepresentation', - 'id': 'label9', - 'name': 'Label9', - 'type': 'dropdown', - 'value': 'Choose one...', - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': true, - 'options': [ - { - 'id': 'empty', - 'name': 'Choose one...' - } - ], - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'endpoint': null, - 'requestHeaders': null - }, - { - 'fieldType': 'RestFieldRepresentation', - 'id': 'label12', - 'name': 'Label12', - 'type': 'radio-buttons', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': [ - { - 'id': 'option_1', - 'name': 'Option 1' - }, - { - 'id': 'option_2', - 'name': 'Option 2' - } - ], - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'endpoint': null, - 'requestHeaders': null - } - ], - '2': [ - { - 'fieldType': 'RestFieldRepresentation', - 'id': 'label10', - 'name': 'Label10', - 'type': 'typeahead', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': 'tab1', - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'endpoint': null, - 'requestHeaders': null - } - ] - } - } - ], - 'outcomes': [], - 'javascriptEvents': [], - 'className': '', - 'style': '', - 'customFieldTemplates': {}, - 'metadata': {}, - 'variables': [], - 'gridsterForm': false, - 'globalDateFormat': 'D-M-YYYY' - }; - } - - static getSimpleFormDefinition(): any { - return { - 'id': 1001, - 'name': 'SIMPLE_FORM_EXAMPLE', - 'description': '', - 'version': 1, - 'lastUpdatedBy': 2, - 'lastUpdatedByFullName': 'Test01 01Test', - 'lastUpdated': '2018-02-26T17:44:04.543+0000', - 'stencilSetId': 0, - 'referenceId': null, - 'taskId': '9999', - 'formDefinition': { - 'tabs': [], - 'fields': [ - { - 'fieldType': 'ContainerRepresentation', - 'id': '1519666726245', - 'name': 'Label', - 'type': 'container', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': null, - 'className': null, - 'dateDisplayFormat': null, - 'layout': null, - 'sizeX': 2, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'numberOfColumns': 2, - 'fields': { - '1': [ - { - 'fieldType': 'RestFieldRepresentation', - 'id': 'typeaheadField', - 'name': 'TypeaheadField', - 'type': 'typeahead', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': 'https://jsonplaceholder.typicode.com/users', - 'restResponsePath': null, - 'restIdProperty': 'id', - 'restLabelProperty': 'name', - 'tab': null, - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'endpoint': null, - 'requestHeaders': null - } - ], - '2': [ - { - 'fieldType': 'RestFieldRepresentation', - 'id': 'radioButton', - 'name': 'RadioButtons', - 'type': 'radio-buttons', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': [ - { - 'id': 'option_1', - 'name': 'Option 1' - }, - { - 'id': 'option_2', - 'name': 'Option 2' - }, - { - 'id': 'option_3', - 'name': 'Option 3' - } - ], - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': null, - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 1 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 2, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'endpoint': null, - 'requestHeaders': null - } - ] - } - }, - { - 'fieldType': 'ContainerRepresentation', - 'id': '1519666735185', - 'name': 'Label', - 'type': 'container', - 'value': null, - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': null, - 'hasEmptyValue': null, - 'options': null, - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': null, - 'className': null, - 'dateDisplayFormat': null, - 'layout': null, - 'sizeX': 2, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'numberOfColumns': 2, - 'fields': { - '1': [ - { - 'fieldType': 'RestFieldRepresentation', - 'id': 'selectBox', - 'name': 'SelectBox', - 'type': 'dropdown', - 'value': 'Choose one...', - 'required': false, - 'readOnly': false, - 'overrideId': false, - 'colspan': 1, - 'placeholder': null, - 'minLength': 0, - 'maxLength': 0, - 'minValue': null, - 'maxValue': null, - 'regexPattern': null, - 'optionType': 'manual', - 'hasEmptyValue': true, - 'options': [ - { - 'id': 'empty', - 'name': 'Choose one...' - }, - { - 'id': 'option_1', - 'name': '1' - }, - { - 'id': 'option_2', - 'name': '2' - }, - { - 'id': 'option_3', - 'name': '3' - } - ], - 'restUrl': null, - 'restResponsePath': null, - 'restIdProperty': null, - 'restLabelProperty': null, - 'tab': null, - 'className': null, - 'params': { - 'existingColspan': 1, - 'maxColspan': 2 - }, - 'dateDisplayFormat': null, - 'layout': { - 'row': -1, - 'column': -1, - 'colspan': 1 - }, - 'sizeX': 1, - 'sizeY': 1, - 'row': -1, - 'col': -1, - 'visibilityCondition': null, - 'endpoint': null, - 'requestHeaders': null - } - ], - '2': [] - } - } - ], - 'outcomes': [], - 'javascriptEvents': [], - 'className': '', - 'style': '', - 'customFieldTemplates': {}, - 'metadata': {}, - 'variables': [], - 'customFieldsValueInfo': {}, - 'gridsterForm': false - } - }; - } - -} diff --git a/lib/core/form/components/form-field/form-field.component.ts b/lib/core/form/components/form-field/form-field.component.ts index 1488733856..4108359560 100644 --- a/lib/core/form/components/form-field/form-field.component.ts +++ b/lib/core/form/components/form-field/form-field.component.ts @@ -117,7 +117,7 @@ export class FormFieldComponent implements OnInit, OnDestroy { if (this.field && this.field.params) { const wrappedField = this.field.params.field; if (wrappedField && wrappedField.type) { - return wrappedField; + return wrappedField as FormFieldModel; } } return this.field; diff --git a/lib/core/form/components/widgets/core/form-field-metadata.ts b/lib/core/form/components/widgets/core/form-field-metadata.ts index 77a2c45d0e..295d1a1199 100644 --- a/lib/core/form/components/widgets/core/form-field-metadata.ts +++ b/lib/core/form/components/widgets/core/form-field-metadata.ts @@ -23,4 +23,10 @@ export interface FormFieldMetadata { [key: string]: any; fileSource?: FormFieldFileSource; link?: boolean; + field?: { + id: string; + name: string; + type: string; + }; + responseVariable?: boolean; } diff --git a/lib/core/form/components/widgets/core/form-field.model.spec.ts b/lib/core/form/components/widgets/core/form-field.model.spec.ts index fffc71e25e..300941e754 100644 --- a/lib/core/form/components/widgets/core/form-field.model.spec.ts +++ b/lib/core/form/components/widgets/core/form-field.model.spec.ts @@ -443,4 +443,74 @@ describe('FormFieldModel', () => { field.updateForm(); expect(form.values['dropdown_field'].name).toEqual('Option 1'); }); + + describe('variables', () => { + + let form: FormModel; + + beforeEach(() => { + form = new FormModel({ + variables: [ + { + 'id': 'bfca9766-7bc1-45cc-8ecf-cdad551e36e2', + 'name': 'name2', + 'type': 'string', + 'value': 'default hello' + } + ], + processVariables: [ + { + 'serviceName': 'denys-variable-mapping-rb', + 'serviceFullName': 'denys-variable-mapping-rb', + 'serviceVersion': '', + 'appName': 'denys-variable-mapping', + 'appVersion': '', + 'serviceType': null, + 'id': 3, + 'type': 'string', + 'name': 'variables.name1', + 'createTime': 1566989626284, + 'lastUpdatedTime': 1566989626284, + 'executionId': null, + 'value': 'hello', + 'markedAsDeleted': false, + 'processInstanceId': '1be4785f-c982-11e9-bdd8-96d6903e4e44', + 'taskId': '1beab9f6-c982-11e9-bdd8-96d6903e4e44', + 'taskVariable': true + } + ] + }); + }); + + it('it should get a process value for readonly field', () => { + const field = new FormFieldModel(form, { + type: FormFieldTypes.DISPLAY_VALUE, + params: { + field: { + id: 'name1', + name: 'name1', + type: 'string' + } + } + }); + + expect(field.value).toBe('hello'); + }); + + it('it should fallback to a form variable for readonly field', () => { + const field = new FormFieldModel(form, { + type: FormFieldTypes.DISPLAY_VALUE, + params: { + responseVariable: true, + field: { + id: 'name2', + name: 'name2', + type: 'string' + } + } + }); + + expect(field.value).toBe('default hello'); + }); + }); }); diff --git a/lib/core/form/components/widgets/core/form-field.model.ts b/lib/core/form/components/widgets/core/form-field.model.ts index 035abf82fe..7880f31aba 100644 --- a/lib/core/form/components/widgets/core/form-field.model.ts +++ b/lib/core/form/components/widgets/core/form-field.model.ts @@ -173,23 +173,31 @@ export class FormFieldModel extends FormWidgetModel { this.placeholder = json.placeholder; } - if (FormFieldTypes.isReadOnlyType(json.type)) { - if (json.params && json.params.field) { + if (FormFieldTypes.isReadOnlyType(this.type)) { + if (this.params && this.params.field) { + let valueFound = false; + if (form.processVariables) { - const processVariable = this.getProcessVariableValue(json.params.field, form); + const processVariable = this.getProcessVariableValue(this.params.field, form); + if (processVariable) { + valueFound = true; this.value = processVariable; } - } else if (json.params.responseVariable && form.json.variables) { - const formVariable = this.getVariablesValue(json.params.field.name, form); - if (formVariable) { - this.value = formVariable; + } + + if (!valueFound && this.params.responseVariable) { + const defaultValue = form.getFormVariableValue(this.params.field.name); + + if (defaultValue) { + valueFound = true; + this.value = defaultValue; } } } } - if (FormFieldTypes.isContainerType(json.type)) { + if (FormFieldTypes.isContainerType(this.type)) { this.containerFactory(json, form); } } @@ -219,42 +227,12 @@ export class FormFieldModel extends FormWidgetModel { return name += '_LABEL'; } - private getProcessVariableValue(field: any, form: FormModel) { + private getProcessVariableValue(field: any, form: FormModel): any { let fieldName = field.name; if (this.isTypeaheadFieldType(field.type)) { fieldName = this.getFieldNameWithLabel(field.id); } - return this.findProcessVariableValue(fieldName, form); - } - - private getVariablesValue(variableName: string, form: FormModel) { - const variable = form.json.variables.find((currentVariable) => { - return currentVariable.name === variableName; - }); - - if (variable) { - if (variable.type === 'boolean') { - return JSON.parse(variable.value); - } - - return variable.value; - } - - return null; - } - - private findProcessVariableValue(variableName: string, form: FormModel) { - if (form.processVariables) { - const variable = form.processVariables.find((currentVariable) => { - return currentVariable.name === variableName; - }); - - if (variable) { - return variable.type === 'boolean' ? JSON.parse(variable.value) : variable.value; - } - } - - return undefined; + return form.getProcessVariableValue(fieldName); } private containerFactory(json: any, form: FormModel): void { @@ -270,7 +248,7 @@ export class FormFieldModel extends FormWidgetModel { if (json.fields.hasOwnProperty(currentField)) { const col = new ContainerColumnModel(); - const fields: FormFieldModel[] = (json.fields[currentField] || []).map((f) => new FormFieldModel(form, f)); + const fields: FormFieldModel[] = (json.fields[currentField] || []).map((field) => new FormFieldModel(form, field)); col.fields = fields; col.rowspan = json.fields[currentField].length; diff --git a/lib/core/form/components/widgets/core/form-variable.model.ts b/lib/core/form/components/widgets/core/form-variable.model.ts new file mode 100644 index 0000000000..f2f0c8bad0 --- /dev/null +++ b/lib/core/form/components/widgets/core/form-variable.model.ts @@ -0,0 +1,23 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export interface FormVariableModel { + id: string; + name: string; + type: string; + value?: any; +} diff --git a/lib/core/form/components/widgets/core/form.model.spec.ts b/lib/core/form/components/widgets/core/form.model.spec.ts index aafd5bf6dd..fe2c78d6fc 100644 --- a/lib/core/form/components/widgets/core/form.model.spec.ts +++ b/lib/core/form/components/widgets/core/form.model.spec.ts @@ -26,7 +26,6 @@ import { FormModel } from './form.model'; import { TabModel } from './tab.model'; describe('FormModel', () => { - let formService: FormService; beforeEach(() => { @@ -428,4 +427,135 @@ describe('FormModel', () => { expect(form.fieldValidators.length).toBe(defaultLength + 1); expect(FORM_FIELD_VALIDATORS.length).toBe(defaultLength); }); + + describe('variables', () => { + let form: FormModel; + + beforeEach(() => { + const variables = [ + { + 'id': 'bfca9766-7bc1-45cc-8ecf-cdad551e36e2', + 'name': 'name1', + 'type': 'string', + 'value': 'hello' + }, + { + 'id': '3ed9f28a-dbae-463f-b991-47ef06658bb6', + 'name': 'name2', + 'type': 'date', + 'value': '29.09.2019' + }, + { + 'id': 'booleanVar', + 'name': 'bool', + 'type': 'boolean', + 'value': 'true' + } + ]; + + const processVariables = [ + { + 'serviceName': 'denys-variable-mapping-rb', + 'serviceFullName': 'denys-variable-mapping-rb', + 'serviceVersion': '', + 'appName': 'denys-variable-mapping', + 'appVersion': '', + 'serviceType': null, + 'id': 3, + 'type': 'string', + 'name': 'variables.name1', + 'createTime': 1566989626284, + 'lastUpdatedTime': 1566989626284, + 'executionId': null, + 'value': 'hello', + 'markedAsDeleted': false, + 'processInstanceId': '1be4785f-c982-11e9-bdd8-96d6903e4e44', + 'taskId': '1beab9f6-c982-11e9-bdd8-96d6903e4e44', + 'taskVariable': true + }, + { + 'serviceName': 'denys-variable-mapping-rb', + 'serviceFullName': 'denys-variable-mapping-rb', + 'serviceVersion': '', + 'appName': 'denys-variable-mapping', + 'appVersion': '', + 'serviceType': null, + 'id': 1, + 'type': 'boolean', + 'name': 'booleanVar', + 'createTime': 1566989626283, + 'lastUpdatedTime': 1566989626283, + 'executionId': null, + 'value': 'true', + 'markedAsDeleted': false, + 'processInstanceId': '1be4785f-c982-11e9-bdd8-96d6903e4e44', + 'taskId': '1beab9f6-c982-11e9-bdd8-96d6903e4e44', + 'taskVariable': true + } + ]; + + form = new FormModel({ + variables, + processVariables + }); + }); + + it('should parse form variables', () => { + expect(form.variables.length).toBe(3); + expect(form.variables[0].id).toBe('bfca9766-7bc1-45cc-8ecf-cdad551e36e2'); + expect(form.variables[1].id).toBe('3ed9f28a-dbae-463f-b991-47ef06658bb6'); + expect(form.variables[2].id).toBe('booleanVar'); + }); + + it('should find a variable by or name', () => { + const result1 = form.getFormVariable('bfca9766-7bc1-45cc-8ecf-cdad551e36e2'); + const result2 = form.getFormVariable('name1'); + + expect(result1).toEqual(result2); + }); + + it('should not find a variable', () => { + expect(form.getFormVariable(null)).toBeUndefined(); + expect(form.getFormVariable('')).toBeUndefined(); + expect(form.getFormVariable('missing')).toBeUndefined(); + }); + + it('should find a form variable value', () => { + const result1 = form.getFormVariableValue('name1'); + const result2 = form.getFormVariableValue('bfca9766-7bc1-45cc-8ecf-cdad551e36e2'); + + expect(result1).toEqual(result2); + expect(result1).toEqual('hello'); + }); + + it('should convert the date variable value', () => { + const value = form.getFormVariableValue('name2'); + expect(value).toBe('29.09.2019T00:00:00.000Z'); + }); + + it('should convert the boolean variable value', () => { + const value = form.getFormVariableValue('bool'); + expect(value).toEqual(true); + }); + + it('should not find variable value', () => { + const value = form.getFormVariableValue('missing'); + expect(value).toBeUndefined(); + }); + + it('should find a process variable by form variable name', () => { + const value = form.getProcessVariableValue('variables.name1'); + expect(value).toBe('hello'); + }); + + it('should find a process variable by name', () => { + const value = form.getProcessVariableValue('booleanVar'); + expect(value).toEqual(true); + }); + + it('should not find a process variable', () => { + const missing = form.getProcessVariableValue('missing'); + expect(missing).toBeUndefined(); + }); + }); }); diff --git a/lib/core/form/components/widgets/core/form.model.ts b/lib/core/form/components/widgets/core/form.model.ts index 9cec695ac3..59bf9a8319 100644 --- a/lib/core/form/components/widgets/core/form.model.ts +++ b/lib/core/form/components/widgets/core/form.model.ts @@ -35,6 +35,8 @@ import { FormFieldValidator } from './form-field-validator'; import { FormBaseModel } from '../../form-base.model'; +import { FormVariableModel } from './form-variable.model'; +import { ProcessVariableModel } from './process-variable.model'; export class FormModel extends FormBaseModel { @@ -48,7 +50,8 @@ export class FormModel extends FormBaseModel { fieldValidators: FormFieldValidator[] = [...FORM_FIELD_VALIDATORS]; readonly selectedOutcome: string; - processVariables: any; + processVariables: ProcessVariableModel[] = []; + variables: FormVariableModel[] = []; constructor(formRepresentationJSON?: any, formValues?: FormValues, readOnly: boolean = false, protected formService?: FormService) { super(); @@ -65,11 +68,11 @@ export class FormModel extends FormBaseModel { this.customFieldTemplates = formRepresentationJSON.customFieldTemplates || {}; this.selectedOutcome = formRepresentationJSON.selectedOutcome || {}; this.className = formRepresentationJSON.className || ''; + this.variables = formRepresentationJSON.variables || []; + this.processVariables = formRepresentationJSON.processVariables || []; const tabCache: FormWidgetModelCache = {}; - this.processVariables = formRepresentationJSON.processVariables; - this.tabs = (formRepresentationJSON.tabs || []).map((t) => { const model = new TabModel(this, t); tabCache[model.id] = model; @@ -226,4 +229,66 @@ export class FormModel extends FormBaseModel { } } } + + /** + * Returns a form variable that matches the identifier. + * @param identifier The `name` or `id` value. + */ + getFormVariable(identifier: string): FormVariableModel { + if (identifier) { + return this.variables.find( + variable => + variable.name === identifier || + variable.id === identifier + ); + } + return undefined; + } + + /** + * Returns a value of the form variable that matches the identifier. + * Provides additional conversion of types (date, boolean). + * @param identifier The `name` or `id` value + */ + getFormVariableValue(identifier: string): any { + const variable = this.getFormVariable(identifier); + + if (variable) { + switch (variable.type) { + case 'date': + return `${variable.value}T00:00:00.000Z`; + case 'boolean': + return JSON.parse(variable.value); + default: + return variable.value; + } + } + + return undefined; + } + + /** + * Returns a process variable value. + * @param name Variable name + */ + getProcessVariableValue(name: string): any { + if (this.processVariables) { + const names = [`variables.${name}`, name]; + + const variable = this.processVariables.find( + entry => names.includes(entry.name) + ); + + if (variable) { + switch (variable.type) { + case 'boolean': + return JSON.parse(variable.value); + default: + return variable.value; + } + } + } + + return undefined; + } } diff --git a/lib/core/form/components/widgets/core/index.ts b/lib/core/form/components/widgets/core/index.ts index 2d98dcae1f..9dbd603c39 100644 --- a/lib/core/form/components/widgets/core/index.ts +++ b/lib/core/form/components/widgets/core/index.ts @@ -36,3 +36,5 @@ export * from './error-message.model'; export * from './external-content'; export * from './external-content-link'; export * from './group.model'; +export * from './form-variable.model'; +export * from './process-variable.model'; diff --git a/lib/core/form/components/widgets/core/process-variable.model.ts b/lib/core/form/components/widgets/core/process-variable.model.ts new file mode 100644 index 0000000000..ec4b64097c --- /dev/null +++ b/lib/core/form/components/widgets/core/process-variable.model.ts @@ -0,0 +1,36 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export interface ProcessVariableModel { + serviceName?: string; + serviceFullName?: string; + serviceVersion?: string; + appName?: string; + appVersion?: string; + serviceType?: string; + id?: number; + type?: string; + name?: string; + createTime?: number; + lastUpdatedTime?: number; + executionId?: string; + value?: any; + markedAsDeleted?: boolean; + processInstanceId?: string; + taskId?: string; + taskVariable?: boolean; +} diff --git a/lib/core/form/services/widget-visibility.service.ts b/lib/core/form/services/widget-visibility.service.ts index 49fefa3ceb..7cfeb33f30 100644 --- a/lib/core/form/services/widget-visibility.service.ts +++ b/lib/core/form/services/widget-visibility.service.ts @@ -193,40 +193,24 @@ export class WidgetVisibilityService { getVariableValue(form: FormModel, name: string, processVarList: TaskProcessVariableModel[]): string { const processVariableValue = this.getProcessVariableValue(name, processVarList); - const variableDefaultValue = this.getFormVariableDefaultValue(form, name); + const variableDefaultValue = form.getFormVariableValue(name); + return (processVariableValue === undefined) ? variableDefaultValue : processVariableValue; } - private getFormVariableDefaultValue(form: FormModel, identifier: string): string { - const variables = this.getFormVariables(form); - if (variables) { - const formVariable = variables.find((formVar) => { - return formVar.name === identifier || formVar.id === identifier; - }); - - let value; - if (formVariable) { - value = formVariable.value; - if (formVariable.type === 'date') { - value += 'T00:00:00.000Z'; - } - } - return value; - } - } - - private getFormVariables(form: FormModel): any[] { - return form.json.variables; - } - private getProcessVariableValue(name: string, processVarList: TaskProcessVariableModel[]): string { if (processVarList) { - const processVariable = processVarList.find((variable) => variable.id === name); + const processVariable = processVarList.find( + variable => + variable.id === name || + variable.id === `variables.${name}` + ); + if (processVariable) { return processVariable.value; } } - + return undefined; } evaluateLogicalOperation(logicOp, previousValue, newValue): boolean { diff --git a/lib/core/mock/form/demo-form.mock.ts b/lib/core/mock/form/demo-form.mock.ts index 573428587b..38481487ca 100644 --- a/lib/core/mock/form/demo-form.mock.ts +++ b/lib/core/mock/form/demo-form.mock.ts @@ -15,23 +15,6 @@ * limitations under the License. */ -/*! -* @license -* Copyright 2019 Alfresco Software, Ltd. -* -* Licensed under the Apache License, Version 2.0 (the 'License'); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an 'AS IS' BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - export class DemoForm { easyForm = { @@ -358,7 +341,25 @@ export class DemoForm { 'style': '', 'customFieldTemplates': {}, 'metadata': {}, - 'variables': [], + 'variables': [ + { + 'id': 'bfca9766-7bc1-45cc-8ecf-cdad551e36e2', + 'name': 'name1', + 'type': 'string', + 'value': '' + }, + { + 'id': '3ed9f28a-dbae-463f-b991-47ef06658bb6', + 'name': 'name2', + 'type': 'string', + 'value': '' + }, + { + 'id': 'a7710978-1e9c-4b54-a19c-c6267d2b19a2', + 'name': 'input02', + 'type': 'integer' + } + ], 'customFieldsValueInfo': {}, 'gridsterForm': false, 'globalDateFormat': 'D-M-YYYY' @@ -1458,7 +1459,25 @@ export class DemoForm { 'style': '', 'customFieldTemplates': {}, 'metadata': {}, - 'variables': [], + 'variables': [ + { + 'id': 'bfca9766-7bc1-45cc-8ecf-cdad551e36e2', + 'name': 'name1', + 'type': 'string', + 'value': '' + }, + { + 'id': '3ed9f28a-dbae-463f-b991-47ef06658bb6', + 'name': 'name2', + 'type': 'string', + 'value': '' + }, + { + 'id': 'a7710978-1e9c-4b54-a19c-c6267d2b19a2', + 'name': 'input02', + 'type': 'integer' + } + ], 'gridsterForm': false, 'globalDateFormat': 'D-M-YYYY' }; @@ -1722,7 +1741,25 @@ export class DemoForm { 'style': '', 'customFieldTemplates': {}, 'metadata': {}, - 'variables': [], + 'variables': [ + { + 'id': 'bfca9766-7bc1-45cc-8ecf-cdad551e36e2', + 'name': 'name1', + 'type': 'string', + 'value': '' + }, + { + 'id': '3ed9f28a-dbae-463f-b991-47ef06658bb6', + 'name': 'name2', + 'type': 'string', + 'value': '' + }, + { + 'id': 'a7710978-1e9c-4b54-a19c-c6267d2b19a2', + 'name': 'input02', + 'type': 'integer' + } + ], 'customFieldsValueInfo': {}, 'gridsterForm': false }