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.
This commit is contained in:
Nathan Sobo
2014-01-09 15:46:32 -07:00
parent af3ca57094
commit 5309d5f24d
2 changed files with 21 additions and 2 deletions

View File

@@ -3,3 +3,13 @@
module.exports =
class FocusContext extends Model
@property 'focusedObject', null
blurSuppressionCounter: 0
isBlurSuppressed: ->
@blurSuppressionCounter > 0
suppressBlur: (fn) ->
@blurSuppressionCounter++
fn()
@blurSuppressionCounter--

View File

@@ -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()