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() {
launchModalForm("{% url 'stock-item-create' %}", {
reload: true,
createNewStockItem({
data: {
part: {{ part.part.id }},
supplier_part: {{ part.id }},
},
secondary: [
{
field: 'location',
label: '{% trans "New Location" %}',
title: '{% trans "Create New Location" %}',
url: "{% url 'stock-location-create' %}",
}
]
reload: true,
});
return false;
});

View File

@ -25,14 +25,12 @@
{{ block.super }}
$('#add-stock-item').click(function () {
launchModalForm(
"{% url 'stock-item-create' %}",
{
reload: true,
data: {
part: {{ part.id }}
}
});
createNewStockItem({
reload: true,
data: {
part: {{ part.id }},
}
});
});
loadStockTable($("#stock-table"), {
@ -64,37 +62,12 @@
});
$('#item-create').click(function () {
launchModalForm("{% url 'stock-item-create' %}", {
createNewStockItem({
reload: true,
data: {
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' %}",
}
]
part: {{ part.id }},
}
});
return false;
});
{% endblock %}

View File

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

View File

@ -204,59 +204,13 @@
{% endif %}
$('#item-create').click(function () {
launchModalForm("{% url 'stock-item-create' %}",
{
follow: true,
data: {
{% if location %}
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;
createNewStockItem({
data: {
{% if location %}
location: {{ location.id }}
{% endif %}
}
});
});
loadStockTable($("#stock-table"), {

View File

@ -734,4 +734,60 @@ function loadStockTrackingTable(table, options) {
reload: true,
});
});
}
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);
}