diff options
| author | Jez Ng | 2012-10-20 23:17:30 -0400 |
|---|---|---|
| committer | Jez Ng | 2012-10-20 23:17:45 -0400 |
| commit | ea73be17468b1bd02171c82e1fb21b1bc16ccd0e (patch) | |
| tree | 223e4a3b56a410f599e19f81b38752ded86d44c6 /lib | |
| parent | 2ed217b43663a553c78fa08a8a10152a43e93cf5 (diff) | |
| download | vimium-ea73be17468b1bd02171c82e1fb21b1bc16ccd0e.tar.bz2 | |
Refactor handlerStack. Closes #657.
Previously, handlerStack was designed only for removal of the handler
right at the top of the stack. However, some handlers sought to remove
themselves when they were not at the top of the stack, creating
confusion. The new handlerStack ensures that such removal can always be
done safely.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/dom_utils.coffee | 4 | ||||
| -rw-r--r-- | lib/handler_stack.coffee | 37 |
2 files changed, 41 insertions, 0 deletions
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index 30530e0d..501e43a5 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -131,5 +131,9 @@ DomUtils = document.documentElement.appendChild(flashEl) setTimeout((-> DomUtils.removeElement flashEl), 400) + suppressEvent: (event) -> + event.preventDefault() + event.stopPropagation() + root = exports ? window root.DomUtils = DomUtils diff --git a/lib/handler_stack.coffee b/lib/handler_stack.coffee new file mode 100644 index 00000000..858f2ec9 --- /dev/null +++ b/lib/handler_stack.coffee @@ -0,0 +1,37 @@ +root = exports ? window + +class root.HandlerStack + + constructor: -> + @stack = [] + @counter = 0 + + genId: -> @counter = ++@counter & 0xffff + + # Adds a handler to the stack. Returns a unique ID for that handler that can be used to remove it later. + push: (handler) -> + handler.id = @genId() + @stack.push handler + handler.id + + # Called whenever we receive a key event. Each individual handler has the option to stop the event's + # propagation by returning a falsy value. + bubbleEvent: (type, event) -> + for i in [(@stack.length - 1)..0] by -1 + handler = @stack[i] + # We need to check for existence of handler because the last function call may have caused the release + # of more than one handler. + if handler && handler[type] + @currentId = handler.id + passThrough = handler[type].call(@, event) + if not passThrough + DomUtils.suppressEvent(event) + return false + true + + remove: (id = @currentId) -> + for i in [(@stack.length - 1)..0] by -1 + handler = @stack[i] + if handler.id == id + @stack.splice(i, 1) + break |
