]> git.r.bdr.sh - rbdr/custom_event_support.lua/commitdiff
Actually translate
authorBen Beltran <redacted>
Tue, 19 Mar 2013 03:01:56 +0000 (21:01 -0600)
committerBen Beltran <redacted>
Tue, 19 Mar 2013 03:01:56 +0000 (21:01 -0600)
README.md
lib/custom_event.lua
lib/custom_event_support.lua

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..78af6123dc1d24ddab9e9649b794cf5bcd3935ba 100644 (file)
--- a/README.md
+++ b/README.md
@@ -0,0 +1,53 @@
+# custom_event_support.lua #
+
+This is a lua port of CustomEventSupport.js by @azendal. It's a
+simple implementation of the observer pattern.
+
+## Dependencies ##
+
+[Middleclass][https://github.com/kikito/middleclass]
+
+## Usage ##
+
+Step 1. Require it somewhere
+
+```lua
+CustomEvent = require('custom_event')
+CustomEventSupport = require('custom_event_support')
+```
+
+Step 2. Include it in a class
+
+```
+local MyClass = class('MyClass')
+MyClass:include(CustomEventSupport)
+```
+
+Step 3a. Bind events
+
+```lua
+-- Bind them to the class
+MyClass:bind('click', function (self, ev)
+  print("Hey, I caught the event")
+end)
+
+
+-- or bind them to an instance
+local my_instance = MyClass:new()
+my_instance:bind('click', function (self, ev)
+  print("Hey, I caught another event: "..tostring(ev.message))
+end)
+```
+
+Step 3b. Dispatch events
+
+```lua
+-- Dispatch them from the class. You can optionally pass data as a
+table.
+MyClass:dispatch('click')
+
+-- or dispatch them from an instance
+my_instance:dispatch('click', {message = "Instance event!"})
+```
+
+That's it. Enjoy :)
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c0341d502138bf4ecb5fcd9a857c4a4d6a911974 100644 (file)
@@ -0,0 +1,39 @@
+local CustomEvent = class('CustomEvent')
+
+function CustomEvent:initialize(event_type, data)
+  -- defaults
+  self.bubbles = true
+  self.cancelable = true
+  self.current_target = nil
+  self.timestamp = 0
+  self.target = nil
+  self.type = ''
+  self.is_propagation_stopped = false
+  self.is_default_prevented = false
+  self.is_immediate_propagation_stopped = false
+  self.are_immediate_handlers_prevented = false
+
+  -- init
+  self.type = event_type
+  for k, v in pairs(data) do
+    self[k] = v
+  end
+end
+
+function CustomEvent:stop_propagation()
+  self.is_propagation_stopped = true
+end
+
+function CustomEvent:prevent_default()
+  self.is_default_prevented = true
+end
+
+function CustomEvent:stop_immediate_propagation()
+  self.is_immediate_propagation_stopped = true
+end
+
+function CustomEvent:prevent_immediate_handlers()
+  self.are_immediate_handlers_prevented = true
+end
+
+return CustomEvent;
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..73686816c2479964f881ed0fa4169d1389bc820c 100644 (file)
@@ -0,0 +1,179 @@
+local CustomEventSupport = { static = {} }
+
+CustomEventSupport.static.event_listeners = nil
+
+function CustomEventSupport.static:bind(event_type, event_handler)
+  local found, listeners
+
+  if not self.static.event_listeners then
+    self.static.event_listeners = {}
+  end
+
+  if not self.static.event_listeners[event_type] then
+    self.static.event_listeners[event_type] = {}
+  end
+
+  found = false
+
+  listeners = self.static.event_listeners[event_type]
+  for i, v in ipairs(listeners) do
+    if v == event_handler then
+      found = true
+      break
+    end
+  end
+
+  if not found then
+    table.insert(self.static.event_listeners[event_type], event_handler)
+  end
+
+  return self.static
+
+end
+
+function CustomEventSupport.static:unbind(event_type, event_handler)
+  local found, listeners, counter
+
+  if not self.static.event_listeners then
+    self.static.event_listeners = {}
+  end
+
+  if not self.static.event_listeners[event_type] then
+    self.static.event_listeners[event_type] = {}
+  end
+
+  found = false
+
+  listeners = self.static.event_listeners[event_type]
+  for i, v in ipairs(listeners) do
+    if v == event_handler then
+      found = true
+      counter = i
+      break
+    end
+  end
+
+  if found then
+    table.remove(self.static.event_listeners[event_type], counter)
+  end
+
+  return self.static
+end
+
+function CustomEventSupport.static:dispatch(event_type, data)
+  local event, listeners, instance, i
+
+  if not self.static.event_listeners then
+    self.static.event_listeners = {}
+  end
+
+  if not data then
+    data = {}
+  end
+
+  if not data.target then
+    data.target = self
+  end
+
+  -- Create new CustomEvent
+  event = CustomEvent:new(event_type, data)
+  listeners = self.static.event_listeners[event_type] or {}
+  instance = self.static
+
+  for i, v in ipairs(listeners) do
+    v(instance, event)
+    if event.are_immediate_handlers_prevented == true then
+      break
+    end
+  end
+end
+
+CustomEventSupport.event_listeners = nil
+
+function CustomEventSupport:bind(event_type, event_handler)
+  local found, listeners
+
+  if not self.event_listeners then
+    self.event_listeners = {}
+  end
+
+  if not self.event_listeners[event_type] then
+    self.event_listeners[event_type] = {}
+  end
+
+  found = false
+
+  listeners = self.event_listeners[event_type]
+  for i, v in ipairs(listeners) do
+    if v == event_handler then
+      found = true
+      break
+    end
+  end
+
+  if not found then
+    table.insert(self.event_listeners[event_type], event_handler)
+  end
+
+  return self
+
+end
+
+function CustomEventSupport:unbind(event_type, event_handler)
+  local found, listeners, counter
+
+  if not self.event_listeners then
+    self.event_listeners = {}
+  end
+
+  if not self.event_listeners[event_type] then
+    self.event_listeners[event_type] = {}
+  end
+
+  found = false
+
+  listeners = self.event_listeners[event_type]
+  for i, v in ipairs(listeners) do
+    if v == event_handler then
+      found = true
+      counter = i
+      break
+    end
+  end
+
+  if found then
+    table.remove(self.event_listeners[event_type], counter)
+  end
+
+  return self
+end
+
+function CustomEventSupport:dispatch(event_type, data)
+  local event, listeners, instance, i
+
+  if not self.event_listeners then
+    self.event_listeners = {}
+  end
+
+  if not data then
+    data = {}
+  end
+
+  if not data.target then
+    data.target = self
+  end
+
+  -- Create new CustomEvent
+  event = CustomEvent:new(event_type, data)
+  listeners = self.event_listeners[event_type] or {}
+  instance = self
+
+  for i, v in ipairs(listeners) do
+    v(instance, event)
+    if event.are_immediate_handlers_prevented == true then
+      break
+    end
+  end
+end
+
+return CustomEventSupport