Skip to content

Commit

Permalink
Added tag helpers, cleaned up manage form, added ability to add games
Browse files Browse the repository at this point in the history
  • Loading branch information
pathartl committed Dec 30, 2018
1 parent ef1667e commit 4f41c76
Show file tree
Hide file tree
Showing 34 changed files with 1,750 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
@@ -0,0 +1,3 @@
{
"directory": "BleemSync/wwwroot/lib"
}
16 changes: 16 additions & 0 deletions BleemSync.UI/BleemSync.UI.csproj
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>BleemSync.UI</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Folder Include="TagHelpers\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1" />
</ItemGroup>

</Project>
160 changes: 160 additions & 0 deletions BleemSync.UI/TagHelpers/Button.cs
@@ -0,0 +1,160 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;

namespace BleemSync.UI
{
[HtmlTargetElement("button")]
public class ButtonHelper : TagHelper
{
private readonly IHtmlGenerator _generator;
private readonly IHttpContextAccessor _contextAccessor;
private readonly HttpRequest _request;
private IDictionary<string, string> _routeValues;
private IUrlHelperFactory _urlHelperFactory;
private IUrlHelper _urlHelper;

[HtmlAttributeName("asp-for")]
public ModelExpression For { get; set; }

[HtmlAttributeName("asp-action")]
public string Action { get; set; }

[HtmlAttributeName("asp-controller")]
public string Controller { get; set; }

[HtmlAttributeName("asp-all-route-data", DictionaryAttributePrefix = "asp-route-")]
public IDictionary<string, string> RouteValues
{
get
{
if (_routeValues == null)
_routeValues = (IDictionary<string, string>)new Dictionary<string, string>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
return _routeValues;
}
set
{
_routeValues = value;
}
}

[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

public ButtonHelper(IHtmlGenerator generator, IUrlHelperFactory urlHelperFactory)
{
_generator = generator;
_urlHelperFactory = urlHelperFactory;
}

public override void Process(TagHelperContext context, TagHelperOutput output)
{
_urlHelper = _urlHelperFactory.GetUrlHelper(ViewContext);

var isRaised = output.Attributes.SingleOrDefault(a => a.Name == "raised") != null;
var isFlat = output.Attributes.SingleOrDefault(a => a.Name == "flat") != null;
var isOutline = output.Attributes.SingleOrDefault(a => a.Name == "outline") != null;
var isFloating = output.Attributes.SingleOrDefault(a => a.Name == "floating") != null;
var isLg = output.Attributes.SingleOrDefault(a => a.Name == "lg") != null;
var isSm = output.Attributes.SingleOrDefault(a => a.Name == "sm") != null;
var isBlock = output.Attributes.SingleOrDefault(a => a.Name == "block") != null;
// Colors
var isDefault = output.Attributes.SingleOrDefault(a => a.Name == "default") != null;
var isPrimary = output.Attributes.SingleOrDefault(a => a.Name == "primary") != null;
var isSuccess = output.Attributes.SingleOrDefault(a => a.Name == "success") != null;
var isInfo = output.Attributes.SingleOrDefault(a => a.Name == "info") != null;
var isWarning = output.Attributes.SingleOrDefault(a => a.Name == "warning") != null;
var isDanger = output.Attributes.SingleOrDefault(a => a.Name == "danger") != null;
var isLink = output.Attributes.SingleOrDefault(a => a.Name == "link") != null;

var icon = output.Attributes.SingleOrDefault(a => a.Name == "icon");
var text = output.Attributes.SingleOrDefault(a => a.Name == "text");
var action = output.Attributes.SingleOrDefault(a => a.Name == "asp-action");
var controller = output.Attributes.SingleOrDefault(a => a.Name == "asp-controller");

output.Attributes.RemoveAll("raised");
output.Attributes.RemoveAll("flat");
output.Attributes.RemoveAll("outline");
output.Attributes.RemoveAll("floating");
output.Attributes.RemoveAll("lg");
output.Attributes.RemoveAll("sm");
output.Attributes.RemoveAll("block");
output.Attributes.RemoveAll("default");
output.Attributes.RemoveAll("primary");
output.Attributes.RemoveAll("success");
output.Attributes.RemoveAll("info");
output.Attributes.RemoveAll("warning");
output.Attributes.RemoveAll("danger");
output.Attributes.RemoveAll("link");
output.Attributes.RemoveAll("icon");
output.Attributes.RemoveAll("text");

var buttonClass = "btn";

var labelAttributes = new Dictionary<string, object>();

if (isRaised) buttonClass = $"{buttonClass} pmd-btn-raised";
if (isFlat) buttonClass = $"{buttonClass} pmd-btn-flat";
if (isOutline) buttonClass = $"{buttonClass} pmd-btn-outline";
if (isFloating) buttonClass = $"{buttonClass} pmd-btn-fab";
if (isLg) buttonClass = $"{buttonClass} btn-lg";
if (isSm) buttonClass = $"{buttonClass} btn-sm";
if (isBlock) buttonClass = $"{buttonClass} btn-block";
if (isDefault) buttonClass = $"{buttonClass} btn-default";
if (isPrimary) buttonClass = $"{buttonClass} btn-primary";
if (isSuccess) buttonClass = $"{buttonClass} btn-success";
if (isInfo) buttonClass = $"{buttonClass} btn-info";
if (isWarning) buttonClass = $"{buttonClass} btn-warning";
if (isDanger) buttonClass = $"{buttonClass} btn-danger";
if (isLink) buttonClass = $"{buttonClass} btn-link";

if (!isDefault && !isPrimary && !isSuccess && !isInfo && !isWarning && !isDanger && !isLink)
{
buttonClass = $"{buttonClass} btn-primary";
}

var inputPre = "";
var inputPost = "";
var labelClass = "control-label";

if (icon != null)
{
inputPre = $"{inputPre}<div class=\"input-group\"><div class=\"input-group-addon\"><i class=\"material-icons md-dark pmd-sm\">{icon.Value}</i></div>";
inputPost = $"{inputPost}</div>";
labelClass = $"{labelClass} pmd-input-group-label";
}

labelAttributes.Add("class", labelClass);


string buttonOutput = "";

if (Action != null || Controller != null)
{
var url = _urlHelper.Action(Action, Controller, _routeValues);

output.Attributes.SetAttribute("href", url);
output.TagName = "a";
}
else
{

output.Attributes.SetAttribute("type", "submit");
output.TagName = "button";
}

buttonOutput = text.Value.ToString();

output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("class", buttonClass);
output.Content.SetHtmlContent(buttonOutput);
}
}
}
49 changes: 49 additions & 0 deletions BleemSync.UI/TagHelpers/Card.cs
@@ -0,0 +1,49 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BleemSync.UI
{
[HtmlTargetElement("card")]
public class CardTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var classAttr = output.Attributes.SingleOrDefault(a => a.Name == "class");
var titleAttr = output.Attributes.SingleOrDefault(a => a.Name == "title");
var secondaryTitleAttr = output.Attributes.SingleOrDefault(a => a.Name == "secondary-title");
var isEditableTitle = output.Attributes.SingleOrDefault(a => a.Name == "editable-title") != null;

var classString = classAttr != null ? classAttr.Value : "";
var titleString = titleAttr != null ? titleAttr.Value : "";
var secondaryTitleString = secondaryTitleAttr != null ? secondaryTitleAttr.Value : "";

output.Attributes.RemoveAll("class");
output.Attributes.RemoveAll("title");
output.Attributes.RemoveAll("secondary-title");
output.Attributes.RemoveAll("editable-title");

output.TagName = "div";
output.Attributes.SetAttribute("class", $"{classString} pmd-card pmd-card-default pmd-z-depth");

var pre = "<div class=\"pmd-card-body\">";
var post = "</div>";

if (titleString.ToString() != "")
{
secondaryTitleString = secondaryTitleAttr != null ? $"<span class=\"pmd-card-subtitle-text\">{secondaryTitleString}</span>" : "";
pre = $"<div class=\"pmd-card-title\"><h2 class=\"pmd-card-title-text\">{titleString}</h2>{secondaryTitleString}</div>{pre}";
}

if (isEditableTitle)
{

}

output.PreContent.SetHtmlContent(pre);
output.PostContent.SetHtmlContent(post);
}
}
}
94 changes: 94 additions & 0 deletions BleemSync.UI/TagHelpers/Checkbox.cs
@@ -0,0 +1,94 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System.IO;

