From ce500ea957241aa0dd1edefd4d98859209a8419f Mon Sep 17 00:00:00 2001 From: jlukic Date: Wed, 2 Oct 2013 14:40:02 -0400 Subject: [PATCH] Modal: Adds attach events method and focus/blur on field on show/hide, fixes #124 --- src/modules/modal.js | 47 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/modules/modal.js b/src/modules/modal.js index e7ddf698c..6070c98e8 100755 --- a/src/modules/modal.js +++ b/src/modules/modal.js @@ -48,6 +48,8 @@ $.fn.modal = function(parameters) { $context = $(settings.context), $otherModals = $allModules.not($module), $close = $module.find(selector.close), + + $focusedElement, $dimmer, element = this, @@ -99,6 +101,26 @@ $.fn.modal = function(parameters) { module.set.position(); }, + attachEvents: function(selector, event) { + var + $toggle = $(selector) + ; + event = $.isFunction(module[event]) + ? module[event] + : module.toggle + ; + if($toggle.size() > 0) { + module.debug('Attaching modal events to element', selector, event); + $toggle + .off(eventNamespace) + .on('click' + eventNamespace, event) + ; + } + else { + module.error(error.notFound); + } + }, + event: { close: function() { module.verbose('Close button pressed'); @@ -144,13 +166,18 @@ $.fn.modal = function(parameters) { $module .transition(settings.transition + ' in', settings.duration, function() { module.set.active(); + module.save.focus(); module.set.type(); }) ; } else { $module - .fadeIn(settings.duration, settings.easing, module.set.active) + .fadeIn(settings.duration, settings.easing, function() { + module.set.active(); + module.save.focus(); + module.set.type(); + }) ; } module.debug('Triggering dimmer'); @@ -176,12 +203,16 @@ $.fn.modal = function(parameters) { $module .transition(settings.transition + ' out', settings.duration, function() { module.remove.active(); + module.restore.focus(); }) ; } else { $module - .fadeOut(settings.duration, settings.easing, module.remove.active) + .fadeOut(settings.duration, settings.easing, function() { + module.remove.active(); + module.restore.focus(); + }) ; } $.proxy(settings.onHide, element)(); @@ -203,6 +234,18 @@ $.fn.modal = function(parameters) { } }, + save: { + focus: function() { + $focusedElement = $(document.activeElement).blur(); + } + }, + + restore: { + focus: function() { + $focusedElement.focus(); + } + }, + remove: { active: function() { $module.removeClass(className.active);