2 * jQuery UI Dialog 1.8.10
4 * Copyright 2011, 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.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.attr('scrollTop'), scrollLeft: self.element.attr('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
321 uiDialog.bind('keypress.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>')
382 props.click.apply(self.element[0], arguments);
384 .appendTo(uiButtonSet);
389 uiDialogButtonPane.appendTo(self.uiDialog);
393 _makeDraggable: function() {
395 options = self.options,
399 function filteredUi(ui) {
401 position: ui.position,
406 self.uiDialog.draggable({
407 cancel: '.ui-dialog-content, .ui-dialog-titlebar-close',
408 handle: '.ui-dialog-titlebar',
409 containment: 'document',
410 start: function(event, ui) {
411 heightBeforeDrag = options.height === "auto" ? "auto" : $(this).height();
412 $(this).height($(this).height()).addClass("ui-dialog-dragging");
413 self._trigger('dragStart', event, filteredUi(ui));
415 drag: function(event, ui) {
416 self._trigger('drag', event, filteredUi(ui));
418 stop: function(event, ui) {
419 options.position = [ui.position.left - doc.scrollLeft(),
420 ui.position.top - doc.scrollTop()];
421 $(this).removeClass("ui-dialog-dragging").height(heightBeforeDrag);
422 self._trigger('dragStop', event, filteredUi(ui));
423 $.ui.dialog.overlay.resize();
428 _makeResizable: function(handles) {
429 handles = (handles === undefined ? this.options.resizable : handles);
431 options = self.options,
432 // .ui-resizable has position: relative defined in the stylesheet
433 // but dialogs have to use absolute or fixed positioning
434 position = self.uiDialog.css('position'),
435 resizeHandles = (typeof handles === 'string' ?
437 'n,e,s,w,se,sw,ne,nw'
440 function filteredUi(ui) {
442 originalPosition: ui.originalPosition,
443 originalSize: ui.originalSize,
444 position: ui.position,
449 self.uiDialog.resizable({
450 cancel: '.ui-dialog-content',
451 containment: 'document',
452 alsoResize: self.element,
453 maxWidth: options.maxWidth,
454 maxHeight: options.maxHeight,
455 minWidth: options.minWidth,
456 minHeight: self._minHeight(),
457 handles: resizeHandles,
458 start: function(event, ui) {
459 $(this).addClass("ui-dialog-resizing");
460 self._trigger('resizeStart', event, filteredUi(ui));
462 resize: function(event, ui) {
463 self._trigger('resize', event, filteredUi(ui));
465 stop: function(event, ui) {
466 $(this).removeClass("ui-dialog-resizing");
467 options.height = $(this).height();
468 options.width = $(this).width();
469 self._trigger('resizeStop', event, filteredUi(ui));
470 $.ui.dialog.overlay.resize();
473 .css('position', position)
474 .find('.ui-resizable-se').addClass('ui-icon ui-icon-grip-diagonal-se');
477 _minHeight: function() {
478 var options = this.options;
480 if (options.height === 'auto') {
481 return options.minHeight;
483 return Math.min(options.minHeight, options.height);
487 _position: function(position) {
493 // deep extending converts arrays to objects in jQuery <= 1.3.2 :-(
494 // if (typeof position == 'string' || $.isArray(position)) {
495 // myAt = $.isArray(position) ? position : position.split(' ');
497 if (typeof position === 'string' || (typeof position === 'object' && '0' in position)) {
498 myAt = position.split ? position.split(' ') : [position[0], position[1]];
499 if (myAt.length === 1) {
503 $.each(['left', 'top'], function(i, offsetPosition) {
504 if (+myAt[i] === myAt[i]) {
506 myAt[i] = offsetPosition;
513 offset: offset.join(" ")
517 position = $.extend({}, $.ui.dialog.prototype.options.position, position);
519 position = $.ui.dialog.prototype.options.position;
522 // need to show the dialog to get the actual offset in the position plugin
523 isVisible = this.uiDialog.is(':visible');
525 this.uiDialog.show();
528 // workaround for jQuery bug #5781 http://dev.jquery.com/ticket/5781
529 .css({ top: 0, left: 0 })
530 .position($.extend({ of: window }, position));
532 this.uiDialog.hide();
536 _setOptions: function( options ) {
538 resizableOptions = {},
541 $.each( options, function( key, value ) {
542 self._setOption( key, value );
544 if ( key in sizeRelatedOptions ) {
547 if ( key in resizableRelatedOptions ) {
548 resizableOptions[ key ] = value;
555 if ( this.uiDialog.is( ":data(resizable)" ) ) {
556 this.uiDialog.resizable( "option", resizableOptions );
560 _setOption: function(key, value){
562 uiDialog = self.uiDialog;
565 //handling of deprecated beforeclose (vs beforeClose) option
566 //Ticket #4669 http://dev.jqueryui.com/ticket/4669
567 //TODO: remove in 1.9pre
572 self._createButtons(value);
575 // ensure that we always pass a string
576 self.uiDialogTitlebarCloseText.text("" + value);
580 .removeClass(self.options.dialogClass)
581 .addClass(uiDialogClasses + value);
585 uiDialog.addClass('ui-dialog-disabled');
587 uiDialog.removeClass('ui-dialog-disabled');
591 var isDraggable = uiDialog.is( ":data(draggable)" );
592 if ( isDraggable && !value ) {
593 uiDialog.draggable( "destroy" );
596 if ( !isDraggable && value ) {
597 self._makeDraggable();
601 self._position(value);
604 // currently resizable, becoming non-resizable
605 var isResizable = uiDialog.is( ":data(resizable)" );
606 if (isResizable && !value) {
607 uiDialog.resizable('destroy');
610 // currently resizable, changing handles
611 if (isResizable && typeof value === 'string') {
612 uiDialog.resizable('option', 'handles', value);
615 // currently non-resizable, becoming resizable
616 if (!isResizable && value !== false) {
617 self._makeResizable(value);
621 // convert whatever was passed in o a string, for html() to not throw up
622 $(".ui-dialog-title", self.uiDialogTitlebar).html("" + (value || ' '));
626 $.Widget.prototype._setOption.apply(self, arguments);
630 /* If the user has resized the dialog, the .ui-dialog and .ui-dialog-content
631 * divs will both have width and height set, so we need to reset them
633 var options = this.options,
636 isVisible = this.uiDialog.is( ":visible" );
638 // reset content sizing
639 this.element.show().css({
645 if (options.minWidth > options.width) {
646 options.width = options.minWidth;
649 // reset wrapper sizing
650 // determine the height of all the non-content elements
651 nonContentHeight = this.uiDialog.css({
656 minContentHeight = Math.max( 0, options.minHeight - nonContentHeight );
658 if ( options.height === "auto" ) {
659 // only needed for IE6 support
660 if ( $.support.minHeight ) {
662 minHeight: minContentHeight,
666 this.uiDialog.show();
667 var autoHeight = this.element.css( "height", "auto" ).height();
669 this.uiDialog.hide();
671 this.element.height( Math.max( autoHeight, minContentHeight ) );
674 this.element.height( Math.max( options.height - nonContentHeight, 0 ) );
677 if (this.uiDialog.is(':data(resizable)')) {
678 this.uiDialog.resizable('option', 'minHeight', this._minHeight());
683 $.extend($.ui.dialog, {
689 getTitleId: function($el) {
690 var id = $el.attr('id');
695 return 'ui-dialog-title-' + id;
698 overlay: function(dialog) {
699 this.$el = $.ui.dialog.overlay.create(dialog);
703 $.extend($.ui.dialog.overlay, {
705 // reuse old instances due to IE memory leak with alpha transparency (see #5185)
708 events: $.map('focus,mousedown,mouseup,keydown,keypress,click'.split(','),
709 function(event) { return event + '.dialog-overlay'; }).join(' '),
710 create: function(dialog) {
711 if (this.instances.length === 0) {
712 // prevent use of anchors and inputs
713 // we use a setTimeout in case the overlay is created from an
714 // event that we're going to be cancelling (see #2804)
715 setTimeout(function() {
716 // handle $(el).dialog().dialog('close') (see #4065)
717 if ($.ui.dialog.overlay.instances.length) {
718 $(document).bind($.ui.dialog.overlay.events, function(event) {
719 // stop events if the z-index of the target is < the z-index of the overlay
720 // we cannot return true when we don't want to cancel the event (#3523)
721 if ($(event.target).zIndex() < $.ui.dialog.overlay.maxZ) {
728 // allow closing by pressing the escape key
729 $(document).bind('keydown.dialog-overlay', function(event) {
730 if (dialog.options.closeOnEscape && event.keyCode &&
731 event.keyCode === $.ui.keyCode.ESCAPE) {
734 event.preventDefault();
738 // handle window resize
739 $(window).bind('resize.dialog-overlay', $.ui.dialog.overlay.resize);
742 var $el = (this.oldInstances.pop() || $('<div></div>').addClass('ui-widget-overlay'))
743 .appendTo(document.body)
746 height: this.height()
753 this.instances.push($el);
757 destroy: function($el) {
758 var indexOf = $.inArray($el, this.instances);
760 this.oldInstances.push(this.instances.splice(indexOf, 1)[0]);
763 if (this.instances.length === 0) {
764 $([document, window]).unbind('.dialog-overlay');
769 // adjust the maxZ to allow other modal dialogs to continue to work (see #4309)
771 $.each(this.instances, function() {
772 maxZ = Math.max(maxZ, this.css('z-index'));
781 if ($.browser.msie && $.browser.version < 7) {
782 scrollHeight = Math.max(
783 document.documentElement.scrollHeight,
784 document.body.scrollHeight
786 offsetHeight = Math.max(
787 document.documentElement.offsetHeight,
788 document.body.offsetHeight
791 if (scrollHeight < offsetHeight) {
792 return $(window).height() + 'px';
794 return scrollHeight + 'px';
796 // handle "good" browsers
798 return $(document).height() + 'px';
806 if ($.browser.msie && $.browser.version < 7) {
807 scrollWidth = Math.max(
808 document.documentElement.scrollWidth,
809 document.body.scrollWidth
811 offsetWidth = Math.max(
812 document.documentElement.offsetWidth,
813 document.body.offsetWidth
816 if (scrollWidth < offsetWidth) {
817 return $(window).width() + 'px';
819 return scrollWidth + 'px';
821 // handle "good" browsers
823 return $(document).width() + 'px';
828 /* If the dialog is draggable and the user drags it past the
829 * right edge of the window, the document becomes wider so we
830 * need to stretch the overlay. If the user then drags the
831 * dialog back to the left, the document will become narrower,
832 * so we need to shrink the overlay to the appropriate size.
833 * This is handled by shrinking the overlay before setting it
834 * to the full document size.
836 var $overlays = $([]);
837 $.each($.ui.dialog.overlay.instances, function() {
838 $overlays = $overlays.add(this);
845 width: $.ui.dialog.overlay.width(),
846 height: $.ui.dialog.overlay.height()
851 $.extend($.ui.dialog.overlay.prototype, {
852 destroy: function() {
853 $.ui.dialog.overlay.destroy(this.$el);