namespace BleemSync.UI
{
[HtmlTargetElement("checkbox", TagStructure = TagStructure.WithoutEndTag)]
public class CheckBoxHelper : TagHelper
{
private readonly IHtmlGenerator _generator;

[HtmlAttributeName("asp-for")]
public ModelExpression For { get; set; }

[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

public CheckBoxHelper(IHtmlGenerator generator)
{
_generator = generator;
}

public override void Process(TagHelperContext context, TagHelperOutput output)
{
var text = output.Attributes.SingleOrDefault(a => a.Name == "text");
var isDisabled = output.Attributes.SingleOrDefault(a => a.Name == "disabled") != null;
var isReadOnly = output.Attributes.SingleOrDefault(a => a.Name == "readonly") != null;
var isRequired = output.Attributes.SingleOrDefault(a => a.Name == "required") != null;

output.Attributes.RemoveAll("disabled");
output.Attributes.RemoveAll("readonly");
output.Attributes.RemoveAll("required");

var labelAttributes = new Dictionary<string, object>
{
{ "class", "pmd-checkbox checkbox-pmd-ripple-effect" }
};

var checkboxAttributes = new Dictionary<string, object>
{
{ "type", "checkbox" }
};

if (isDisabled) checkboxAttributes.Add("disabled", "disabled");
if (isReadOnly) checkboxAttributes.Add("readonly", "readonly");
if (isRequired) checkboxAttributes.Add("required", "required");

var inputPre = "";
var inputPost = "";

var value = For.ModelExplorer.Model == null ? false : (bool)For.ModelExplorer.Model;

var labelText = text == null ? For.Metadata.DisplayName : text.Value.ToString();

var input = _generator.GenerateCheckBox(ViewContext, For.ModelExplorer, For.Name, value, checkboxAttributes);
var label = _generator.GenerateLabel(ViewContext, For.ModelExplorer, For.Name, "", labelAttributes);

// Strip end tag from label
var labelStart = "";
using (var writer = new StringWriter())
{
label.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
labelStart = writer.ToString();
labelStart = labelStart.Replace("</label>", "");
}

inputPre = $"{labelStart}{inputPre}";
inputPost = $"{inputPost}<span class=\"pmd-checkbox-label\">&nbsp;</span><span class=\"pmd-checkbox\">{labelText}</span></label>";

output.TagName = "div";
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("class", "checkbox pmd-default-theme");

string textboxOutput;

using (var writer = new StringWriter())
{
writer.Write(inputPre);
input.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
writer.Write(inputPost);
textboxOutput = writer.ToString();
}

output.Content.SetHtmlContent(textboxOutput);
}
}
}

0 comments on commit 4f41c76

Please sign in to comment.