2011-04-08 13:31:06 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI popup utility
|
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
|
|
|
|
var idIncrement = 0;
|
|
|
|
|
|
|
|
$.widget( "ui.popup", {
|
|
|
|
options: {
|
|
|
|
position: {
|
|
|
|
my: "left top",
|
|
|
|
at: "left bottom"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_create: function() {
|
|
|
|
if ( !this.options.trigger ) {
|
|
|
|
this.options.trigger = this.element.prev();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !this.element.attr( "id" ) ) {
|
|
|
|
this.element.attr( "id", "ui-popup-" + idIncrement++ );
|
|
|
|
this.generatedId = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !this.element.attr( "role" ) ) {
|
|
|
|
// TODO alternatives to tooltip are dialog and menu, all three aren't generic popups
|
|
|
|
this.element.attr( "role", "tooltip" );
|
|
|
|
this.generatedRole = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.options.trigger
|
2011-04-13 16:37:32 +00:00
|
|
|
.attr( "aria-haspopup", true )
|
2011-04-08 13:31:06 +00:00
|
|
|
.attr( "aria-owns", this.element.attr( "id" ) );
|
|
|
|
|
|
|
|
this.element
|
|
|
|
.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 ) {
|
|
|
|
// prevent space-to-open to scroll the page
|
|
|
|
if (event.keyCode == $.ui.keyCode.SPACE) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
},
|
2011-04-08 13:31:06 +00:00
|
|
|
click: function( event ) {
|
|
|
|
event.preventDefault();
|
2011-05-04 09:06:32 +00:00
|
|
|
if (this.open) {
|
|
|
|
// let it propagate to close
|
|
|
|
return;
|
|
|
|
}
|
2011-04-13 16:37:32 +00:00
|
|
|
var that = this;
|
|
|
|
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-04-18 15:07:08 +00:00
|
|
|
this._bind(this.element, {
|
2011-05-04 09:06:32 +00:00
|
|
|
// TODO also triggered when open and clicking the trigger again
|
|
|
|
// figure out how to close in that case, while still closing on regular blur
|
2011-05-04 09:14:32 +00:00
|
|
|
//blur: "close"
|
2011-04-18 15:07:08 +00:00
|
|
|
});
|
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this._bind({
|
2011-05-04 09:06:32 +00:00
|
|
|
// TODO only triggerd on element if it can receive focus
|
|
|
|
// bind to document instead?
|
2011-04-08 13:31:06 +00:00
|
|
|
keyup: function( event ) {
|
|
|
|
if (event.keyCode == $.ui.keyCode.ESCAPE && this.element.is( ":visible" )) {
|
2011-05-04 09:14:32 +00:00
|
|
|
this.close( event );
|
2011-04-08 13:31:06 +00:00
|
|
|
this.options.trigger.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._bind(document, {
|
|
|
|
click: function( event ) {
|
|
|
|
if (this.open && !$(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
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_destroy: function() {
|
|
|
|
this.element
|
|
|
|
.show()
|
|
|
|
.removeClass( "ui-popup" )
|
|
|
|
.removeAttr( "aria-hidden" )
|
|
|
|
.removeAttr( "aria-expanded" );
|
2011-04-13 16:37:32 +00:00
|
|
|
|
|
|
|
this.options.trigger
|
|
|
|
.removeAttr( "aria-haspopup" )
|
|
|
|
.removeAttr( "aria-owns" );
|
2011-04-08 13:31:06 +00:00
|
|
|
|
|
|
|
if ( this.generatedId ) {
|
|
|
|
this.element.removeAttr( "id" );
|
|
|
|
}
|
|
|
|
if ( this.generatedRole ) {
|
|
|
|
this.element.removeAttr( "role" );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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()
|
|
|
|
.attr( "aria-hidden", false )
|
|
|
|
.attr( "aria-expanded", true )
|
|
|
|
.position( position )
|
|
|
|
.focus();
|
2011-04-18 15:07:08 +00:00
|
|
|
|
|
|
|
// take trigger out of tab order to allow shift-tab to skip trigger
|
|
|
|
this.options.trigger.attr("tabindex", -1);
|
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this.open = true;
|
|
|
|
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()
|
|
|
|
.attr( "aria-hidden", true )
|
|
|
|
.attr( "aria-expanded", false );
|
2011-04-18 15:07:08 +00:00
|
|
|
|
|
|
|
this.options.trigger.attr("tabindex", 0);
|
|
|
|
|
2011-04-08 13:31:06 +00:00
|
|
|
this.open = false;
|
|
|
|
this._trigger( "close", event );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}(jQuery));
|