2 * jQuery UI Dialog 1.8.4
4 * Copyright 2010, 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 ' +
27 $.widget("ui.dialog", {
47 // ensure that the titlebar is never outside the document
48 using: function(pos) {
49 var topOffset = $(this).css(pos).offset().top;
51 $(this).css('top', pos.top - topOffset);
64 this.originalTitle = this.element.attr('title');
65 // #5742 - .attr() might return a DOMElement
66 if ( typeof this.originalTitle !== "string" ) {
67 this.originalTitle = "";
71 options = self.options,
73 title = options.title || self.originalTitle || ' ',
74 titleId = $.ui.dialog.getTitleId(self.element),
76 uiDialog = (self.uiDialog = $('<div></div>'))
77 .appendTo(document.body)
79 .addClass(uiDialogClasses + options.dialogClass)
81 zIndex: options.zIndex
83 // setting tabIndex makes the div focusable
84 // setting outline to 0 prevents a border on focus in Mozilla
85 .attr('tabIndex', -1).css('outline', 0).keydown(function(event) {
86 if (options.closeOnEscape && event.keyCode &&
87 event.keyCode === $.ui.keyCode.ESCAPE) {
90 event.preventDefault();
95 'aria-labelledby': titleId
97 .mousedown(function(event) {
98 self.moveToTop(false, event);
101 uiDialogContent = self.element
105 'ui-dialog-content ' +
109 uiDialogTitlebar = (self.uiDialogTitlebar = $('<div></div>'))
111 'ui-dialog-titlebar ' +
112 'ui-widget-header ' +
116 .prependTo(uiDialog),
118 uiDialogTitlebarClose = $('<a href="#"></a>')
120 'ui-dialog-titlebar-close ' +
123 .attr('role', 'button')
126 uiDialogTitlebarClose.addClass('ui-state-hover');
129 uiDialogTitlebarClose.removeClass('ui-state-hover');
133 uiDialogTitlebarClose.addClass('ui-state-focus');
136 uiDialogTitlebarClose.removeClass('ui-state-focus');
138 .click(function(event) {
142 .appendTo(uiDialogTitlebar),
144 uiDialogTitlebarCloseText = (self.uiDialogTitlebarCloseText = $('<span></span>'))
149 .text(options.closeText)
150 .appendTo(uiDialogTitlebarClose),
152 uiDialogTitle = $('<span></span>')
153 .addClass('ui-dialog-title')
156 .prependTo(uiDialogTitlebar);
158 //handling of deprecated beforeclose (vs beforeClose) option
159 //Ticket #4669 http://dev.jqueryui.com/ticket/4669
160 //TODO: remove in 1.9pre
161 if ($.isFunction(options.beforeclose) && !$.isFunction(options.beforeClose)) {
162 options.beforeClose = options.beforeclose;
165 uiDialogTitlebar.find("*").add(uiDialogTitlebar).disableSelection();
167 if (options.draggable && $.fn.draggable) {
168 self._makeDraggable();
170 if (options.resizable && $.fn.resizable) {
171 self._makeResizable();
174 self._createButtons(options.buttons);
175 self._isOpen = false;
183 if ( this.options.autoOpen ) {
188 destroy: function() {
192 self.overlay.destroy();
194 self.uiDialog.hide();
197 .removeData('dialog')
198 .removeClass('ui-dialog-content ui-widget-content')
199 .hide().appendTo('body');
200 self.uiDialog.remove();
202 if (self.originalTitle) {
203 self.element.attr('title', self.originalTitle);
210 return this.uiDialog;
213 close: function(event) {
217 if (false === self._trigger('beforeClose', event)) {
222 self.overlay.destroy();
224 self.uiDialog.unbind('keypress.ui-dialog');
226 self._isOpen = false;
228 if (self.options.hide) {
229 self.uiDialog.hide(self.options.hide, function() {
230 self._trigger('close', event);
233 self.uiDialog.hide();
234 self._trigger('close', event);
237 $.ui.dialog.overlay.resize();
239 // adjust the maxZ to allow other modal dialogs to continue to work (see #4309)
240 if (self.options.modal) {
242 $('.ui-dialog').each(function() {
243 if (this !== self.uiDialog[0]) {
244 maxZ = Math.max(maxZ, $(this).css('z-index'));
247 $.ui.dialog.maxZ = maxZ;
257 // the force parameter allows us to move modal dialogs to their correct
259 moveToTop: function(force, event) {
261 options = self.options,
264 if ((options.modal && !force) ||
265 (!options.stack && !options.modal)) {
266 return self._trigger('focus', event);
269 if (options.zIndex > $.ui.dialog.maxZ) {
270 $.ui.dialog.maxZ = options.zIndex;
273 $.ui.dialog.maxZ += 1;
274 self.overlay.$el.css('z-index', $.ui.dialog.overlay.maxZ = $.ui.dialog.maxZ);
277 //Save and then restore scroll since Opera 9.5+ resets when parent z-Index is changed.
278 // http://ui.jquery.com/bugs/ticket/3193
279 saveScroll = { scrollTop: self.element.attr('scrollTop'), scrollLeft: self.element.attr('scrollLeft') };
280 $.ui.dialog.maxZ += 1;
281 self.uiDialog.css('z-index', $.ui.dialog.maxZ);
282 self.element.attr(saveScroll);
283 self._trigger('focus', event);
289 if (this._isOpen) { return; }
292 options = self.options,
293 uiDialog = self.uiDialog;
295 self.overlay = options.modal ? new $.ui.dialog.overlay(self) : null;
296 if (uiDialog.next().length) {
297 uiDialog.appendTo('body');
300 self._position(options.position);
301 uiDialog.show(options.show);
302 self.moveToTop(true);
304 // prevent tabbing out of modal dialogs
306 uiDialog.bind('keypress.ui-dialog', function(event) {
307 if (event.keyCode !== $.ui.keyCode.TAB) {
311 var tabbables = $(':tabbable', this),
312 first = tabbables.filter(':first'),
313 last = tabbables.filter(':last');
315 if (event.target === last[0] && !event.shiftKey) {
318 } else if (event.target === first[0] && event.shiftKey) {
325 // set focus to the first tabbable element in the content area or the first button
326 // if there are no tabbable elements, set focus on the dialog itself
327 $(self.element.find(':tabbable').get().concat(
328 uiDialog.find('.ui-dialog-buttonpane :tabbable').get().concat(
329 uiDialog.get()))).eq(0).focus();
331 self._trigger('open');
337 _createButtons: function(buttons) {
340 uiDialogButtonPane = $('<div></div>')
342 'ui-dialog-buttonpane ' +
343 'ui-widget-content ' +
346 uiButtonSet = $( "<div></div>" )
347 .addClass( "ui-dialog-buttonset" )
348 .appendTo( uiDialogButtonPane );
350 // if we already have a button pane, remove it
351 self.uiDialog.find('.ui-dialog-buttonpane').remove();
353 if (typeof buttons === 'object' && buttons !== null) {
354 $.each(buttons, function() {
355 return !(hasButtons = true);
359 $.each(buttons, function(name, fn) {
360 var button = $('<button type="button"></button>')
362 .click(function() { fn.apply(self.element[0], arguments); })
363 .appendTo(uiButtonSet);
368 uiDialogButtonPane.appendTo(self.uiDialog);
372 _makeDraggable: function() {
374 options = self.options,
378 function filteredUi(ui) {
380 position: ui.position,
385 self.uiDialog.draggable({
386 cancel: '.ui-dialog-content, .ui-dialog-titlebar-close',
387 handle: '.ui-dialog-titlebar',
388 containment: 'document',
389 start: function(event, ui) {
390 heightBeforeDrag = options.height === "auto" ? "auto" : $(this).height();
391 $(this).height($(this).height()).addClass("ui-dialog-dragging");
392 self._trigger('dragStart', event, filteredUi(ui));
394 drag: function(event, ui) {
395 self._trigger('drag', event, filteredUi(ui));
397 stop: function(event, ui) {
398 options.position = [ui.position.left - doc.scrollLeft(),
399 ui.position.top - doc.scrollTop()];
400 $(this).removeClass("ui-dialog-dragging").height(heightBeforeDrag);
401 self._trigger('dragStop', event, filteredUi(ui));
402 $.ui.dialog.overlay.resize();
407 _makeResizable: function(handles) {
408 handles = (handles === undefined ? this.options.resizable : handles);
410 options = self.options,
411 // .ui-resizable has position: relative defined in the stylesheet
412 // but dialogs have to use absolute or fixed positioning
413 position = self.uiDialog.css('position'),
414 resizeHandles = (typeof handles === 'string' ?
416 'n,e,s,w,se,sw,ne,nw'
419 function filteredUi(ui) {
421 originalPosition: ui.originalPosition,
422 originalSize: ui.originalSize,
423 position: ui.position,
428 self.uiDialog.resizable({
429 cancel: '.ui-dialog-content',
430 containment: 'document',
431 alsoResize: self.element,
432 maxWidth: options.maxWidth,
433 maxHeight: options.maxHeight,
434 minWidth: options.minWidth,
435 minHeight: self._minHeight(),
436 handles: resizeHandles,
437 start: function(event, ui) {
438 $(this).addClass("ui-dialog-resizing");
439 self._trigger('resizeStart', event, filteredUi(ui));
441 resize: function(event, ui) {
442 self._trigger('resize', event, filteredUi(ui));
444 stop: function(event, ui) {
445 $(this).removeClass("ui-dialog-resizing");
446 options.height = $(this).height();
447 options.width = $(this).width();
448 self._trigger('resizeStop', event, filteredUi(ui));
449 $.ui.dialog.overlay.resize();
452 .css('position', position)
453 .find('.ui-resizable-se').addClass('ui-icon ui-icon-grip-diagonal-se');
456 _minHeight: function() {
457 var options = this.options;
459 if (options.height === 'auto') {
460 return options.minHeight;
462 return Math.min(options.minHeight, options.height);
466 _position: function(position) {
472 // deep extending converts arrays to objects in jQuery <= 1.3.2 :-(
473 // if (typeof position == 'string' || $.isArray(position)) {
474 // myAt = $.isArray(position) ? position : position.split(' ');
476 if (typeof position === 'string' || (typeof position === 'object' && '0' in position)) {
477 myAt = position.split ? position.split(' ') : [position[0], position[1]];
478 if (myAt.length === 1) {
482 $.each(['left', 'top'], function(i, offsetPosition) {
483 if (+myAt[i] === myAt[i]) {
485 myAt[i] = offsetPosition;
492 offset: offset.join(" ")
496 position = $.extend({}, $.ui.dialog.prototype.options.position, position);
498 position = $.ui.dialog.prototype.options.position;
501 // need to show the dialog to get the actual offset in the position plugin
502 isVisible = this.uiDialog.is(':visible');
504 this.uiDialog.show();
507 // workaround for jQuery bug #5781 http://dev.jquery.com/ticket/5781
508 .css({ top: 0, left: 0 })
511 this.uiDialog.hide();
515 _setOption: function(key, value){
517 uiDialog = self.uiDialog,
518 isResizable = uiDialog.is(':data(resizable)'),
522 //handling of deprecated beforeclose (vs beforeClose) option
523 //Ticket #4669 http://dev.jqueryui.com/ticket/4669
524 //TODO: remove in 1.9pre
529 self._createButtons(value);
533 // convert whatever was passed in to a string, for text() to not throw up
534 self.uiDialogTitlebarCloseText.text("" + value);
538 .removeClass(self.options.dialogClass)
539 .addClass(uiDialogClasses + value);
543 uiDialog.addClass('ui-dialog-disabled');
545 uiDialog.removeClass('ui-dialog-disabled');
550 self._makeDraggable();
552 uiDialog.draggable('destroy');
560 uiDialog.resizable('option', 'maxHeight', value);
566 uiDialog.resizable('option', 'maxWidth', value);
572 uiDialog.resizable('option', 'minHeight', value);
578 uiDialog.resizable('option', 'minWidth', value);
583 self._position(value);
586 // currently resizable, becoming non-resizable
587 if (isResizable && !value) {
588 uiDialog.resizable('destroy');
591 // currently resizable, changing handles
592 if (isResizable && typeof value === 'string') {
593 uiDialog.resizable('option', 'handles', value);
596 // currently non-resizable, becoming resizable
597 if (!isResizable && value !== false) {
598 self._makeResizable(value);
602 // convert whatever was passed in o a string, for html() to not throw up
603 $(".ui-dialog-title", self.uiDialogTitlebar).html("" + (value || ' '));
610 $.Widget.prototype._setOption.apply(self, arguments);
617 /* If the user has resized the dialog, the .ui-dialog and .ui-dialog-content
618 * divs will both have width and height set, so we need to reset them
620 var options = this.options,
623 // reset content sizing
624 // hide for non content measurement because height: 0 doesn't work in IE quirks mode (see #4350)
631 if (options.minWidth > options.width) {
632 options.width = options.minWidth;
635 // reset wrapper sizing
636 // determine the height of all the non-content elements
637 nonContentHeight = this.uiDialog.css({
644 .css(options.height === 'auto' ? {
645 minHeight: Math.max(options.minHeight - nonContentHeight, 0),
649 height: Math.max(options.height - nonContentHeight, 0)
653 if (this.uiDialog.is(':data(resizable)')) {
654 this.uiDialog.resizable('option', 'minHeight', this._minHeight());
659 $.extend($.ui.dialog, {
665 getTitleId: function($el) {
666 var id = $el.attr('id');
671 return 'ui-dialog-title-' + id;
674 overlay: function(dialog) {
675 this.$el = $.ui.dialog.overlay.create(dialog);
679 $.extend($.ui.dialog.overlay, {
681 // reuse old instances due to IE memory leak with alpha transparency (see #5185)
684 events: $.map('focus,mousedown,mouseup,keydown,keypress,click'.split(','),
685 function(event) { return event + '.dialog-overlay'; }).join(' '),
686 create: function(dialog) {
687 if (this.instances.length === 0) {
688 // prevent use of anchors and inputs
689 // we use a setTimeout in case the overlay is created from an
690 // event that we're going to be cancelling (see #2804)
691 setTimeout(function() {
692 // handle $(el).dialog().dialog('close') (see #4065)
693 if ($.ui.dialog.overlay.instances.length) {
694 $(document).bind($.ui.dialog.overlay.events, function(event) {
695 // stop events if the z-index of the target is < the z-index of the overlay
696 return ($(event.target).zIndex() >= $.ui.dialog.overlay.maxZ);
701 // allow closing by pressing the escape key
702 $(document).bind('keydown.dialog-overlay', function(event) {
703 if (dialog.options.closeOnEscape && event.keyCode &&
704 event.keyCode === $.ui.keyCode.ESCAPE) {
707 event.preventDefault();
711 // handle window resize
712 $(window).bind('resize.dialog-overlay', $.ui.dialog.overlay.resize);
715 var $el = (this.oldInstances.pop() || $('<div></div>').addClass('ui-widget-overlay'))
716 .appendTo(document.body)
719 height: this.height()
726 this.instances.push($el);
730 destroy: function($el) {
731 this.oldInstances.push(this.instances.splice($.inArray($el, this.instances), 1)[0]);
733 if (this.instances.length === 0) {
734 $([document, window]).unbind('.dialog-overlay');
739 // adjust the maxZ to allow other modal dialogs to continue to work (see #4309)
741 $.each(this.instances, function() {
742 maxZ = Math.max(maxZ, this.css('z-index'));
751 if ($.browser.msie && $.browser.version < 7) {
752 scrollHeight = Math.max(
753 document.documentElement.scrollHeight,
754 document.body.scrollHeight
756 offsetHeight = Math.max(
757 document.documentElement.offsetHeight,
758 document.body.offsetHeight
761 if (scrollHeight < offsetHeight) {
762 return $(window).height() + 'px';
764 return scrollHeight + 'px';
766 // handle "good" browsers
768 return $(document).height() + 'px';
776 if ($.browser.msie && $.browser.version < 7) {
777 scrollWidth = Math.max(
778 document.documentElement.scrollWidth,
779 document.body.scrollWidth
781 offsetWidth = Math.max(
782 document.documentElement.offsetWidth,
783 document.body.offsetWidth
786 if (scrollWidth < offsetWidth) {
787 return $(window).width() + 'px';
789 return scrollWidth + 'px';
791 // handle "good" browsers
793 return $(document).width() + 'px';
798 /* If the dialog is draggable and the user drags it past the
799 * right edge of the window, the document becomes wider so we
800 * need to stretch the overlay. If the user then drags the
801 * dialog back to the left, the document will become narrower,
802 * so we need to shrink the overlay to the appropriate size.
803 * This is handled by shrinking the overlay before setting it
804 * to the full document size.
806 var $overlays = $([]);
807 $.each($.ui.dialog.overlay.instances, function() {
808 $overlays = $overlays.add(this);
815 width: $.ui.dialog.overlay.width(),
816 height: $.ui.dialog.overlay.height()
821 $.extend($.ui.dialog.overlay.prototype, {
822 destroy: function() {
823 $.ui.dialog.overlay.destroy(this.$el);