js/jQuery Drag'n'Drop, recalculate the drop targets - javascript

I have the following issue, I have a large tree which has subnodes which can be folded and unfolded on demand (the data within nodes gets fetched with AJAX). However, I use jquery.event.drop/drag to create my drag/drop targets.
However, when I fold/unfold the drop targets change position and I need to recalculate. This is how I wanted to do that:
function create_drop_targets() {
$('li a')
.bind('dropstart', function(event) {
})
.bind('drop', function(event) {
})
.bind('dropend', function(event) {
});
}
create_drop_targets() is called upon fold/unfold.
However, this doesn't work. I have located the following within jquery.event.drop:
var drop = $.event.special.drop = {
setup: function(){
drop.$elements = drop.$elements.add( this );
drop.data[ drop.data.length ] = drop.locate( this );
},
locate: function( elem ){ // return { L:left, R:right, T:top, B:bottom, H:height, W:width }
var $el = $(elem), pos = $el.offset(), h = $el.outerHeight(), w = $el.outerWidth();
return { elem: elem, L: pos.left, R: pos.left+w, T: pos.top, B: pos.top+h, W: w, H: h };
}
Now I need to know how I can call the setup() method again so it repopulates $elements with the new positions for the droppables.

Just had the same issue. I wandered around within the source-code of jQuery and found this (in ui.droppable.js):
drag: function(draggable, event) {
//If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
if(draggable.options.refreshPositions) $.ui.ddmanager.prepareOffsets(draggable, event);
...
So, you'd just have to use
$(".cocktails").draggable({
refreshPositions: true,
});
Seems not to be documented very much... but it fixed my problem. Makes everything a bit slower of course, I would advise some usage-dependent tweaking (enable it before the changes occur, and disable it once the user has moved his mouse and the changes have occured).

Maybe it will be better to add live events introduced in jQuery 1.3?
$("li a").live("dropstart", function(){...});

I ran into the same issue when I tried to combine scrolling with draggable rows in liteGrid, but I found a work-around. Your mileage may vary, but what I did was add logic to my drag event handler that would check to see if the grid was being scrolled (which is when I needed to force the droppable positions to be refreshed), and if so, I set refreshPositions to true on the draggable. This doesn't immediately refresh the positions, but it will cause them to refresh the next time the drag handle moves. Since refreshPositions slows things down, I then re-disable it the next time my drag event handler fires. The net result is that refreshPositions is enabled only when the grid is scrolling in liteGrid, and its disabled the rest of the time. Here's some code to illustrate:
//This will be called every time the user moves the draggable helper.
function onDrag(event, ui) {
//We need to re-aquire the drag handle; we don't
//hardcode it to a selector, so this event can be
//used by multiple draggables.
var dragHandle = $(event.target);
//If refreshOptions *was* true, jQueryUI has already refreshed the droppables,
//so we can now switch this option back off.
if (dragHandle.draggable('option', 'refreshPositions')) {
dragHandle.draggable('option', 'refreshPositions', false)
}
//Your other drag handling code
if (/* logic to determine if your droppables need to be refreshed */) {
dragHandle.draggable('option', 'refreshPositions', true);
}
}
$("#mydraggable").draggable({
//Your options here, note that refreshPositions is off.
drag: onDrag
});
I hope that saves you from ramming your head into the keyboard as many times as I did...

I realize the original question is quite old now, but one little trick I came up with to refresh the position of draggable elements without much overhead (AFAICT) is to disable and immediately re-enable them wherever appropriate.
For instance, I noticed that resizing my browser window would not refresh the position of my draggable table rows, so I did this:
$(window).resize(function () {
$(".draggable").draggable("option", "disabled", true);
$(".draggable").draggable("option", "disabled", false);
});
I hope this helps someone out there!

Related

Prevent child event from firing

I have a slider that I am currently making. I am making slow progress, but I am making progress nonetheless!
Currently I have this:
http://codepen.io/r3plica/pen/mEKyGG?editors=1011#0
There are 2 things you can do with this control, the first thing is you can drag left or right. The second thing you can do is click a "point" and it will scroll to the center.
The problem I have is that if I start dragging from a point, when I let go it will invoke the moveToCenter method.
I have tried to prevent this by adding
// Stop from accessing any child events
e.preventDefault();
e.stopPropagation();
to the end of the dragEventHandler, but this did not work.
I also have 2 boolean values options.drag and options.start. I though I might be able to use them somehow (if the drag has started and is enabled then don't perform the moveToCenter but this didn't work either.
Do anyone have any idea how to get this to work?
Maybe this will help. You can register your events in bubbling or capturing mode, using addEventListener method. It defines orders of processing your events - child -> parent (bubbling), or vice versa (capturing).
http://www.quirksmode.org/js/events_advanced.html
So, if you use addEventListener(event, handler, true), it will use capturing event mode.
Codepen:
http://codepen.io/anon/pen/bZKdqV?editors=1011
divs.forEach(function (div) {
div.addEventListener('click', function(e) {
e.stopPropagation();
console.log('parent');
}, true);
});
Be aware of browser support (IE9+). All modern browsers - yes, of course.
http://caniuse.com/#search=addeventlistener
Update
So it turned out to be easier than first approach. (no need for capturing)
Check out codepen:
http://codepen.io/anon/pen/QExjzV?editors=1010
Changes from your sample:
At the beginning of moveToCenter: function(e, options, animate) function
if (options.started) {
return;
}
In if (['mouseup', 'mouseleave'].indexOf(e.type) > -1):
setTimeout(function() {
options.started = false;
} , 100);
instead of
options.started = false;
Hope this helps.

How to disable/stop drag action when using dojo/dnd/Moveable?

I am using a dojo/dnd/Moveable in order to drag a div on a page, similarly to the example below.
During event 'move' which run when user is dragging the div I need to apply some logic (in my real example I need to check for some mouse coordinate), if logic is not valid I need to STOP dragging the div.
I have tried to use dojo.dnd.manager.stopDrag() with no success.
I would like to know:
How to STOP/DISABLE the drag action?
A sample of working code would be appreciated. Thanks.
https://jsfiddle.net/mbrhb7nn/
require(["dojo/dnd/Moveable", "dojo/dom", "dojo/on", "dojo/domReady!"],
function(Moveable, dom, on){
on(dom.byId("doIt"), "click", function(){
var dnd = new Moveable(dom.byId("dndOne"));
dnd.on('Move', function (event) {
// add some logic here and stop drag
}.bind(this));
});
});
I was able to solve my problem using constrainedMoveable from dojo/dnd/move.
Basically it is possible to setup an "area" where the dragging is only allowed, I used constrainedMoveable as I need to dynamically calculated the "area" .
Actually dojo/dnd/move offers severals specialized Movables:
constrainedMoveable
used to constrain a move to a dynamically calculated box.
Notes: the function constraints (must be overwritten) and has to return an object with the the following properties for l,t,w,h (left/top/width/height).
boxConstrainedMoveable
can be used to constrain a move to a predefined box.
parentConstrainedMoveable
can be used to constrain the move by the boundaries of the node’s parent.
More information can be found here.
Working example here:
https://jsfiddle.net/mbrhb7nn/5/
require(["dojo/dnd/move", "dojo/dom", "dojo/on", "dojo/domReady!"],
function (move, dom, on) {
var dnd = new move.constrainedMoveable(dom.byId("dndOne"));
dnd.constraints = function (event) {
// defines the area where dragging is allowed
return {
l: 0,
t: 0,
w: 300,
h: 300
};
}
});

NVD3 Chart Suppress Rendering for Hidden Charts

I faced a similar problem to what was described HERE:
The solution that worked for me, was to implement the following code:
$(function () {
$(document).on('shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
window.dispatchEvent(new Event('resize'));
});
});
I have a feeling, however, that ALL of the charts are being re-rendered, regardless of whether they are on the active tab (visible) or in the non-selected tabs (hidden). If for example I have 20 tab pages, it takes far longer to re-render than it does for 2 tab pages.
Does anyone know how to ensure ONLY the active chart gets resized / redrawn? Ie how can the resize / redraw event be suppressed if the chart is not visible?
What I did was to store all my charts in an array when they are first created. I know that 'chart1' is a child of 'tab1', 'chart2' is a child of 'tab2' etc... (by design), so I can determine the index in the array using some regex.
Once the index is known, we can refresh the chart object directly, accessed from the array by zero-based index.
//Resize Event needs to be triggered when tab changes.
$(document).on('shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
var plotID, ev;
try{
plotID = $(e.target).attr("href").replace(/[#A-Za-z$-]/g,"")
d3.select("#chart"+ plotID +" svg").call(charts[(plotID-1)])
}catch(err){ //Fallback
ev = document.createEvent('Event');
ev.initEvent('resize', true, true);
window.dispatchEvent(ev);
}
});
Net result, redraw times much much faster when compared to triggering resize event.

Large Isotope Gallery is Very Slow

I have an Isotope gallery (version 2) with almost 400 elements. A typical gallery item looks like this:
<div class="element hybrid a" data-category="hybrid">
<p class="type">H</p>
<h2 class="name">Name</h2>
<p class="strain-info">No Info Available</p>
<p class="review"><a class="button radius small black review-form-lb" href="#review-form-lightbox">Review</a></p>
</div>
For instance when I run the code below, which basically adds a class to the element clicked, it takes several seconds for the element to enlarge.
$container.on( 'click', '.element', function() {
$( this ).toggleClass('large');
$container.isotope('layout');
});
Another example is if I have a button group that contains several options to filter the gallery, again it takes several seconds. Filter JS:
$('#filters').on( 'click', '.button', function() {
var $this = $(this);
// get group key
var $buttonGroup = $this.parents('.button-group');
var filterGroup = $buttonGroup.attr('data-filter-group');
// set filter for group
filters[ filterGroup ] = $this.attr('data-filter');
// combine filters
var filterValue = '';
for ( var prop in filters ) {
filterValue += filters[ prop ];
}
// set filter for Isotope
$container.isotope({ filter: filterValue });
});
Here is the variable for $container
// init Isotope
var $container = $('.isotope').isotope({
itemSelector: '.element',
layoutMode: 'fitRows',
filter: function() {
return qsRegex ? $(this).text().match( qsRegex ) : true;
}
});
I should note that the above code works great when there is a small number of items. How could I improve the performance of the gallery?
I should note that in version 1 of Isotopes I have the same gallery and it works fine. I am upgrading because of enhanced abilities of v2 of isotopes.
Here is the live site - IMPORTANT! - This site is for a marijuana dispensary in Colorado, USA. Website may not be appropriate for work.
Update
I tried changing all divs to li elements. Didn't see much improvement.
I tried turning on hardware acceleration per mfirdaus's answer but it didn't appear to work.
Note: The above link to the live site is a stripped down version where I removed everything not required for the Isotope gallery. It is slightly faster than when I originally posted this, however, it needs to be more responsive. Try using it on a tablet and it takes several seconds for the Isotope item to respond.
Update 2
Version One of Isotopes performs much better with a large gallery and I ended up using version one because of this.
I have a suggestion. First of all, you can add the css property transform:translate3d(0,0,0) to your .element css selector. This turns on hardware acceleration and should speed up the page reflow. This is a tip often used to make highly animated pages smoother, especially on mobile. so something ilke:
.element {
...
/* add this. prefixes for compabtibility */
transform:translate3d(0,0,0);
-webkit-transform:translate3d(0,0,0);
-moz-transform:translate3d(0,0,0);
}
In my tests it seems to speed it up quite nicely.
Another suggestion would be to replace .on("click") with .on("mousedown"). click fires after the mouse is released whereas mousedown would fire immediately after the user press their mouse. there is like a ~0.2s difference but it's somewhat noticable.
Here's an example made from isotope's default example which has 400 elements.
Update
After testing out touchstart, it kinda helps but it fires even when scrolling. So this helps only if you disable scroll/have fixed viewport. Maybe you could add iScroll, but I reckon that's even more bloat.
One option you could try is to force an element to resize first, before waiting for isotope to recalculate the positions. We can do this by using setTimeout on the isotope refresh. Not sure if the behaviour is acceptable but this will allow the user to get feedback faster. So your toggling code will be something like:
$container.on( 'click', '.element',function() {
$( this ).toggleClass('large');
setTimeout(function(){ $container.isotope('layout'); },20); //queue this later
});
Demo. Tested this on my android device and there seemed to be an improvement.
Well, the author of the isotope plugin recommends 50 items max if you're doing filtering or sorting. You have 400!.. So I'm afraid you'll need to decrease the num. of items for better performance.
Some side notes:
Try touchstart or mousedown events instead of click for a bit more quick responses on mobile.
Check for Object.keys(filters).forEach performance on mobile (instead of for in)..
jQuery's closest() method might be faster than parents() in some browsers.
Instead of using toggleClass, try directly manipulating the style.
$( this ).css({ /* styles for large */});
While I understand that this may not be an issue directly related to your scenario, I had the same experience (mostly on mobile and tablet devices) where I had only 36 images in an Isotope gallery, and I was waiting anywhere from 3-5 seconds for the reflow to occur.
In my case it was due to the fact that I had multiple (3) event listeners on the 'resize' event. Bad move! The resize event is fired several hundred times as it keeps firing until the user stops resizing the screen.
It also turns out that scrolling on iOS devices fires the resize event as well, so I had a resource intensive function (cloning a complex mega menu and rebuilding it for mobile devices) queuing hundreds of times, thus causing all js related tasks to lockup or become intensely slow.
The solution in my case was to implement a debounce function to debounce the functions attached to the 'resize' event, thus firing them once and only when the resize is complete.
For what it's worth, I adapted the Underscore.js debounce function so that I could use it locally and outside of Underscore.js, which was borrowed from this blog post by David Walsh. Here is an example of how I implemented it:
var debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var checkWindowWidthForNav = debounce(function(e) {
sdWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
if(sdWidth <= 990){
window.simulateMobileNav = true;
sdMobileSnapMenus();
}else{
window.mobileScaffoldingDone = false;
window.simulateMobileNav = false;
sdMobileSnapMenus();
$("[data-action=\"hover-toggle\"]").bind("mouseenter", function(e){
var $target = $(e.currentTarget).attr("data-target");
var $hide = $(e.currentTarget).attr("data-hide");
$($hide).hide();
$($target).show();
});
$(".subMenuWrapper:visible").hide();
$(".subMenuWrapper").bind("mouseleave", function(e){
$(".subMenuWrapper").fadeOut(500, function(){
$(".sdSubMenu .sdHideFilters, #sdArchitecturalFilters, #sdInteriorFilters, #sdUrbanFilters").hide();
});
return false;
e.preventDefault();
e.returnValue = false;
});
$(".sdMainMenu > ul li a[data-action], .sdSubMenu a[data-action]").bind("click", function(e){
e.preventDefault();
e.returnValue = false;
return false;
});
$(".sdMainMenu > ul .sdSubMenu").hide();
}
}, 600); // Debounced function occurs 600ms after event ends
window.addEventListener("resize", checkWindowWidthForNav, false);
Debouncing my events resulted in a DRAMATIC increase in speed for all JS related events. I no longer had the Isotope lag, my reflow was instant.
This may help someone who was in the same boat I was in.

Is there an event called while a jQuery UI draggable element is being reverted to its original position?

I have a draggable element using jQuery UI, and have a function attached the drag event that gets continuously called with the element's position while the user is dragging it.
I also have revert: true set on this element, so when the user stops dragging the element springs back to its original position. Is there a way I can attach a listener to perform the same functionality as when it's being manually moved?
I can't see anything specifically related to the revert property in the docs, so if that's not possible is there a more general event called while an element is moving?
Thanks!
It appears there's nothing in the API for this, so I've had to hack it using setInterval.
var revertInterval;
var revertEvent = function(el) {
revertInterval = setInterval(function() {
// Do things
// e.g. console.log(el.position().top);
}, 5);
}
$('#draggable').draggable({
revert: function() {
revertEvent($(this));
return true;
},
stop: function() {
clearInterval(revertInterval);
}
});
the revert is accept function as follow
revert: function(param){}
DEMO

Categories

Resources