Skip to main content

Search and Top Navigation

#8359 closed enhancement (wontfix)

Opened May 29, 2012 02:51PM UTC

Closed May 29, 2012 03:12PM UTC

Add "beforeOpen" event to Dialog

Reported by: BenVercammen Owned by:
Priority: minor Milestone: 1.9.0
Component: ui.dialog Version: 1.8.20
Keywords: Cc:
Blocked by: Blocking:
Description

I would like to have a "beforeOpen" event for the Dialog class. I've already patched jquery.ui.dialog.js like this:

	open: function() {
		if (this._isOpen) { return; }

		var self = this,
			options = self.options,
			uiDialog = self.uiDialog;

+		if (false === self._trigger('beforeOpen')) {
+			return;
+		}
+
		self.overlay = options.modal ? new $.ui.dialog.overlay(self) : null;
		self._size();
		self._position(options.position);
		uiDialog.show(options.show);
		self.moveToTop(true);

I need this event for the following situation:

  • when opening/closing a Dialog, the "focus" is reset (to adapt to the new elements being visible/hidden)
  • this means that you lose focus after closing a dialog and have to start "tabbing" again to get to the point where you left

With my current solution being:

  • use the "beforeOpen" event to attach the current ":focus" element to the dialog (via .prop())
  • use the "close" event to set focus again
/**
 * @param id	The id of the dialog element
 */
function bindDialogFocusEvents(id) {
	$('#'+id).bind('dialogclose', function(event) {
		$(this).prop('focused').focus();
	});
	$('#'+id).bind('dialogbeforeopen', function(event) {
		var target = $(':focus');
		$(this).prop('focused', target);
	});
}
$("#new-dialog").dialog({modal: true, ...});
bindDialogFocusEvents('new-dialog');

Maybe it's possible to add the "focus restoration" functionality to the dialog code as well, but I would be happy with just the "beforeOpen" event for now...

Attachments (0)
Change History (1)

Changed May 29, 2012 03:12PM UTC by scottgonzalez comment:1

resolution: → wontfix
status: newclosed

This just needs to be built-in behavior. The only tricky part is figuring out what to do when non-modal dialogs close and the user has interacted with other elements after opening the dialog. This should be addressed in the API redesign, but feel free to create a new ticket for this right now if you want to.