define([
'businesscode.dt/js/api/Utils',
'components/js/fieldValueEditor/FieldValueEditorUtils',
'components/js/fieldValueEditor/FieldValueEditorValueMode',
'components/js/utils/TypeConverter',
'entity/js/api/PropertyType'
], function(
Utils,
FieldValueEditorUtils,
FieldValueEditorValueMode,
TypeConverter,
PropertyType
) {
'use strict';
var FieldValueEditorUtilsDt = function() {
AbcsLib.throwStaticClassError();
};
/**
* Convert the given external parameters to the value of the input component.
*
* @param {string} staticValue The static value.
* @param {string} referenceValue The reference value.
* @param {string} expressionValue The expression value.
* @param {string} inputMode The input mode.
* @param {string} propertyType The target property type.
* @param {string} fieldId (Optional) Required only when propertyType is PropertyType.REFERENCE.
* @param {string} targetEntity (Optional) Required only when propertyType is PropertyType.REFERENCE.
* @return {string} The internalized data object representing the value of the input component.
*/
FieldValueEditorUtilsDt.convertExternalToInput = function (staticValue, referenceValue, expressionValue, inputMode, propertyType, fieldId, targetEntity) {
var result = FieldValueEditorUtils.getComponentTypeAndRelation(inputMode, propertyType, fieldId, targetEntity);
var componentType = result.componentType;
var value;
switch (componentType) {
case TypeConverter.TEXT_INPUT_TYPE:
if (staticValue === undefined) {
value = '';
// internalized.shouldFixStaticValue = true;
} else {
value = staticValue;
}
break;
case TypeConverter.TIME_INPUT_TYPE:
case TypeConverter.DATE_INPUT_TYPE:
case TypeConverter.DATE_TIME_INPUT_TYPE:
case TypeConverter.NUMBER_INPUT_TYPE:
value = staticValue;
break;
case 'ojSelect-boolean':
if (staticValue === undefined) {
value = [];
} else {
value = [staticValue];
}
break;
case 'abcsComboBox':
value = staticValue;
break;
case 'ojInputText-expression':
if (expressionValue === undefined || expressionValue[0] === '=') {
value = expressionValue;
} else {
value = '=' + expressionValue;
}
break;
case 'ojSelect-reference':
if (referenceValue === undefined) {
value = [];
} else {
value = [referenceValue];
}
break;
default:
throw new Error('Unexpected component type');
}
return value;
};
/**
* Convert the current value of the input component to the external parameters.
*
* @param {string} value The value of the input component
* @param {string} propertyType The target property type.
* @param {string} inputMode The input mode.
* @param {string} fieldId (Optional) Required only when propertyType is PropertyType.REFERENCE.
* @param {string} targetEntity (Optional) Required only when propertyType is PropertyType.REFERENCE.
* @return {string} The externalized data object representing the external param value.
*/
FieldValueEditorUtilsDt.convertInputToExternal = function (value, inputMode, propertyType, fieldId, targetEntity) {
var result = FieldValueEditorUtils.getComponentTypeAndRelation(inputMode, propertyType, fieldId, targetEntity);
var componentType = result.componentType;
var externalized = {};
externalized.inputMode = inputMode;
externalized.propertyType = propertyType;
externalized.componentType = componentType;
switch (componentType) {
case TypeConverter.TEXT_INPUT_TYPE:
if (value === undefined) {
externalized.value = '';
} else {
externalized.value = value;
}
break;
case TypeConverter.TIME_INPUT_TYPE:
case TypeConverter.DATE_INPUT_TYPE:
case TypeConverter.DATE_TIME_INPUT_TYPE:
case TypeConverter.NUMBER_INPUT_TYPE:
externalized.value = value;
break;
case 'ojSelect-boolean':
if (value === undefined || value.length === 0) {
externalized.value = undefined;
} else {
externalized.value = value[0];
}
break;
case 'abcsComboBox':
externalized.value = value;
break;
case 'ojInputText-expression':
if (value === undefined || value[0] === '=') {
externalized.value = value;
} else {
var fixed = '=' + value;
externalized.value = fixed;
externalized.shouldFixValue = true;
}
break;
case 'ojSelect-reference':
if (value === undefined || value.length === 0) {
externalized.value = undefined;
} else {
externalized.value = value[0];
}
break;
default:
throw new Error('Unexpected component type');
}
return externalized;
};
/**
* Convert the current value of the input component into the script for consumption by the code generator.
*
* @param {string} value The value of the input component
* @param {string} propertyType The target property type.
* @param {string} inputMode The input mode.
* @param {string} fieldId (Optional) Required only when propertyType is PropertyType.REFERENCE.
* @param {string} targetEntity (Optional) Required only when propertyType is PropertyType.REFERENCE.
* @return {string} The script representing the input value with the given property type and input mode.
*/
FieldValueEditorUtilsDt.convertInputToScript = function (value, inputMode, propertyType, fieldId, targetEntity, wrapExperession) {
var externalized = FieldValueEditorUtilsDt.convertInputToExternal(value, inputMode, propertyType, fieldId, targetEntity);
var expression = FieldValueEditorUtilsDt.convertExternalToExpression(externalized);
return FieldValueEditorUtilsDt.convertExpressionToScript(expression, wrapExperession);
};
FieldValueEditorUtilsDt.convertExternalToExpression = function (externalized) {
if (externalized.inputMode === FieldValueEditorValueMode.STATIC) {
return FieldValueEditorUtilsDt.convertStaticToExpression(externalized.propertyType, externalized.value);
} else if (externalized.inputMode === FieldValueEditorValueMode.REFERENCE) {
return FieldValueEditorUtilsDt.convertReferenceToExpression(externalized.value);
} else if (externalized.inputMode === FieldValueEditorValueMode.EXPRESSION) {
return externalized.value;
} else {
throw new Error('Unexpected input mode');
}
};
FieldValueEditorUtilsDt.isStringAssignableProperty = function (propType) {
var stringAssignable = propType === PropertyType.KEY || propType === PropertyType.TEXT || propType === PropertyType.EMAIL ||
propType === PropertyType.PHONE || propType === PropertyType.URL ||
propType === PropertyType.DATE;
return stringAssignable;
};
FieldValueEditorUtilsDt.convertStaticToExpression = function (targetPropType, staticValue) {
if (FieldValueEditorUtilsDt.isStringAssignableProperty(targetPropType) || (targetPropType === PropertyType.REFERENCE && isNaN(staticValue))) {
return (staticValue !== undefined && staticValue !== null) ? '=' + FieldValueEditorUtilsDt.stringifyUsingSingleQuote(staticValue) : undefined;
} else if (targetPropType === PropertyType.DATETIME) {
return (staticValue !== undefined && staticValue !== null) ? '=' + FieldValueEditorUtilsDt.stringifyUsingSingleQuote(staticValue.replace('T', ' ')) : undefined;
} else if (targetPropType === PropertyType.TIME) {
return (staticValue !== undefined && staticValue !== null) ? '=' + FieldValueEditorUtilsDt.stringifyUsingSingleQuote(staticValue.replace('T', '')) : undefined;
} else if (targetPropType === PropertyType.NUMBER) {
return (staticValue !== undefined && staticValue !== null && staticValue !== '') ? '=' + staticValue : undefined;
} else {
return (staticValue !== undefined && staticValue !== null) ? '=' + staticValue : undefined;
}
};
/**
* Convert the expression value to a script form by removing the expression prefix ('=').
*
* @param {string} The expression value.
* @return {string} The script value. Returns undefined if the expression value is undefined or equal to '='.
*/
FieldValueEditorUtilsDt.convertReferenceToExpression = function (referenceValue) {
return (referenceValue !== undefined && referenceValue !== null) ? '=' + referenceValue : undefined;
};
FieldValueEditorUtilsDt.convertExpressionToScript = function (expressionValue, wrapExpression) {
if (expressionValue === undefined || expressionValue === null || expressionValue === '=') {
return undefined;
} else if (wrapExpression) {
var expressionValueSubstring = Utils.reindent(4, '\n' + expressionValue.substring(1) + '\n', true);
return (expressionValue[0] === '=') ? '{' + expressionValueSubstring + '}.call()' : expressionValue;
} else {
return (expressionValue[0] === '=') ? expressionValue.substring(1) : expressionValue;
}
};
/**
* Stringify the given string value using the single-quote character. The string escaping logic
* is adopted from a double-quoted implementation at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
*
* @param {string} text The target text.
* @returns {string} The stringified text.
*/
FieldValueEditorUtilsDt.stringifyUsingSingleQuote = function (text) {
var escMap = {'\'': '\\\'', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'};
var escFunc = function (m) {
return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1);
};
var escRE = /[\\'\u0000-\u001F\u2028\u2029]/g;
if (text === null || text === undefined) {
return 'null';
}
return '\'' + text.replace(escRE, escFunc) + '\'';
};
return FieldValueEditorUtilsDt;
});