Skip to content

Add keep css class option #854

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions dist/html5sortable.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,10 @@ define(function () { 'use strict';
dragging.style.display = dragging.oldDisplay;
delete dragging.oldDisplay;
}
// We need to reset placeholder if any css class were added.
options.keepCssClass.forEach(function (className) {
store(sortableElement).placeholder.classList.remove(className);
});
}
else {
// set the dropped value to 'false' to delete copied dragging at the time of 'dragend'
Expand Down Expand Up @@ -1188,6 +1192,13 @@ define(function () { 'use strict';
store(sortableElement).placeholder.style.height = draggingHeight + 'px';
store(sortableElement).placeholder.style.width = draggingWidth + 'px';
}
// Keep some CSS class fo the dragged element, to keep style.
// Usefull if sortable elements have different dimensions according class.
options.keepCssClass.forEach(function (className) {
if (element.classList.contains(className)) {
store(sortableElement).placeholder.classList.add(className);
}
});
// if element the draggedItem is dragged onto is within the array of all elements in list
// (not only items, but also disabled, etc.)
if (Array.from(sortableElement.children).indexOf(element) > -1) {
Expand Down
11 changes: 11 additions & 0 deletions dist/html5sortable.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,10 @@ function sortable(sortableElements, options) {
dragging.style.display = dragging.oldDisplay;
delete dragging.oldDisplay;
}
// We need to reset placeholder if any css class were added.
options.keepCssClass.forEach(function (className) {
store(sortableElement).placeholder.classList.remove(className);
});
}
else {
// set the dropped value to 'false' to delete copied dragging at the time of 'dragend'
Expand Down Expand Up @@ -1188,6 +1192,13 @@ function sortable(sortableElements, options) {
store(sortableElement).placeholder.style.height = draggingHeight + 'px';
store(sortableElement).placeholder.style.width = draggingWidth + 'px';
}
// Keep some CSS class fo the dragged element, to keep style.
// Usefull if sortable elements have different dimensions according class.
options.keepCssClass.forEach(function (className) {
if (element.classList.contains(className)) {
store(sortableElement).placeholder.classList.add(className);
}
});
// if element the draggedItem is dragged onto is within the array of all elements in list
// (not only items, but also disabled, etc.)
if (Array.from(sortableElement.children).indexOf(element) > -1) {
Expand Down
13 changes: 12 additions & 1 deletion dist/html5sortable.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,10 @@ function sortable(sortableElements, options) {
dragging.style.display = dragging.oldDisplay;
delete dragging.oldDisplay;
}
// We need to reset placeholder if any css class were added.
options.keepCssClass.forEach(function (className) {
store(sortableElement).placeholder.classList.remove(className);
});
}
else {
// set the dropped value to 'false' to delete copied dragging at the time of 'dragend'
Expand Down Expand Up @@ -1186,6 +1190,13 @@ function sortable(sortableElements, options) {
store(sortableElement).placeholder.style.height = draggingHeight + 'px';
store(sortableElement).placeholder.style.width = draggingWidth + 'px';
}
// Keep some CSS class fo the dragged element, to keep style.
// Usefull if sortable elements have different dimensions according class.
options.keepCssClass.forEach(function (className) {
if (element.classList.contains(className)) {
store(sortableElement).placeholder.classList.add(className);
}
});
// if element the draggedItem is dragged onto is within the array of all elements in list
// (not only items, but also disabled, etc.)
if (Array.from(sortableElement.children).indexOf(element) > -1) {
Expand Down Expand Up @@ -1302,4 +1313,4 @@ sortable.__testing = {
removeContainerEvents: removeContainerEvents
};

export default sortable;
export { sortable as default };
11 changes: 11 additions & 0 deletions dist/html5sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,10 @@ var sortable = (function () {
dragging.style.display = dragging.oldDisplay;
delete dragging.oldDisplay;
}
// We need to reset placeholder if any css class were added.
options.keepCssClass.forEach(function (className) {
store(sortableElement).placeholder.classList.remove(className);
});
}
else {
// set the dropped value to 'false' to delete copied dragging at the time of 'dragend'
Expand Down Expand Up @@ -1189,6 +1193,13 @@ var sortable = (function () {
store(sortableElement).placeholder.style.height = draggingHeight + 'px';
store(sortableElement).placeholder.style.width = draggingWidth + 'px';
}
// Keep some CSS class fo the dragged element, to keep style.
// Usefull if sortable elements have different dimensions according class.
options.keepCssClass.forEach(function (className) {
if (element.classList.contains(className)) {
store(sortableElement).placeholder.classList.add(className);
}
});
// if element the draggedItem is dragged onto is within the array of all elements in list
// (not only items, but also disabled, etc.)
if (Array.from(sortableElement.children).indexOf(element) > -1) {
Expand Down
2 changes: 1 addition & 1 deletion dist/html5sortable.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/html5sortable.min.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/html5sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ export default function sortable (sortableElements, options: configuration|objec
dragging.style.display = dragging.oldDisplay
delete dragging.oldDisplay
}
// We need to reset placeholder if any css class were added.
options.keepCssClass.forEach(className => {
store(sortableElement).placeholder.classList.remove(className)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done in one step https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/remove sot that all classes are provided at once.

I think this should work: remove(...options.keepCssClass)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I will change that.

});
} else {
// set the dropped value to 'false' to delete copied dragging at the time of 'dragend'
data(dragging, 'dropped', 'false')
Expand Down Expand Up @@ -552,6 +556,13 @@ export default function sortable (sortableElements, options: configuration|objec
store(sortableElement).placeholder.style.height = draggingHeight + 'px'
store(sortableElement).placeholder.style.width = draggingWidth + 'px'
}
// Keep some CSS class fo the dragged element, to keep style.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fo = for in comment

// Usefull if sortable elements have different dimensions according class.
options.keepCssClass.forEach(className => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think from a performance point of view you should first get the elements you want to add. Maybe use filter to get the elements that are in both arrays.

Afterwards just like with remove, you can add all at once. DOM interactions are often expensive.
add(...comparedArray)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry you lost me in the first sentence.
Second sentence nice did't know that too.

if (element.classList.contains(className)) {
store(sortableElement).placeholder.classList.add(className)
}
});
// if element the draggedItem is dragged onto is within the array of all elements in list
// (not only items, but also disabled, etc.)
if (Array.from(sortableElement.children).indexOf(element) > -1) {
Expand Down
7 changes: 4 additions & 3 deletions src/types/configuration.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
interface configuration {
items?: string,
handle?: string,
forcePlaceholderSize?:boolean;
forcePlaceholderSize?: boolean,
keepCssClass?: Array<string>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this to keepClassesForPlaceholder.

connectWith?: string,
acceptFrom?: void,
placeholder?: void,
Expand All @@ -12,9 +13,9 @@ interface configuration {
copy?: boolean,
itemSerializer?: (serializedItem: serializedItem, sortableContainer: HTMLElement) => serializedItem,
containerSerializer?: (serializedContainer: object) => object,
customDragImage?:void,
customDragImage?: void,
disableIEFix?: boolean,
debounce?: number,
throttleTime?: number,
orientation?: 'vertical'|'horizontal'
orientation?: 'vertical' | 'horizontal'
}