-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
14 changed files
with
378 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/Libraries/Nop.Core/ComponentModel/GenericDictionaryTypeConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
||
namespace Nop.Core.ComponentModel | ||
{ | ||
/// <summary> | ||
/// Generic Dictionary type converted | ||
/// </summary> | ||
/// <typeparam name="K">Key type (simple)</typeparam> | ||
/// <typeparam name="V">Value type (simple)</typeparam> | ||
public class GenericDictionaryTypeConverter<K, V> : TypeConverter | ||
{ | ||
protected readonly TypeConverter typeConverterKey; | ||
protected readonly TypeConverter typeConverterValue; | ||
|
||
/// <summary> | ||
/// Ctor | ||
/// </summary> | ||
public GenericDictionaryTypeConverter() | ||
{ | ||
typeConverterKey = TypeDescriptor.GetConverter(typeof(K)); | ||
if (typeConverterKey == null) | ||
throw new InvalidOperationException("No type converter exists for type " + typeof(K).FullName); | ||
typeConverterValue = TypeDescriptor.GetConverter(typeof(V)); | ||
if (typeConverterValue == null) | ||
throw new InvalidOperationException("No type converter exists for type " + typeof(V).FullName); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this converter can | ||
/// convert an object in the given source type to the native type of the converter | ||
/// using the context. | ||
/// </summary> | ||
/// <param name="context">Context</param> | ||
/// <param name="sourceType">Source type</param> | ||
/// <returns>Result</returns> | ||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) | ||
{ | ||
if (sourceType == typeof(string)) | ||
return true; | ||
|
||
return base.CanConvertFrom(context, sourceType); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the given object to the converter's native type. | ||
/// </summary> | ||
/// <param name="context">Context</param> | ||
/// <param name="culture">Culture</param> | ||
/// <param name="value">Value</param> | ||
/// <returns>Result</returns> | ||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | ||
{ | ||
if (value is string) | ||
{ | ||
string input = (string)value; | ||
string[] items = string.IsNullOrEmpty(input) ? new string[0] : input.Split(';').Select(x => x.Trim()).ToArray(); | ||
|
||
var result = new Dictionary<K,V>(); | ||
Array.ForEach(items, s => | ||
{ | ||
string[] keyValueStr = string.IsNullOrEmpty(s) ? new string[0] : s.Split(',').Select(x => x.Trim()).ToArray(); | ||
if (keyValueStr.Length == 2) | ||
{ | ||
object dictionaryKey = (K)typeConverterKey.ConvertFromInvariantString(keyValueStr[0]); | ||
object dictionaryValue = typeConverterKey.ConvertFromInvariantString(keyValueStr[1]); | ||
if (dictionaryKey != null && dictionaryValue != null) | ||
{ | ||
if (!result.ContainsKey((K)dictionaryKey)) | ||
result.Add((K) dictionaryKey, (V) dictionaryValue); | ||
} | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
return base.ConvertFrom(context, culture, value); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the given value object to the specified destination type using the specified context and arguments | ||
/// </summary> | ||
/// <param name="context">Context</param> | ||
/// <param name="culture">Culture</param> | ||
/// <param name="value">Value</param> | ||
/// <param name="destinationType">Destination type</param> | ||
/// <returns>Result</returns> | ||
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) | ||
{ | ||
if (destinationType == typeof(string)) | ||
{ | ||
string result = string.Empty; | ||
if (value != null) | ||
{ | ||
//we don't use string.Join() because it doesn't support invariant culture | ||
int counter = 0; | ||
var dictionary = (IDictionary<K, V>)value; | ||
foreach (var keyValue in dictionary) | ||
{ | ||
result += string.Format("{0}, {1}", Convert.ToString(keyValue.Key, CultureInfo.InvariantCulture), Convert.ToString(keyValue.Value, CultureInfo.InvariantCulture)); | ||
//don't add ; after the last element | ||
if (counter != dictionary.Count - 1) | ||
result += ";"; | ||
counter++; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
return base.ConvertTo(context, culture, value, destinationType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Presentation/Nop.Web/Administration/Models/Settings/SortOptionModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Web.Mvc; | ||
using Nop.Web.Framework; | ||
using Nop.Web.Framework.Mvc; | ||
|
||
namespace Nop.Admin.Models.Settings | ||
{ | ||
public partial class SortOptionModel : BaseNopEntityModel | ||
{ | ||
[NopResourceDisplayName("Admin.Configuration.Settings.Catalog.SortOptions.Name")] | ||
[AllowHtml] | ||
public string Name { get; set; } | ||
|
||
[NopResourceDisplayName("Admin.Configuration.Settings.Catalog.SortOptions.IsActive")] | ||
[AllowHtml] | ||
public bool IsActive { get; set; } | ||
|
||
[NopResourceDisplayName("Admin.Configuration.Settings.Catalog.SortOptions.DisplayOrder")] | ||
public int DisplayOrder { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.