Add Document, Window, and Body

Fixes #124
This commit is contained in:
Frank A. Krueger 2018-04-16 13:52:12 -07:00
parent 8489c4a77f
commit cd5ea217b5
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
6 changed files with 173 additions and 1 deletions

13
Ooui/Body.cs Normal file
View File

@ -0,0 +1,13 @@
using System;
namespace Ooui
{
public class Body : Element
{
public Body ()
: base ("Body")
{
Id = "document.body";
}
}
}

23
Ooui/Document.cs Normal file
View File

@ -0,0 +1,23 @@
using System;
namespace Ooui
{
public class Document : EventTarget
{
public Window Window { get; } = new Window ();
public Body Body { get; } = new Body ();
public Document ()
: base ("document")
{
Id = "document";
Window.MessageSent += Proxy_MessageSent;
Body.MessageSent += Proxy_MessageSent;
}
void Proxy_MessageSent (Message message)
{
Send (message);
}
}
}

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Threading;
namespace Ooui namespace Ooui
{ {
@ -8,6 +9,8 @@ namespace Ooui
{ {
readonly Dictionary<string, object> attributes = new Dictionary<string, object> (); readonly Dictionary<string, object> attributes = new Dictionary<string, object> ();
Document document = null;
public string ClassName { public string ClassName {
get => GetStringAttribute ("class", ""); get => GetStringAttribute ("class", "");
set => SetAttributeProperty ("class", value); set => SetAttributeProperty ("class", value);
@ -90,6 +93,17 @@ namespace Ooui
remove => RemoveEventListener ("wheel", value); remove => RemoveEventListener ("wheel", value);
} }
public Document Document {
get {
if (document == null) {
if (Interlocked.CompareExchange (ref document, new Document (), null) == null) {
document.MessageSent += Document_MessageSent;
}
}
return document;
}
}
/// <summary> /// <summary>
/// A signal to Ooui that this element should take up the /// A signal to Ooui that this element should take up the
/// entire browser window. /// entire browser window.
@ -229,6 +243,11 @@ namespace Ooui
SendSet ("style." + Style.GetJsName (e.PropertyName), Style[e.PropertyName]); SendSet ("style." + Style.GetJsName (e.PropertyName), Style[e.PropertyName]);
} }
void Document_MessageSent (Message message)
{
Send (message);
}
protected virtual bool HtmlNeedsFullEndElement => false; protected virtual bool HtmlNeedsFullEndElement => false;
#if !NO_XML #if !NO_XML

View File

@ -13,7 +13,7 @@ namespace Ooui
readonly Dictionary<string, List<TargetEventHandler>> eventListeners = readonly Dictionary<string, List<TargetEventHandler>> eventListeners =
new Dictionary<string, List<TargetEventHandler>> (); new Dictionary<string, List<TargetEventHandler>> ();
public string Id { get; private set; } = GenerateId (); public string Id { get; protected set; } = GenerateId ();
public string TagName { get; private set; } public string TagName { get; private set; }

24
Ooui/Window.cs Normal file
View File

@ -0,0 +1,24 @@
namespace Ooui
{
public class Window : EventTarget
{
string location = "";
public string Location {
get => location;
set {
if (string.IsNullOrEmpty (value) || location == value)
return;
location = value;
Send (Message.Set ("window", "location", value));
OnPropertyChanged ("Location");
}
}
public Window ()
: base ("window")
{
Id = "window";
}
}
}

93
Tests/WindowTests.cs Normal file
View File

@ -0,0 +1,93 @@
using System;
#if NUNIT
using NUnit.Framework;
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
using TestMethodAttribute = NUnit.Framework.TestCaseAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
using Ooui;
namespace Tests
{
[TestClass]
public class WindowTests
{
[TestMethod]
public void ElementDocumentNotNull ()
{
var b = new Button ();
Assert.IsNotNull (b.Document);
}
[TestMethod]
public void DocumentWindowNotNull ()
{
var d = new Document ();
Assert.IsNotNull (d.Window);
}
[TestMethod]
public void DocumentBodyNotNull ()
{
var d = new Document ();
Assert.IsNotNull (d.Body);
}
[TestMethod]
public void WindowIdIsWindow ()
{
Assert.AreEqual ("window", new Window ().Id);
}
[TestMethod]
public void DocumentIdIsDocument ()
{
Assert.AreEqual ("document", new Document ().Id);
}
[TestMethod]
public void BodyIdIsDocumentBody ()
{
Assert.AreEqual ("document.body", new Body ().Id);
}
[TestMethod]
public void DocumentGetsWindowMessages ()
{
var d = new Document ();
var received = false;
d.MessageSent += m => {
received = m.TargetId == "window";
};
d.Window.Location = "http://google.com";
Assert.IsTrue (received);
}
[TestMethod]
public void ElementGetsWindowMessages ()
{
var b = new Button ();
var received = false;
b.MessageSent += m => {
received = m.TargetId == "window";
};
b.Document.Window.Location = "http://google.com";
Assert.IsTrue (received);
}
[TestMethod]
public void ElementGetsBodyMessages ()
{
var b = new Button ();
var received = false;
b.MessageSent += m => {
received = m.TargetId == "document.body";
};
b.Document.Body.Call ("foo");
Assert.IsTrue (received);
}
}
}