Simplified custom button text for modal forms

- Now only configured from the javascript end
- No server-side configuration of button text
This commit is contained in:
Oliver Walters 2019-04-13 23:02:23 +10:00
parent 0e7472dabc
commit e6e03963c4
8 changed files with 24 additions and 19 deletions

View File

@ -56,7 +56,6 @@ class AjaxMixin(object):
ajax_form_action = ''
ajax_form_title = ''
ajax_submit_text = 'Submit'
def get_data(self):
return {}
@ -76,8 +75,6 @@ class AjaxMixin(object):
data['title'] = self.ajax_form_title
data['submit_label'] = self.ajax_submit_text
data['html_form'] = render_to_string(
self.getAjaxTemplate(),
context,

View File

@ -109,7 +109,8 @@
$("#build-cancel").click(function() {
launchModalForm("{% url 'build-cancel' build.id %}",
{
reload: true
reload: true,
submit_text: "Cancel",
});
});
{% endblock %}

View File

@ -40,7 +40,6 @@ class BuildCancel(AjaxView):
model = Build
template_name = 'build/cancel.html'
ajax_form_title = 'Cancel Build'
ajax_submit_text = 'Cancel'
context_object_name = 'build'
fields = []

View File

@ -57,8 +57,9 @@ function modalSetContent(modal, content='') {
$(modal).find('.modal-form-content').html(content);
}
function modalSetButtonText(modal, text) {
$(modal).find("#modal-form-submit").html(text);
function modalSetButtonText(modal, submit_text, close_text) {
$(modal).find("#modal-form-submit").html(submit_text);
$(modal).find("#modal-form-close").html(close_text);
}
function closeModal(modal='#modal-form') {
@ -102,9 +103,11 @@ function openModal(options) {
modalSetContent(modal, options.content);
}
if (options.submit_label) {
modalSetButtonText(modal, options.submit_label);
}
// Default labels for 'Submit' and 'Close' buttons in the form
var submit_text = options.submit_text || 'Submit';
var close_text = options.close_text || 'Close';
modalSetButtonText(modal, submit_text, close_text);
$(modal).modal({
backdrop: 'static',
@ -243,23 +246,27 @@ function launchModalForm(url, options = {}) {
var modal = options.modal || '#modal-form';
// Default labels for 'Submit' and 'Close' buttons in the form
var submit_text = options.submit_text || 'Submit';
var close_text = options.close_text || 'Close';
// Form the ajax request to retrieve the django form data
ajax_data = {
url: url,
type: 'get',
dataType: 'json',
beforeSend: function () {
openModal({modal: modal});
openModal({
modal: modal,
submit_text: submit_text,
close_text: close_text,
});
},
success: function(response) {
if (response.title) {
modalSetTitle(modal, response.title);
}
if (response.submit_label) {
modalSetButtonText(modal, String(response.submit_label));
}
if (response.html_form) {
injectModalForm(modal, response.html_form);
handleModalForm(url, options);

View File

@ -207,7 +207,7 @@ function moveStockItems(items, options) {
openModal({
modal: modal,
title: "Move " + items.length + " stock items",
submit_label: "Move"
submit_text: "Move"
});
// Extact part row info

View File

@ -135,7 +135,8 @@
launchModalForm(
"{% url 'stock-item-edit' item.id %}",
{
reload: true
reload: true,
submit_text: "Save",
});
});
@ -145,6 +146,7 @@
"{% url 'stock-item-move' item.id %}",
{
reload: true,
submit_text: "Move"
});
});

View File

@ -134,7 +134,6 @@ class StockItemMove(AjaxUpdateView):
template_name = 'modal_form.html'
context_object_name = 'item'
ajax_form_title = 'Move Stock Item'
ajax_submit_text = 'Move'
form_class = MoveStockItemForm
def post(self, request, *args, **kwargs):

View File

@ -10,7 +10,7 @@
<div class='modal-form-content'>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
<button type='button' class='btn btn-default' id='modal-form-close' data-dismiss='modal'>Close</button>
<button type='button' class='btn btn-primary' id='modal-form-submit'>Submit</button>
</div>
</div>