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-10-19 09:41:33 +00:00
|
|
|
var idIncrement = 0,
|
|
|
|
suppressExpandOnFocus = false;
|
2011-04-08 13:31:06 +00:00
|
|
|
|
|
|
|
$.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"
|
2011-09-21 15:42:07 +00:00
|
|
|
},
|
2011-10-19 09:41:33 +00:00
|
|
|
managed: false,
|
|
|
|
expandOnFocus: false,
|
2011-09-21 20:48:55 +00:00
|
|
|
show: {
|
|
|
|
effect: "slideDown",
|
|
|
|
duration: "fast"
|
|
|
|
},
|
|
|
|
hide: {
|
|
|
|
effect: "fadeOut",
|
|
|
|
duration: "fast"
|
|
|
|
}
|
2011-04-08 13:31:06 +00:00
|
|
|
},
|
|
|
|
_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" ) ) {
|
2011-10-19 09:41:33 +00:00
|
|
|
if ( !this.options.managed ) {
|
|
|
|
this.element.attr( "role", "dialog" );
|
|
|
|
this.generatedRole = true;
|
|
|
|
}
|
2011-04-08 13:31:06 +00:00
|
|
|
}
|
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-09-21 20:48:55 +00:00
|
|
|
.addClass( "ui-popup" );
|
2011-09-21 15:42:07 +00:00
|
|
|
this._beforeClose();
|
|
|
|
this.element.hide();
|
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-10-19 09:41:33 +00:00
|
|
|
switch ( event.keyCode ) {
|
|
|
|
case $.ui.keyCode.TAB:
|
|
|
|
// Waiting for close() will make popup hide too late, which breaks tab key behavior
|
|
|
|
this.element.hide();
|
|
|
|
this.close( event );
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.ESCAPE:
|
|
|
|
if ( this.isOpen ) {
|
|
|
|
this.close( event );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.SPACE:
|
|
|
|
// prevent space-to-open to scroll the page, only happens for anchor ui.button
|
|
|
|
// TODO check for $.ui.button before using custom selector, once more below
|
|
|
|
if ( this.options.trigger.is( "a:ui-button" ) ) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (this.options.trigger.is( "a:not(:ui-button)" ) ) {
|
|
|
|
this.options.trigger.trigger( "click", event );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case $.ui.keyCode.DOWN:
|
|
|
|
case $.ui.keyCode.UP:
|
|
|
|
// prevent scrolling
|
|
|
|
event.preventDefault();
|
|
|
|
clearTimeout( this.closeTimer );
|
2011-10-19 15:13:05 +00:00
|
|
|
this._delay(function() {
|
|
|
|
this.open( event );
|
|
|
|
this.focusPopup( event );
|
2011-10-19 09:41:33 +00:00
|
|
|
}, 1);
|
|
|
|
break;
|
2011-05-07 13:18:50 +00:00
|
|
|
}
|
2011-05-04 09:20:04 +00:00
|
|
|
},
|
2011-04-08 13:31:06 +00:00
|
|
|
click: function( event ) {
|
2011-10-19 09:41:33 +00:00
|
|
|
event.stopPropagation();
|
2011-04-08 13:31:06 +00:00
|
|
|
event.preventDefault();
|
2011-10-19 09:41:33 +00:00
|
|
|
},
|
|
|
|
mousedown: function( event ) {
|
|
|
|
var noFocus = false;
|
|
|
|
/* TODO: Determine in which cases focus should stay on the trigger after the popup opens
|
|
|
|
(should apply for any trigger that has other interaction besides opening the popup, e.g. a text field) */
|
|
|
|
if ( $( event.target ).is( "input" ) ) {
|
|
|
|
noFocus = true;
|
|
|
|
}
|
2011-05-05 12:20:56 +00:00
|
|
|
if (this.isOpen) {
|
2011-10-19 09:41:33 +00:00
|
|
|
suppressExpandOnFocus = true;
|
|
|
|
this.close();
|
2011-05-04 09:06:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-10-19 09:41:33 +00:00
|
|
|
this.open( event );
|
2011-05-06 17:21:13 +00:00
|
|
|
clearTimeout( this.closeTimer );
|
2011-10-19 15:13:05 +00:00
|
|
|
this._delay( function() {
|
2011-10-19 09:41:33 +00:00
|
|
|
if ( !noFocus ) {
|
2011-10-19 15:13:05 +00:00
|
|
|
this.focusPopup();
|
2011-10-19 09:41:33 +00:00
|
|
|
}
|
2011-10-19 15:13:05 +00:00
|
|
|
}, 1 );
|
2011-04-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
});
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-10-19 09:41:33 +00:00
|
|
|
if ( this.options.expandOnFocus ) {
|
|
|
|
this._bind( this.options.trigger, {
|
|
|
|
focus : function( event ) {
|
|
|
|
if ( !suppressExpandOnFocus ) {
|
2011-10-19 15:13:05 +00:00
|
|
|
this._delay( function() {
|
|
|
|
if ( !this.isOpen ) {
|
|
|
|
this.open( event );
|
2011-10-19 09:41:33 +00:00
|
|
|
}
|
|
|
|
}, 1);
|
|
|
|
}
|
2011-10-19 15:13:05 +00:00
|
|
|
this._delay( function() {
|
2011-10-19 09:41:33 +00:00
|
|
|
suppressExpandOnFocus = false;
|
|
|
|
}, 100);
|
|
|
|
},
|
|
|
|
blur: function( event ) {
|
|
|
|
suppressExpandOnFocus = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if ( !this.options.managed ) {
|
|
|
|
//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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this._bind({
|
2011-10-19 09:41:33 +00:00
|
|
|
focusout: function( event ) {
|
|
|
|
// use a timer to allow click to clear it and letting that
|
|
|
|
// handle the closing instead of opening again
|
2011-10-19 15:13:05 +00:00
|
|
|
this.closeTimer = this._delay( function() {
|
|
|
|
this.close( event );
|
2011-11-09 13:45:10 +00:00
|
|
|
}, 150);
|
2011-10-19 09:41:33 +00:00
|
|
|
},
|
|
|
|
focusin: function( event ) {
|
|
|
|
clearTimeout( this.closeTimer );
|
|
|
|
},
|
|
|
|
mouseup: function( event ) {
|
|
|
|
clearTimeout( this.closeTimer );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._bind({
|
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-10-19 09:41:33 +00:00
|
|
|
this.focusTrigger();
|
2011-04-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-05-30 21:37:14 +00:00
|
|
|
|
2011-10-20 20:57:47 +00:00
|
|
|
this._bind( this.document, {
|
2011-04-08 13:31:06 +00:00
|
|
|
click: function( event ) {
|
2011-10-19 09:41:33 +00:00
|
|
|
if ( this.isOpen && !$( event.target ).closest( this.element.add( this.options.trigger ) ).length ) {
|
2011-05-04 09:14:32 +00:00
|
|
|
this.close( event );
|
2011-04-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-21 20:48:55 +00:00
|
|
|
});
|
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 );
|
|
|
|
|
2011-09-21 15:42:07 +00:00
|
|
|
this._show( this.element, this.options.show );
|
2011-04-08 13:31:06 +00:00
|
|
|
this.element
|
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-10-19 09:41:33 +00:00
|
|
|
// take trigger out of tab order to allow shift-tab to skip trigger
|
|
|
|
this.options.trigger.attr( "tabindex", -1 );
|
|
|
|
this.isOpen = true;
|
|
|
|
this._trigger( "open", event );
|
|
|
|
},
|
|
|
|
|
|
|
|
focusPopup: function( event ) {
|
|
|
|
if ( !this.options.managed ) {
|
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-10-19 09:41:33 +00:00
|
|
|
this._trigger( "focusPopup", event );
|
|
|
|
},
|
2011-05-06 17:28:18 +00:00
|
|
|
|
2011-10-19 09:41:33 +00:00
|
|
|
focusTrigger: function( event ) {
|
|
|
|
suppressExpandOnFocus = true;
|
|
|
|
this.options.trigger.focus();
|
|
|
|
this._trigger( "focusTrigger", event );
|
2011-04-08 13:31:06 +00:00
|
|
|
},
|
|
|
|
|
2011-05-04 09:14:32 +00:00
|
|
|
close: function( event ) {
|
2011-09-21 15:42:07 +00:00
|
|
|
this._beforeClose();
|
|
|
|
this._hide( this.element, this.options.hide );
|
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 );
|
2011-09-21 15:42:07 +00:00
|
|
|
},
|
2011-09-21 20:48:55 +00:00
|
|
|
|
2011-09-21 15:42:07 +00:00
|
|
|
_beforeClose: function() {
|
|
|
|
this.element
|
|
|
|
.attr( "aria-hidden", "true" )
|
|
|
|
.attr( "aria-expanded", "false" );
|
2011-04-08 13:31:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}(jQuery));
|