Adds SFRA 6.0

This commit is contained in:
Isaac Vallee
2021-12-21 10:57:31 -08:00
parent d04eb5dd16
commit 823c7608c3
1257 changed files with 137087 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
'use strict';
module.exports = function (action) {
return {
description: action.description || null,
label: action.label || null,
submitted: action.submitted,
triggered: action.triggered,
formType: 'formAction'
};
};

View File

@@ -0,0 +1,134 @@
'use strict';
var resource = require('dw/web/Resource');
var secureEncoder = require('dw/util/SecureEncoder');
/**
* Function to conver <dw.web.FormField> object to plain JS object.
* @param {dw.web.FormField} field original formField object.
* @return {Object} Plain JS object representing formField.
*/
function formField(field) {
var result = {};
Object.defineProperty(result, 'attributes', {
get: function () {
var attributes = '';
attributes += 'name="' + result.htmlName + '"';
if (result.mandatory) {
attributes += ' required';
attributes += ' aria-required="true"';
}
if (field.options && field.options.optionsCount > 0) {
return attributes;
}
if (field.type === field.FIELD_TYPE_BOOLEAN && result.checked) {
attributes += ' checked="' + result.checked + '"';
}
var value = field.htmlValue == null ? '' : field.htmlValue;
attributes += ' value="' + secureEncoder.forHtmlInDoubleQuoteAttribute(value) + '"';
if (result.maxValue) {
attributes += ' max="' + result.maxValue + '"';
}
if (result.minValue) {
attributes += ' min="' + result.minValue + '"';
}
if (result.maxLength) {
attributes += ' maxLength="' + result.maxLength + '"';
}
if (result.minLength) {
attributes += ' minLength="' + result.minLength + '"';
}
if (result.regEx) {
attributes += ' pattern="' + result.regEx + '"';
}
return attributes;
}
});
Object.defineProperty(result, 'value', {
configurable: true,
get: function () {
return field.value;
},
set: function (value) {
field.value = value; // eslint-disable-line no-param-reassign
// reset htmlValue
result.htmlValue = field.htmlValue || '';
}
});
var attributesToCopy = {
string: ['maxLength', 'minLength', 'regEx'],
bool: ['checked', 'selected'],
int: ['maxValue', 'minValue'],
common: ['htmlValue', 'mandatory',
'dynamicHtmlName', 'htmlName', 'valid'
],
resources: ['error', 'description', 'label']
};
if (field.type === field.FIELD_TYPE_BOOLEAN && field.mandatory && !field.checked) {
field.invalidateFormElement();
}
attributesToCopy.common.forEach(function (item) {
if (item !== 'valid') {
result[item] = field[item] || '';
} else {
result.valid = field.valid;
}
});
attributesToCopy.resources.forEach(function (item) {
if (field[item]) {
result[item] = resource.msg(field[item], 'forms', null);
}
});
if (field.options && field.options.optionsCount > 0) {
result.options = [];
for (var i = 0, l = field.options.optionsCount; i < l; i++) {
result.options.push({
checked: field.options[i].checked,
htmlValue: field.options[i].htmlValue,
label: field.options[i].label
? resource.msg(field.options[i].label, 'forms', null)
: '',
id: field.options[i].optionId,
selected: field.options[i].selected,
value: field.options[i].value
});
}
result.selectedOption = field.selectedOption ? field.selectedOption.optionId : '';
}
switch (field.type) {
case field.FIELD_TYPE_BOOLEAN:
attributesToCopy.bool.forEach(function (item) {
result[item] = field[item];
});
break;
case field.FIELD_TYPE_DATE:
case field.FIELD_TYPE_INTEGER:
case field.FIELD_TYPE_NUMBER:
attributesToCopy.int.forEach(function (item) {
result[item] = field[item];
});
break;
case field.FIELD_TYPE_STRING:
attributesToCopy.string.forEach(function (item) {
result[item] = field[item];
});
break;
default:
break;
}
result.formType = 'formField';
return result;
}
module.exports = formField;

View File

@@ -0,0 +1,117 @@
'use strict';
var field = require('./formField');
var action = require('./formAction');
/**
* Convert dw.web.Form or dw.web.FormGroup to plain JS object
* @param {dw.web.Form|dw.web.FormGroup} form Form to be parsed
* @return {Object} Plain JS form object
*/
function parseForm(form) {
var formField = require('dw/web/FormField');
var formAction = require('dw/web/FormAction');
var formGroup = require('dw/web/FormGroup');
var result = {
valid: form.valid,
htmlName: form.htmlName,
dynamicHtmlName: form.dynamicHtmlName,
error: form.error || null,
attributes: 'name = "' + form.htmlName + '" id = "' + form.htmlName + '"',
formType: 'formGroup'
};
Object.keys(form).forEach(function (key) {
if (form[key] instanceof formField) {
result[key] = field(form[key]);
} else if (form[key] instanceof formAction) {
result[key] = action(form[key]);
} else if (form[key] instanceof formGroup) {
result[key] = parseForm(form[key]);
}
});
return result;
}
/**
* Copy the values of an object to form
* @param {Object} object - the object to set the new form values to
* @param {dw.web.Form|dw.web.FormGroup} currentForm - Form to be parsed
*/
function copyObjectToForm(object, currentForm) {
Object.keys(currentForm).forEach(function (key) {
if (currentForm[key] && currentForm[key].formType === 'formGroup') {
copyObjectToForm(object, currentForm[key]);
} else if (object[key] && !Object.hasOwnProperty.call(currentForm[key], 'options')) {
currentForm[key].value = object[key]; // eslint-disable-line no-param-reassign
} else if (object[key] && Object.hasOwnProperty.call(currentForm[key], 'options')) {
currentForm[key].options.forEach(function (option) {
if (option.value === object[key]) {
option.selected = true; // eslint-disable-line no-param-reassign
}
});
}
});
}
/**
* Get values of a formGroup to object
* @param {dw.web.FormGroup} formGroup - Form group
* @param {string} name - The name of the formGroup
* @return {Object} Object with nested values
*/
function findValue(formGroup, name) {
var ObjectWrapper = {};
ObjectWrapper[name] = {};
Object.keys(formGroup).forEach(function (key) {
var formField = formGroup[key];
if (formField instanceof Object) {
if (formField.formType === 'formField') {
ObjectWrapper[name][key] = formField.value;
} else if (formField.formType === 'formGroup') {
ObjectWrapper[name][key] = findValue(formField, key)[key];
}
}
});
return ObjectWrapper;
}
module.exports = function (session) {
return {
getForm: function (name) {
var currentForm = session.forms[name];
var result = parseForm(currentForm);
result.base = currentForm;
result.clear = function () {
currentForm.clearFormElement();
var clearedForm = parseForm(currentForm);
Object.keys(clearedForm).forEach(function (key) {
this[key] = clearedForm[key];
}, this);
};
result.copyFrom = function (object) {
copyObjectToForm(object, result);
};
result.toObject = function () {
var formObj = {};
var form = this;
Object.keys(form).forEach(function (key) {
var formField = form[key];
if (typeof form[key] !== 'function'
&& formField instanceof Object) {
if (formField.formType === 'formField') {
formObj[key] = formField.value;
} else if (formField.formType === 'formGroup') {
var nested = findValue(formField, key);
formObj[key] = nested[key];
}
}
});
return formObj;
};
return result;
}
};
};