-
Notifications
You must be signed in to change notification settings - Fork 28
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
Performance enhancements #81
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace RJP.MultiUrlPicker.Models | ||
namespace RJP.MultiUrlPicker.Models | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
@@ -8,6 +8,7 @@ | |
using Newtonsoft.Json.Linq; | ||
|
||
using Umbraco.Core.Logging; | ||
using Umbraco.Web; | ||
|
||
[Obsolete("Use IEnumerable<Link> instead")] | ||
public class MultiUrls : IEnumerable<Link> | ||
|
@@ -39,12 +40,14 @@ public MultiUrls(string propertyData) | |
|
||
private void Initialize(JArray data) | ||
{ | ||
var umbracoHelper = new Lazy<UmbracoHelper>(() => new UmbracoHelper(UmbracoContext.Current)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By using |
||
|
||
foreach (var item in data) | ||
{ | ||
var newLink = new Link(item); | ||
var newLink = new Link(item, umbracoHelper); | ||
if (!newLink.Deleted) | ||
{ | ||
_multiUrls.Add(new Link(item)); | ||
_multiUrls.Add(newLink); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just add the link, no need to create a yet another one 😄 |
||
} | ||
else | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,22 +37,24 @@ public override bool IsConverter(PublishedPropertyType propertyType) | |
|
||
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) | ||
{ | ||
if (string.IsNullOrWhiteSpace(source?.ToString())) | ||
var sourceString = source?.ToString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convert the source to a string once and then use it everywhere, no need to do the same over and over again 😀 |
||
if (string.IsNullOrWhiteSpace(sourceString)) | ||
{ | ||
return null; | ||
} | ||
|
||
if (source.ToString().Trim().StartsWith("[")) | ||
if (sourceString.DetectIsJson()) | ||
{ | ||
try | ||
{ | ||
return JArray.Parse(source.ToString()); | ||
return JArray.Parse(sourceString); | ||
} | ||
catch (Exception ex) | ||
{ | ||
LogHelper.Error<MultiUrlPickerValueConverter>("Error parsing JSON", ex); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
@@ -65,14 +67,16 @@ public override object ConvertSourceToObject(PublishedPropertyType propertyType, | |
} | ||
|
||
var urls = new MultiUrls((JArray)source); | ||
if(isMultiple) | ||
if (isMultiple) | ||
{ | ||
if(maxNumberOfItems > 0) | ||
if (maxNumberOfItems > 0) | ||
{ | ||
return urls.Take(maxNumberOfItems); | ||
} | ||
|
||
return urls; | ||
} | ||
|
||
return urls.FirstOrDefault(); | ||
} | ||
|
||
|
@@ -82,6 +86,7 @@ public Type GetPropertyValueType(PublishedPropertyType propertyType) | |
{ | ||
return typeof(IEnumerable<Link>); | ||
} | ||
|
||
return typeof(Link); | ||
} | ||
|
||
|
@@ -108,12 +113,9 @@ private bool IsMultipleDataType(int dataTypeId, out int maxNumberOfItems) | |
if (preValues.TryGetValue("maxNumberOfItems", out PreValue maxNumberOfItemsPreValue) && | ||
int.TryParse(maxNumberOfItemsPreValue.Value, out maxNumberOfItems)) | ||
{ | ||
PreValue versionPreValue; | ||
Version version; | ||
// for backwards compatibility, always return true if version | ||
// is less than 2.0.0 | ||
if (preValues.TryGetValue("version", out versionPreValue) && | ||
Version.TryParse(versionPreValue.Value, out version) | ||
// For backwards compatibility, always return true if version is less than 2.0.0 | ||
if (preValues.TryGetValue("version", out PreValue versionPreValue) && | ||
Version.TryParse(versionPreValue.Value, out Version version) | ||
&& version >= new Version(2, 0, 0)) | ||
{ | ||
return maxNumberOfItems != 1; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this code is copied from the
ToPublishedContent()
method, but now uses the sameUmbracoHelper
.