Toot toot - it's the refactor tractor!

- New function for launching a CreateStockItem form
- Wraps up the previous code improvements into a single function
- Oh, the ease!
This commit is contained in:
Oliver Walters 2020-08-27 00:08:02 +10:00
parent 4be1b2928b
commit 146dae6d43
5 changed files with 79 additions and 109 deletions

View File

@ -44,23 +44,13 @@
}); });
$("#item-create").click(function() { $("#item-create").click(function() {
launchModalForm("{% url 'stock-item-create' %}", { createNewStockItem({
reload: true,
data: { data: {
part: {{ part.part.id }}, part: {{ part.part.id }},
supplier_part: {{ part.id }}, supplier_part: {{ part.id }},
}, },
secondary: [ reload: true,
{
field: 'location',
label: '{% trans "New Location" %}',
title: '{% trans "Create New Location" %}',
url: "{% url 'stock-location-create' %}",
}
]
}); });
return false;
}); });

View File

@ -25,14 +25,12 @@
{{ block.super }} {{ block.super }}
$('#add-stock-item').click(function () { $('#add-stock-item').click(function () {
launchModalForm( createNewStockItem({
"{% url 'stock-item-create' %}", reload: true,
{ data: {
reload: true, part: {{ part.id }},
data: { }
part: {{ part.id }} });
}
});
}); });
loadStockTable($("#stock-table"), { loadStockTable($("#stock-table"), {
@ -64,37 +62,12 @@
}); });
$('#item-create').click(function () { $('#item-create').click(function () {
launchModalForm("{% url 'stock-item-create' %}", { createNewStockItem({
reload: true, reload: true,
data: { data: {
part: {{ part.id }} part: {{ part.id }},
}, }
secondary: [
{
field: 'part',
label: '{% trans "New Part" %}',
title: '{% trans "Create New Part" %}',
url: "{% url 'part-create' %}",
},
{
field: 'supplier_part',
label: '{% trans "New Supplier Part" %}',
title: '{% trans "Create new Supplier Part" %}',
url: "{% url 'supplier-part-create' %}",
data: {
part: {{ part.id }}
},
},
{
field: 'location',
label: '{% trans "New Location" %}',
title: '{% trans "Create New Location" %}',
url: "{% url 'stock-location-create' %}",
}
]
}); });
return false;
}); });
{% endblock %} {% endblock %}

View File

@ -320,15 +320,12 @@ $("#print-label").click(function() {
}); });
$("#stock-duplicate").click(function() { $("#stock-duplicate").click(function() {
launchModalForm( createNewStockItem({
"{% url 'stock-item-create' %}", follow: true,
{ data: {
follow: true, copy: {{ item.id }},
data: {
copy: {{ item.id }},
},
} }
); });
}); });
$("#stock-edit").click(function () { $("#stock-edit").click(function () {

View File

@ -204,59 +204,13 @@
{% endif %} {% endif %}
$('#item-create').click(function () { $('#item-create').click(function () {
launchModalForm("{% url 'stock-item-create' %}", createNewStockItem({
{ data: {
follow: true, {% if location %}
data: { location: {{ location.id }}
{% if location %} {% endif %}
location: {{ location.id }} }
{% endif %} });
},
callback: [
{
field: 'part',
action: function(value) {
reloadFieldOptions(
'supplier_part',
{
url: "{% url 'api-supplier-part-list' %}",
params: {
part: value,
pretty: true,
},
text: function(item) {
return item.pretty_name;
}
}
)
}
},
],
secondary: [
{
field: 'part',
label: 'New Part',
title: 'Create New Part',
url: "{% url 'part-create' %}",
},
{
field: 'supplier_part',
label: 'New Supplier Part',
title: 'Create new Supplier Part',
url: "{% url 'supplier-part-create' %}"
},
{
field: 'location',
label: 'New Location',
title: 'Create New Location',
url: "{% url 'stock-location-create' %}",
}
]
});
return false;
}); });
loadStockTable($("#stock-table"), { loadStockTable($("#stock-table"), {

View File

@ -735,3 +735,59 @@ function loadStockTrackingTable(table, options) {
}); });
}); });
} }
function createNewStockItem(options) {
/* Launch a modal form to create a new stock item.
*
* This is really just a helper function which calls launchModalForm,
* but it does get called a lot, so here we are ...
*/
// Add in some funky options
options.callback = [
{
field: 'part',
action: function(value) {
reloadFieldOptions(
'supplier_part',
{
url: "{% url 'api-supplier-part-list' %}",
params: {
part: value,
pretty: true,
},
text: function(item) {
return item.pretty_name;
}
}
);
}
},
];
options.secondary = [
{
field: 'part',
label: '{% trans "New Part" %}',
title: '{% trans "Create New Part" %}',
url: "{% url 'part-create' %}",
},
{
field: 'supplier_part',
label: '{% trans "New Supplier Part" %}',
title: '{% trans "Create new Supplier Part" %}',
url: "{% url 'supplier-part-create' %}"
},
{
field: 'location',
label: '{% trans "New Location" %}',
title: '{% trans "Create New Location" %}',
url: "{% url 'stock-location-create' %}",
},
];
launchModalForm("{% url 'stock-item-create' %}", options);
}