From 7537736a0898f736d689092b9bd350886f327ab0 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 3 Jan 2015 18:32:47 +0000 Subject: Modes; add ConstrainedMode. --- content_scripts/mode.coffee | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'content_scripts/mode.coffee') diff --git a/content_scripts/mode.coffee b/content_scripts/mode.coffee index a19c3df0..e4e2679d 100644 --- a/content_scripts/mode.coffee +++ b/content_scripts/mode.coffee @@ -83,7 +83,26 @@ class MultiMode extends Mode exit: -> mode.exit() for mode in modes +# When the user clicks anywhere outside of the given element, the mode is exited. +class ConstrainedMode extends Mode + constructor: (@element, options) -> + options.name = if options.name? then "constrained-#{options.name}" else "constrained" + super options + + @handlers.push handlerStack.push + "click": (event) => + @exit() unless @isDOMDescendant @element, event.srcElement + @continueBubbling + + isDOMDescendant: (parent, child) -> + node = child + while (node != null) + return true if (node == parent) + node = node.parentNode + false + root = exports ? window root.Mode = Mode root.SingletonMode = SingletonMode root.MultiMode = MultiMode +root.ConstrainedMode = ConstrainedMode -- cgit v1.2.3