Skip to content

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

Cleanup code and add checked rows highlight

dominik-zeglend153837d15c2
diff --git a/saleor/static/dashboard/js/components/bulk-actions.js b/saleor/static/dashboard/js/components/bulk-actions.js
index 4823cab1c8..70ec02789e 100644
--- a/saleor/static/dashboard/js/components/bulk-actions.js
+++ b/saleor/static/dashboard/js/components/bulk-actions.js
@@ -1,3 +1,14 @@
+/**
+ * This function adds .highlight class on checked table rows on page load.
+ * Fixes bug when user checks some items, goes to another page, then goes back
+ * in browsing history and some rows are checked but have no .highlight class.
+ */
+function initCheckedRows () {
+ $('.select-item:checked:not(#select-all-products)').each((itemIndex, item) => {
+ $(item).parent().parent().addClass('highlight');
+ })
+}
+
function moveSelectAllCheckbox(bulk, selectAllState) {
const $selectAll = $('#select-all-products').parent()[0].innerHTML;
const $headerContainer = $('thead .bulk-checkbox');
@@ -13,22 +24,22 @@ function moveSelectAllCheckbox(bulk, selectAllState) {
$('.select-all').on('change', onSelectAll);
}
-function updateSelectedItemsText (selectAllState) {
- if ([true, false].indexOf(selectAllState) === -1) {
- selectAllState = false;
- }
+function onItemSelect (e) {
const count = $('.select-item:checked').length - $('#select-all-products:checked').length;
- const $counterTextNode = $('.data-table-bulk-actions__selected-items');
- const $header = $('.data-table-bulk-actions');
- const counterText = ngettext('item selected', 'items selected', count);
+ const maxCount = $('.select-item').length - 1;
+ const $target = $(e.currentTarget);
+ $target.parent().parent().toggleClass('highlight', $target.checked);
+ updateSelectedItemsText(count === maxCount);
+}
- if (!count) {
- $header.addClass('single').removeClass('bulk');
- moveSelectAllCheckbox(false, selectAllState);
- } else {
- $counterTextNode.html(`${count} ${counterText}`);
- $header.addClass('bulk').removeClass('single');
- moveSelectAllCheckbox(true, selectAllState);
+function onPageInit () {
+ if (document.querySelector('#bulk-action')) {
+ initCheckedRows();
+ updateSelectedItemsText();
+ $('.select-all').on('click', onSelectAll);
+ $('.select-item').on('change', onItemSelect);
+ $('.data-table-bulk-actions__action-choice a').on('click', onSubmit);
+ $('.data-table-bulk-actions__dropdown-container .dropdown-content a').on('click', onSubmit);
}
}
@@ -37,22 +48,30 @@ function onSelectAll (e) {
const $targetForm = $target.parents('form');
const $items = $targetForm.find('.switch-actions');
$items.prop('checked', $target[0].checked);
- console.log($target[0].checked);
$target.off('change');
updateSelectedItemsText($target[0].checked);
}
function onSubmit (e) {
- e.preventDefault();
const a = $(e.currentTarget);
+ e.preventDefault();
$('#bulk-action').val(a.attr('data-action'));
$('#bulk-actions-form').submit();
}
+function updateSelectedItemsText (selectAllState) {
+ const count = $('.select-item:checked').length - $('#select-all-products:checked').length;
+ const $counterTextNode = $('.data-table-bulk-actions__selected-items');
+ const $header = $('.data-table-bulk-actions');
+ const counterText = ngettext('item selected', 'items selected', count);
+
+ if (count) {
+ $counterTextNode.html(`${count} ${counterText}`);
+ }
+ $header.toggleClass('show', count > 0);
+ moveSelectAllCheckbox(count > 0, selectAllState);
+}
+
// -----
-updateSelectedItemsText();
-$('.select-all').on('click', onSelectAll);
-$('.select-item').on('change', updateSelectedItemsText);
-$('.data-table-bulk-actions__action-choice a').on('click', onSubmit);
-$('.data-table-bulk-actions__dropdown-container .dropdown-content a').on('click', onSubmit);
+onPageInit();
diff --git a/saleor/static/dashboard/scss/components/_data-table.scss b/saleor/static/dashboard/scss/components/_data-table.scss
index 82ef96af75..e2b9705974 100644
--- a/saleor/static/dashboard/scss/components/_data-table.scss
+++ b/saleor/static/dashboard/scss/components/_data-table.scss
@@ -32,26 +32,13 @@
height: 56px;
overflow: visible;
}
- &.bulk {
- background: #fff;
- color: $off-black;
- display: block;
- }
- &.single {
- background: #fff;
- .show-single {
- display: block;
- }
- th, a {
- color: $secondary-color;
- }
- }
a {
color: $secondary-color;
}
- &__selected-items {
- margin-left: -10px;
- line-height: 56px;
+ &.show {
+ background: #fff;
+ color: $off-black;
+ display: block;
}
&__action-choice {
width: calc(100% - 60px);
@@ -89,6 +76,10 @@
margin: 6px 8px 0 24px;
}
}
+ &__selected-items {
+ margin-left: -10px;
+ line-height: 56px;
+ }
}
&-header {
@@ -135,14 +126,17 @@
}
tr {
+ &.highlight {
+ background-color: #f8f8f8;
+ }
&.last-no-border {
&:last-of-type {
border-bottom: none;
}
}
&[data-action-go] > td:not(.ignore-link):hover {
+ cursor: pointer;
}
- cursor: pointer;
}
.danger {
diff --git a/templates/dashboard/product/_bulk_actions_bar.html b/templates/dashboard/product/_bulk_actions_bar.html
index ac260d983d..88779dd0a8 100644
--- a/templates/dashboard/product/_bulk_actions_bar.html
+++ b/templates/dashboard/product/_bulk_actions_bar.html
@@ -1,6 +1,6 @@
{% load static %}
-<nav class="data-table-bulk-actions single">
+<nav class="data-table-bulk-actions">
<div class="data-table-bulk-actions__select-all"></div>
<div class="data-table-bulk-actions__selected-items col inline"></div>
<div class="data-table-bulk-actions__action-choice">