Skip to content

Commit

Permalink
Allow actions from different views on same page
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosenpfand committed Oct 28, 2023
1 parent 1c3af9b commit 3b91a7f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
15 changes: 5 additions & 10 deletions flask_appbuilder/static/appbuilder/js/ab_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//------------------------------------------------------
// AdminActions holds methods to handle UI for actions
//------------------------------------------------------
var AdminActions = function() {
var AdminActions = function(form_id) {

var chkAllFlag = true;
var multiple = false;
Expand Down Expand Up @@ -37,7 +37,7 @@ var AdminActions = function() {
};

function single_form_submit() {
form = $('#action_form');
form = $('#' + form_id);
$(form).attr('action', action_url);
form.trigger("submit");
return false;
Expand Down Expand Up @@ -66,7 +66,7 @@ var AdminActions = function() {

function form_submit() {
// Update hidden form and submit it
var form = $('#action_form');
var form = $('#' + form_id);
$('#action', form).val(action_name);

$('input.action_check', form).remove();
Expand Down Expand Up @@ -110,13 +110,8 @@ var AdminActions = function() {
}
// POST for delete endpoint necessary to send CSRF token from list view
if (single_delete) {
var form = undefined;
if ( $('#action_form').length ) {
form = $('#action_form');
}
else {
form = $('#delete_form');
} $(form).attr('action', action_url);
form = $('#' + form_id);
$(form).attr('action', action_url);
form.trigger('submit');
return false;
}
Expand Down
21 changes: 11 additions & 10 deletions flask_appbuilder/templates/appbuilder/general/lib.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% set endpoint = modelview_name + '.action' %}
{% set path = endpoint | safe_url_for(name = action.name, pk = pk) %}
{% if path %}
<form id="action_form" action="{{ path }}" method="POST" style="display: none">
<form id="action_form_{{ modelview_name }}" action="{{ path }}" method="POST" style="display: none">
{% if csrf_token is defined and csrf_token %}
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
{% endif %}
Expand All @@ -35,10 +35,10 @@
document.getElementById("btn-action-{{ endpoint }}-{{ action.name }}")
.addEventListener("click", function () {
{% if action.confirmation %}
const adminAction = new AdminActions();
const adminAction = new AdminActions('action_form_{{ modelview_name }}');
return adminAction.execute_single('{{path}}','{{action.confirmation}}');
{% else %}
const adminAction = new AdminActions();
const adminAction = new AdminActions('action_form_{{ modelview_name }}');
return adminAction.execute_single('{{path}}',false);
{% endif %}
})
Expand All @@ -52,15 +52,15 @@
{% set endpoint = modelview_name + '.action_post' %}
{% set path = endpoint | safe_url_for %}
{% if path %}
<form id="action_form" action="{{ path }}" method="POST" style="display: none">
<form id="action_form_{{ modelview_name }}" action="{{ path }}" method="POST" style="display: none">
{% if csrf_token is defined and csrf_token %}
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
{% endif %}
<input type="hidden" id="action" name="action" />
</form>
{% endif %}
{% else %}
<form id="delete_form" action="" method="POST" style="display: none">
<form id="action_form_{{ modelview_name }}" action="" method="POST" style="display: none">
{% if csrf_token is defined and csrf_token %}
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
{% endif %}
Expand Down Expand Up @@ -93,9 +93,9 @@
{% set action = actions.get(action_key) %}
$('.{{action.name}}_menu_item').on('click', function(){
{% if action.confirmation %}
return modelActions.execute_multiple('{{action.name}}','{{action.confirmation}}');
return modelActions{{ modelview_name }}.execute_multiple('{{action.name}}','{{action.confirmation}}');
{% else %}
return modelActions.execute_multiple('{{action.name}}', false);
return modelActions{{ modelview_name }}.execute_multiple('{{action.name}}', false);
{% endif %}
})
{% endfor %}
Expand Down Expand Up @@ -356,7 +356,7 @@ <h4 class="panel-title">
{% set endpoint = modelview_name + '.delete' %}
{% set path = endpoint | safe_url_for(pk=pk) %}
{% if path %}
{{ lnk_delete(path) }}
{{ lnk_delete(path, modelview_name) }}
{% endif %}
{% endif %}
</div>
Expand Down Expand Up @@ -401,7 +401,8 @@ <h4 class="panel-title">
</a>
{% endmacro %}

{% macro lnk_delete(my_href) %}
{% macro lnk_delete(my_href, modelview_name) %}
{% set path = endpoint | safe_url_for(pk=pk) %}
<a
id="btn-delete-{{ my_href }}"
href="#"
Expand All @@ -415,7 +416,7 @@ <h4 class="panel-title">
<script nonce="{{ baselib.get_nonce() }}">
document.getElementById("btn-delete-{{ my_href }}")
.addEventListener("click", function () {
const adminAction = new AdminActions();
const adminAction = new AdminActions('action_form_{{ modelview_name }}');
return adminAction.execute_single_delete('{{my_href}}','{{_('Are you sure you want to delete this item?')}}');
})
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<script nonce="{{ baselib.get_nonce() }}">
$(document).ready(function() {
window.modelActions = new AdminActions();
window.modelActions{{ modelview_name }} = new AdminActions('action_form_{{ modelview_name }}');
});
</script>

Expand Down

0 comments on commit 3b91a7f

Please sign in to comment.