Skip to content

Commit

Permalink
Yet more changes to the unfocus feature to work on more sites
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-petrov committed Mar 24, 2014
1 parent af6be6d commit f073257
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions coffee/frontend/modules/unfocus.coffee
@@ -1,25 +1,21 @@
# TODO: doesn't work for Gmail when the reply text field is open
# TODO: doesn't work for http://3v4l.org/XXbtf
# TODO: doesn't work for http://www.twoo.com/

# sites to test with:
# https://encrypted.google.com/
# http://pagemon.net/
# https://imo.im/register
# http://www.edreams.com/
# http://mailinator.com/
# http://zamunda.net/login.php
# http://www.twoo.com/

class window.Unfocus
disabledElements = []
observer = null

onFocus = (e) ->
element = e.target
# In Chrome, caller is null if the user initiated the focus
# or the element has the 'autofocus' attribute,
# and non-null if the focus was caused by a call to element.focus().
nonUserInitiated = onFocus.caller or element.autofocus
element.blur() if Option.get('disable_autofocus') and nonUserInitiated
e.target.blur() if Option.get('disable_autofocus')

addOnFocus = (element) ->
if isEditableElement element
Expand All @@ -32,12 +28,17 @@ class window.Unfocus
element.removeEventListener 'focus', onFocus, false for element in disabledElements
disabledElements = []

@didReceiveInput: =>
onClick = (e) =>
clickedElement = document.elementFromPoint(e.pageX - window.pageXOffset, e.pageY - window.pageYOffset)
do @didReceiveInput
do clickedElement.focus

@didReceiveInput: ->
do observer.disconnect
$(document.documentElement).off 'click', @didReceiveInput
$(document.documentElement).off 'click', onClick
do removeOnFocus

$(document.documentElement).click(@didReceiveInput).focus()
$(document.documentElement).click(onClick).focus()

observer = new WebKitMutationObserver (mutations) ->
for mutation in mutations
Expand Down

0 comments on commit f073257

Please sign in to comment.