Skip to content
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

Callback support for acceptFrom #646

Open
wants to merge 7 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ In the example the current list `.sortable` allows items within it to be sorted

If you want to be able to move items between to sortables, the `acceptFrom` option must be present on both of them.
Copy link
Owner

Choose a reason for hiding this comment

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

I know this is not you, but if you change it in any case, can you fix the typo

move items between to sortables
to
move items between two sortables


Can also accept a callback function with `destinationSortable` and `draggedElement` as paramaters, This function should return `true` or `false`:
Copy link
Owner

Choose a reason for hiding this comment

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

Can you please change this to:

You can also provide a function for the acceptFrom option. The function will receive two parameters, destinationSortable and draggedElement. Your function must return a boolean value (true or false).


``` javascript
sortable('.sortable', {
acceptFrom: function(destinationSortable, draggedElement){
Copy link
Owner

Choose a reason for hiding this comment

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

Can you please change this to use a fat arrow function and change it to the below:

(destinationSortable, draggedElement) => {
  // your custom function
  if (draggedElement.classList.contains('canDrop')) {
    return true
  } else {
    return false
  }
}

if(draggedElement.classList.contains('canDrop')){
return true;
} else {
return false;
}
}
});

### placeholder
Use the `placeholder` option to specify the markup of the placeholder:

Expand Down
4 changes: 2 additions & 2 deletions src/html5sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export default function sortable (sortableElements, options: configuration|objec
* Fires when valid drop target area is hit
*/
on(sortableElement, 'drop', function (e) {
if (!listsConnected(sortableElement, dragging.parentElement)) {
if (!listsConnected(sortableElement, dragging)) {
return
}
e.preventDefault()
Expand Down Expand Up @@ -622,7 +622,7 @@ export default function sortable (sortableElements, options: configuration|objec
let element = e.target
const sortableElement = element.isSortable === true ? element : findSortable(element, e)
element = findDragElement(sortableElement, element)
if (!dragging || !listsConnected(sortableElement, dragging.parentElement) || data(sortableElement, '_disabled') === 'true') {
if (!dragging || !listsConnected(sortableElement, dragging) || data(sortableElement, '_disabled') === 'true') {
return
}
const options = data(sortableElement, 'opts')
Expand Down
22 changes: 13 additions & 9 deletions src/isConnected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@ import store from './store'
/**
* Check if curList accepts items from destList
* @param {sortable} destination the container an item is move to
* @param {sortable} origin the container an item comes from
* @param {sortable} dom node currently being dragged
*/
export default (destination: sortable, origin: sortable) => {
export default (destination: sortable, dragging: node) => {
// check if valid sortable
if (destination.isSortable === true) {
const acceptFrom = store(destination).getConfig('acceptFrom')
// check if acceptFrom is valid
if (acceptFrom !== null && acceptFrom !== false && typeof acceptFrom !== 'string') {
if (acceptFrom !== null && acceptFrom !== false && typeof acceptFrom !== 'string' && typeof acceptFrom !== 'function') {
throw new Error('HTML5Sortable: Wrong argument, "acceptFrom" must be "null", "false", or a valid selector string.')
Copy link
Owner

Choose a reason for hiding this comment

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

@mike16889 please change this to:

'HTML5Sortable:` Wrong argument, "acceptFrom" must be "null", "false", a function returning a boolean, or a valid selector string.'

}

if (acceptFrom !== null) {
return acceptFrom !== false && acceptFrom.split(',').filter(function (sel) {
return sel.length > 0 && origin.matches(sel)
}).length > 0
if (typeof acceptFrom === 'function') {
return acceptFrom(destination, dragging);
} else {
return acceptFrom !== false && acceptFrom.split(',').filter(function (sel) {
return sel.length > 0 && dragging.parentElement.matches(sel)
}).length > 0
}
}
// drop in same list
if (destination === origin) {
if (destination === dragging.parentElement) {
return true
}
// check if lists are connected with connectWith
if (store(destination).getConfig('connectWith') !== undefined && store(destination).getConfig('connectWith') !== null) {
return store(destination).getConfig('connectWith') === store(origin).getConfig('connectWith')
if ((store(destination).getConfig('connectWith') !== undefined && store(destination).getConfig('connectWith') !== null) && store(dragging.parentElement).getConfig('connectWith') !== undefined && store(dragging.parentElement).getConfig('connectWith') !== null) {
Copy link
Owner

Choose a reason for hiding this comment

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

@mike16889 why dod you add this check for the parent? I think this may create an issue if you have a place that does not accept, but only provide items to drag.

return store(destination).getConfig('connectWith') === store(dragging.parentElement).getConfig('connectWith')
}
}
return false
Expand Down