Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Refactor bulk actions code

dominik-zeglen807a669e1388
diff --git a/saleor/static/dashboard/js/components/bulk-actions.js b/saleor/static/dashboard/js/components/bulk-actions.js
index 53f6e3646a..bc05a2c9b2 100644
--- a/saleor/static/dashboard/js/components/bulk-actions.js
+++ b/saleor/static/dashboard/js/components/bulk-actions.js
@@ -1,4 +1,4 @@
-function updateHeader () {
+function updateSelectedItemsText () {
const count = $('.select-item:checked').length - $('#select-all-products:checked').length;
const $counterTextNode = $('.data-table-header-action-selected-items');
const $header = $('.data-table-header-alternative');
@@ -12,33 +12,45 @@ function updateHeader () {
}
}
-$(document).ready((e) => {
- updateHeader();
- $('.select-all').on('change', function () {
- let $items = $(this).parents('form').find('.switch-actions');
- if (this.checked) {
- $items.prop('checked', true);
- } else {
- $items.prop('checked', false);
- }
- updateHeader();
- });
- $('.select-item').on('change', updateHeader);
- $('.switch-actions').on('change', function () {
- let $btnChecked = $(this).parents('form').find('.btn-show-when-checked');
- let $btnUnchecked = $(this).parents('form').find('.btn-show-when-unchecked');
- if ($(this).parents('form').find('.switch-actions:checked').length) {
- $btnChecked.show();
- $btnUnchecked.hide();
- } else {
- $btnUnchecked.show();
- $btnChecked.hide();
- }
- });
- $('.bulk-actions a').on('click', (e) => {
- e.preventDefault();
- const a = $(e.currentTarget);
- $('#bulk-action').val(a.attr('data-action'));
- $('#bulk-actions-form').submit();
+function onSwitchActions (e) {
+ const $target = $(e.currentTarget);
+ const $targetForm = $target.parents('form');
+ const $btnChecked = $targetForm.find('.btn-show-when-checked');
+ const $btnUnchecked = $targetForm.find('.btn-show-when-unchecked');
+ if ($targetForm.find('.switch-actions:checked').length) {
+ $btnChecked.show();
+ $btnUnchecked.hide();
+ } else {
+ $btnUnchecked.show();
+ $btnChecked.hide();
+ }
+}
+
+function onSelectAll (e) {
+ const $target = $(e.currentTarget);
+ const $targetForm = $target.parents('form');
+ const $items = $targetForm.find('.switch-actions');
+ $items.prop('checked', $target[0].checked);
+ updateSelectedItemsText();
+}
+
+function onSubmit (e) {
+ e.preventDefault();
+ const a = $(e.currentTarget);
+ $('#bulk-action').val(a.attr('data-action'));
+ $('#bulk-actions-form').submit();
+}
+
+function init () {
+ return $(document).ready(() => {
+ updateSelectedItemsText();
+ $('.select-all').on('change', onSelectAll);
+ $('.switch-actions').on('change', onSwitchActions);
+ $('.select-item').on('change', updateSelectedItemsText);
+ $('.bulk-actions a').on('click', onSubmit);
});
-});
+}
+
+export {
+ init as default,
+};