mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Refactor form field definition copying
This commit is contained in:
		@@ -366,6 +366,14 @@ function constructFormBody(fields, options) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Initialize an "empty" field for each specified field
 | 
				
			||||||
 | 
					    for (field in displayed_fields) {
 | 
				
			||||||
 | 
					        if (!(field in fields)) {
 | 
				
			||||||
 | 
					            console.log("adding blank field for ", field);
 | 
				
			||||||
 | 
					            fields[field] = {};
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Provide each field object with its own name
 | 
					    // Provide each field object with its own name
 | 
				
			||||||
    for(field in fields) {
 | 
					    for(field in fields) {
 | 
				
			||||||
        fields[field].name = field;
 | 
					        fields[field].name = field;
 | 
				
			||||||
@@ -383,57 +391,18 @@ function constructFormBody(fields, options) {
 | 
				
			|||||||
            // Override existing query filters (if provided!)
 | 
					            // Override existing query filters (if provided!)
 | 
				
			||||||
            fields[field].filters = Object.assign(fields[field].filters || {}, field_options.filters);
 | 
					            fields[field].filters = Object.assign(fields[field].filters || {}, field_options.filters);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // TODO: Refactor the following code with Object.assign (see above)
 | 
					            for (var opt in field_options) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // "before" and "after" renders
 | 
					                var val = field_options[opt];
 | 
				
			||||||
            fields[field].before = field_options.before;
 | 
					 | 
				
			||||||
            fields[field].after = field_options.after;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Secondary modal options
 | 
					                if (opt == 'filters') {
 | 
				
			||||||
            fields[field].secondary = field_options.secondary;
 | 
					                    // ignore filters (see above)
 | 
				
			||||||
 | 
					                } else if (opt == 'icon') {
 | 
				
			||||||
            // Edit callback
 | 
					                    // Specify custom icon
 | 
				
			||||||
            fields[field].onEdit = field_options.onEdit;
 | 
					                    fields[field].prefix = `<span class='fas ${val}'></span>`;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
            fields[field].multiline = field_options.multiline;
 | 
					                    fields[field][opt] = field_options[opt];
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            // Custom help_text
 | 
					 | 
				
			||||||
            if (field_options.help_text) {
 | 
					 | 
				
			||||||
                fields[field].help_text = field_options.help_text;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Custom label
 | 
					 | 
				
			||||||
            if (field_options.label) {
 | 
					 | 
				
			||||||
                fields[field].label = field_options.label;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Custom placeholder
 | 
					 | 
				
			||||||
            if (field_options.placeholder) {
 | 
					 | 
				
			||||||
                fields[field].placeholder = field_options.placeholder;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Choices
 | 
					 | 
				
			||||||
            if (field_options.choices) {
 | 
					 | 
				
			||||||
                fields[field].choices = field_options.choices;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Group
 | 
					 | 
				
			||||||
            if (field_options.group) {
 | 
					 | 
				
			||||||
                fields[field].group = field_options.group;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Field prefix
 | 
					 | 
				
			||||||
            if (field_options.prefix) {
 | 
					 | 
				
			||||||
                fields[field].prefix = field_options.prefix;
 | 
					 | 
				
			||||||
            } else if (field_options.icon) {
 | 
					 | 
				
			||||||
                // Specify icon like 'fa-user'
 | 
					 | 
				
			||||||
                fields[field].prefix = `<span class='fas ${field_options.icon}'></span>`;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            fields[field].hidden = field_options.hidden;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (field_options.read_only != null) {
 | 
					 | 
				
			||||||
                fields[field].read_only = field_options.read_only;
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -477,8 +446,6 @@ function constructFormBody(fields, options) {
 | 
				
			|||||||
    if (options.current_group) {
 | 
					    if (options.current_group) {
 | 
				
			||||||
        // Close out the current group
 | 
					        // Close out the current group
 | 
				
			||||||
        html += `</div></div>`;
 | 
					        html += `</div></div>`;
 | 
				
			||||||
 | 
					 | 
				
			||||||
        console.log(`finally, ending group '${console.current_group}'`);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Create a new modal if one does not exists
 | 
					    // Create a new modal if one does not exists
 | 
				
			||||||
@@ -878,6 +845,7 @@ function handleFormErrors(errors, fields, options) {
 | 
				
			|||||||
    non_field_errors.append(
 | 
					    non_field_errors.append(
 | 
				
			||||||
        `<div class='alert alert-block alert-danger'>
 | 
					        `<div class='alert alert-block alert-danger'>
 | 
				
			||||||
            <b>{% trans "Form errors exist" %}</b>
 | 
					            <b>{% trans "Form errors exist" %}</b>
 | 
				
			||||||
 | 
					            <span id='form-errors-info' class='float-right fas fa-info-circle icon-red'></span>
 | 
				
			||||||
        </div>`
 | 
					        </div>`
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -947,7 +915,10 @@ function addFieldCallbacks(fields, options) {
 | 
				
			|||||||
function addFieldCallback(name, field, options) {
 | 
					function addFieldCallback(name, field, options) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $(options.modal).find(`#id_${name}`).change(function() {
 | 
					    $(options.modal).find(`#id_${name}`).change(function() {
 | 
				
			||||||
        field.onEdit(name, field, options);
 | 
					
 | 
				
			||||||
 | 
					        var value = getFormFieldValue(name, field, options);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        field.onEdit(value, name, field, options);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1429,8 +1400,6 @@ function constructField(name, parameters, options) {
 | 
				
			|||||||
    if (options.current_group && parameters.group != options.current_group) {
 | 
					    if (options.current_group && parameters.group != options.current_group) {
 | 
				
			||||||
        html += `</div></div>`;
 | 
					        html += `</div></div>`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        console.log(`ending group '${options.current_group}'`);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Null out the current "group" so we can start a new one
 | 
					        // Null out the current "group" so we can start a new one
 | 
				
			||||||
        options.current_group = null;
 | 
					        options.current_group = null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -1446,8 +1415,6 @@ function constructField(name, parameters, options) {
 | 
				
			|||||||
        // Add HTML for the start of a separate panel
 | 
					        // Add HTML for the start of a separate panel
 | 
				
			||||||
        if (parameters.group != options.current_group) {
 | 
					        if (parameters.group != options.current_group) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            console.log(`starting group '${group}'`);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            html += `
 | 
					            html += `
 | 
				
			||||||
            <div class='panel form-panel' id='form-panel-${group}' group='${group}'>
 | 
					            <div class='panel form-panel' id='form-panel-${group}' group='${group}'>
 | 
				
			||||||
                <div class='panel-heading form-panel-heading' id='form-panel-heading-${group}'>`;
 | 
					                <div class='panel-heading form-panel-heading' id='form-panel-heading-${group}'>`;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user