Skip to content

Commit

Permalink
uComponents#149 Raw data storing format is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeyamalashevich committed Sep 30, 2016
1 parent 8cf6e71 commit fbe52da
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
2 changes: 1 addition & 1 deletion source/nuPickers/Picker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public IEnumerable<string> PickedKeys
else
{
this.pickedKeys = this.SavedValue != null ? SaveFormat
.GetSavedKeys(this.SavedValue.ToString())
.GetSavedKeys(this.SavedValue.ToString(), this.GetDataTypePreValue("saveFormat").Value)
.ToArray() : new string[]{};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private void Saved(IService sender, IEnumerable<IContentBase> savedEntities)
else
{
// manually set on picker obj, so it doesn't then attempt to read picked keys from the database
picker.PickedKeys = SaveFormat.GetSavedKeys(picker.SavedValue.ToString()).ToArray();
picker.PickedKeys = SaveFormat.GetSavedKeys(picker.SavedValue.ToString(), picker.GetDataTypePreValue("saveFormat").Value).ToArray();

// delete saved value (setting it to null)
savedEntity.SetValue(propertyType.Alias, null);
Expand Down
16 changes: 10 additions & 6 deletions source/nuPickers/Shared/SaveFormat/SaveFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ internal static class SaveFormat
/// ignore the specified saved format, and try and restore collection directly from the saved value
/// </summary>
/// <param name="savedValue"></param>
/// /// <param name="saveFormat"></param>
/// <returns></returns>
internal static IEnumerable<string> GetSavedKeys(string savedValue)
internal static IEnumerable<string> GetSavedKeys(string savedValue, string saveFormat)
{
if (!string.IsNullOrWhiteSpace(savedValue))
{
switch (savedValue[0])
switch (saveFormat)
{
case '[':
case "json":
// TODO: check json is valid
return JsonConvert.DeserializeObject<JArray>(savedValue).Select(x => x["key"].ToString());

case '<':
case "xml":
// TODO: check xml is valid
return XDocument.Parse(savedValue).Descendants("Picked").Select(x => x.Attribute("Key").Value);

default: // csv
return savedValue.Split(',');
case "csv":
return savedValue.Split(',');

default: // raw
return new[] { savedValue };
}
}

Expand Down
42 changes: 15 additions & 27 deletions source/nuPickers/Shared/SaveFormat/SaveFormatResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,26 @@ angular.module('umbraco.resources')
/// saveValue is expected to be a string or an array of { 'key': '', 'label': '' } objects
getSavedKeys: function (saveValue, saveFormat) {

// json save format
if (saveValue instanceof Array)
{
return saveValue.map(function (option) { return option.key }).join().split(',');
}
switch (saveFormat) {

// parse string for nested json save format (fix to support the Doc Type Grid Editor package)
try {
var jsonSaveValue = JSON.parse(saveValue);
return jsonSaveValue.map(function (option) { return option.key }).join().split(',');
}
catch (error) { } // suppress
case 'json':
return JSON.parse(saveValue).map(function (option) { return option.key }).join().split(',');

// xml save format
try {
var xml = $.parseXML(saveValue);
var keys = new Array();
$(xml).find('Picked').each(function () {
keys.push($(this).attr('Key'));
});
case 'xml':
var xml = $.parseXML(saveValue);
var keys = new Array();
$(xml).find('Picked').each(function () {
keys.push($(this).attr('Key'));
});

return keys;
}
catch (error) { } // suppress
return keys;

// csv save format
if (saveFormat === 'csv') {
return saveValue.split(',');
}
case 'csv':
return saveValue.split(',');

// raw format
return [saveValue];
default: // raw format
return [saveValue];
}
},

/// returns an array of { 'key': '', 'label': '' } objects
Expand Down

0 comments on commit fbe52da

Please sign in to comment.