-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Look for presets in base theme if current preset specified in current…
… theme (#389) * Look for presets in base theme if current preset specified in current theme * Fixed bug with additional settings in current theme if preset specified * Add SettingJsonOverlayMerger * Fix typo * Fix naming * Added tests * Fixed tests & bug in theme * Fixed naming * Fixed backward compatibility * Refactoring * Added additional test for case when current is object Co-authored-by: Eugeny Tatarincev <[email protected]>
- Loading branch information
1 parent
5dd323f
commit 814ebb9
Showing
3 changed files
with
406 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json.Linq; | ||
using VirtoCommerce.Storefront.Model.Common.Exceptions; | ||
|
||
namespace VirtoCommerce.LiquidThemeEngine | ||
{ | ||
public static class SettingsManager | ||
{ | ||
public class Settings | ||
{ | ||
public Preset CurrentPreset { get; set; } | ||
|
||
public IList<Preset> Presets { get; set; } = new List<Preset>(); | ||
} | ||
|
||
public class Preset | ||
{ | ||
public string Name { get; set; } | ||
|
||
public JObject Json { get; set; } | ||
} | ||
|
||
public static JObject Merge(JObject baseJson, JObject currentJson) | ||
{ | ||
if (baseJson == null) | ||
{ | ||
throw new ArgumentNullException(nameof(baseJson)); | ||
} | ||
if (currentJson == null) | ||
{ | ||
throw new ArgumentNullException(nameof(currentJson)); | ||
} | ||
|
||
var baseSettings = ReadSettings(baseJson); | ||
var currentSettings = ReadSettings(currentJson); | ||
//Change the current preset for base doc according to head preset value if it specified | ||
if (!string.IsNullOrEmpty(currentSettings.CurrentPreset.Name)) | ||
{ | ||
baseSettings.CurrentPreset = baseSettings.Presets.FirstOrDefault(x => x.Name == currentSettings.CurrentPreset.Name); | ||
} | ||
var result = baseSettings.CurrentPreset?.Json ?? new JObject(); | ||
result.Merge(currentSettings.CurrentPreset.Json, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Merge }); | ||
return result; | ||
} | ||
|
||
public static Settings ReadSettings(JObject json) | ||
{ | ||
var result = new Settings | ||
{ | ||
CurrentPreset = new Preset | ||
{ | ||
Json = json | ||
} | ||
}; | ||
|
||
if (json.GetValue("presets") is JObject presetsJson) | ||
{ | ||
var allPresetsJsonProperties = presetsJson.Children().Cast<JProperty>().ToList(); | ||
foreach (var presetJsonProperty in allPresetsJsonProperties) | ||
{ | ||
var preset = new Preset | ||
{ | ||
Name = presetJsonProperty.Name, | ||
Json = presetJsonProperty.Value as JObject | ||
}; | ||
result.Presets.Add(preset); | ||
} | ||
} | ||
|
||
var currentPresetJsonToken = json.GetValue("current"); | ||
if (currentPresetJsonToken is JValue currentPresetJsonValue) | ||
{ | ||
var presetName = currentPresetJsonValue.Value.ToString(); | ||
var currentPresetJson = result.Presets.FirstOrDefault(x => x.Name == presetName)?.Json; | ||
if (currentPresetJson == null && result.Presets.Any()) | ||
{ | ||
throw new StorefrontException($"Setting preset with name '{presetName}' not found"); | ||
} | ||
result.CurrentPreset.Name = presetName; | ||
result.CurrentPreset.Json = currentPresetJson ?? json; | ||
} | ||
if (currentPresetJsonToken is JObject) | ||
{ | ||
result.CurrentPreset.Json = currentPresetJsonToken as JObject; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
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.