blob: 728ea4bc239141ce96ce90867f069e971a158009 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 | root = exports ? window
class root.HandlerStack
  constructor: ->
    @stack = []
    @counter = 0
    @passThrough = {}
  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
      console.log i, type
      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
        # If @passThrough is returned, then discontinue further bubbling and pass the event through to the
        # underlying page.  The event is not suppresssed.
        if passThrough == @passThrough
          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
root.handlerStack = new HandlerStack
 |