From 5309d5f24d17c0cc808eecdb50de470890272fe0 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 9 Jan 2014 15:46:32 -0700 Subject: [PATCH] Add ability to suppress blur on all focusable objects in a focus context When a view receives a 'focusout' event, we relay that to the model by calling ::blur. This is great for when users initiate the change in focus, but 'focusout' events can *also* be caused by elements being temporarily detached from the DOM. The ::suppressBlur method gives the ability to ignore blur calls during a certain operation. This is helpful, for example, when we want to detach a model and reattach it somewhere else without changing its focus state. --- src/focus-context.coffee | 10 ++++++++++ src/focusable.coffee | 13 +++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/focus-context.coffee b/src/focus-context.coffee index e52b0487f..c17514471 100644 --- a/src/focus-context.coffee +++ b/src/focus-context.coffee @@ -3,3 +3,13 @@ module.exports = class FocusContext extends Model @property 'focusedObject', null + + blurSuppressionCounter: 0 + + isBlurSuppressed: -> + @blurSuppressionCounter > 0 + + suppressBlur: (fn) -> + @blurSuppressionCounter++ + fn() + @blurSuppressionCounter-- diff --git a/src/focusable.coffee b/src/focusable.coffee index 69c966896..c046e7cd2 100644 --- a/src/focusable.coffee +++ b/src/focusable.coffee @@ -12,8 +12,17 @@ class Focusable extends Mixin focus: -> throw new Error("Object must be assigned a focusContext to be focus") unless @focusContext - @focusContext.focusedObject = this + unless @focused + @suppressBlur => + @focusContext.focusedObject = this blur: -> throw new Error("Object must be assigned a focusContext to be blurred") unless @focusContext - @focusContext.focusedObject = null if @focused + if @focused and not @focusContext.isBlurSuppressed() + @focusContext.focusedObject = null + + suppressBlur: (fn) -> + if @focusContext? + @focusContext.suppressBlur(fn) + else + fn()