Sha256: 6dd7f387ec59c80bc0df9424cb3b3ae401f8d0b6fdf1d668b28ec73665e2db29
Contents?: true
Size: 1.96 KB
Versions: 55
Compression:
Stored size: 1.96 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.originalEvent.target); }); } end # puts "Register: #{event} - #{binding.binding_name} - #{binding.object_id}" @events[event][binding.binding_name] ||= {} @events[event][binding.binding_name][binding.object_id] = handler end def handle(event_name, event, target) # puts "Handle: #{event_name} on #{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
55 entries across 55 versions & 1 rubygems