-
Notifications
You must be signed in to change notification settings - Fork 980
4 callbacks added #143
base: master
Are you sure you want to change the base?
4 callbacks added #143
Conversation
afterInit onStartEvent
What you think about this solution instead? var onEndEvent = function(e)
{
if (list.dragEl) {
e.preventDefault();
var item = list.dragEl.find('.'+list.options.itemClass);
list.dragRootEl.trigger('dragEnd', [
item, // List item
list.el, // Source list
list.dragRootEl // Destination list
]);
list.dragStop(e.touches ? e.touches[0] : e);
}
}; Usage: $('.dd').nestable()
.on('dragEnd', function(event, item, source, destination) {
console.log(item, source, destination);
}); |
Thanks bigfoot, |
By this way you can attach more than one listener on The code listeners are decoupled from the list initialization, so you can also split your code in more files, for example 3rd party plugins. I've also added three parameters because I need to know the list item I'm moving, and the source and destination lists. This same method is also applicable to |
bigfoot90, |
removed unnecessary variables to be returned (they were redundant)
jquery.nestable.withCallbacks.js
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is not a destination list on dragStart
event
Thank you. I made the changes |
destination list from dragMove is back now. |
Would be nice if the new position of the dragged element could be added to the dragEnd event arguments. Having this information, would simplify back-end handling/storage of the tree. |
jquery.nestable.withCallbacks.js
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the destination
parameter yet.
I there any reason why you have removed it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bigfoot90 Thanks for checking up. I double checked my previous modification few days ago. See the line 138, list.dragRootEl // Destination.
@webtweakers I just woke up now. I get back to you as soon as I can.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Come to think of it, having some other functionality available at the dragEnd event would be really helpful as well:
- a reference to the new parent, if any
- being able to return false in the dragEnd callback/listener as a way to undo the move
As far as I have checked the code, this kind of functionality seems to fall slightly out of the scope of the project. Unfortunately I have a lack of time at the moment, otherwise I'd implement it myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BeFiveINFO Have you pushed it?
https://github.com/dbushell/Nestable/pull/143/files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that I have, but I might have made a mistake?
BeFiveINFO@0f3282d
@webtweakers For position in destination you can use list.dragRootEl.trigger('dragEnd', [
item, // List item
list.el, // Source list
list.dragRootEl, // Destination list
list.placeEl.index() // Position
]); |
@bigfoot90 Nice one, thanks. |
@BeFiveINFO What is the state of this? |
hello bigfoot90, thanks for checking up! I just wanted to ask you about the timing where the trigger function should be at. Currently the trigger is called before "list.dragStop(e.touches ? e.touches[0] : e);" (ref: line 156) The action is not completed yet at the point of trigger to be precise. But placing the trigger for dragEnd right after dragStop let dragEl and dragRootEl inaccessible. I wonder what we could do for this issue. I am now thinking to place the trigger function right after the dragStop, then return just ID of the handle (as String) with the trigger. For example:
Thank you for your advices in advance! |
Placing the trigger after is right, but there is no way to prevent Currently I'm using this ugly workaround: var onEndEvent = function(e)
{
if (!list.dragEl) return;
e.preventDefault();
var feedback = {abort: false};
var item = list.dragEl.find('.'+list.options.itemClass);
var sourceList = list.el;
var destinationList = list.dragRootEl;
var position = list.placeEl.index();
destinationList.trigger('beforeDragEnd', [
item, // List item
sourceList, // Source list
destinationList, // Destination list
position, // Position
feedback
]);
if (feedback.abort) return;
list.dragStop(e.touches ? e.touches[0] : e);
destinationList.trigger('dragEnd', [
item, // List item
sourceList, // Source list
destinationList, // Destination list
position // Position
]);
}; And use case: $(this)
.nestable()
.on('beforeDragEnd', function(event, item, source, destination, position, feedback) {
if (source[0] === destination[0]) return;
feedback.abort = !window.confirm('Continue?');
})
.on('dragEnd', function(event, item, source, destination, position) {
if (source[0] === destination[0]) return;
// Make an ajax request to persist move on database
// here i need to pass item-id, source-id, destination-id, position index to the server
// ....
}); @BeFiveINFO What do you think? |
Thanks to bigfoot90
Thanks @bigfoot90! I have made the changes just as you suggested. |
Good ;-) Just few questions/suggesstions:
|
now beforeDragStart to be in line with other event names
@bigfoot90
I will work on the issue below. I will post as soon as I have something to show.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List events with the correct timeline order
afterInit
beforeDragStart
dragStart
dragMove
beforeDragEnd
dragEnd
I have noticed there are some minor errors in Corrected: $('#example-list-element').nestable({
afterInit: function ( event ) {
console.log( event );
}
})
.on('beforeDragStart', function(handle) {
console.log('dragStart', handle);
})
.on('dragStart', function(event, item, source) {
console.log('dragStart', event, item, source);
})
.on('dragMove', function(event, item, source, destination) {
console.log('dragMove', event, item, source);
})
.on('dragEnd', function(event, item, source, destination) {
console.log('dragEnd', event, item, source, destination);
})
.on('beforeDragEnd', function(event, item, source, destination, position, feedback) {
// If you need to persist list items order if changes, you need to comment the next line
if (source[0] === destination[0]) { feedback.abort = true; return; }
feedback.abort = !window.confirm('Continue?');
})
.on('dragEnd', function(event, item, source, destination, position) {
// Make an ajax request to persist move on database
// here you can pass item-id, source-id, destination-id and position index to the server
// ....
console.log('dragEnd', event, item, source, destination, position);
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update beforeDragStart
event to pass the same parameters as dragStart
event do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, bigfoot90.
Readme has been updated.
Updating the file so that beforeDragStart returns the parameters. I forgot declaring the item variables. hold on
FYI I just added you as a collaborator at the repo, https://github.com/BeFiveINFO/Nestable Please feel free to make any changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bigfoot90 , I just realised that the code needed to return just handle, because item may not exist depending on cases.
In my case, I use "beforeDragStart" event to determine a clicked handle element (item). When the handle is clicked, there is an information pane right next to nestable where you can edit details of selected item. I hope that you will get the image of my case scenario.
Is there a clever way of canceling drop and return item to previous position ? |
@Sysa Currently the @BeFiveINFO You have reverted your commit, What is the problem? |
@bigfoot90 |
For what it is worth, I used your code to get the element and write it to the database. Here was what I used:
Javascript:
|
+1 ;-) |
+1 |
Would someone consider pushing the Merge button on this one? Two years and its still not merged? This is very helpful functionality. |
I added 4 callbacks: afterInit, onStartEvent, onMoveEvent and onEndEvent. Any chance we could add those callbacks?
Test code as follows.