From f8c5e75070dc88b94248c5daa506cfbebd8b619b Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sat, 24 Jun 2017 16:25:14 -0700 Subject: [PATCH] Complete the Todo Sample Fixes #17 --- Ooui/Client.js | 1 + Ooui/Form.cs | 6 +++ Ooui/Input.cs | 5 ++- Ooui/Node.cs | 10 +++++ Ooui/Paragraph.cs | 18 +++++++++ Samples/TodoSample.cs | 94 ++++++++++++++++++++++++++++++++++--------- 6 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 Ooui/Paragraph.cs diff --git a/Ooui/Client.js b/Ooui/Client.js index 4216db8..8f554aa 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -104,6 +104,7 @@ function msgListen (m) { const ems = JSON.stringify (em); socket.send (ems); if (debug) console.log ("Event", em); + return true; }); } diff --git a/Ooui/Form.cs b/Ooui/Form.cs index 77ab132..dc1eee6 100644 --- a/Ooui/Form.cs +++ b/Ooui/Form.cs @@ -4,6 +4,12 @@ namespace Ooui { public class Form : Element { + string action = ""; + public string Action { + get => action; + set => SetProperty (ref action, value ?? "", "action"); + } + public event EventHandler Submitted { add => AddEventListener ("submit", value); remove => RemoveEventListener ("submit", value); diff --git a/Ooui/Input.cs b/Ooui/Input.cs index b6bab86..3c592cc 100644 --- a/Ooui/Input.cs +++ b/Ooui/Input.cs @@ -43,7 +43,10 @@ namespace Ooui bool isChecked = false; public bool IsChecked { get => isChecked; - set => SetProperty (ref isChecked, value, "checked"); + set { + SetProperty (ref isChecked, value, "checked"); + TriggerEventFromMessage (Message.Event (Id, "change", isChecked)); + } } double minimum = 0; diff --git a/Ooui/Node.cs b/Ooui/Node.cs index b072702..5a80ca1 100644 --- a/Ooui/Node.cs +++ b/Ooui/Node.cs @@ -16,6 +16,16 @@ namespace Ooui } } + public Node FirstChild { + get { + lock (children) { + if (children.Count > 0) + return children[0]; + return null; + } + } + } + public virtual string Text { get { return String.Join ("", from c in Children select c.Text); } set { diff --git a/Ooui/Paragraph.cs b/Ooui/Paragraph.cs new file mode 100644 index 0000000..d160656 --- /dev/null +++ b/Ooui/Paragraph.cs @@ -0,0 +1,18 @@ +using System; + +namespace Ooui +{ + public class Paragraph : Element + { + public Paragraph () + : base ("p") + { + } + + public Paragraph (string text) + : this () + { + Text = text; + } + } +} diff --git a/Samples/TodoSample.cs b/Samples/TodoSample.cs index ff7bf6c..d560366 100644 --- a/Samples/TodoSample.cs +++ b/Samples/TodoSample.cs @@ -1,48 +1,102 @@ using System; using System.Collections.Generic; +using System.Linq; using Ooui; namespace Samples { public class TodoSample { - List items = new List (); + List items = new List () { + ClassName = "list-group", + }; + + public TodoSample () + { + items.Style.MarginTop = "1em"; + } class Item : ListItem { + Element label = new Div (); + + bool isDone; + public bool IsDone { + get => isDone; + set { + isDone = value; + label.Style.TextDecoration = + isDone ? "line-through" : "none"; + label.Style.FontWeight = + isDone ? "normal" : "bold"; + label.Style.Color = + isDone ? "#999" : "#000"; + } + } + public Item (string text) { - var check = new Input { - Type = InputType.Checkbox, - }; - var label = new Label { - Text = text, - For = check - }; - check.Changed += (s,e) => { - label.Style.TextDecoration = - check.IsChecked ? "line-through" : "none"; - }; - AppendChild (check); + ClassName = "list-group-item"; + Style.Cursor = "pointer"; + label.Text = text; + label.Style.FontWeight = "bold"; AppendChild (label); } } Element MakeTodo () - { - var input = new Input (); - var button = new Button ("Add the item"); - button.Clicked += (s, e) => { + { + var heading = new Heading ("Todo List"); + var subtitle = new Paragraph ("This is the shared todo list of the world."); + var inputForm = new Form { + ClassName = "form-inline" + }; + var input = new Input { + ClassName = "form-control" + }; + var addbtn = new Button ("Add") { + Type = ButtonType.Submit, + ClassName = "btn btn-primary", + }; + addbtn.Style.MarginLeft = "1em"; + var clearbtn = new Button ("Clear Completed") { + Type = ButtonType.Submit, + ClassName = "btn btn-danger", + }; + void AddItem () + { if (string.IsNullOrWhiteSpace (input.Value)) return; var item = new Item (input.Value); - items.AppendChild (item); + item.Clicked += (s, e) => { + item.IsDone = !item.IsDone; + }; + items.InsertBefore (item, items.FirstChild); input.Value = ""; + } + addbtn.Clicked += (s, e) => { + AddItem (); + }; + inputForm.Submitted += (s, e) => { + AddItem (); + }; + clearbtn.Clicked += (s, e) => { + var toremove = new List (); + foreach (Item i in items.Children) { + if (i.IsDone) toremove.Add (i); + } + foreach (var i in toremove) { + items.RemoveChild (i); + } }; var app = new Div (); - app.AppendChild (input); - app.AppendChild (button); + app.AppendChild (heading); + app.AppendChild (subtitle); + inputForm.AppendChild (input); + inputForm.AppendChild (addbtn); + app.AppendChild (inputForm); app.AppendChild (items); + app.AppendChild (clearbtn); return app; }