8000 feat: send post request if toolbar button has `cms-form-post-method` class by fsbraun · Pull Request #7890 · django-cms/django-cms · GitHub
[go: up one dir, main page]

Skip to content

feat: send post request if toolbar button has cms-form-post-method class #7890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cms/static/cms/js/admin/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function($) {
$(function() {
// INFO: it is not possible to put a form inside a form, so
// the versioning actions have to create their own form on click.
// the actions have to create their own form on click.
// Note for any apps inheriting the burger menu, this will also capture those events.

function closeSideFrame() {
Expand Down
41 changes: 31 additions & 10 deletions cms/static/cms/js/modules/cms.toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ var Toolbar = new Class({
var link = $(el);

// in case the button has a data-rel attribute
if (link.attr('data-rel')) {
if (link.attr('data-rel') || link.hasClass('cms-form-post-method')) {
link.off(that.click).on(that.click, function(e) {
e.preventDefault();
that._delegate($(this));
Expand Down Expand Up @@ -586,23 +586,44 @@ var Toolbar = new Class({
case 'sideframe':
// If the sideframe is enabled, show it
if (typeof CMS.settings.sideframe_enabled === 'undefined' || CMS.settings.sideframe_enabled) {
var sideframe = CMS.API.Sideframe || new Sideframe({
onClose: el.data('on-close')
});

sideframe.open({
url: el.attr('href'),
animate: true
});
this._openSideFrame(el);
break;
}
// Else fall through to default, the sideframe is disabled

default:
Helpers._getWindow().location.href = el.attr('href');
if (el.hasClass('cms-form-post-method')) {
this._sendPostRequest(el);
} else {
Helpers._getWindow().location.href = el.attr('href');
}
}
},

_openSideFrame: function _openSideFrame(el) {
var sideframe = CMS.API.Sideframe || new Sideframe({
onClose: el.data('on-close')
});

sideframe.open({
url: el.attr('href'),
animate: true
});
},

_sendPostRequest: function _sendPostRequest(el) {
/* Allow post method to be used */
var formToken = document.querySelector('form input[name="csrfmiddlewaretoken"]');
var csrfToken = '<input type="hidden" name="csrfmiddlewaretoken" value="' +
((formToken ? formToken.value : formToken) || window.CMS.config.csrf) + '">';
var fakeForm = $(
'<form style="display: none" action="' + el.attr('href') + '" method="POST">' + csrfToken +
'</form>'
);

fakeForm.appendTo(Helpers._getWindow().document.body).submit();
},

/**
* Handles the debug bar when `DEBUG=true` on top of the toolbar.
*
Expand Down
6 changes: 4 additions & 2 deletions 10000 cms/tests/frontend/unit/cms.messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ describe('CMS.Messages', function() {
it('has options', function() {
expect(messages.options).toEqual({
messageDuration: 300,
messageDelay: 3000
messageDelay: 3000,
messageLength: 160
});
messages = new CMS.Messages({
messageDuration: 100,
messageDelay: 0
});
expect(messages.options).toEqual({
messageDuration: 100,
messageDelay: 0
messageDelay: 0,
messageLength: 160
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion docs/how_to/13-toolbar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Action Text link variant
Open link :meth:`~cms.toolbar.items.ToolbarAPIMixin.add_link_item` :meth:`~cms.toolbar.toolbar.CMSToolbar.add_button`
Open link in sideframe :meth:`~cms.toolbar.items.ToolbarAPIMixin.add_sideframe_item` :meth:`~cms.toolbar.toolbar.CMSToolbar.add_sideframe_button`
Open link in modal :meth:`~cms.toolbar.items.ToolbarAPIMixin.add_modal_item` :meth:`~cms.toolbar.toolbar.CMSToolbar.add_modal_button`
POST action :meth:`~cms.toolbar.items.ToolbarAPIMixin.add_ajax_item`
Ajax POST action :meth:`~cms.toolbar.items.ToolbarAPIMixin.add_ajax_item`
====================== ============================================================= ===========================================================

The basic form for using any of these is:
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0