jQuery UI Menubar Phantom Submenu Issue - jquery-ui

I am creating a jQuery menubar, and everything is looking good but I get a weird effect when I mouse over a top level menu item that has a submenu where the first item also has a sub menu. If I mouse in and out (moving left and right over the View menu item) like 20 times, I will start to see the View>Encoding sub-submenu moving more and more to the right.
I can recreate the issue with a modified version of the menubar demo example. I am on firefox 20.0.1.
See here: http://jsfiddle.net/Njjgm/
I figure it is finding the right edge of the subsub menu and then setting the new left edge of the same subsub menu to that position... So if I slow down the opening or fix the positioning math then I shouldn't have that bug.
I'm looking in the jquery.ui.menubar.js file to see if I can adjust the setTimeouts, or fix the subsub menu positioning but not having any luck.
I'm looking at this (from jquery.ui.menubar.js line 262):
__applyMouseBehaviorForSubmenuHavingMenuItem: function (input, menubar) {
var menu = input.next( menubar.options.menuElement ),
mouseBehaviorCallback = function( event ) {
// ignore triggered focus event
if ( event.type === "focus" && !event.originalEvent ) {
return;
}
if (event.type === "mouseenter") {
this.element.find(":focus").focusout();
if (this.stashedOpenMenu) {
this._open( event, menu);
}
this.stashedOpenMenu = undefined;
}
if ((this.open && event.type === "mouseenter")
|| this.options.autoExpand) {
if (this.options.autoExpand) {
clearTimeout( this.closeTimer );
}
this._open( event, menu );
}
};
And also at this: (from jquery.ui.menubar.js line 68)
focusin: function( event ) {
clearTimeout( menubar.closeTimer );
},
focusout: function( event ) {
menubar.closeTimer = setTimeout (function() {
menubar._close( event );
}, 150 );
},
"mouseleave .ui-menubar-item": function( event ) {
if ( menubar.options.autoExpand ) {
menubar.closeTimer = setTimeout( function() {
menubar._close( event );
}, 150 );
}
},
"mouseenter .ui-menubar-item": function( event ) {
clearTimeout( menubar.closeTimer );
}
Has anyone with jquery ui menubar experience seen and fixed this before? Does anyone know a fix with setTimeout? HoverIntent seems to use the same set/clearTimeout technique as menubar, so I don't want to rip out all the logic from menubar to add that in. Any suggestions welcome. Thanks.

Well I found a work around to the issue, though I didn't fix the buggy positioning (but I think I found where it could be fixed).
The code by default selects the first list element in a opened menu, and if that element has a menu too, that menu also opens. I took out the code that puts focus on the first child element (why does it even need to do that?)
from jquery.ui.menubar.js line 451
this.active = menu
.show()
.position($.extend({
of: button
}, this.options.position));
//.removeAttr("aria-hidden")
//.attr("aria-expanded", "true")
//.menu("focus", event, menu.children(".ui-menu-item").first() )
// TODO need a comment here why both events are triggered
//.focus()
//.focusin();
And Presto-Changeo! No more phantom drop down menus... I could have tried to figure out the menu.position but this worked for me.

Related

Apply effect on an unselected tab anchor

Is there a way to apply a jQuery UI effect, such as the pulsate effect (as described here) on an unselected tab of a tab strip, leaving that tab unselected?
What I'm trying to obtain is to tell the user to pay attention on a tab X while he's working on a tab Y, because some action caused the tab X to reload its contents.
You can create a customized tabs widget with the notification behavior built into it:
$.widget( "app.tabs", $.ui.tabs, {
// Applies the "ui-state-highlight" class
// to the given tab index.
notify: function( index ) {
if ( this.active.index() === index ) {
return;
}
this.tabs.eq( index )
.addClass( "ui-state-highlight" );
},
// Removes the "ui-state-highlight" class
// from the given tab index.
unnotify: function( index ) {
this.tabs.eq( index )
.removeClass( "ui-state-highlight" );
},
// Make sure active tabs don't have the
// "ui-state-highlight" class applied.
_toggle: function( e, ui ) {
this._super( e, ui );
this.unnotify( ui.newTab.index() );
}
});
$(function() {
$( "#tabs" ).tabs()
.tabs( "notify", 1 );
});
You're basically just adding two new methods to the widget - notify() and unnotify(). The _toggle() method we've overridden here just makes sure that when we navigate to the tab that's been notified, we turn off the notification by calling unnotify().
This code is just adding the ui-state-highlight class as the notification mechanism, but that can easily be replaced with something else, like an effect. Here's the original fiddle.

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.

jQuery-ui Tooltip get Title on click

I'm working with jquery-ui. I can create elements with titles and show the titles. However on a click, I would like to take the title and populate another div (this is because touch enabled devices do not have tooltips). I can get a click event, but I can't get the title while in the click event.
$("div").click(function( event ) {
// just to prove that we are entering this event
$("#preShow").html ( Date() );
// show the title
var toolTip = this.attributes.title.value;
$("#show").html ( toolTip );
// just to prove that there was no crash
$("#postShow").html ( Date() );
});
I have also tried using
var toolTip = $(this).attr ("title");
Here is the jsfiddle showing the problem
http://jsfiddle.net/jK5xQ/
The same code works if I create an HTML file and run it in Firefox with a breakpoint at the first line of the click event. Has anyone experienced this?
This is because jQueryUI's Tooltip removes the title and uses it. Try going about it like this...
$(document).ready(function () {
$( document ).tooltip( {
track: true,
content: function() {
return $( this ).attr( "title" );
}
});
$('div').click(function(){
$('#show').html($('#' + $(this).attr('aria-describedby')).children().html());
});
});
DEMO: http://jsfiddle.net/jK5xQ/4/
Let me know if you have any questions!

jquery ui drag element without keeping mouse button down (follow the cursor)

I am trying to drag an element without keeping the mouse button down.
The behavior I would like is :
I click on a draggable item
Drag my item without keeping mouse left click down : just follow the mouse as a cursor
I click on a droppable container to confirm and append my item
I am able to simulate this behavior if I add an alert box durring the start event.
start : function(){
alert('test')
},
Here is the fiddle : http://jsfiddle.net/QvRjL/103/
How is it possible to code this behavior without the alert box?
Here is a good solution about this problem : Trigger Click-and-Hold Event
http://jsfiddle.net/VXbpu/1/
http://jsfiddle.net/vPruR/70/
var click = false;
$(document).bind('mousemove', function (e) {
if (click == true) {
$('#your_div_id').css({
left: e.pageX,
top: e.pageY
});
}
});
$('#your_div_id').click(function() {
click = !click;
return false;
});
$('html').click(function() {
click = false;
});

jquery ui tooltip manual open /close

is there a way to manually open close the jquery ui tooltip? I just want it to react to a click event toggling on/off. You can unbind all mouse events and it will rebind them when calling .tooltip('open'), even though that should not initialize or set events imo, since if you try to run .tooltip('open') without initializing, it complains loudly about not being initialized.
jltwoo, can I suggest to use two different boolean switches to enable auto-open and auto-close? With this change your code will look like this:
(function( $ ) {
$.widget( "custom.tooltipX", $.ui.tooltip, {
options: {
autoShow: true,
autoHide: true
},
_create: function() {
this._super();
if(!this.options.autoShow){
this._off(this.element, "mouseover focusin");
}
},
_open: function( event, target, content ) {
this._superApply(arguments);
if(!this.options.autoHide){
this._off(target, "mouseleave focusout");
}
}
});
}( jQuery ) );
In this way, initializing the tooltip as:
$(someDOM).tooltipX({ autoHide:false });
it shows by itself when the mouse is over the element but you have to manually close it.
If you want to manually control both open and close actions, you can simply use:
$(someDOM).tooltipX({ autoShow:false, autoHide:false });
If you want to just unbind the events and woudn't like to make your own custom tooltip.
$("#some-id").tooltip(tooltip_settings)
.on('mouseout focusout', function(event) {
event.stopImmediatePropagation();
});
$("#some-id").attr("title", "Message");
$("#some-id").tooltip("open");
mouseout blocks the tooltop disappearing by moving the mouse cursor
focusout blocks the tooltop disappearing by keyboard navigation
The tooltip have a disable option. Well i used it and here is the code:
$('a').tooltip({
disabled: true
}).click(function(){
if($(this).tooltip('option', 'disabled'))
$(this).tooltip('option', {disabled: false}).tooltip('open');
else
$(this).tooltip('option', {disabled: true}).tooltip('close');
}).hover(function(){
$(this).tooltip('option', {disabled: true}).tooltip('close');
}, function(){
$(this).tooltip('option', {disabled: true}).tooltip('close');
});
Related to my other comment, I looked into the original code and achieved manual open/close by extending the widget and adding a autoHide option with version JQuery-UI v1.10.3. Basically I just remove the mouse listeners that were added in _create and the internal _open call.
Edit: Separated autoHide and autoShow as two separate flags as suggested by #MscG
Demo Here:
http://jsfiddle.net/BfSz3/
(function( $ ) {
$.widget( "custom.tooltipX", $.ui.tooltip, {
options: {
autoHide:true,
autoShow: true
},
_create: function() {
this._super();
if(!this.options.autoShow){
this._off(this.element, "mouseover focusin");
}
},
_open: function( event, target, content ) {
this._superApply(arguments);
if(!this.options.autoHide){
this._off(target, "mouseleave focusout");
}
}
});
}( jQuery ) );
Now when you initialize you can set the tooltip to manually show or hide by setting autoHide : false:
$(someDOM).tooltipX({ autoHide:false });
And just directly perform standard open/close calls in your code as needed elsewhere
$(someDOM).tooltipX("open"); // displays tooltip
$(someDOM).tooltipX("close"); // closes tooltip
A simple hotfix, until I have the time to do official pull request, this will have to do.
Some compilation from other SO questions.
Example
Show tooltip on hint click, and hide tooltip on elsevere click
$(document).on('click', '.hint', function(){ //init new tooltip on click
$(this).tooltip({
position: { my: 'left+15 center', at: 'center right' },
show: false,
hide: false
}).tooltip('open'); // show new tooltip
}).on('click', function(event){ // click everywhere
if(!$(event.target).hasClass('hint'))
$(".hint").each(function(){
var $element = $(this);
if($element.data('ui-tooltip')) { // remove tooltip only from initialized elements
$element.tooltip('destroy');
}
})
});
$('.hint').on('mouseout focusout', function(event) { // prevent auto hide tooltip
event.stopImmediatePropagation();
});

Resources