Fix event reception bubbling

This commit is contained in:
Frank A. Krueger 2017-06-24 12:58:14 -07:00
parent d9efbcd494
commit d4bae780d3
4 changed files with 34 additions and 6 deletions

View File

@ -1,5 +1,5 @@
const debug = false;
const debug = true;
const nodes = {};

View File

@ -134,9 +134,8 @@ namespace Ooui
{
if (message == null)
return;
if (message.TargetId != Id)
return;
SaveStateMessageIfNeeded (message);
if (message.TargetId == Id)
SaveStateMessageIfNeeded (message);
TriggerEventFromMessage (message);
}
@ -168,8 +167,10 @@ namespace Ooui
}
}
protected virtual void TriggerEventFromMessage (Message message)
protected virtual bool TriggerEventFromMessage (Message message)
{
if (message.TargetId != Id)
return false;
List<EventHandler> handlers = null;
lock (eventListeners) {
List<EventHandler> hs;
@ -177,11 +178,12 @@ namespace Ooui
handlers = new List<EventHandler> (hs);
}
}
if (handlers == null) return;
if (handlers == null) return true;
var args = EventArgs.Empty;
foreach (var h in handlers) {
h.Invoke (this, args);
}
return true;
}
}

View File

@ -133,5 +133,17 @@ namespace Ooui
break;
}
}
protected override bool TriggerEventFromMessage (Message message)
{
if (base.TriggerEventFromMessage (message))
return true;
var ch = Children;
for (var i = 0; i < ch.Count; i++) {
if (ch[i].TriggerEventFromMessage (message))
return true;
}
return false;
}
}
}

View File

@ -37,5 +37,19 @@ namespace Tests
Assert.AreEqual (c0, c0s.GetValue (0));
Assert.AreEqual (c2, c0s.GetValue (1));
}
[TestMethod]
public void EventReceptionBubblesDown ()
{
var p = new Div ();
var b = new Button ();
p.AppendChild (b);
var clicked = false;
b.Clicked += (s, e) => {
clicked = true;
};
p.Receive (Message.Event (b.Id, "click"));
Assert.IsTrue (clicked);
}
}
}