2017-06-12 19:09:09 +00:00
|
|
|
using System;
|
2017-06-12 20:19:18 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2017-06-12 19:09:09 +00:00
|
|
|
|
2017-06-12 18:45:24 +00:00
|
|
|
namespace Ooui
|
|
|
|
{
|
2017-06-12 19:09:09 +00:00
|
|
|
public class Element
|
2017-06-12 18:45:24 +00:00
|
|
|
{
|
2017-06-12 20:19:18 +00:00
|
|
|
public string Id { get; private set; } = GenerateId ();
|
|
|
|
|
|
|
|
public HtmlMapping Mapping { get; private set; }
|
|
|
|
|
|
|
|
readonly List<Message> messages = new List<Message> ();
|
|
|
|
readonly List<Message> stateMessages = new List<Message> ();
|
2017-06-12 19:09:09 +00:00
|
|
|
|
|
|
|
public Element ()
|
|
|
|
{
|
2017-06-12 20:19:18 +00:00
|
|
|
Mapping = HtmlMapping.Get (GetType ());
|
|
|
|
LogCreate ();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void Log (Message message)
|
|
|
|
{
|
|
|
|
messages.Add (message);
|
|
|
|
|
|
|
|
switch (message.MessageType) {
|
|
|
|
case MessageType.Create:
|
|
|
|
stateMessages.Add (message);
|
|
|
|
break;
|
|
|
|
case MessageType.SetProperty:
|
|
|
|
{
|
|
|
|
var old = stateMessages.FirstOrDefault (
|
|
|
|
x => x.MessageType == MessageType.SetProperty &&
|
|
|
|
x.Member == message.Member);
|
|
|
|
if (old != null) {
|
|
|
|
stateMessages.Remove (old);
|
|
|
|
}
|
|
|
|
stateMessages.Add (message);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void LogCreate ()
|
|
|
|
{
|
|
|
|
Log (new Message {
|
|
|
|
MessageType = MessageType.Create,
|
|
|
|
TargetId = Id,
|
|
|
|
Value = Mapping.TagName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void LogSetProperty (string propertyName, object value)
|
|
|
|
{
|
|
|
|
var m = new Message {
|
|
|
|
MessageType = MessageType.SetProperty,
|
|
|
|
TargetId = Id,
|
|
|
|
Member = Mapping.GetMemberPath (propertyName),
|
|
|
|
};
|
|
|
|
m.SetValue (value);
|
|
|
|
Log (m);
|
2017-06-12 19:09:09 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 20:19:18 +00:00
|
|
|
protected bool SetProperty<T> (ref T backingStore, T newValue, string propertyName = "")
|
2017-06-12 19:09:09 +00:00
|
|
|
{
|
|
|
|
if (!backingStore.Equals (newValue)) {
|
|
|
|
backingStore = newValue;
|
2017-06-12 20:19:18 +00:00
|
|
|
LogSetProperty (propertyName, newValue);
|
2017-06-12 19:09:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string IdChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
|
|
|
|
static string GenerateId ()
|
|
|
|
{
|
|
|
|
var rand = new Random();
|
|
|
|
var chars = new char[8];
|
|
|
|
for (var i = 0; i < chars.Length; i++) {
|
|
|
|
chars[i] = IdChars[rand.Next(0, IdChars.Length)];
|
|
|
|
}
|
|
|
|
return new string(chars);
|
|
|
|
}
|
2017-06-12 18:45:24 +00:00
|
|
|
}
|
2017-06-12 19:09:09 +00:00
|
|
|
}
|