2 * jQuery UI Dialog 1.8.24
4 * Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
5 * Dual licensed under the MIT or GPL Version 2 licenses.
6 * http://jquery.org/license
8 * http://docs.jquery.com/UI/Dialog
14 * jquery.ui.draggable.js
16 * jquery.ui.position.js
17 * jquery.ui.resizable.js
19 (function( $, undefined ) {
24 'ui-widget-content ' +
26 sizeRelatedOptions
= {
35 resizableRelatedOptions
= {
42 $.widget("ui.dialog", {
61 // ensure that the titlebar is never outside the document
62 using: function(pos
) {
63 var topOffset
= $(this).css(pos
).offset().top
;
65 $(this).css('top', pos
.top
- topOffset
);
78 this.originalTitle
= this.element
.attr('title');
79 // #5742 - .attr() might return a DOMElement
80 if ( typeof this.originalTitle
!== "string" ) {
81 this.originalTitle
= "";
84 this.options
.title
= this.options
.title
|| this.originalTitle
;
86 options
= self
.options
,
88 title
= options
.title
|| ' ',
89 titleId
= $.ui
.dialog
.getTitleId(self
.element
),
91 uiDialog
= (self
.uiDialog
= $('<div></div>'))
92 .appendTo(document
.body
)
94 .addClass(uiDialogClasses
+ options
.dialogClass
)
96 zIndex
: options
.zIndex
98 // setting tabIndex makes the div focusable
99 // setting outline to 0 prevents a border on focus in Mozilla
100 .attr('tabIndex', -1).css('outline', 0).keydown(function(event
) {
101 if (options
.closeOnEscape
&& !event
.isDefaultPrevented() && event
.keyCode
&&
102 event
.keyCode
=== $.ui
.keyCode
.ESCAPE
) {
105 event
.preventDefault();
110 'aria-labelledby': titleId
112 .mousedown(function(event
) {
113 self
.moveToTop(false, event
);
116 uiDialogContent
= self
.element
120 'ui-dialog-content ' +
124 uiDialogTitlebar
= (self
.uiDialogTitlebar
= $('<div></div>'))
126 'ui-dialog-titlebar ' +
127 'ui-widget-header ' +
131 .prependTo(uiDialog
),
133 uiDialogTitlebarClose
= $('<a href="#"></a>')
135 'ui-dialog-titlebar-close ' +
138 .attr('role', 'button')
141 uiDialogTitlebarClose
.addClass('ui-state-hover');
144 uiDialogTitlebarClose
.removeClass('ui-state-hover');
148 uiDialogTitlebarClose
.addClass('ui-state-focus');
151 uiDialogTitlebarClose
.removeClass('ui-state-focus');
153 .click(function(event
) {
157 .appendTo(uiDialogTitlebar
),
159 uiDialogTitlebarCloseText
= (self
.uiDialogTitlebarCloseText
= $('<span></span>'))
164 .text(options
.closeText
)
165 .appendTo(uiDialogTitlebarClose
),
167 uiDialogTitle
= $('<span></span>')
168 .addClass('ui-dialog-title')
171 .prependTo(uiDialogTitlebar
);
173 //handling of deprecated beforeclose (vs beforeClose) option
174 //Ticket #4669 http://dev.jqueryui.com/ticket/4669
175 //TODO: remove in 1.9pre
176 if ($.isFunction(options
.beforeclose
) && !$.isFunction(options
.beforeClose
)) {
177 options
.beforeClose
= options
.beforeclose
;
180 uiDialogTitlebar
.find("*").add(uiDialogTitlebar
).disableSelection();
182 if (options
.draggable
&& $.fn
.draggable
) {
183 self
._makeDraggable();
185 if (options
.resizable
&& $.fn
.resizable
) {
186 self
._makeResizable();
189 self
._createButtons(options
.buttons
);
190 self
._isOpen
= false;
198 if ( this.options
.autoOpen
) {
203 destroy: function() {
207 self
.overlay
.destroy();
209 self
.uiDialog
.hide();
212 .removeData('dialog')
213 .removeClass('ui-dialog-content ui-widget-content')
214 .hide().appendTo('body');
215 self
.uiDialog
.remove();
217 if (self
.originalTitle
) {
218 self
.element
.attr('title', self
.originalTitle
);
225 return this.uiDialog
;
228 close: function(event
) {
232 if (false === self
._trigger('beforeClose', event
)) {
237 self
.overlay
.destroy();
239 self
.uiDialog
.unbind('keypress.ui-dialog');
241 self
._isOpen
= false;
243 if (self
.options
.hide
) {
244 self
.uiDialog
.hide(self
.options
.hide
, function() {
245 self
._trigger('close', event
);
248 self
.uiDialog
.hide();
249 self
._trigger('close', event
);
252 $.ui
.dialog
.overlay
.resize();
254 // adjust the maxZ to allow other modal dialogs to continue to work (see #4309)
255 if (self
.options
.modal
) {
257 $('.ui-dialog').each(function() {
258 if (this !== self
.uiDialog
[0]) {
259 thisZ
= $(this).css('z-index');
261 maxZ
= Math
.max(maxZ
, thisZ
);
265 $.ui
.dialog
.maxZ
= maxZ
;
275 // the force parameter allows us to move modal dialogs to their correct
277 moveToTop: function(force
, event
) {
279 options
= self
.options
,
282 if ((options
.modal
&& !force
) ||
283 (!options
.stack
&& !options
.modal
)) {
284 return self
._trigger('focus', event
);
287 if (options
.zIndex
> $.ui
.dialog
.maxZ
) {
288 $.ui
.dialog
.maxZ
= options
.zIndex
;
291 $.ui
.dialog
.maxZ
+= 1;
292 self
.overlay
.$el
.css('z-index', $.ui
.dialog
.overlay
.maxZ
= $.ui
.dialog
.maxZ
);
295 //Save and then restore scroll since Opera 9.5+ resets when parent z-Index is changed.
296 // http://ui.jquery.com/bugs/ticket/3193
297 saveScroll
= { scrollTop
: self
.element
.scrollTop(), scrollLeft
: self
.element
.scrollLeft() };
298 $.ui
.dialog
.maxZ
+= 1;
299 self
.uiDialog
.css('z-index', $.ui
.dialog
.maxZ
);
300 self
.element
.attr(saveScroll
);
301 self
._trigger('focus', event
);
307 if (this._isOpen
) { return; }
310 options
= self
.options
,
311 uiDialog
= self
.uiDialog
;
313 self
.overlay
= options
.modal
? new $.ui
.dialog
.overlay(self
) : null;
315 self
._position(options
.position
);
316 uiDialog
.show(options
.show
);
317 self
.moveToTop(true);
319 // prevent tabbing out of modal dialogs
320 if ( options
.modal
) {
321 uiDialog
.bind( "keydown.ui-dialog", function( event
) {
322 if ( event
.keyCode
!== $.ui
.keyCode
.TAB
) {
326 var tabbables
= $(':tabbable', this),
327 first
= tabbables
.filter(':first'),
328 last
= tabbables
.filter(':last');
330 if (event
.target
=== last
[0] && !event
.shiftKey
) {
333 } else if (event
.target
=== first
[0] && event
.shiftKey
) {
340 // set focus to the first tabbable element in the content area or the first button
341 // if there are no tabbable elements, set focus on the dialog itself
342 $(self
.element
.find(':tabbable').get().concat(
343 uiDialog
.find('.ui-dialog-buttonpane :tabbable').get().concat(
344 uiDialog
.get()))).eq(0).focus();
347 self
._trigger('open');
352 _createButtons: function(buttons
) {
355 uiDialogButtonPane
= $('<div></div>')
357 'ui-dialog-buttonpane ' +
358 'ui-widget-content ' +
361 uiButtonSet
= $( "<div></div>" )
362 .addClass( "ui-dialog-buttonset" )
363 .appendTo( uiDialogButtonPane
);
365 // if we already have a button pane, remove it
366 self
.uiDialog
.find('.ui-dialog-buttonpane').remove();
368 if (typeof buttons
=== 'object' && buttons
!== null) {
369 $.each(buttons
, function() {
370 return !(hasButtons
= true);
374 $.each(buttons
, function(name
, props
) {
375 props
= $.isFunction( props
) ?
376 { click
: props
, text
: name
} :
378 var button
= $('<button type="button"></button>')
380 props
.click
.apply(self
.element
[0], arguments
);
382 .appendTo(uiButtonSet
);
383 // can't use .attr( props, true ) with jQuery 1.3.2.
384 $.each( props
, function( key
, value
) {
385 if ( key
=== "click" ) {
388 if ( key
in button
) {
389 button
[ key
]( value
);
391 button
.attr( key
, value
);
398 uiDialogButtonPane
.appendTo(self
.uiDialog
);
402 _makeDraggable: function() {
404 options
= self
.options
,
408 function filteredUi(ui
) {
410 position
: ui
.position
,
415 self
.uiDialog
.draggable({
416 cancel
: '.ui-dialog-content, .ui-dialog-titlebar-close',
417 handle
: '.ui-dialog-titlebar',
418 containment
: 'document',
419 start: function(event
, ui
) {
420 heightBeforeDrag
= options
.height
=== "auto" ? "auto" : $(this).height();
421 $(this).height($(this).height()).addClass("ui-dialog-dragging");
422 self
._trigger('dragStart', event
, filteredUi(ui
));
424 drag: function(event
, ui
) {
425 self
._trigger('drag', event
, filteredUi(ui
));
427 stop: function(event
, ui
) {
428 options
.position
= [ui
.position
.left
- doc
.scrollLeft(),
429 ui
.position
.top
- doc
.scrollTop()];
430 $(this).removeClass("ui-dialog-dragging").height(heightBeforeDrag
);
431 self
._trigger('dragStop', event
, filteredUi(ui
));
432 $.ui
.dialog
.overlay
.resize();
437 _makeResizable: function(handles
) {
438 handles
= (handles
=== undefined ? this.options
.resizable
: handles
);
440 options
= self
.options
,
441 // .ui-resizable has position: relative defined in the stylesheet
442 // but dialogs have to use absolute or fixed positioning
443 position
= self
.uiDialog
.css('position'),
444 resizeHandles
= (typeof handles
=== 'string' ?
446 'n,e,s,w,se,sw,ne,nw'
449 function filteredUi(ui
) {
451 originalPosition
: ui
.originalPosition
,
452 originalSize
: ui
.originalSize
,
453 position
: ui
.position
,
458 self
.uiDialog
.resizable({
459 cancel
: '.ui-dialog-content',
460 containment
: 'document',
461 alsoResize
: self
.element
,
462 maxWidth
: options
.maxWidth
,
463 maxHeight
: options
.maxHeight
,
464 minWidth
: options
.minWidth
,
465 minHeight
: self
._minHeight(),
466 handles
: resizeHandles
,
467 start: function(event
, ui
) {
468 $(this).addClass("ui-dialog-resizing");
469 self
._trigger('resizeStart', event
, filteredUi(ui
));
471 resize: function(event
, ui
) {
472 self
._trigger('resize', event
, filteredUi(ui
));
474 stop: function(event
, ui
) {
475 $(this).removeClass("ui-dialog-resizing");
476 options
.height
= $(this).height();
477 options
.width
= $(this).width();
478 self
._trigger('resizeStop', event
, filteredUi(ui
));
479 $.ui
.dialog
.overlay
.resize();
482 .css('position', position
)
483 .find('.ui-resizable-se').addClass('ui-icon ui-icon-grip-diagonal-se');
486 _minHeight: function() {
487 var options
= this.options
;
489 if (options
.height
=== 'auto') {
490 return options
.minHeight
;
492 return Math
.min(options
.minHeight
, options
.height
);
496 _position: function(position
) {
502 // deep extending converts arrays to objects in jQuery <= 1.3.2 :-(
503 // if (typeof position == 'string' || $.isArray(position)) {
504 // myAt = $.isArray(position) ? position : position.split(' ');
506 if (typeof position
=== 'string' || (typeof position
=== 'object' && '0' in position
)) {
507 myAt
= position
.split
? position
.split(' ') : [position
[0], position
[1]];
508 if (myAt
.length
=== 1) {
512 $.each(['left', 'top'], function(i
, offsetPosition
) {
513 if (+myAt
[i
] === myAt
[i
]) {
515 myAt
[i
] = offsetPosition
;
522 offset
: offset
.join(" ")
526 position
= $.extend({}, $.ui
.dialog
.prototype.options
.position
, position
);
528 position
= $.ui
.dialog
.prototype.options
.position
;
531 // need to show the dialog to get the actual offset in the position plugin
532 isVisible
= this.uiDialog
.is(':visible');
534 this.uiDialog
.show();
537 // workaround for jQuery bug #5781 http://dev.jquery.com/ticket/5781
538 .css({ top
: 0, left
: 0 })
539 .position($.extend({ of: window
}, position
));
541 this.uiDialog
.hide();
545 _setOptions: function( options
) {
547 resizableOptions
= {},
550 $.each( options
, function( key
, value
) {
551 self
._setOption( key
, value
);
553 if ( key
in sizeRelatedOptions
) {
556 if ( key
in resizableRelatedOptions
) {
557 resizableOptions
[ key
] = value
;
564 if ( this.uiDialog
.is( ":data(resizable)" ) ) {
565 this.uiDialog
.resizable( "option", resizableOptions
);
569 _setOption: function(key
, value
){
571 uiDialog
= self
.uiDialog
;
574 //handling of deprecated beforeclose (vs beforeClose) option
575 //Ticket #4669 http://dev.jqueryui.com/ticket/4669
576 //TODO: remove in 1.9pre
581 self
._createButtons(value
);
584 // ensure that we always pass a string
585 self
.uiDialogTitlebarCloseText
.text("" + value
);
589 .removeClass(self
.options
.dialogClass
)
590 .addClass(uiDialogClasses
+ value
);
594 uiDialog
.addClass('ui-dialog-disabled');
596 uiDialog
.removeClass('ui-dialog-disabled');
600 var isDraggable
= uiDialog
.is( ":data(draggable)" );
601 if ( isDraggable
&& !value
) {
602 uiDialog
.draggable( "destroy" );
605 if ( !isDraggable
&& value
) {
606 self
._makeDraggable();
610 self
._position(value
);
613 // currently resizable, becoming non-resizable
614 var isResizable
= uiDialog
.is( ":data(resizable)" );
615 if (isResizable
&& !value
) {
616 uiDialog
.resizable('destroy');
619 // currently resizable, changing handles
620 if (isResizable
&& typeof value
=== 'string') {
621 uiDialog
.resizable('option', 'handles', value
);
624 // currently non-resizable, becoming resizable
625 if (!isResizable
&& value
!== false) {
626 self
._makeResizable(value
);
630 // convert whatever was passed in o a string, for html() to not throw up
631 $(".ui-dialog-title", self
.uiDialogTitlebar
).html("" + (value
|| ' '));
635 $.Widget
.prototype._setOption
.apply(self
, arguments
);
639 /* If the user has resized the dialog, the .ui-dialog and .ui-dialog-content
640 * divs will both have width and height set, so we need to reset them
642 var options
= this.options
,
645 isVisible
= this.uiDialog
.is( ":visible" );
647 // reset content sizing
648 this.element
.show().css({
654 if (options
.minWidth
> options
.width
) {
655 options
.width
= options
.minWidth
;
658 // reset wrapper sizing
659 // determine the height of all the non-content elements
660 nonContentHeight
= this.uiDialog
.css({
665 minContentHeight
= Math
.max( 0, options
.minHeight
- nonContentHeight
);
667 if ( options
.height
=== "auto" ) {
668 // only needed for IE6 support
669 if ( $.support
.minHeight
) {
671 minHeight
: minContentHeight
,
675 this.uiDialog
.show();
676 var autoHeight
= this.element
.css( "height", "auto" ).height();
678 this.uiDialog
.hide();
680 this.element
.height( Math
.max( autoHeight
, minContentHeight
) );
683 this.element
.height( Math
.max( options
.height
- nonContentHeight
, 0 ) );
686 if (this.uiDialog
.is(':data(resizable)')) {
687 this.uiDialog
.resizable('option', 'minHeight', this._minHeight());
692 $.extend($.ui
.dialog
, {
698 getTitleId: function($el
) {
699 var id
= $el
.attr('id');
704 return 'ui-dialog-title-' + id
;
707 overlay: function(dialog
) {
708 this.$el
= $.ui
.dialog
.overlay
.create(dialog
);
712 $.extend($.ui
.dialog
.overlay
, {
714 // reuse old instances due to IE memory leak with alpha transparency (see #5185)
717 events
: $.map('focus,mousedown,mouseup,keydown,keypress,click'.split(','),
718 function(event
) { return event
+ '.dialog-overlay'; }).join(' '),
719 create: function(dialog
) {
720 if (this.instances
.length
=== 0) {
721 // prevent use of anchors and inputs
722 // we use a setTimeout in case the overlay is created from an
723 // event that we're going to be cancelling (see #2804)
724 setTimeout(function() {
725 // handle $(el).dialog().dialog('close') (see #4065)
726 if ($.ui
.dialog
.overlay
.instances
.length
) {
727 $(document
).bind($.ui
.dialog
.overlay
.events
, function(event
) {
728 // stop events if the z-index of the target is < the z-index of the overlay
729 // we cannot return true when we don't want to cancel the event (#3523)
730 if ($(event
.target
).zIndex() < $.ui
.dialog
.overlay
.maxZ
) {
737 // allow closing by pressing the escape key
738 $(document
).bind('keydown.dialog-overlay', function(event
) {
739 if (dialog
.options
.closeOnEscape
&& !event
.isDefaultPrevented() && event
.keyCode
&&
740 event
.keyCode
=== $.ui
.keyCode
.ESCAPE
) {
743 event
.preventDefault();
747 // handle window resize
748 $(window
).bind('resize.dialog-overlay', $.ui
.dialog
.overlay
.resize
);
751 var $el
= (this.oldInstances
.pop() || $('<div></div>').addClass('ui-widget-overlay'))
752 .appendTo(document
.body
)
755 height
: this.height()
762 this.instances
.push($el
);
766 destroy: function($el
) {
767 var indexOf
= $.inArray($el
, this.instances
);
769 this.oldInstances
.push(this.instances
.splice(indexOf
, 1)[0]);
772 if (this.instances
.length
=== 0) {
773 $([document
, window
]).unbind('.dialog-overlay');
778 // adjust the maxZ to allow other modal dialogs to continue to work (see #4309)
780 $.each(this.instances
, function() {
781 maxZ
= Math
.max(maxZ
, this.css('z-index'));
790 if ($.browser
.msie
&& $.browser
.version
< 7) {
791 scrollHeight
= Math
.max(
792 document
.documentElement
.scrollHeight
,
793 document
.body
.scrollHeight
795 offsetHeight
= Math
.max(
796 document
.documentElement
.offsetHeight
,
797 document
.body
.offsetHeight
800 if (scrollHeight
< offsetHeight
) {
801 return $(window
).height() + 'px';
803 return scrollHeight
+ 'px';
805 // handle "good" browsers
807 return $(document
).height() + 'px';
815 if ( $.browser
.msie
) {
816 scrollWidth
= Math
.max(
817 document
.documentElement
.scrollWidth
,
818 document
.body
.scrollWidth
820 offsetWidth
= Math
.max(
821 document
.documentElement
.offsetWidth
,
822 document
.body
.offsetWidth
825 if (scrollWidth
< offsetWidth
) {
826 return $(window
).width() + 'px';
828 return scrollWidth
+ 'px';
830 // handle "good" browsers
832 return $(document
).width() + 'px';
837 /* If the dialog is draggable and the user drags it past the
838 * right edge of the window, the document becomes wider so we
839 * need to stretch the overlay. If the user then drags the
840 * dialog back to the left, the document will become narrower,
841 * so we need to shrink the overlay to the appropriate size.
842 * This is handled by shrinking the overlay before setting it
843 * to the full document size.
845 var $overlays
= $([]);
846 $.each($.ui
.dialog
.overlay
.instances
, function() {
847 $overlays
= $overlays
.add(this);
854 width
: $.ui
.dialog
.overlay
.width(),
855 height
: $.ui
.dialog
.overlay
.height()
860 $.extend($.ui
.dialog
.overlay
.prototype, {
861 destroy: function() {
862 $.ui
.dialog
.overlay
.destroy(this.$el
);