Skip to content

Instantly share code, notes, and snippets.

@chadmyers
Created March 8, 2010 18:03
Show Gist options
  • Save chadmyers/325412 to your computer and use it in GitHub Desktop.
Save chadmyers/325412 to your computer and use it in GitHub Desktop.
public class DovetailHtmlConventions : HtmlConventionRegistry
{
public DovetailHtmlConventions()
{
validationAttributes();
numbers();
dates();
Profile("edit", x =>
{
x.Editors.Builder<LeafListBuilder>();
x.Editors.Builder<MultiListBuilder>();
x.Editors.Builder<EditInPlaceBuilder>();
});
Labels.Builder<LabelBuilder>();
Editors.Builder<LeafListBuilder>();
Editors.Builder<MultiListBuilder>();
Editors.Builder<ListValueDropdownBuilder>();
Editors.IfPropertyIs<bool>().BuildBy(request => new CheckboxTag(request.Value<bool>())
.Style("width", "auto !important")
.Attr("value", request.ElementId));
Editors.Always.Modify((request, tag) =>
{
tag.Attr("label", request.Header());
tag.Attr("name", request.ElementId);
});
Displays.Builder<LogDateDisplay>();
Displays.Builder<DisplayBuilder>();
}
private void numbers()
{
Editors.IfPropertyIs<Int32>().Attr("max", Int32.MaxValue);
Editors.IfPropertyIs<Int16>().Attr("max", Int16.MaxValue);
Editors.IfPropertyIs<Int64>().Attr("max", Int64.MaxValue);
Editors.IfPropertyTypeIs(t => t.IsIntegerBased()).AddClass("integer");
Editors.IfPropertyTypeIs(t => t.IsFloatingPoint()).AddClass("number");
}
private void dates()
{
Editors.IfPropertyTypeIs(t => t.IsDateTime()).Modify(x =>
{
if (!x.HasMetaData(EditInPlaceBuilder.EDITABLE_ATTRIBUTE_NAME))
{
x.AddClass("DatePicker");
}
});
Editors.If(prop => prop.Accessor.InnerProperty.IsDateAndTime()).Modify(x =>
{
if (!x.HasMetaData(EditInPlaceBuilder.EDITABLE_ATTRIBUTE_NAME))
{
x.AddClass("time-picker");
}
});
}
private void validationAttributes()
{
Editors.AddClassForAttribute<RequiredAttribute>("required");
Editors.ModifyForAttribute<MaximumStringLengthAttribute>((tag, att) =>
{
if (att.Length < Entity.UnboundedStringLength)
{
tag.Attr("maxlength", att.Length);
}
});
Editors.ModifyForAttribute<GreaterOrEqualToZeroAttribute>(tag => tag.Attr("min", 0));
Editors.ModifyForAttribute<GreaterThanZeroAttribute>(tag => tag.Attr("min", 1));
}
}
public class MultiListBuilder : ElementBuilder
{
protected override bool matches(AccessorDef def)
{
bool matches = false;
def.Accessor.ForAttribute<ListValueAttribute>(att =>
{
Debug.WriteLine("Looking at prop: " + def.Accessor.Name);
var list = ListValueRegistry.ListFor(att.GetListName(def.ModelType));
matches = list.IsTopLevel();
});
return matches;
}
public override HtmlTag Build(ElementRequest request)
{
return new MultiListEditorTag(request);
}
}
public class MultiListEditorTag : HtmlTag
{
public MultiListEditorTag(ElementRequest request)
: base("a")
{
AddClass("list-selector");
AddClasses("fg-button", "fg-button-icon-right", "ui-widget", "ui-state-default", "ui-corner-all");
var text = request.ValueIsEmpty()
? CoreMessageKeys.SELECT_PREFIX + " " + request.Header()
: request.LocalizedListValue();
var display = Add("span").Text(text);
// TODO -- Chad, the ui-icon class pulls in a display:block that causes a line break
Add("span").AddClasses("ui-icon", "ui-icon-triangle-1-s");
var hidden = Child<HiddenTag>().AddClass("list-value").Attr("name", request.ElementId).Value(request.StringValue());
request.Accessor.ForAttribute<RequiredAttribute>(att =>
{
hidden.AddClass("required");
});
request.ForListName(listName =>
{
MetaData("listName", listName);
hidden.Id("valueOf" + listName);
hidden.MetaData("listName", listName);
display.Id("displayOf" + listName);
var list = ListValueRegistry.ListFor(listName);
// hide the leaf values before the initial selection
if (list.Parent != null)
{
hidden.MetaData("parentList", list.Parent.ListName);
Style("display", "none");
}
else
{
hidden.AddClass("top-level-list");
}
if (list.IsTopLevel())
{
hidden.Next = new MultiListMenuTag(list);
}
});
}
}
public class MultiListMenuTag : HtmlTag
{
public MultiListMenuTag(ValueList list)
: base("div")
{
Style("display", "none");
Id("menu-data-" + list.ListName);
var listTag = new ValueListTag(list.ListName, list.GetAllActive());
Child(listTag);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment