Commits

pypyja committed 072bf60

improvement of r12a.events.events

Comments (0)

Files changed (3)

src/r12a/__init__.py

 from array import *
 from base import *
 from disposable import *
+from object import *
 from structs import *
-from object import *
 
 __all__ = [
     'isArray',
+    'isString',
     'isFunction',
-    'isString',
     'getUid',
     'Disposable',
     'getUndisposedObjects',
     'clearUndisposedObjects',
     'dispose',
+    'extend',
     'structs',
 ]
 # -*- coding: utf-8 -*-
 
 import random
+import types
 
 
-def isArray(type):
-    return isinstance(type, list)
+def typeOf(value):
+    u"""
+    This is a "fixed" version of the typeof operator.  It differs from the typeof
+    operator in such a way that null returns 'null' and arrays return 'array'.
+    """
 
+    pass
 
-def isFunction(f):
-    #TODO: xxx
-    return hasattr(f, '__call__')
 
-def isString(s):
-    return isinstance(s, str)
+def isArray(value):
+    u"""
+    Returns true if the specified value is an array
+    """
+
+    return isinstance(value, list)
+
+
+def isString(value):
+    u"""
+    Returns true if the specified value is a string
+    """
+
+    return isinstance(value, str)
+
+
+def isFunction(value):
+    u"""
+    Returns true if the specified value is a function
+    """
+
+    return type(value) is types.FunctionType
 
 
 uidCounter_ = 0
 
 
 def getUid(obj):
+    #TODO: xxx
+
     global uidCounter_
     global UID_PROPERTY
     if hasattr(obj, UID_PROPERTY_):

src/r12a/events/events.py

 # -*- coding: utf-8 -*-
 
 import r12a
+#r12a.events.Event
 
 
 # Container for storing event listeners and their proxies
 keySeparator_ = '_'
 
 # Whether the browser natively supports full W3C event propagation.
-global requiresSyntheticEventPropagation_
+requiresSyntheticEventPropagation_ = None
 
 
 def listen(src, type, listener, *args):
         return key
 
 
+def listenOnce(src, type, listener, *args):
+    u"""
+    TODO: remove *args and set **kws or arg that have default value.
+
+    Adds an event listener for a specific event on a DomNode or an object
+    that has implemented {@link r12a.events.EventTarget}. After the event
+    has fired the event listener is removed from the target
+    """
+
+    opt_capt = args[0] if len(args) > 0 else None
+    opt_handler = args[1] if len(args) > 1 else None
+
+    #TODO: xxx
+    pass
+
+
+def listenWithWrapper(src, wrapper, listener, *args):
+    u"""
+    TODO: remove *args and set **kws or arg that have default value.
+
+    Adds an event listener with a specific event wrapper on a DOM Node or an
+    object that has implemented {@link r12a.events.EventTarget}. A listener
+    can only be added once to an object.
+    """
+
+    opt_capt = args[0] if len(args) > 0 else None
+    opt_handler = args[1] if len(args) > 1 else None
+
+    wrapper.listen(src, listener, opt_capt, opt_handler)
+
+
 def unlisten(src, type, listener, *args):
     u"""
+    TODO: remove *args and set **kws or arg that have default value.
+
     Removes an event listener which was added with listen().
     """
 
     return True
 
 
-def cleanUp_():
+def unlistenWithWrapper(src, wrapper, listener, *args):
+    u"""
+    TODO: remove *args and set **kws or arg that have default value.
+
+    Removes an event listener which was added with listenWithWrapper().
+    """
+
+    opt_capt = args[0] if len(args) > 0 else None
+    opt_handler = args[1] if len(args) > 1 else None
+
+    wrapper.unlisten(src, listener, opt_capt, opt_handler)
+
+
+def cleanUp_(type, capture, srcUid, listenerArray):
+    u"""
+    Cleans up the listener array as well as the listener tree
+    """
+
+    # The listener array gets locked during the dispatch phase so that
+    # removals of listeners during this phase does not screw up the indeces.
+    # This method is called after we have removed a listener as well as
+    # after the dispatch phase in case any listeners were removed.
+
+    if not hasattr(listenerArray, 'locked_'):
+        # catches both 0 and not set
+
+        if hasattr(listenerArray, 'needsCleanup_'):
+            # Loop over the listener array and remove listeners that have
+            # removed set to true. This could have been done with filter or
+            # something similar but we want to change the array in place and
+            # we want to minimize allocations. Adding a listener during this
+            # phase adds to the end of the array so that works fine as long
+            # as the length is rechecked every in iteration.
+
+            pass
+
     #TODO: xxx
-    pass
 
 
 def getListeners_(obj, type, capture):
     return None
 
 
+def getOnString_(type):
+    u"""
+    Returns a string wth on prepended to the specified type. This is used
+    for IE which expects "on" to be prepended. This function caches the
+    string in order to avoid extra allocations in steady state.
+    """
+
+    if type in r12a.events.onStringMap_:
+        return r12a.events.onStringMap_[type]
+
+    r12a.events.onStringMap_[type] = r12a.events.onString_ + type
+
+    return r12a.events.onStringMap_[type]
+
+
 def dispatchEvent(src, e):
     u"""
     Dispatches an event (or event like object) and calls all listeners
         e = r12a.events.Event(type, src)
         r12a.object.extend(e, oldEvent)
     else:
-        # TODO: xxx
-        # javascript: e.target = e.target || src
-        e.target = e.target or src
+        e.target = e.target if hasattr(e, 'target') else src
 
     rv = 1
     ancestors = None
         # Build ancestors now
         ancestors = []
 
-        print(src)
+        parent = src
+        while True:
+            if parent is None:
+                break
+            ancestors.append(parent)
+            parent = parent.getParentEventTarget()
 
         # TODO: xxx