Example #1
0
 /// <summary>
 /// Adds an event listener to the event target. The same handler can only be
 /// added once per the type. Even if you add the same handler multiple times
 /// using the same type then it will only be called once when the event is
 /// dispatched.
 /// </summary>
 /// <param name="type">The type of the event to listen for.</param>
 /// <param name="handler">The function
 /// to handle the event. The handler can also be an object that implements
 /// the handleEvent method which takes the event object as argument.</param>
 /// <param name="opt_capture">In DOM-compliant browsers, this determines
 /// whether the listener is fired during the capture or bubble phase
 /// of the event.</param>
 /// <param name="opt_handlerScope">Object in whose scope to call
 /// the listener.</param>
 public void addEventListener(string type, Delegate handler, bool opt_capture = false,
                              object opt_handlerScope = null)
 {
     if (orgTarget_ != null)
     {
         orgTarget_.AddEventListener(type, handler, opt_capture);
     }
     else
     {
         goog.events.listen(this, type, handler, opt_capture, opt_handlerScope);
     }
 }
Example #2
0
        /// <summary>
        /// Note that a one-off listener will not change an existing listener,
        /// if any. On the other hand a normal listener will change existing
        /// one-off listener to become a normal listener.
        /// </summary>
        /// <param name="src">The node to listen to events on.</param>
        /// <param name="type">Event type.</param>
        /// <param name="listener">Callback function.</param>
        /// <param name="callOnce">Whether the listener is a one-off
        /// listener or otherwise.</param>
        /// <param name="opt_capt">Whether to fire in capture phase (defaults to
        /// false).</param>
        /// <param name="opt_handler">Element in whose scope to call the listener.</param>
        /// <returns>Unique key for the listener.</returns>
        private static Listener listen_(Bridge.Html5.EventTarget src, string type, Delegate listener,
                                        bool callOnce, bool opt_capt = false, object opt_handler = null)
        {
            if (type == null)
            {
                throw new Exception("Invalid event type");
            }

            var capture = !!opt_capt;

            if (capture && !goog.events.BrowserFeature.HAS_W3C_EVENT_SUPPORT)
            {
                if (goog.events.CAPTURE_SIMULATION_MODE ==
                    goog.events.CaptureSimulationMode.OFF_AND_FAIL)
                {
                    goog.asserts.fail("Can not register capture listener in IE8-.");
                    return(null);
                }
                else if (
                    goog.events.CAPTURE_SIMULATION_MODE ==
                    goog.events.CaptureSimulationMode.OFF_AND_SILENT)
                {
                    return(null);
                }
            }

            var listenerMap = goog.events.getListenerMap_(src);

            if (listenerMap == null)
            {
                src[goog.events.LISTENER_MAP_PROP_] = listenerMap =
                    new goog.events.ListenerMap(src);
            }

            var listenerObj =
                listenerMap.add(type, listener, callOnce, opt_capt, opt_handler);

            // If the listenerObj already has a proxy, it has been set up
            // previously. We simply return.
            if (listenerObj.proxy != null)
            {
                return(listenerObj);
            }

            var proxy = goog.events.getProxy();

            listenerObj.proxy = proxy;

            proxy.src      = src;
            proxy.listener = listenerObj.listener;
#if false
            // Attach the proxy through the browser's API
            if (Script.IsDefined(src, "addEventListener"))
            {
                src.AddEventListener(type, proxy.Instance, capture);
            }
            else if (Script.IsDefined(src, "attachEvent"))
            {
                // The else if above used to be an unconditional else. It would call
                // exception on IE11, spoiling the day of some callers. The previous
                // incarnation of this code, from 2007, indicates that it replaced an
                // earlier still version that caused excess allocations on IE6.
                Script.Get <Action <object, Delegate> >(src, "attachEvent")(goog.events.getOnString_(type), proxy.Instance);
            }
            else
            {
                throw new Exception("addEventListener and attachEvent are unavailable.");
            }
#else
            src.AddEventListener(type, proxy.Instance, capture);
#endif
            goog.events.listenerCountEstimate_++;
            return(listenerObj);
        }