-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathdialogData.ts
210 lines (187 loc) · 7.23 KB
/
dialogData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import * as _ from 'lodash';
import * as angular from 'angular';
import {__} from '../../common/translateFunction';
export default class DialogDataService {
/**
* Sets up and configures properties for a dialog field
* @memberof DialogDataService
* @function setupField
* @param data {any} This is a object that is all the information for a particular dialog field
*
**/
public setupField(data: any) {
let field = _.cloneDeep(data);
const dropDownValues = [];
field.fieldBeingRefreshed = (angular.isDefined(field.fieldBeingRefreshed) ? field.fieldBeingRefreshed : false);
if (angular.isUndefined(field.fieldValidation)) {
field.fieldValidation = '';
field.errorMessage = '';
}
const sortableFieldTypes = ['DialogFieldDropDownList', 'DialogFieldRadioButton'];
if (_.includes(sortableFieldTypes,field.type)) {
for (let option of field.values) {
if (option[0] === String(field.default_value)) {
field.selected = option;
}
const value = ((field.data_type === 'integer' && option[0] !== null) ? parseInt(option[0], 10) : option[0]);
const description = (!Number.isInteger(option[1]) ? option[1] : parseInt(option[1], 10));
dropDownValues.push([value, description]);
}
field.values = dropDownValues;
if (data.options.sort_by !== 'none') {
field.values = this.updateFieldSortOrder(field);
}
}
if (field.type === 'DialogFieldDateTimeControl') {
if (_.isNull(field.values) || _.isUndefined(field.values)) {
field.dateField = field.timeField = new Date();
} else {
field.dateField = field.timeField = new Date(data.values);
}
}
field.default_value = this.setDefaultValue(field);
return field;
}
/**
*
* This method updates sort order of dialog options for a dialog field that is a drop down.
* @memberof DialogDataService
* @function updateFieldSortOrder
* @param data {any} This is a object that is all the information for a particular dialog field
*
**/
private updateFieldSortOrder(data) {
const SORT_DESCRIPTION = 1;
const SORT_VALUE = 0;
const FIRST_OPTION = 0;
const VALUE = 0;
const sortBy = (data.options.sort_by === 'value' ? SORT_VALUE : SORT_DESCRIPTION);
let tempValues = [...data.values];
let defaultDropdownField = [];
// The following if deals with a empty default option if it exists
if (data.data_type === 'integer' && _.isNaN(tempValues[FIRST_OPTION][VALUE]) ||
_.isNull(tempValues[FIRST_OPTION][VALUE])) {
defaultDropdownField = tempValues.shift();
}
let values = _.sortBy(tempValues, sortBy);
const sortedValues : any = data.options.sort_order === 'ascending' ? values : values.reverse();
if (defaultDropdownField.length) {
sortedValues.unshift(defaultDropdownField);
}
return sortedValues;
}
/**
*
* This method sets default value for a dialog field
* @memberof DialogDataService
* @function setDefaultValue
* @param data {any} This is a object that is all the information for a particular dialog field
*
**/
private setDefaultValue(data): any {
let defaultValue: any = '';
const firstOption = 0; // these are meant to help make code more readable
const fieldValue = 0;
if (_.isObject(data.values)) {
defaultValue = data.values[firstOption][fieldValue];
}
if (data.type === 'DialogFieldDateControl' || data.type === 'DialogFieldDateTimeControl') {
defaultValue = data.values ? new Date(data.values) : new Date();
}
if (data.default_value) {
defaultValue = data.default_value;
}
if (data.type === 'DialogFieldDropDownList' && data.options.force_multi_value && data.default_value) {
defaultValue = JSON.parse(data.default_value);
}
if (data.type === 'DialogFieldTagControl') {
// setting the default_value for a tag control's select box
// In case the default_value is not set for the ng-model of the component, an empty value option is displayed
let defaultOption = _.find(data.values, { id: null });
if (defaultOption) {
defaultOption.id = 0;
defaultValue = defaultOption.id;
}
}
if (this.checkboxNeedsNewDefaultValue(data)) {
defaultValue = data.values;
}
return defaultValue;
}
private checkboxNeedsNewDefaultValue(data): boolean {
return (data.type === 'DialogFieldCheckBox' && data.dynamic && data.values !== data.default_value);
}
/**
*
* Validates a dialog field to ensure that the values supplied meet required criteria
* @memberof DialogDataService
* @function validateField
* @param field {any} This is a object that is all the information for a particular dialog field
* @param value {any} Field is optional. Allows you to explicitly pass in the value to verify for a field
**/
public validateField(field, value): any {
const fieldValue = (value ? value : field.default_value);
const validation = {
isValid: true,
field: '',
message: ''
};
validation.field = field.label;
if (field.required) {
if (field.type === 'DialogFieldCheckBox' && fieldValue === 'f') {
validation.isValid = false;
validation.message = __('This field is required');
} else if (field.type === 'DialogFieldTagControl') {
if (this.isInvalidTagControl(field.options.force_single_value, fieldValue)) {
validation.isValid = false;
validation.message = __('This field is required');
}
} else if (_.isEmpty(fieldValue)) {
validation.isValid = false;
validation.message = __('This field is required');
}
}
// Run check if someone has specified a regex. Make sure if its required it is not blank
if (field.validator_rule && validation.isValid === true) {
if (angular.isDefined(fieldValue) && !_.isEmpty(fieldValue)) {
// This use case ensures that an optional field doesnt check a regex if field is blank
const regexPattern = field.validator_rule.replace(/\\A/i, '^').replace(/\\Z/i,'$');
const regex = new RegExp(regexPattern);
const regexValidates = regex.test(fieldValue);
validation.isValid = regexValidates;
validation.message = __('Entered text should match the format:') + ' ' + regexPattern;
}
}
if (field.type === 'DialogFieldDateTimeControl') {
if (field.dateField === undefined) {
validation.isValid = false;
validation.message = __('Select a valid date');
}
}
return validation;
}
/**
* Determines if a value is a tag control and whether or not that value is valid
* @memberof DialogDataService
* @function isInvalidTagControl
* @param forceSingleValue {boolean} Whether or not the field allows multiple selections
* @param fieldValue {any} This is the value of the field in question to be validated
**/
private isInvalidTagControl(forceSingleValue, fieldValue) {
let invalid = false;
if (forceSingleValue) {
if (_.isNumber(fieldValue)) {
if (fieldValue === 0) {
invalid = true;
}
} else if (_.isEmpty(fieldValue)) {
invalid = true;
}
} else {
if (_.isEmpty(fieldValue)) {
invalid = true;
}
}
return invalid;
}
}