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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,7 +10,7 @@
<div class='modal-form-content'> <div class='modal-form-content'>
</div> </div>
<div class='modal-footer'> <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> <button type='button' class='btn btn-primary' id='modal-form-submit'>Submit</button>
</div> </div>
</div> </div>