2 * jQuery UI Dialog 1.10.3
5 * Copyright 2013 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/dialog/
15 * jquery.ui.draggable.js
17 * jquery.ui.position.js
18 * jquery.ui.resizable.js
20 (function( $, undefined ) {
22 var sizeRelatedOptions = {
31 resizableRelatedOptions = {
38 $.widget( "ui.dialog", {
60 // Ensure the titlebar is always visible
61 using: function( pos ) {
62 var topOffset = $( this ).css( pos ).offset().top;
63 if ( topOffset < 0 ) {
64 $( this ).css( "top", pos.top - topOffset );
88 display: this.element[0].style.display,
89 width: this.element[0].style.width,
90 minHeight: this.element[0].style.minHeight,
91 maxHeight: this.element[0].style.maxHeight,
92 height: this.element[0].style.height
94 this.originalPosition = {
95 parent: this.element.parent(),
96 index: this.element.parent().children().index( this.element )
98 this.originalTitle = this.element.attr("title");
99 this.options.title = this.options.title || this.originalTitle;
101 this._createWrapper();
106 .addClass("ui-dialog-content ui-widget-content")
107 .appendTo( this.uiDialog );
109 this._createTitlebar();
110 this._createButtonPane();
112 if ( this.options.draggable && $.fn.draggable ) {
113 this._makeDraggable();
115 if ( this.options.resizable && $.fn.resizable ) {
116 this._makeResizable();
119 this._isOpen = false;
123 if ( this.options.autoOpen ) {
128 _appendTo: function() {
129 var element = this.options.appendTo;
130 if ( element && (element.jquery || element.nodeType) ) {
133 return this.document.find( element || "body" ).eq( 0 );
136 _destroy: function() {
138 originalPosition = this.originalPosition;
140 this._destroyOverlay();
144 .removeClass("ui-dialog-content ui-widget-content")
145 .css( this.originalCss )
146 // Without detaching first, the following becomes really slow
149 this.uiDialog.stop( true, true ).remove();
151 if ( this.originalTitle ) {
152 this.element.attr( "title", this.originalTitle );
155 next = originalPosition.parent.children().eq( originalPosition.index );
156 // Don't try to place the dialog next to itself (#8613)
157 if ( next.length && next[0] !== this.element[0] ) {
158 next.before( this.element );
160 originalPosition.parent.append( this.element );
165 return this.uiDialog;
171 close: function( event ) {
174 if ( !this._isOpen || this._trigger( "beforeClose", event ) === false ) {
178 this._isOpen = false;
179 this._destroyOverlay();
181 if ( !this.opener.filter(":focusable").focus().length ) {
182 // Hiding a focused element doesn't trigger blur in WebKit
183 // so in case we have nothing to focus on, explicitly blur the active element
184 // https://bugs.webkit.org/show_bug.cgi?id=47182
185 $( this.document[0].activeElement ).blur();
188 this._hide( this.uiDialog, this.options.hide, function() {
189 that._trigger( "close", event );
197 moveToTop: function() {
201 _moveToTop: function( event, silent ) {
202 var moved = !!this.uiDialog.nextAll(":visible").insertBefore( this.uiDialog ).length;
203 if ( moved && !silent ) {
204 this._trigger( "focus", event );
211 if ( this._isOpen ) {
212 if ( this._moveToTop() ) {
213 this._focusTabbable();
219 this.opener = $( this.document[0].activeElement );
223 this._createOverlay();
224 this._moveToTop( null, true );
225 this._show( this.uiDialog, this.options.show, function() {
226 that._focusTabbable();
227 that._trigger("focus");
230 this._trigger("open");
233 _focusTabbable: function() {
234 // Set focus to the first match:
235 // 1. First element inside the dialog matching [autofocus]
236 // 2. Tabbable element inside the content element
237 // 3. Tabbable element inside the buttonpane
238 // 4. The close button
239 // 5. The dialog itself
240 var hasFocus = this.element.find("[autofocus]");
241 if ( !hasFocus.length ) {
242 hasFocus = this.element.find(":tabbable");
244 if ( !hasFocus.length ) {
245 hasFocus = this.uiDialogButtonPane.find(":tabbable");
247 if ( !hasFocus.length ) {
248 hasFocus = this.uiDialogTitlebarClose.filter(":tabbable");
250 if ( !hasFocus.length ) {
251 hasFocus = this.uiDialog;
253 hasFocus.eq( 0 ).focus();
256 _keepFocus: function( event ) {
257 function checkFocus() {
258 var activeElement = this.document[0].activeElement,
259 isActive = this.uiDialog[0] === activeElement ||
260 $.contains( this.uiDialog[0], activeElement );
262 this._focusTabbable();
265 event.preventDefault();
266 checkFocus.call( this );
268 // IE <= 8 doesn't prevent moving focus even with event.preventDefault()
269 // so we check again later
270 this._delay( checkFocus );
273 _createWrapper: function() {
274 this.uiDialog = $("<div>")
275 .addClass( "ui-dialog ui-widget ui-widget-content ui-corner-all ui-front " +
276 this.options.dialogClass )
279 // Setting tabIndex makes the div focusable
283 .appendTo( this._appendTo() );
285 this._on( this.uiDialog, {
286 keydown: function( event ) {
287 if ( this.options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode &&
288 event.keyCode === $.ui.keyCode.ESCAPE ) {
289 event.preventDefault();
294 // prevent tabbing out of dialogs
295 if ( event.keyCode !== $.ui.keyCode.TAB ) {
298 var tabbables = this.uiDialog.find(":tabbable"),
299 first = tabbables.filter(":first"),
300 last = tabbables.filter(":last");
302 if ( ( event.target === last[0] || event.target === this.uiDialog[0] ) && !event.shiftKey ) {
304 event.preventDefault();
305 } else if ( ( event.target === first[0] || event.target === this.uiDialog[0] ) && event.shiftKey ) {
307 event.preventDefault();
310 mousedown: function( event ) {
311 if ( this._moveToTop( event ) ) {
312 this._focusTabbable();
317 // We assume that any existing aria-describedby attribute means
318 // that the dialog content is marked up properly
319 // otherwise we brute force the content as the description
320 if ( !this.element.find("[aria-describedby]").length ) {
322 "aria-describedby": this.element.uniqueId().attr("id")
327 _createTitlebar: function() {
330 this.uiDialogTitlebar = $("<div>")
331 .addClass("ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix")
332 .prependTo( this.uiDialog );
333 this._on( this.uiDialogTitlebar, {
334 mousedown: function( event ) {
335 // Don't prevent click on close button (#8838)
336 // Focusing a dialog that is partially scrolled out of view
337 // causes the browser to scroll it into view, preventing the click event
338 if ( !$( event.target ).closest(".ui-dialog-titlebar-close") ) {
339 // Dialog isn't getting focus when dragging (#8063)
340 this.uiDialog.focus();
345 this.uiDialogTitlebarClose = $("<button></button>")
347 label: this.options.closeText,
349 primary: "ui-icon-closethick"
353 .addClass("ui-dialog-titlebar-close")
354 .appendTo( this.uiDialogTitlebar );
355 this._on( this.uiDialogTitlebarClose, {
356 click: function( event ) {
357 event.preventDefault();
362 uiDialogTitle = $("<span>")
364 .addClass("ui-dialog-title")
365 .prependTo( this.uiDialogTitlebar );
366 this._title( uiDialogTitle );
369 "aria-labelledby": uiDialogTitle.attr("id")
373 _title: function( title ) {
374 if ( !this.options.title ) {
375 title.html(" ");
377 title.text( this.options.title );
380 _createButtonPane: function() {
381 this.uiDialogButtonPane = $("<div>")
382 .addClass("ui-dialog-buttonpane ui-widget-content ui-helper-clearfix");
384 this.uiButtonSet = $("<div>")
385 .addClass("ui-dialog-buttonset")
386 .appendTo( this.uiDialogButtonPane );
388 this._createButtons();
391 _createButtons: function() {
393 buttons = this.options.buttons;
395 // if we already have a button pane, remove it
396 this.uiDialogButtonPane.remove();
397 this.uiButtonSet.empty();
399 if ( $.isEmptyObject( buttons ) || ($.isArray( buttons ) && !buttons.length) ) {
400 this.uiDialog.removeClass("ui-dialog-buttons");
404 $.each( buttons, function( name, props ) {
405 var click, buttonOptions;
406 props = $.isFunction( props ) ?
407 { click: props, text: name } :
409 // Default to a non-submitting button
410 props = $.extend( { type: "button" }, props );
411 // Change the context for the click callback to be the main element
413 props.click = function() {
414 click.apply( that.element[0], arguments );
421 delete props.showText;
422 $( "<button></button>", props )
423 .button( buttonOptions )
424 .appendTo( that.uiButtonSet );
426 this.uiDialog.addClass("ui-dialog-buttons");
427 this.uiDialogButtonPane.appendTo( this.uiDialog );
430 _makeDraggable: function() {
432 options = this.options;
434 function filteredUi( ui ) {
436 position: ui.position,
441 this.uiDialog.draggable({
442 cancel: ".ui-dialog-content, .ui-dialog-titlebar-close",
443 handle: ".ui-dialog-titlebar",
444 containment: "document",
445 start: function( event, ui ) {
446 $( this ).addClass("ui-dialog-dragging");
448 that._trigger( "dragStart", event, filteredUi( ui ) );
450 drag: function( event, ui ) {
451 that._trigger( "drag", event, filteredUi( ui ) );
453 stop: function( event, ui ) {
455 ui.position.left - that.document.scrollLeft(),
456 ui.position.top - that.document.scrollTop()
458 $( this ).removeClass("ui-dialog-dragging");
459 that._unblockFrames();
460 that._trigger( "dragStop", event, filteredUi( ui ) );
465 _makeResizable: function() {
467 options = this.options,
468 handles = options.resizable,
469 // .ui-resizable has position: relative defined in the stylesheet
470 // but dialogs have to use absolute or fixed positioning
471 position = this.uiDialog.css("position"),
472 resizeHandles = typeof handles === "string" ?
474 "n,e,s,w,se,sw,ne,nw";
476 function filteredUi( ui ) {
478 originalPosition: ui.originalPosition,
479 originalSize: ui.originalSize,
480 position: ui.position,
485 this.uiDialog.resizable({
486 cancel: ".ui-dialog-content",
487 containment: "document",
488 alsoResize: this.element,
489 maxWidth: options.maxWidth,
490 maxHeight: options.maxHeight,
491 minWidth: options.minWidth,
492 minHeight: this._minHeight(),
493 handles: resizeHandles,
494 start: function( event, ui ) {
495 $( this ).addClass("ui-dialog-resizing");
497 that._trigger( "resizeStart", event, filteredUi( ui ) );
499 resize: function( event, ui ) {
500 that._trigger( "resize", event, filteredUi( ui ) );
502 stop: function( event, ui ) {
503 options.height = $( this ).height();
504 options.width = $( this ).width();
505 $( this ).removeClass("ui-dialog-resizing");
506 that._unblockFrames();
507 that._trigger( "resizeStop", event, filteredUi( ui ) );
510 .css( "position", position );
513 _minHeight: function() {
514 var options = this.options;
516 return options.height === "auto" ?
518 Math.min( options.minHeight, options.height );
521 _position: function() {
522 // Need to show the dialog to get the actual offset in the position plugin
523 var isVisible = this.uiDialog.is(":visible");
525 this.uiDialog.show();
527 this.uiDialog.position( this.options.position );
529 this.uiDialog.hide();
533 _setOptions: function( options ) {
536 resizableOptions = {};
538 $.each( options, function( key, value ) {
539 that._setOption( key, value );
541 if ( key in sizeRelatedOptions ) {
544 if ( key in resizableRelatedOptions ) {
545 resizableOptions[ key ] = value;
553 if ( this.uiDialog.is(":data(ui-resizable)") ) {
554 this.uiDialog.resizable( "option", resizableOptions );
558 _setOption: function( key, value ) {
559 /*jshint maxcomplexity:15*/
560 var isDraggable, isResizable,
561 uiDialog = this.uiDialog;
563 if ( key === "dialogClass" ) {
565 .removeClass( this.options.dialogClass )
569 if ( key === "disabled" ) {
573 this._super( key, value );
575 if ( key === "appendTo" ) {
576 this.uiDialog.appendTo( this._appendTo() );
579 if ( key === "buttons" ) {
580 this._createButtons();
583 if ( key === "closeText" ) {
584 this.uiDialogTitlebarClose.button({
585 // Ensure that we always pass a string
590 if ( key === "draggable" ) {
591 isDraggable = uiDialog.is(":data(ui-draggable)");
592 if ( isDraggable && !value ) {
593 uiDialog.draggable("destroy");
596 if ( !isDraggable && value ) {
597 this._makeDraggable();
601 if ( key === "position" ) {
605 if ( key === "resizable" ) {
606 // currently resizable, becoming non-resizable
607 isResizable = uiDialog.is(":data(ui-resizable)");
608 if ( isResizable && !value ) {
609 uiDialog.resizable("destroy");
612 // currently resizable, changing handles
613 if ( isResizable && typeof value === "string" ) {
614 uiDialog.resizable( "option", "handles", value );
617 // currently non-resizable, becoming resizable
618 if ( !isResizable && value !== false ) {
619 this._makeResizable();
623 if ( key === "title" ) {
624 this._title( this.uiDialogTitlebar.find(".ui-dialog-title") );
629 // If the user has resized the dialog, the .ui-dialog and .ui-dialog-content
630 // divs will both have width and height set, so we need to reset them
631 var nonContentHeight, minContentHeight, maxContentHeight,
632 options = this.options;
634 // Reset content sizing
635 this.element.show().css({
642 if ( options.minWidth > options.width ) {
643 options.width = options.minWidth;
646 // reset wrapper sizing
647 // determine the height of all the non-content elements
648 nonContentHeight = this.uiDialog.css({
653 minContentHeight = Math.max( 0, options.minHeight - nonContentHeight );
654 maxContentHeight = typeof options.maxHeight === "number" ?
655 Math.max( 0, options.maxHeight - nonContentHeight ) :
658 if ( options.height === "auto" ) {
660 minHeight: minContentHeight,
661 maxHeight: maxContentHeight,
665 this.element.height( Math.max( 0, options.height - nonContentHeight ) );
668 if (this.uiDialog.is(":data(ui-resizable)") ) {
669 this.uiDialog.resizable( "option", "minHeight", this._minHeight() );
673 _blockFrames: function() {
674 this.iframeBlocks = this.document.find( "iframe" ).map(function() {
675 var iframe = $( this );
679 position: "absolute",
680 width: iframe.outerWidth(),
681 height: iframe.outerHeight()
683 .appendTo( iframe.parent() )
684 .offset( iframe.offset() )[0];
688 _unblockFrames: function() {
689 if ( this.iframeBlocks ) {
690 this.iframeBlocks.remove();
691 delete this.iframeBlocks;
695 _allowInteraction: function( event ) {
696 if ( $( event.target ).closest(".ui-dialog").length ) {
700 // TODO: Remove hack when datepicker implements
701 // the .ui-front logic (#8989)
702 return !!$( event.target ).closest(".ui-datepicker").length;
705 _createOverlay: function() {
706 if ( !this.options.modal ) {
711 widgetFullName = this.widgetFullName;
712 if ( !$.ui.dialog.overlayInstances ) {
713 // Prevent use of anchors and inputs.
714 // We use a delay in case the overlay is created from an
715 // event that we're going to be cancelling. (#2804)
716 this._delay(function() {
717 // Handle .dialog().dialog("close") (#4065)
718 if ( $.ui.dialog.overlayInstances ) {
719 this.document.bind( "focusin.dialog", function( event ) {
720 if ( !that._allowInteraction( event ) ) {
721 event.preventDefault();
722 $(".ui-dialog:visible:last .ui-dialog-content")
723 .data( widgetFullName )._focusTabbable();
730 this.overlay = $("<div>")
731 .addClass("ui-widget-overlay ui-front")
732 .appendTo( this._appendTo() );
733 this._on( this.overlay, {
734 mousedown: "_keepFocus"
736 $.ui.dialog.overlayInstances++;
739 _destroyOverlay: function() {
740 if ( !this.options.modal ) {
744 if ( this.overlay ) {
745 $.ui.dialog.overlayInstances--;
747 if ( !$.ui.dialog.overlayInstances ) {
748 this.document.unbind( "focusin.dialog" );
750 this.overlay.remove();
756 $.ui.dialog.overlayInstances = 0;
759 if ( $.uiBackCompat !== false ) {
760 // position option with array notation
761 // just override with old implementation
762 $.widget( "ui.dialog", $.ui.dialog, {
763 _position: function() {
764 var position = this.options.position,
770 if ( typeof position === "string" || (typeof position === "object" && "0" in position ) ) {
771 myAt = position.split ? position.split(" ") : [ position[0], position[1] ];
772 if ( myAt.length === 1 ) {
776 $.each( [ "left", "top" ], function( i, offsetPosition ) {
777 if ( +myAt[ i ] === myAt[ i ] ) {
778 offset[ i ] = myAt[ i ];
779 myAt[ i ] = offsetPosition;
784 my: myAt[0] + (offset[0] < 0 ? offset[0] : "+" + offset[0]) + " " +
785 myAt[1] + (offset[1] < 0 ? offset[1] : "+" + offset[1]),
790 position = $.extend( {}, $.ui.dialog.prototype.options.position, position );
792 position = $.ui.dialog.prototype.options.position;
795 // need to show the dialog to get the actual offset in the position plugin
796 isVisible = this.uiDialog.is(":visible");
798 this.uiDialog.show();
800 this.uiDialog.position( position );
802 this.uiDialog.hide();