Skip to content

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

Unify handling sortable items

Anna Siwik16053763d7d2
diff --git a/saleor/static/dashboard/js/components/image-gallery.js b/saleor/static/dashboard/js/components/image-gallery.js
index 9eb50b462b..4fc2c43a6f 100644
--- a/saleor/static/dashboard/js/components/image-gallery.js
+++ b/saleor/static/dashboard/js/components/image-gallery.js
@@ -8,8 +8,6 @@ function createLink (link, index, replacement) {
return outputLink.join('/');
}
-const productGallery = document.getElementById('product-gallery');
-
// -----
$('#product-image-form').dropzone({
@@ -39,26 +37,3 @@ $('#product-image-form').dropzone({
});
}
});
-
-if (productGallery) {
- Sortable.create(productGallery, {
- handle: '.sortable__drag-area',
- onUpdate: () => {
- const orderedImages = $(productGallery)
- .find('.product-gallery-item[data-id]')
- .map((index, item) => item.dataset.id)
- .toArray();
-
- // TODO: Get rid of ajax() in favour of fetch()
- $.ajax({
- method: 'POST',
- url: $(productGallery).data('post-url'),
- data: {ordered_images: orderedImages},
- traditional: true,
- headers: {
- 'X-CSRFToken': $.cookie('csrftoken')
- }
- });
- }
- });
-}
diff --git a/saleor/static/dashboard/js/components/index.js b/saleor/static/dashboard/js/components/index.js
index ec400cf55c..064c0ca02c 100644
--- a/saleor/static/dashboard/js/components/index.js
+++ b/saleor/static/dashboard/js/components/index.js
@@ -1,5 +1,4 @@
import './async-form';
-import './attribute-values.js';
import './bulk-actions';
import './datepicker';
import './editor';
@@ -7,12 +6,12 @@ import './filters';
import './google-preview';
import './image-gallery';
import './load-action';
-import './menu';
import './messages';
import './misc';
import './modal';
import './search';
import './selects';
import './side-menu';
+import './sortable.js';
import './tab';
import './vouchers';
diff --git a/saleor/static/dashboard/js/components/menu.js b/saleor/static/dashboard/js/components/menu.js
deleted file mode 100644
index 682e2a83ab..0000000000
--- a/saleor/static/dashboard/js/components/menu.js
+++ /dev/null
@@ -1,27 +0,0 @@
-import Sortable from 'sortablejs';
-
-const menuItems = document.getElementById('menu-items');
-
-if (menuItems) {
- Sortable.create(menuItems, {
- handle: '.sortable__drag-area',
- animation: 150,
- onUpdate: () => {
- const orderedMenuItems = $(menuItems)
- .find('.menu-item[data-id]')
- .map((index, item) => item.dataset.id)
- .toArray();
-
- // TODO: Get rid of ajax() in favour of fetch()
- $.ajax({
- method: 'POST',
- url: $(menuItems).data('post-url'),
- data: {ordered_menu_items: orderedMenuItems},
- traditional: true,
- headers: {
- 'X-CSRFToken': $.cookie('csrftoken')
- }
- });
- }
- });
-}
diff --git a/saleor/static/dashboard/js/components/attribute-values.js b/saleor/static/dashboard/js/components/sortable.js
similarity index 100%
rename from saleor/static/dashboard/js/components/attribute-values.js
rename to saleor/static/dashboard/js/components/sortable.js
diff --git a/templates/dashboard/includes/_menu_items.html b/templates/dashboard/includes/_menu_items.html
index 201aa74588..f6c4021ef1 100644
--- a/templates/dashboard/includes/_menu_items.html
+++ b/templates/dashboard/includes/_menu_items.html
@@ -32,9 +32,9 @@
{% endif %}
</tr>
</thead>
- <tbody id="menu-items" class="menu-items" data-post-url="{% if menu_item %}{% url 'dashboard:menu-items-reorder' menu_pk=menu.pk root_pk=menu_item.pk %}{% else %}{% url 'dashboard:menu-items-reorder' menu_pk=menu.pk %}{% endif %}">
+ <tbody class="sortable-items" data-post-url="{% if menu_item %}{% url 'dashboard:menu-items-reorder' menu_pk=menu.pk root_pk=menu_item.pk %}{% else %}{% url 'dashboard:menu-items-reorder' menu_pk=menu.pk %}{% endif %}" data-post-name="ordered_menu_items">
{% for item in menu_items %}
- <tr data-action-go="{% url 'dashboard:menu-item-details' menu_pk=menu.pk item_pk=item.pk %}" class="menu-item" data-id="{{ item.pk }}">
+ <tr data-action-go="{% url 'dashboard:menu-item-details' menu_pk=menu.pk item_pk=item.pk %}" class="sortable-item" data-id="{{ item.pk }}">
{% if perms.menu.edit_menu %}
<td class="data-table-icon ignore-link sortable__drag-area">
<svg data-src="{% static "dashboard/images/reorder.svg" %}" height="20" width="20" fill="#000" />
diff --git a/templates/dashboard/product/product_image/list.html b/templates/dashboard/product/product_image/list.html
index 05e3d3b44d..67e2a6f2c7 100644
--- a/templates/dashboard/product/product_image/list.html
+++ b/templates/dashboard/product/product_image/list.html
@@ -52,10 +52,10 @@
<div class="dz-message"></div>
{% csrf_token %}
<div class="row">
- <ul class="product-gallery" id="product-gallery" data-post-url="{% url 'dashboard:product-images-reorder' product_pk=product.pk %}">
+ <ul class="product-gallery sortable-items" data-post-url="{% url 'dashboard:product-images-reorder' product_pk=product.pk %}" data-post-name="ordered_images">
{% if images %}
{% for image in images %}
- <li class="col s12 m6 l4 product-gallery-item"
+ <li class="col s12 m6 l4 sortable-item"
data-id="{{ image.pk }}">
<div class="card">
<div class="card-image product-gallery-item-image">