droppable out event: How to get element where mouse moves to - jquery-ui

During droppable out event, I need the element the mouse cursor moves to. If this element is not droppable, a default behaviour should be executed.
I tried the following:
$(dropSelector).droppable({
over: function (event, ui) {
ui.helper.css("cursor", "copy");
// Some code on drop enter here...
},
out: function (event, ui) {
// New element: mouse pointer might move to another droppable element...
// How to obtain the element where the mouse moves to?
if (/* next element is NOT droppable*/) {
ui.helper.css("cursor", "no-drop");
// Some code on drop out here...
}
},
drop: function (event, ui) {
// handle drop event
}
});
However, I could not find a way to get the element where the mouse cursor moves to during out event.. I tried event.target and event.currentTarget but they are not the elements I am looking for.

I used a different solution. Obviously the over event for the next element fires before the out event of the old element. So I check if the out target is the last element entered.
var _target = null;
$(dropSelector).droppable({
over: function (event, ui) {
_target = event.target;
ui.helper.css("cursor", "copy");
// Some code on drop enter here...
},
out: function (event, ui) {
if (_target === event.target) {
// No 'over' occurred for a new element
ui.helper.css("cursor", "no-drop");
// Some code on drop out here...
}
else {
// Some code on drop out of old element
// Perhaps some code on 'over' that has to be done after
// drop out of old element
}
},
drop: function (event, ui) {
// handle drop event
}
});
But: Can I rely on the fact that over on next element fires before out on old element?

Related

Get draggable object in the ui object on drop

