/**********************************************************************************/
// this keypress event is attached to the document and captures every ENTER key hit.
// If the focused element is not a textarea or button then it finds the first button
// with a class of default and raises a click event on that button.
/**********************************************************************************/
//
(function($) {
$().ready(function() {
$(document).keypress(function(e) {
if (e.which === 13 && (typeof nicEditors === 'undefined')) {
var $ele_with_max_zindex = $(document);
$('.ui-widget-overlay,.ui-dialog:visible').each(function(index) {
if (index === 0 || $(this).css('z-index') > $ele_with_max_zindex.css('z-index')) {
$ele_with_max_zindex = $(this);
}
});
var $defaultBtn = $([]);
if ($ele_with_max_zindex.hasClass('ui-dialog')) {
$defaultBtn = $('.ui-button.default:visible:first', $ele_with_max_zindex);
}
if ($defaultBtn.length === 0) {
$defaultBtn = $('.ui-button.default:visible:first');
}
if ($defaultBtn.length === 1 && $defaultBtn.closest('.protected').length === 0) {
$('.ui-button.ui-state-focus:visible:not(.default)', $ele_with_max_zindex).removeClass('ui-state-focus');
$defaultBtn.addClass('ui-state-focus');
var $target = $(e.target);
if (!$target.is('textarea') && !$target.hasClass('ui-button')) {
$defaultBtn.click();
return false;
}
}
}
});
});
})(jQuery);
/***********************************************************************************/
/* NOTE THAT THE DIALOG EXTENSION BELOW IS DEPENDENT ON THE KEYPRESS FUNCTIONALITY ABOVE. */
/* THE REVERSE IS NOT THE CASE HOWEVER. BUTTONS CAN BE MADE DEFAULT WITHOUT BEING IN A DIALOG. */
/****************** Start of uiDialog widget extension ****************/
(function($) {
var _create = $.ui.dialog.prototype._create;
$.ui.dialog.prototype._create = function() {
var self = this;
_create.apply(this, arguments);
var saveLeft, saveTop;
if (self.options.sticky) {
self.uiDialog.css('position', 'fixed');
}
this.uiDialog
.bind('dialogopen', function(event, ui) {
self.uiDialog.find(':button.ui-state-focus').removeClass('ui-state-focus');
if (self.options.defaultButton !== '') {
self.uiDialog.find(':button:contains(' + self.options.defaultButton + '):first')
.addClass('ui-state-focus')
.addClass('default');
}
var $ele = self.uiDialog.find('input:text:visible:first');
if ($ele.length === 0 || $ele.closest('.protected').length === 1) {
$ele = self.uiDialog.find(':button.ui-state-focus.default:visible:first');
}
if ($ele.length === 0 || $ele.closest('.protected').length === 1) {
$ele = $(document).find(':button.ui-state-focus.default:visible:first');
}
if ($ele.length === 0 || $ele.closest('.protected').length === 1) {
$ele = $(document).find(':button:visible:first');
}
if ($ele.length === 1 && $ele.closest('.protected').length === 0) {
$ele.focus();
}
})
.bind('dragstop', function(event, ui) {
if (self.options.sticky) {
saveLeft = ui.position.left;
saveTop = ui.position.top;
}
})
.bind('resizestart', function(event, ui) {
if (self.options.sticky) {
saveLeft = ui.position.left - $(window).scrollLeft();
saveTop = ui.position.top - $(window).scrollTop();
}
})
.bind('resizestop', function(event, ui) {
if (self.options.sticky) {
self.uiDialog.css({ 'position': 'fixed', 'left': saveLeft, 'top': saveTop });
}
});
};
$.ui.dialog.prototype.options.sticky = true;
$.ui.dialog.prototype.options.defaultButton = '';
})(jQuery);