From b4ff8f79f0420eb211b10e2fe0eb0dd8a62362f6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 26 Apr 2018 22:38:48 +1000 Subject: [PATCH] Fixed position header + footer for modals - Fun with CSS! - Header title provided by AJAX data - Footer static - Submit button text can be customized via JSON dat --- InvenTree/InvenTree/views.py | 9 +++--- InvenTree/static/css/inventree.css | 7 ++++- InvenTree/static/script/modal_form.js | 40 +++++++++++++++++---------- InvenTree/templates/modal.html | 12 ++++++++ InvenTree/templates/modal_form.html | 18 ++---------- 5 files changed, 51 insertions(+), 35 deletions(-) diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 07a09da1c4..4ea1c09b97 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -23,12 +23,13 @@ class AjaxView(object): def renderJsonResponse(self, request, form, data={}): - context = {'form': form, - 'form_action': self.ajax_form_action, - 'form_title': self.ajax_form_title, - 'submit_text': self.ajax_submit_text, + context = {'form': form } + data['title'] = self.ajax_form_title + + data['submit_text'] = self.ajax_submit_text + data['html_form'] = render_to_string( self.getAjaxTemplate(), context, diff --git a/InvenTree/static/css/inventree.css b/InvenTree/static/css/inventree.css index 4e8d4fa883..5201ca555a 100644 --- a/InvenTree/static/css/inventree.css +++ b/InvenTree/static/css/inventree.css @@ -40,7 +40,12 @@ overflow: hidden; } -.modal-content { +.modal-content h3 { + margin-top: 3px; + margin-bottom: 3px; +} + +.modal-form-content { border-radius: 0; position:relative; height: auto !important; diff --git a/InvenTree/static/script/modal_form.js b/InvenTree/static/script/modal_form.js index c581f669a8..1723bc7868 100644 --- a/InvenTree/static/script/modal_form.js +++ b/InvenTree/static/script/modal_form.js @@ -1,7 +1,17 @@ +function attachSelect(modal) { + + // Attach to any 'select' inputs on the modal + // Provide search filtering of dropdown items + $(modal + ' .select').select2({ + dropdownParent: $(modal), + dropdownAutoWidth: true, + }); +} + function launchModalForm(modal, url, data) { $(modal).on('shown.bs.modal', function () { - $(modal + ' .modal-content').scrollTop(0); // animate({ scrollTop: 0 }, 'fast'); + $(modal + ' .modal-form-content').scrollTop(0); }); $.ajax({ @@ -13,15 +23,17 @@ function launchModalForm(modal, url, data) { $(modal).modal('show'); }, success: function(response) { + if (response.title) { + $(modal + ' #modal-title').html(response.title); + } + if (response.submit_text) { + $(modal + ' #modal-form-submit').html(response.submit_text); + } if (response.html_form) { - var target = modal + ' .modal-content'; + var target = modal + ' .modal-form-content'; $(target).html(response.html_form); - var select = modal + ' .select'; - $(select).select2({ - dropdownParent: $(modal), - dropdownAutoWidth: true - }); + attachSelect(modal); } else { alert('JSON response missing form data'); @@ -34,8 +46,10 @@ function launchModalForm(modal, url, data) { } }); - $(modal).on('submit', '.js-modal-form', function() { - var form = $(this); + $(modal).on('click', '#modal-form-submit', function() { + + // Extract the form from the modal dialog + var form = $(modal).find(".js-modal-form"); $.ajax({ url: url, @@ -48,14 +62,10 @@ function launchModalForm(modal, url, data) { $(modal).modal('hide'); } else if (response.html_form) { - var target = modal + ' .modal-content'; + var target = modal + ' .modal-form-content'; $(target).html(response.html_form); - var select = modal + ' .select'; - $(select).select2({ - dropdownParent: $(modal), - dropdownAutoWidth: true - }); + attachSelect(modal); } else { alert('JSON response missing form data'); diff --git a/InvenTree/templates/modal.html b/InvenTree/templates/modal.html index 393af246be..5c270c8f61 100644 --- a/InvenTree/templates/modal.html +++ b/InvenTree/templates/modal.html @@ -1,6 +1,18 @@ \ No newline at end of file diff --git a/InvenTree/templates/modal_form.html b/InvenTree/templates/modal_form.html index 5140c08a86..35032be825 100644 --- a/InvenTree/templates/modal_form.html +++ b/InvenTree/templates/modal_form.html @@ -1,17 +1,5 @@ -
+ {% csrf_token %} - - - + {% load crispy_forms_tags %} + {% crispy form %}
\ No newline at end of file