When using jQuery-UI's droppable widget, the drop function returns an "ui" object in which you can access to a "draggable" object which is the DOM element of the dragged object. But with the fullCalendar's drop function, i get the "ui" object without the "draggable" one. Here's a JSFiddle in which you can test what i'm talking about : http://jsfiddle.net/vfaethbd/
$('#calendar').fullCalendar({
header: {
left: 'title',
center: 'agendaDay,agendaWeek,month',
right: 'today prev,next'
},
droppable: true,
drop: function (date, jsEvent, ui) {
alert(JSON.stringify(ui, null, 4));
}
});
$("#droppable-area").droppable({
drop: function (event, ui) {
alert(JSON.stringify(ui, null, 4));
}
});
/* returns "draggable": {
"0": {
"jQuery111104109880250544967": 6
},
"context": {
"jQuery111104109880250544967": 6
},
"length": 1
}
*/
If you drop an event in the calendar, you won't have the draggable object, but if you drop it in the other droppable area you'll get it, since this one uses jQuery-UI.
Thanks
Implement what the external dragging example has in the drop callback:
drop: function(date) { // this function is called when something is dropped
// retrieve the dropped element's stored Event Object
var originalEventObject = $(this).data('eventObject');
// we need to copy it, so that multiple events don't have a reference to the same object
var copiedEventObject = $.extend({}, originalEventObject);
// assign it the date that was reported
copiedEventObject.start = date;
// render the event on the calendar
// the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
$('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
// is the "remove after drop" checkbox checked?
if ($('#drop-remove').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$(this).remove();
}
}
Full code example here and documentation about external events here.
Also, make sure your events and calendar are editable, for events this include:
allDay
durationEditable
startEditable
if not it may seem like your events lost the drag option

There any possibility to add data dropping a dom element into the tree

I want to enable the drag and drop to add child node but the child node is not into the tree. So I'm thinking in use draggable jquery ui function to drag a element into a jqtree node but I don't know how to capture the node dropped. Any ideas?
EDIT (More info)
I've tried this.
$('#tree1').tree({
data: data,
dragAndDrop: true,
onCreateLi: function (node, $li) {
$li.droppable({
drop: function (event, ui) {
alert($(this).attr("id"));
}
});
}
});
but the this element does not contain information about node.
I'm going to answser myself. Based on this example I attached a droppable object to a tree element and when a object is dropped I access to a custom property.
$('#tree1').tree({
data: data,
dragAndDrop: true,
onCreateLi: function (node, $li) {
$li.find(".jqtree-element").prop("id",node.id);
$li.find(".jqtree-element").droppable({
hoverClass: "ui-state-active",
drop: function (event, ui) {
var id = $(this).prop("id");
alert(id);
}
});
}
});

How to stop jQuery drag/drop event on stacked DIVs?

I know I'm missing something very basic, but when using jQuery in a situation where you have stacked "droppable" DIVs on top of each other (think nested boxes), how do you allow and accept an element drop on the top most DIV and then cancel the drag/drop event so it is not also sent to the other "droppable" DIVs below?
$('#'+objectID+" .task-droppable").droppable({
accept: function(d) {
if(d.hasClass("source-task")||d.hasClass("source-sequence")){ //sequences can contain both sequences and tasks
return true;
} //end if
}, //end accept
activeClass: "isDropDest",
//hoverClass: "isDragging",
//this is used for both drag/drop and item moves
drop: function(event, ui) {
var draggableId = ui.draggable.attr("id");
var droppableId = $(this).attr("id");
//var sender_id = ui.sender.attr('id');
//var receiver_id = $(this).attr('id');
//var item_id = ui.item.attr('id');
//var above_id = ui.item.prev().attr('id');
//var below_id = ui.item.next().attr('id');
//check if this is a drag/drop or a move by looking for the object class
if(!$('#'+draggableId).hasClass('object')) {
$('#'+draggableId).css('top', '0px');
$('#'+draggableId).css('left', '0px');
createObject(draggableId, droppableId);
} else {
//handle the move - do nothing
} //end if
event.stopPropagation();
} //end drop
}); //end droppable
Sorry, not enough coffee today.
Sounds like you may need to use the greedy option
By default, when an element is dropped on nested droppables, each
droppable will receive the element. However, by setting this option to
true, any parent droppables will not receive the element.
$( ".selector" ).droppable({ greedy: true });
Working Example

How to make hidden div appear in another sortable list

I'm dragging sortables from a grey ul list (parent group) to two yellow lists underneath. I am trying to have a hidden button in the grey sortable list appear when dragged into the yellow sortable lists.
I'm using a stupid method of display none and using a mouseUp event handler to make the hidden button appear. It's not achieving the desired effect.
it is also important that the hidden button only appear in the bottom yellow boxes and not appear in the grey sortable lists.
Any help would be much appreciated. Thanks.
http://jsfiddle.net/equiroga/4At6J/4/
//Show js
$(function(){
$(document).mouseup(function(){
$('.button, .button2, .button3, .button4, .button5').show();
});
});
//Sortable js
$(function () {
$("#sortable1").sortable({
helper: "clone",
connectWith: ".sortable",
start: function (event, ui) {
$(ui.item).show();
clone = $(ui.item).clone();
before = $(ui.item).prev();
position = $(ui.item).index();
},
beforeStop: function (event, ui) {
if ($(ui.item).closest('ul#sortable1').length > 0) $(this).sortable('cancel');
},
stop: function (event, ui) {
if (position == 0) $("#sortable1").prepend(clone);
else before.after(clone);
}
});
$(".sortable").sortable({connectWith: ".sortable:not('#sortable1')"});
});
Remove the top code and add this in the stop function:
ui.item.children('.button').show();
EDIT
I added a bt common class to all the buttons and then:
ui.item.children('.bt').show();
http://jsfiddle.net/stevemarvell/4At6J/6/

swipe-right triggered twice in jQuery Mobile

I'm trying to integrate this plug-in into my site so I can swipe to delete. The problem however is that this plugin is triggered with a 'swiperight', the same swipe event is used to reveal my panel. I managed to separate the events using event.target.tagName. When it's a A(link), I want to activate the swipe to delete button and otherwise I want my panel to slide in.
With other words the pageinit event is triggered twice so the swipe to delete button starts to appear then the same event is triggered again. I want to somehow cancel one action but i can't make it work. I already tried:
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
I also tried to use some solutions given here but with no luck:
jQuery Mobile: document ready vs page events
A demo of my problem can be found snip and my current pageinit function is this:
$(document).on('pageinit', function() {
//Activate horizontal swipe after x px.
$.event.special.swipe.horizontalDistanceThreshold = 80;
$('div[data-role="content"]').on("swiperight", function(event) {
//If tagname is 'A' you probably want slide to delete not the panel
if(event.target.tagName != 'A') {
$.mobile.activePage.find("#menu").panel("open");
} else {
//Cancel swipe
event.stopImmediatePropagation();
}
});
//Swipe to delete
$("#swipe li").swiper( {
corners: false,
label: "Verwijder",
swipe: function(event, ui) {
alert('trigger');
},
click: function(event, ui) {
var $item = $(this);
//console.log($(this));
$item.fadeOut(250, function() {
$item.remove();
});
}
});
});
Fixed issue using the following plugin: TouchSwipe which has the ability to simple exclude elements from the events.

Resources