Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AlternateTextField, ImageUrlField and NavigateUrlField AdRotator properties #213

Merged
merged 3 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/BlazorWebFormsComponents.Test/AdRotator/Rotate.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
</ComponentUnderTest>
</Fixture>

<Fixture Test="RotateTestWithCustomFieldsAttributes">
<ComponentUnderTest>
<AdRotator AdvertisementFile="Ads4.xml" AlternateTextField="Alt" ImageUrlField="Image" NavigateUrlField="Url" Target="_blank"></AdRotator>
</ComponentUnderTest>
</Fixture>

@code {
void RotateTest(Fixture fixture)
{
Expand All @@ -15,4 +21,15 @@

Assert.Contains(alternatText, new[] { "CSharp", "Visual Basic" });
}

void RotateTestWithCustomFieldsAttributes(Fixture fixture)
{
var cut = fixture.GetComponentUnderTest();
var anchor = cut.Find("a");
var img = cut.Find("img");
var alternatText = img.Attributes["alt"].Value;

Assert.True(anchor.HasAttribute("href"));
Assert.Contains(alternatText, new[] { "CSharp", "Visual Basic" });
}
}
21 changes: 21 additions & 0 deletions src/BlazorWebFormsComponents.Test/Ads4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Advertisements>
<Ad>
<Image>/img/C#.png</Image>
<Height>343</Height>
<Width>397</Width>
<Url>http://www.microsoft.com</Url>
<Alt>CSharp</Alt>
<Impressions>90</Impressions>
<Keyword>c#</Keyword>

</Ad>
<Ad>
<Image>/img/VB.png</Image>
<Height>343</Height>
<Width>397</Width>
<Url>http://www.microsoft.com</Url>
<Alt>Visual Basic</Alt>
<Impressions>80</Impressions>
<Keyword>vb</Keyword>
</Ad>
</Advertisements>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<Configurations>Debug;Release;WebForms</Configurations>
</PropertyGroup>

<ItemGroup>
<None Remove="Ads4.xml" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="bunit" Version="1.0.0-beta-7" />
Expand All @@ -28,6 +32,9 @@
</ItemGroup>

<ItemGroup>
<Content Include="Ads4.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Ads3.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
92 changes: 60 additions & 32 deletions src/BlazorWebFormsComponents/AdRotator.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,71 @@

namespace BlazorWebFormsComponents
{
public partial class AdRotator : BaseWebFormsComponent
{
[Parameter]
public string AdvertisementFile { get; set; }
public partial class AdRotator : BaseWebFormsComponent
{
private static readonly string DefaultAlternateTextField = "AlternateText";
private static readonly string DefaultImageUrlField = "ImageUrl";
private static readonly string DefaultNavigateUrlField = "NavigateUrl";

[Parameter]
public string Target { get; set; }
[Parameter]
public string AlternateTextField { get; set; } = DefaultAlternateTextField;

internal Advertisment GetActiveAdvertisment()
{
var advertisments = GetAdvertismentsFileContent(AdvertisementFile);
[Parameter]
public string AdvertisementFile { get; set; }

if (advertisments == null || advertisments.Count() == 0)
{
return null;
}
[Parameter]
public string ImageUrlField { get; set; } = DefaultImageUrlField;

var rnd = new Random().Next(advertisments.Count());
[Parameter]
public string NavigateUrlField { get; set; } = DefaultNavigateUrlField;

return advertisments.ElementAt(rnd);
}
[Parameter]
public string Target { get; set; }

private static IEnumerable<Advertisment> GetAdvertismentsFileContent(string fileName)
{
var xmlDocument = XDocument.Load(new StreamReader(fileName));

return xmlDocument.Descendants("Ad")
.Select(a => new Advertisment
{
ImageUrl = a.Descendants("ImageUrl").FirstOrDefault()?.Value,
Height = a.Descendants("Height").FirstOrDefault()?.Value,
Width = a.Descendants("Width").FirstOrDefault()?.Value,
NavigateUrl = a.Descendants("NavigateUrl").FirstOrDefault()?.Value,
AlternateText = a.Descendants("AlternateText").FirstOrDefault()?.Value,
Impressions = a.Descendants("Impressions").FirstOrDefault()?.Value,
Keyword = a.Descendants("Keyword").FirstOrDefault()?.Value
});
internal Advertisment GetActiveAdvertisment()
{
if (string.IsNullOrEmpty(AlternateTextField))
{
throw new ArgumentException("AlternateTextField can't be null or empty.", nameof(AlternateTextField));
}

if (string.IsNullOrEmpty(ImageUrlField))
{
throw new ArgumentException("AlternateTextField can't be null or empty.", nameof(ImageUrlField));
}

if (string.IsNullOrEmpty(NavigateUrlField))
{
throw new ArgumentException("AlternateTextField can't be null or empty.", nameof(NavigateUrlField));
}

var advertisments = GetAdvertismentsFileContent(AdvertisementFile);

if (advertisments == null || advertisments.Count() == 0)
{
return null;
}

var rnd = new Random().Next(advertisments.Count());

return advertisments.ElementAt(rnd);
}

private IEnumerable<Advertisment> GetAdvertismentsFileContent(string fileName)
{
var xmlDocument = XDocument.Load(new StreamReader(fileName));

return xmlDocument.Descendants("Ad")
.Select(a => new Advertisment
{
ImageUrl = a.Descendants(ImageUrlField).FirstOrDefault()?.Value,
Height = a.Descendants("Height").FirstOrDefault()?.Value,
Width = a.Descendants("Width").FirstOrDefault()?.Value,
NavigateUrl = a.Descendants(NavigateUrlField).FirstOrDefault()?.Value,
AlternateText = a.Descendants(AlternateTextField).FirstOrDefault()?.Value,
Impressions = a.Descendants("Impressions").FirstOrDefault()?.Value,
Keyword = a.Descendants("Keyword").FirstOrDefault()?.Value
});
}
}
}
}