Replace CompanyCreate and CompanyEdit forms with AJAX form

- Adds the ability to specify an "icon" for each field
This commit is contained in:
Oliver 2021-06-29 21:17:48 +10:00
parent cf0feffe26
commit c25967eff6
5 changed files with 67 additions and 79 deletions

View File

@ -111,11 +111,7 @@
});
$('#company-edit').click(function() {
launchModalForm(
"{% url 'company-edit' company.id %}",
{
reload: true
});
editCompany({{ company.id }});
});
$("#company-order-2").click(function() {

View File

@ -32,49 +32,16 @@
$('#new-company').click(function() {
var fields = companyFormFields();
// Field overrides
fields.is_supplier.value = {% if pagetype == 'suppliers' %}true{% else %}false{% endif %};
fields.is_manufacturer.value = {% if pagetype == 'manufacturers' %}true{% else %}false{% endif %};
fields.is_customer.value = {% if pagetype == 'customers' %}true{% else %}false{% endif %};
createCompany({
fields: {
is_supplier: {
value: {% if pagetype == 'suppliers' %}true{% else %}false{% endif %},
},
is_manufacturer: {
value: {% if pagetype == 'manufacturers' %}true{% else %}false{% endif %},
},
is_customer: {
value: {% if pagetype == 'customers' %}true{% else %}false{% endif %},
},
}
fields: fields,
});
return;
constructForm(
'{% url "api-company-list" %}',
{
method: 'POST',
title: '{% trans "Create new Company" %}',
follow: true,
fields: {
name: {},
description: {},
website: {},
address: {},
currency: {},
phone: {},
email: {},
contact: {},
is_supplier: {
value: {% if pagetype == 'suppliers' %}true{% else %}false{% endif %},
},
is_manufacturer: {
value: {% if pagetype == 'manufacturers' %}true{% else %}false{% endif %},
},
is_customer: {
value: {% if pagetype == 'customers' %}true{% else %}false{% endif %},
},
}
}
);
});
loadCompanyTable("#company-table", "{% url 'api-company-list' %}",

View File

@ -246,23 +246,11 @@ class CompanyImage(AjaxUpdateView):
}
class CompanyEdit(AjaxUpdateView):
""" View for editing a Company object """
model = Company
form_class = EditCompanyForm
context_object_name = 'company'
ajax_template_name = 'modal_form.html'
ajax_form_title = _('Edit Company')
permission_required = 'company.change_company'
def get_data(self):
return {
'info': _('Edited company information'),
}
class CompanyCreate(AjaxCreateView):
""" View for creating a new Company object """
"""
View for creating a new Company object
"""
model = Company
context_object_name = 'company'
form_class = EditCompanyForm

View File

@ -1,5 +1,52 @@
{% load i18n %}
// Returns a default form-set for creating / editing a Company object
function companyFormFields(options={}) {
return {
name: {},
description: {},
website: {
icon: 'fa-globe',
},
address: {
icon: 'fa-envelope',
},
currency: {
icon: 'fa-dollar-sign',
},
phone: {
icon: 'fa-phone',
},
email: {
icon: 'fa-at',
},
contact: {
icon: 'fa-address-card',
},
is_supplier: {},
is_manufacturer: {},
is_customer: {}
};
}
function editCompany(pk, options={}) {
var fields = options.fields || companyFormFields();
constructForm(
`/api/company/${pk}/`,
{
method: 'PATCH',
fields: fields,
reload: true,
title: '{% trans "Edit Company" %}',
}
);
};
/*
* Launches a form to create a new company.
* As this can be called from many different contexts,
@ -8,22 +55,7 @@
function createCompany(options={}) {
// Default field set
var fields = {
name: {},
description: {},
website: {},
address: {},
currency: {},
phone: {},
email: {},
contact: {},
is_supplier: {},
is_manufacturer: {},
is_customer: {}
};
// Override / update default fields as required
fields = Object.assign(fields, options.fields || {});
var fields = options.fields || companyFormFields();
constructForm(
'{% url "api-company-list" %}',

View File

@ -287,7 +287,12 @@ function constructFormBody(fields, options) {
fields[field].onEdit = field_options.onEdit;
// Field prefix
fields[field].prefix = field_options.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>`;
}
// // Field value?
// if (fields[field].value == null) {