diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index bcdafd9401..8d74299975 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -119,7 +119,7 @@ function constructForm(url, method, options={}) { switch (method) { case 'POST': if (canCreate(OPTIONS)) { - console.log('create'); + constructCreateForm(url, OPTIONS.actions.POST); } else { // User does not have permission to POST to the endpoint console.log('cannot POST'); @@ -159,4 +159,138 @@ function constructForm(url, method, options={}) { break; } }); +} + + +/* + * Construct a 'creation' (POST) form, to create a new model in the database. + * + * arguments: + * - fields: The 'actions' object provided by the OPTIONS endpoint + * + * options: + * - + */ +function constructCreateForm(url, fields, options={}) { + + var html = ''; + + for (const key in fields) { + //console.log('field:', key); + + html += constructField(key, fields[key], options); + } + + var modal = '#modal-form'; + + modalEnable(modal, true); + + $(modal).find('.modal-form-content').html(html); + + $(modal).modal('show'); +} + + +/* + * Construct a single form 'field' for rendering in a form. + * + * arguments: + * - name: The 'name' of the field + * - parameters: The field parameters supplied by the DRF OPTIONS method + * + * options: + * - + * + * The function constructs a fieldset which mostly replicates django "crispy" forms: + * + * - Field name + * - Field (depends on specified field type) + * - Field description (help text) + * - Field errors + */ +function constructField(name, parameters, options={}) { + + var field_name = `id_${name}`; + + var html = `
`; + + // Add a label + html += constructLabel(name, parameters); + + html += `
`; + + html += constructInput(name, parameters, options); + html += constructHelpText(name, parameters, options); + + // TODO: Add the "error message" + + html += `
`; // controls + + html += `
`; // form-group + + return html; +} + + +/* + * Construct a 'label' div + * + * arguments: + * - name: The name of the field + * - required: Is this a required field? + */ +function constructLabel(name, parameters) { + + var label_classes = 'control-label'; + + if (parameters.required) { + label_classes += ' requiredField'; + } + + var html =''; + + html += ``; + + return html; +} + + +/* + * Construct a form input based on the field parameters + * + * arguments: + * - name: The name of the field + * - parameters: Field parameters returned by the OPTIONS method + * + */ +function constructInput(name, parameters, options={}) { + + var html = ''; + + // TODO: Construct an input field based on the field type! + + return html; +} + + +/* + * Construct a 'help text' div based on the field parameters + * + * arguments: + * - name: The name of the field + * - parameters: Field parameters returned by the OPTIONS method + * + */ +function constructHelpText(name, parameters, options={}) { + + var html = `
${parameters.help_text}
`; + + return html; } \ No newline at end of file