Sha256: 0572288e726daed54087b53bca1332d024fe00935ddb77c24cca7803ce351489

Contents?: true

Size: 1.83 KB

Versions: 26

Compression:

Stored size: 1.83 KB

Contents

class DocumentEvents
  def initialize
    @events = {}
  end

  def add(event, binding, handler)
    # Track each document event based on the event, element id, then binding.object_id
    unless @events[event]
      # We haven't defined an event of type event yet, lets attach it to the
      # document.

      @events[event] = {}

      that = self

      %x{
        $('body').on(event, function(e) {
          that.$handle(event, e, e.target || e.originalEvent.target);
        });
      }

    end

    @events[event][binding.binding_name] ||= {}
    @events[event][binding.binding_name][binding.object_id] = handler
  end

  def handle(event_name, event, target)
    element = Element.find(target)

    loop do
      # Lookup the handler, make sure to not assume the group
      # exists.
      # TODO: Sometimes the event doesn't exist, but we still get
      # an event.
      handlers = @events[event_name]
      handlers = handlers[element.id] if handlers

      if handlers
        handlers.values.each do |handler|
          # Call each handler for this object
          handler.call(event)
        end
      end

      if element.size == 0
        break
      else
        element = element.parent
      end
    end

    nil
  end

  def remove(event, binding)
    # Remove the event binding
    @events[event][binding.binding_name].delete(binding.object_id)

    # if there are no more handlers for this binding_name (the html id), then
    # we remove the binding name hash
    if @events[event][binding.binding_name].size == 0
      @events[event].delete(binding.binding_name)
    end

    # if there are no more handlers in this event, we can unregister the event
    # from the document
    if @events[event].size == 0
      @events.delete(event)

      # Remove the event from the body
      %x{
        $('body').unbind(event);
      }
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
volt-0.8.14 lib/volt/page/document_events.rb
volt-0.8.13 lib/volt/page/document_events.rb
volt-0.8.11 lib/volt/page/document_events.rb
volt-0.8.10 lib/volt/page/document_events.rb
volt-0.8.9 lib/volt/page/document_events.rb
volt-0.8.8 lib/volt/page/document_events.rb
volt-0.8.7 lib/volt/page/document_events.rb
volt-0.8.6 lib/volt/page/document_events.rb
volt-0.8.5 lib/volt/page/document_events.rb
volt-0.8.4 lib/volt/page/document_events.rb
volt-0.8.3 lib/volt/page/document_events.rb
volt-0.8.2 lib/volt/page/document_events.rb
volt-0.8.1 lib/volt/page/document_events.rb
volt-0.8.0 lib/volt/page/document_events.rb
volt-0.7.23 lib/volt/page/document_events.rb
volt-0.7.22 lib/volt/page/document_events.rb
volt-0.7.21 lib/volt/page/document_events.rb
volt-0.7.20 lib/volt/page/document_events.rb
volt-0.7.19 lib/volt/page/document_events.rb
volt-0.7.18 lib/volt/page/document_events.rb