-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic support for dynamic forms
Loading from XML definition files.
- Loading branch information
Showing
8 changed files
with
195 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const { locateFormDefinitionFile } = require('../utils'); | ||
const { readFileSync } = require('cosmiconfig/dist/readFile'); | ||
const xml2js = require('xml2js'); | ||
const FormGroup = require('dw/web/FormGroup'); | ||
const FormField = require('dw/web/FormField'); | ||
|
||
class FormsLoader { | ||
constructor() { | ||
const self = this; | ||
this._accessor = new Proxy( | ||
{}, | ||
{ | ||
get(obj, formName) { | ||
return self.getForm(formName); | ||
}, | ||
} | ||
); | ||
} | ||
|
||
getAccessor() { | ||
return this._accessor; | ||
} | ||
|
||
getForm(formName) { | ||
const { cartridge, path } = locateFormDefinitionFile( | ||
formName, | ||
request.locale | ||
); | ||
|
||
let formDefinition; | ||
xml2js.parseString(readFileSync(path), (error, result) => { | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
formDefinition = result; | ||
}); | ||
|
||
return this._buildForm(formDefinition); | ||
} | ||
|
||
_buildForm(formDefinition) { | ||
const form = new FormGroup(); | ||
|
||
if (formDefinition.form.group) { | ||
this._processFormGroups(formDefinition.form.group, form); | ||
} | ||
|
||
if (formDefinition.form.field) { | ||
this._processFormFields(formDefinition.form.field, form); | ||
} | ||
|
||
if (formDefinition.form.include) { | ||
this._processIncludes(formDefinition.form.include, form); | ||
} | ||
|
||
form.clearFormElement = () => {}; | ||
|
||
return form; | ||
} | ||
|
||
_processFormGroups(groups, form) { | ||
groups.forEach((group) => { | ||
const formGroup = new FormGroup(); | ||
|
||
group.field.forEach((field) => { | ||
formGroup[field.$.formid] = new FormField(field); | ||
}); | ||
|
||
if (group.include) { | ||
group.include.forEach((include) => { | ||
formGroup[include.$.formid] = this.getForm(include.$.name); | ||
}); | ||
} | ||
|
||
form[group.$.formid] = formGroup; | ||
}); | ||
} | ||
|
||
_processFormFields(fields, form) { | ||
fields.forEach((field) => { | ||
form[field.$.formid] = new FormField(field); | ||
}); | ||
} | ||
|
||
_processIncludes(includes, form) { | ||
includes.forEach((include) => { | ||
form[include.$.formid] = this.getForm(include.$.name); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = new FormsLoader(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class FormElement {} | ||
|
||
module.exports = FormElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,30 @@ | ||
var _super = require('dw/web/FormElement'); | ||
|
||
var FormField = function () {}; | ||
|
||
FormField.prototype = new _super(); | ||
|
||
FormField.prototype.getValue = function () {}; | ||
FormField.prototype.getType = function () {}; | ||
FormField.prototype.setValue = function () {}; | ||
FormField.prototype.getDescription = function () {}; | ||
FormField.prototype.getOptions = function () {}; | ||
FormField.prototype.getError = function () {}; | ||
FormField.prototype.getMaxValue = function () {}; | ||
FormField.prototype.getRegEx = function () {}; | ||
FormField.prototype.getHtmlValue = function () {}; | ||
FormField.prototype.setHtmlValue = function () {}; | ||
FormField.prototype.setOptions = function () {}; | ||
FormField.prototype.getSelectedOption = function () {}; | ||
FormField.prototype.getSelectedOptionObject = function () {}; | ||
FormField.prototype.isChecked = function () {}; | ||
FormField.prototype.isSelected = function () {}; | ||
FormField.prototype.isMandatory = function () {}; | ||
FormField.prototype.getMinLength = function () {}; | ||
FormField.prototype.getMaxLength = function () {}; | ||
FormField.prototype.getMinValue = function () {}; | ||
FormField.prototype.getLabel = function () {}; | ||
FormField.prototype.value = ''; | ||
FormField.prototype.type = null; | ||
FormField.prototype.description = null; | ||
FormField.prototype.options = null; | ||
FormField.prototype.error = null; | ||
FormField.prototype.maxValue = null; | ||
FormField.prototype.regEx = null; | ||
FormField.prototype.htmlValue = null; | ||
FormField.prototype.selectedOption = null; | ||
FormField.prototype.selectedOptionObject = null; | ||
FormField.prototype.minLength = null; | ||
FormField.prototype.maxLength = null; | ||
FormField.prototype.minValue = null; | ||
FormField.prototype.label = null; | ||
const FormElement = require('dw/web/FormElement'); | ||
|
||
class FormField extends FormElement { | ||
_value = ''; | ||
|
||
options = []; | ||
|
||
constructor(field) { | ||
super(); | ||
|
||
if (field.options) { | ||
this.options = this._processOptions(field.options[0].option); | ||
} | ||
} | ||
|
||
get value() { | ||
return this._value; | ||
} | ||
|
||
set value(value) { | ||
this._value = value; | ||
} | ||
|
||
_processOptions(options) { | ||
options.optionsCount = options.length; | ||
return options; | ||
} | ||
} | ||
|
||
module.exports = FormField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var FormElement = require('dw/web/FormElement'); | ||
|
||
class FormGroup extends FormElement {} | ||
|
||
module.exports = FormGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.