Skip to content

Commit

Permalink
Added secondary constructor and static GetFrom methods to the LinkPic…
Browse files Browse the repository at this point in the history
…kerItem class
  • Loading branch information
abjerner committed Sep 12, 2016
1 parent 364483a commit ea3da75
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Skybrud.LinkPicker/LinkPickerItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using AutoMapper.Mappers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Skybrud.LinkPicker.Extensions.Json;
using Skybrud.LinkPicker.Json.Converters;
using umbraco.cms.helpers;
using Umbraco.Core.Models;

namespace Skybrud.LinkPicker {

Expand Down Expand Up @@ -62,6 +66,23 @@ public bool IsValid {

internal LinkPickerItem() { }

/// <summary>
/// Initializes a new link picker item.
/// </summary>
/// <param name="id">The ID of the content or media item.</param>
/// <param name="name">The name (text) of the link.</param>
/// <param name="url">The URL of the link.</param>
/// <param name="target">The target of the link.</param>
/// <param name="mode">The mode of the link - either <see cref="LinkPickerMode.Content"/>,
/// <see cref="LinkPickerMode.Media"/> or <see cref="LinkPickerMode.Url"/>.</param>
public LinkPickerItem(int id, string name, string url, string target, LinkPickerMode mode) {
Id = id;
Name = name;
Url = url;
Target = target;
Mode = mode;
}

#endregion

#region Static methods
Expand Down Expand Up @@ -103,6 +124,35 @@ public static LinkPickerItem Parse(JObject obj) {

}

public static LinkPickerItem GetFromContent(IPublishedContent content) {
if (content == null) throw new ArgumentNullException("content");
return new LinkPickerItem {
Id = content.Id,
Name = content.Name,
Url = content.Url,
Mode = LinkPickerMode.Content
};
}

public static LinkPickerItem GetFromMedia(IPublishedContent media) {
if (media == null) throw new ArgumentNullException("media");
return new LinkPickerItem {
Id = media.Id,
Name = media.Name,
Url = media.Url,
Mode = LinkPickerMode.Media
};
}

public static LinkPickerItem GetFromUrl(string url, string name = null, string target = null) {
if (String.IsNullOrWhiteSpace(url)) throw new ArgumentNullException("url");
return new LinkPickerItem {
Name = name + "",
Url = url,
Mode = LinkPickerMode.Url
};
}

#endregion

}
Expand Down

0 comments on commit ea3da75

Please sign in to comment.