From bc3dca3abaa8c5c51a6dcd372728f60c3c9f2ac3 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 27 Apr 2018 20:42:12 +1000 Subject: [PATCH] Modal for edit and delete part category --- .../part/templates/part/category_delete.html | 62 ++++++++----------- .../part/templates/part/category_detail.html | 26 +++++--- InvenTree/part/views.py | 12 ++-- InvenTree/static/script/modal_form.js | 19 ++++-- 4 files changed, 61 insertions(+), 58 deletions(-) diff --git a/InvenTree/part/templates/part/category_delete.html b/InvenTree/part/templates/part/category_delete.html index 7814317356..8b6d5618f3 100644 --- a/InvenTree/part/templates/part/category_delete.html +++ b/InvenTree/part/templates/part/category_delete.html @@ -1,41 +1,33 @@ -{% extends 'delete_obj.html' %} - -{% block del_title %} Are you sure you want to delete category '{{ category.name }}'? -{% endblock %} -{% block del_body %} +{% if category.children.all|length > 0 %} +

This category contains {{ category.children.all|length }} child categories.
+If this category is deleted, these child categories will be moved to +{% if category.parent %} +the '{{ category.parent.name }}' category. +{% else %} +the top level 'Parts' category. +{% endif %} +

- {% if category.children.all|length > 0 %} -

This category contains {{ category.children.all|length }} child categories.
- If this category is deleted, these child categories will be moved to +

+{% endif %} + +{% if category.parts.all|length > 0 %} +

This category contains {{ category.parts.all|length }} parts.
{% if category.parent %} - the '{{ category.parent.name }}' category. + If this category is deleted, these parts will be moved to the parent category '{{ category.parent.pathstring }}' {% else %} - the top level 'Parts' category. + If this category is deleted, these parts will be moved to the top-level category 'Parts' {% endif %} -

- - - {% endif %} - - {% if category.parts.all|length > 0 %} -

This category contains {{ category.parts.all|length }} parts.
- {% if category.parent %} - If this category is deleted, these parts will be moved to the parent category '{{ category.parent.pathstring }}' - {% else %} - If this category is deleted, these parts will be moved to the top-level category 'Parts' - {% endif %} -

- - {% endif %} - -{% endblock %} \ No newline at end of file +

+ +{% endif %} \ No newline at end of file diff --git a/InvenTree/part/templates/part/category_detail.html b/InvenTree/part/templates/part/category_detail.html index 88325bc82f..20e154dd8d 100644 --- a/InvenTree/part/templates/part/category_detail.html +++ b/InvenTree/part/templates/part/category_detail.html @@ -25,19 +25,14 @@ - - - - - - - - - + + + {% include 'modal.html' %} +{% include 'modal_delete.html' %} {% endblock %} @@ -49,6 +44,19 @@ $(document).ready(function (){ $('#part-list').footable(); + $("#edit-category").click(function () { + launchModalForm("#modal-form", + "{% url 'category-edit' category.id %}", + {reload: true}, + ); + }); + + $("#delete-category").click(function() { + launchDeleteForm("#modal-delete", + "{% url 'category-delete' category.id %}", + {redirect: "{% url 'part-index' %}"}); + }); + $("#create-cat").click(function() { launchModalForm("#modal-form", "{% url 'category-create' %}", diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 7bddfa7c7a..97ad44a91e 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -116,10 +116,12 @@ class CategoryDetail(DetailView): template_name = 'part/category_detail.html' -class CategoryEdit(UpdateView): +class CategoryEdit(AjaxUpdateView): model = PartCategory template_name = 'part/category_edit.html' form_class = EditCategoryForm + ajax_template_name = 'modal_form.html' + ajax_form_title = 'Edit Part Category' def get_context_data(self, **kwargs): context = super(CategoryEdit, self).get_context_data(**kwargs).copy() @@ -129,18 +131,12 @@ class CategoryEdit(UpdateView): return context -class CategoryDelete(DeleteView): +class CategoryDelete(AjaxDeleteView): model = PartCategory template_name = 'part/category_delete.html' context_object_name = 'category' success_url = '/part/' - def post(self, request, *args, **kwargs): - if 'confirm' in request.POST: - return super(CategoryDelete, self).post(request, *args, **kwargs) - else: - return HttpResponseRedirect(self.get_object().get_absolute_url()) - class CategoryCreate(AjaxCreateView): model = PartCategory diff --git a/InvenTree/static/script/modal_form.js b/InvenTree/static/script/modal_form.js index d1bfb69805..98840d3998 100644 --- a/InvenTree/static/script/modal_form.js +++ b/InvenTree/static/script/modal_form.js @@ -51,9 +51,15 @@ function launchDeleteForm(modal, url, options = {}) { success: function (response) { $(modal).modal('hide'); - if (options.redirect) { + if (options.success) { + options.success(); + } + else if (options.redirect) { window.location.href = options.redirect; } + else if (options.reload) { + location.reload(); + } }, error: function (xhr, ajaxOptions, thrownError) { alert('Error deleting item:\n' + thrownError); @@ -123,14 +129,15 @@ function launchModalForm(modal, url, options = {}) { $(modal).modal('hide'); - if (options.redirect) { - window.location.href = options.redirect; - } - if (options.success) { options.success(); } - + else if (options.redirect) { + window.location.href = options.redirect; + } + else if (options.reload) { + location.reload(); + } } else if (response.html_form) { var target = modal + ' .modal-form-content';