2011-04-08 13:31:06 +00:00
|
|
|
/*
|
2011-05-07 14:22:04 +00:00
|
|
|
* jQuery UI Popup @VERSION
|
|
|
|
*
|
|
|
|
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
|
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Popup
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* jquery.ui.core.js
|
|
|
|
* jquery.ui.widget.js
|
|
|
|
* jquery.ui.position.js
|
2011-04-08 13:31:06 +00:00
|
|
|
*/
|
|
|
|
(function($) {
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
var idIncrement = 0;
|
|
|
|
|
|
|
|
$.widget( "ui.popup", {
|
2011-05-28 19:39:55 +00:00
|
|
|
version: "@VERSION",
|
2011-04-08 13:31:06 +00:00
|
|
|
options: {
|
|
|
|
position: {
|
|
|
|
my: "left top",
|
|
|
|
at: "left bottom"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_create: function() {
|
|
|
|
if ( !this.options.trigger ) {
|
|
|
|
this.options.trigger = this.element.prev();
|
|
|
|
}
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
if ( !this.element.attr( "id" ) ) {
|
|
|
|
this.element.attr( "id", "ui-popup-" + idIncrement++ );
|
|
|
|
this.generatedId = true;
|
|
|
|
}
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
if ( !this.element.attr( "role" ) ) {
|
|
|
|
// TODO alternatives to tooltip are dialog and menu, all three aren't generic popups
|
2011-05-25 12:58:26 +00:00
|
|
|
this.element.attr( "role", "dialog" );
|
2011-04-08 13:31:06 +00:00
|
|
|
this.generatedRole = true;
|
|
|
|
}
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this.options.trigger
|
2011-07-12 15:36:34 +00:00
|
|
|
.attr( "aria-haspopup", "true" )
|
2011-04-08 13:31:06 +00:00
|
|
|
.attr( "aria-owns", this.element.attr( "id" ) );
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this.element
|
2011-05-30 21:37:14 +00:00
|
|
|
.addClass( "ui-popup" )
|
2011-05-04 09:14:32 +00:00
|
|
|
this.close();
|
2011-04-18 15:07:08 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this._bind(this.options.trigger, {
|
2011-05-04 09:20:04 +00:00
|
|
|
keydown: function( event ) {
|
2011-05-30 21:37:14 +00:00
|
|
|
// prevent space-to-open to scroll the page, only happens for anchor ui.button
|
|
|
|
if ( this.options.trigger.is( "a:ui-button" ) && event.keyCode == $.ui.keyCode.SPACE ) {
|
|
|
|
event.preventDefault();
|
2011-05-04 09:20:04 +00:00
|
|
|
}
|
2011-05-07 13:18:50 +00:00
|
|
|
// TODO handle SPACE to open popup? only when not handled by ui.button
|
2011-05-30 21:37:14 +00:00
|
|
|
if ( event.keyCode == $.ui.keyCode.SPACE && this.options.trigger.is( "a:not(:ui-button)" ) ) {
|
2011-05-07 13:18:50 +00:00
|
|
|
this.options.trigger.trigger( "click", event );
|
|
|
|
}
|
|
|
|
// translate keydown to click
|
|
|
|
// opens popup and let's tooltip hide itself
|
|
|
|
if ( event.keyCode == $.ui.keyCode.DOWN ) {
|
2011-05-07 14:23:31 +00:00
|
|
|
// prevent scrolling
|
|
|
|
event.preventDefault();
|
2011-05-07 13:18:50 +00:00
|
|
|
this.options.trigger.trigger( "click", event );
|
|
|
|
}
|
2011-05-04 09:20:04 +00:00
|
|
|
},
|
2011-04-08 13:31:06 +00:00
|
|
|
click: function( event ) {
|
|
|
|
event.preventDefault();
|
2011-05-05 12:20:56 +00:00
|
|
|
if (this.isOpen) {
|
2011-05-04 09:06:32 +00:00
|
|
|
// let it propagate to close
|
|
|
|
return;
|
|
|
|
}
|
2011-04-13 16:37:32 +00:00
|
|
|
var that = this;
|
2011-05-06 17:21:13 +00:00
|
|
|
clearTimeout( this.closeTimer );
|
2011-04-13 16:37:32 +00:00
|
|
|
setTimeout(function() {
|
2011-05-04 09:21:30 +00:00
|
|
|
that.open( event );
|
2011-04-13 16:37:32 +00:00
|
|
|
}, 1);
|
2011-04-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
});
|
2011-05-30 21:37:14 +00:00
|
|
|
|
|
|
|
if ( !this.element.is( ":ui-menu" ) ) {
|
|
|
|
//default use case, wrap tab order in popup
|
2011-06-10 09:42:43 +00:00
|
|
|
this._bind({ keydown : function( event ) {
|
|
|
|
if ( event.keyCode !== $.ui.keyCode.TAB ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var tabbables = $( ":tabbable", this.element ),
|
|
|
|
first = tabbables.first(),
|
|
|
|
last = tabbables.last();
|
|
|
|
if ( event.target === last[ 0 ] && !event.shiftKey ) {
|
|
|
|
first.focus( 1 );
|
|
|
|
event.preventDefault();
|
|
|
|
} else if ( event.target === first[ 0 ] && event.shiftKey ) {
|
|
|
|
last.focus( 1 );
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2011-05-30 21:37:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._bind({
|
2011-05-25 12:58:26 +00:00
|
|
|
focusout: function( event ) {
|
2011-05-06 17:21:13 +00:00
|
|
|
var that = this;
|
|
|
|
// use a timer to allow click to clear it and letting that
|
|
|
|
// handle the closing instead of opening again
|
|
|
|
that.closeTimer = setTimeout( function() {
|
|
|
|
that.close( event );
|
|
|
|
}, 100);
|
2011-05-25 12:58:26 +00:00
|
|
|
},
|
|
|
|
focusin: function( event ) {
|
2011-05-30 21:37:14 +00:00
|
|
|
clearTimeout( this.closeTimer );
|
2011-05-06 17:21:13 +00:00
|
|
|
}
|
2011-04-18 15:07:08 +00:00
|
|
|
});
|
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this._bind({
|
2011-05-25 12:58:26 +00:00
|
|
|
// TODO only triggered on element if it can receive focus
|
2011-05-04 09:06:32 +00:00
|
|
|
// bind to document instead?
|
2011-05-07 13:18:50 +00:00
|
|
|
// either element itself or a child should be focusable
|
2011-04-08 13:31:06 +00:00
|
|
|
keyup: function( event ) {
|
2011-05-30 21:37:14 +00:00
|
|
|
if ( event.keyCode == $.ui.keyCode.ESCAPE && this.element.is( ":visible" ) ) {
|
2011-05-04 09:14:32 +00:00
|
|
|
this.close( event );
|
2011-05-07 13:18:50 +00:00
|
|
|
// TODO move this to close()? would allow menu.select to call popup.close, and get focus back to trigger
|
2011-04-08 13:31:06 +00:00
|
|
|
this.options.trigger.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this._bind(document, {
|
|
|
|
click: function( event ) {
|
2011-05-30 21:37:14 +00:00
|
|
|
if ( this.isOpen && !$(event.target).closest(".ui-popup").length ) {
|
2011-05-04 09:14:32 +00:00
|
|
|
this.close( event );
|
2011-04-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
_destroy: function() {
|
|
|
|
this.element
|
|
|
|
.show()
|
|
|
|
.removeClass( "ui-popup" )
|
2011-07-12 15:36:34 +00:00
|
|
|
.removeAttr( "aria-hidden" )
|
2011-05-30 21:37:14 +00:00
|
|
|
.removeAttr( "aria-expanded" )
|
|
|
|
.unbind( "keypress.ui-popup");
|
2011-04-13 16:37:32 +00:00
|
|
|
|
|
|
|
this.options.trigger
|
2011-07-12 15:36:34 +00:00
|
|
|
.removeAttr( "aria-haspopup" )
|
2011-04-13 16:37:32 +00:00
|
|
|
.removeAttr( "aria-owns" );
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
if ( this.generatedId ) {
|
|
|
|
this.element.removeAttr( "id" );
|
|
|
|
}
|
|
|
|
if ( this.generatedRole ) {
|
|
|
|
this.element.removeAttr( "role" );
|
|
|
|
}
|
|
|
|
},
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-05-04 09:21:30 +00:00
|
|
|
open: function( event ) {
|
2011-04-08 13:31:06 +00:00
|
|
|
var position = $.extend( {}, {
|
|
|
|
of: this.options.trigger
|
|
|
|
}, this.options.position );
|
|
|
|
|
|
|
|
this.element
|
|
|
|
.show()
|
2011-07-12 15:36:34 +00:00
|
|
|
.attr( "aria-hidden", "false" )
|
|
|
|
.attr( "aria-expanded", "true" )
|
2011-05-30 21:37:14 +00:00
|
|
|
.position( position );
|
2011-04-18 15:07:08 +00:00
|
|
|
|
2011-05-30 21:37:14 +00:00
|
|
|
if (this.element.is( ":ui-menu" )) { //popup is a menu
|
|
|
|
this.element.menu( "focus", event, this.element.children( "li" ).first() );
|
2011-05-25 12:58:26 +00:00
|
|
|
this.element.focus();
|
2011-05-30 21:37:14 +00:00
|
|
|
} else {
|
2011-05-25 12:58:26 +00:00
|
|
|
// set focus to the first tabbable element in the popup container
|
2011-05-30 21:37:14 +00:00
|
|
|
// if there are no tabbable elements, set focus on the popup itself
|
|
|
|
var tabbables = this.element.find( ":tabbable" );
|
2011-06-10 09:42:43 +00:00
|
|
|
this.removeTabIndex = false;
|
2011-05-30 21:37:14 +00:00
|
|
|
if ( !tabbables.length ) {
|
|
|
|
if ( !this.element.is(":tabbable") ) {
|
|
|
|
this.element.attr("tabindex", "0");
|
2011-06-10 09:42:43 +00:00
|
|
|
this.removeTabIndex = true;
|
2011-05-30 21:37:14 +00:00
|
|
|
}
|
2011-06-10 09:42:43 +00:00
|
|
|
tabbables = tabbables.add( this.element[ 0 ] );
|
2011-05-30 21:37:14 +00:00
|
|
|
}
|
|
|
|
tabbables.first().focus( 1 );
|
2011-05-06 17:28:18 +00:00
|
|
|
}
|
|
|
|
|
2011-04-18 15:07:08 +00:00
|
|
|
// take trigger out of tab order to allow shift-tab to skip trigger
|
2011-05-30 21:37:14 +00:00
|
|
|
this.options.trigger.attr( "tabindex", -1 );
|
2011-05-05 12:20:56 +00:00
|
|
|
this.isOpen = true;
|
2011-04-08 13:31:06 +00:00
|
|
|
this._trigger( "open", event );
|
|
|
|
},
|
|
|
|
|
2011-05-04 09:14:32 +00:00
|
|
|
close: function( event ) {
|
2011-04-08 13:31:06 +00:00
|
|
|
this.element
|
|
|
|
.hide()
|
2011-07-12 15:36:34 +00:00
|
|
|
.attr( "aria-hidden", "true" )
|
|
|
|
.attr( "aria-expanded", "false" );
|
2011-04-18 15:07:08 +00:00
|
|
|
|
2011-06-10 09:42:43 +00:00
|
|
|
this.options.trigger.attr( "tabindex" , 0 );
|
|
|
|
if ( this.removeTabIndex ) {
|
|
|
|
this.element.removeAttr( "tabindex" );
|
|
|
|
}
|
2011-05-05 12:20:56 +00:00
|
|
|
this.isOpen = false;
|
2011-04-08 13:31:06 +00:00
|
|
|
this._trigger( "close", event );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}(jQuery));
|