From 3e95b0468f821e0aebc60c14fd17149bb4237507 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 27 Aug 2013 13:18:47 +1000 Subject: [PATCH] Checkbox list property editor is now working. --- src/Umbraco.Core/Models/PropertyExtensions.cs | 4 +- .../DropDownPropertyPreValueEditorTests.cs | 8 ++-- .../checkboxlist/checkboxlist.controller.js | 33 +++++++++++++++ .../checkboxlist/checkboxlist.html | 14 +++++++ .../dropdown/dropdown.controller.js | 9 ++++- .../CheckBoxListPropertyEditor.cs | 40 +++++++++++++++++++ .../DropDownMultiplePropertyEditor.cs | 2 +- .../DropDownMultipleWithKeysPropertyEditor.cs | 4 +- .../PropertyEditors/DropDownPropertyEditor.cs | 2 +- .../DropDownWithKeysPropertyEditor.cs | 2 +- ...ueEditor.cs => PublishValueValueEditor.cs} | 10 ++--- ...cs => PublishValuesMultipleValueEditor.cs} | 12 ++++-- ...ueEditor.cs => ValueListPreValueEditor.cs} | 14 +++++-- src/Umbraco.Web/Umbraco.Web.csproj | 7 ++-- 14 files changed, 132 insertions(+), 29 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/src/views/propertyeditors/checkboxlist/checkboxlist.controller.js create mode 100644 src/Umbraco.Web.UI.Client/src/views/propertyeditors/checkboxlist/checkboxlist.html create mode 100644 src/Umbraco.Web/PropertyEditors/CheckBoxListPropertyEditor.cs rename src/Umbraco.Web/PropertyEditors/{DropDownValueEditor.cs => PublishValueValueEditor.cs} (79%) rename src/Umbraco.Web/PropertyEditors/{DropDownMultipleValueEditor.cs => PublishValuesMultipleValueEditor.cs} (83%) rename src/Umbraco.Web/PropertyEditors/{DropDownPreValueEditor.cs => ValueListPreValueEditor.cs} (89%) diff --git a/src/Umbraco.Core/Models/PropertyExtensions.cs b/src/Umbraco.Core/Models/PropertyExtensions.cs index 8edf32bb41..3aa62ae00f 100644 --- a/src/Umbraco.Core/Models/PropertyExtensions.cs +++ b/src/Umbraco.Core/Models/PropertyExtensions.cs @@ -47,12 +47,12 @@ namespace Umbraco.Core.Models var cacheValue = propertyEditor.ValueEditor.FormatValueForCache(property); switch (property.PropertyType.DataTypeDatabaseType) - { - case DataTypeDatabaseType.Nvarchar: + { case DataTypeDatabaseType.Date: case DataTypeDatabaseType.Integer: xmlNode.AppendChild(xd.CreateTextNode(cacheValue.ToString())); break; + case DataTypeDatabaseType.Nvarchar: case DataTypeDatabaseType.Ntext: //put text in cdata xmlNode.AppendChild(xd.CreateCDataSection(cacheValue.ToString())); diff --git a/src/Umbraco.Tests/PropertyEditors/DropDownPropertyPreValueEditorTests.cs b/src/Umbraco.Tests/PropertyEditors/DropDownPropertyPreValueEditorTests.cs index e011254ed3..0202d2a6e3 100644 --- a/src/Umbraco.Tests/PropertyEditors/DropDownPropertyPreValueEditorTests.cs +++ b/src/Umbraco.Tests/PropertyEditors/DropDownPropertyPreValueEditorTests.cs @@ -18,7 +18,7 @@ namespace Umbraco.Tests.PropertyEditors public void DropDownMultipleValueEditor_With_Keys_Format_Data_For_Cache() { var dataTypeService = MockRepository.GenerateStub(); - var editor = new DropDownMultipleValueEditor(true, dataTypeService, new ValueEditor()); + var editor = new PublishValuesMultipleValueEditor(true, dataTypeService, new ValueEditor()); var result = editor.FormatValueForCache( new Property(1, Guid.NewGuid(), @@ -40,7 +40,7 @@ namespace Umbraco.Tests.PropertyEditors {"key1", new PreValue(1234, "Value 2")}, {"key2", new PreValue(8910, "Value 3")} })); - var editor = new DropDownMultipleValueEditor(false, dataTypeService, new ValueEditor()); + var editor = new PublishValuesMultipleValueEditor(false, dataTypeService, new ValueEditor()); var result = editor.FormatValueForCache( new Property(1, Guid.NewGuid(), @@ -62,7 +62,7 @@ namespace Umbraco.Tests.PropertyEditors {"key1", new PreValue(1234, "Value 2")}, {"key2", new PreValue(11, "Value 3")} })); - var editor = new DropDownValueEditor(dataTypeService, new ValueEditor()); + var editor = new PublishValueValueEditor(dataTypeService, new ValueEditor()); var result = editor.FormatValueForCache( new Property(1, Guid.NewGuid(), @@ -84,7 +84,7 @@ namespace Umbraco.Tests.PropertyEditors {"item3", new PreValue(3, "Item 3")} }); - var editor = new DropDownPreValueEditor(); + var editor = new ValueListPreValueEditor(); var result = editor.FormatDataForEditor(defaultVals, persisted); diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/checkboxlist/checkboxlist.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/checkboxlist/checkboxlist.controller.js new file mode 100644 index 0000000000..071be3469e --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/checkboxlist/checkboxlist.controller.js @@ -0,0 +1,33 @@ +angular.module("umbraco").controller("Umbraco.Editors.CheckboxListController", + function($scope) { + + $scope.selectedItems = []; + + if (!angular.isObject($scope.model.config.items)) { + throw "The model.config.items property must be either a dictionary"; + } + + //now we need to check if the value is null/undefined, if it is we need to set it to "" so that any value that is set + // to "" gets selected by default + if ($scope.model.value === null || $scope.model.value === undefined) { + $scope.model.value = []; + } + + for (var i in $scope.model.config.items) { + var isChecked = _.contains($scope.model.value, i); + $scope.selectedItems.push({ checked: isChecked, key: i, val: $scope.model.config.items[i] }); + } + + //update the model when the items checked changes + $scope.$watch("selectedItems", function(newVal, oldVal) { + + $scope.model.value = []; + for (var x = 0; x < $scope.selectedItems.length; x++) { + if ($scope.selectedItems[x].checked) { + $scope.model.value.push($scope.selectedItems[x].key); + } + } + + }, true); + + }); diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/checkboxlist/checkboxlist.html b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/checkboxlist/checkboxlist.html new file mode 100644 index 0000000000..a51e07f106 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/checkboxlist/checkboxlist.html @@ -0,0 +1,14 @@ +
+ +
    +
  • + +
  • +
+ +
diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/dropdown/dropdown.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/dropdown/dropdown.controller.js index 483f1ef5cd..4a51b6774e 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/dropdown/dropdown.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/dropdown/dropdown.controller.js @@ -1,5 +1,5 @@ angular.module("umbraco").controller("Umbraco.Editors.DropdownController", - function($scope, notificationsService) { + function($scope) { //setup the default config var config = { @@ -27,7 +27,12 @@ angular.module("umbraco").controller("Umbraco.Editors.DropdownController", //now we need to check if the value is null/undefined, if it is we need to set it to "" so that any value that is set // to "" gets selected by default if ($scope.model.value === null || $scope.model.value === undefined) { - $scope.model.value = ""; + if ($scope.model.config.multiple) { + $scope.model.value = []; + } + else { + $scope.model.value = ""; + } } }); diff --git a/src/Umbraco.Web/PropertyEditors/CheckBoxListPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/CheckBoxListPropertyEditor.cs new file mode 100644 index 0000000000..48c9000535 --- /dev/null +++ b/src/Umbraco.Web/PropertyEditors/CheckBoxListPropertyEditor.cs @@ -0,0 +1,40 @@ +using Umbraco.Core; +using Umbraco.Core.PropertyEditors; + +namespace Umbraco.Web.PropertyEditors +{ + /// + /// A property editor to allow multiple checkbox selection of pre-defined items. + /// + /// + /// Due to remaining backwards compatible, this stores the id of the checkbox items in the database + /// as INT and we have logic in here to ensure it is formatted correctly including ensuring that the string value is published + /// in cache and not the int ID. + /// + [PropertyEditor(Constants.PropertyEditors.CheckBoxList, "Checkbox list", "checkboxlist")] + public class CheckBoxListPropertyEditor : PropertyEditor + { + + /// + /// Return a custom pre-value editor + /// + /// + /// + /// We are just going to re-use the ValueListPreValueEditor + /// + protected override PreValueEditor CreatePreValueEditor() + { + return new ValueListPreValueEditor(); + } + + /// + /// We need to override the value editor so that we can ensure the string value is published in cache and not the integer ID value. + /// + /// + protected override ValueEditor CreateValueEditor() + { + return new PublishValuesMultipleValueEditor(false, base.CreateValueEditor()); + } + + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/PropertyEditors/DropDownMultiplePropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/DropDownMultiplePropertyEditor.cs index 80c0e56a86..94be24168c 100644 --- a/src/Umbraco.Web/PropertyEditors/DropDownMultiplePropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/DropDownMultiplePropertyEditor.cs @@ -17,7 +17,7 @@ namespace Umbraco.Web.PropertyEditors { protected override ValueEditor CreateValueEditor() { - return new DropDownMultipleValueEditor(false, base.CreateValueEditor()); + return new PublishValuesMultipleValueEditor(false, base.CreateValueEditor()); } } diff --git a/src/Umbraco.Web/PropertyEditors/DropDownMultipleWithKeysPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/DropDownMultipleWithKeysPropertyEditor.cs index bdc2f516d4..e4af43f623 100644 --- a/src/Umbraco.Web/PropertyEditors/DropDownMultipleWithKeysPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/DropDownMultipleWithKeysPropertyEditor.cs @@ -17,7 +17,7 @@ namespace Umbraco.Web.PropertyEditors { protected override ValueEditor CreateValueEditor() { - return new DropDownMultipleValueEditor(true, base.CreateValueEditor()); + return new PublishValuesMultipleValueEditor(true, base.CreateValueEditor()); } protected override PreValueEditor CreatePreValueEditor() @@ -32,7 +32,7 @@ namespace Umbraco.Web.PropertyEditors /// /// This is mostly to maintain backwards compatibility with old property editors. Devs can now simply use the Drop down property editor and check the multiple pre-value checkbox /// - internal class DropDownMultiplePreValueEditor : DropDownPreValueEditor + internal class DropDownMultiplePreValueEditor : ValueListPreValueEditor { public DropDownMultiplePreValueEditor() { diff --git a/src/Umbraco.Web/PropertyEditors/DropDownPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/DropDownPropertyEditor.cs index 173ebd4d37..fe357cebf4 100644 --- a/src/Umbraco.Web/PropertyEditors/DropDownPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/DropDownPropertyEditor.cs @@ -25,7 +25,7 @@ namespace Umbraco.Web.PropertyEditors /// protected override ValueEditor CreateValueEditor() { - return new DropDownValueEditor(base.CreateValueEditor()); + return new PublishValueValueEditor(base.CreateValueEditor()); } } diff --git a/src/Umbraco.Web/PropertyEditors/DropDownWithKeysPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/DropDownWithKeysPropertyEditor.cs index d320190b67..4ff3198348 100644 --- a/src/Umbraco.Web/PropertyEditors/DropDownWithKeysPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/DropDownWithKeysPropertyEditor.cs @@ -21,7 +21,7 @@ namespace Umbraco.Web.PropertyEditors /// protected override PreValueEditor CreatePreValueEditor() { - return new DropDownPreValueEditor(); + return new ValueListPreValueEditor(); } } } \ No newline at end of file diff --git a/src/Umbraco.Web/PropertyEditors/DropDownValueEditor.cs b/src/Umbraco.Web/PropertyEditors/PublishValueValueEditor.cs similarity index 79% rename from src/Umbraco.Web/PropertyEditors/DropDownValueEditor.cs rename to src/Umbraco.Web/PropertyEditors/PublishValueValueEditor.cs index aebe4f9bb7..0a9089ce44 100644 --- a/src/Umbraco.Web/PropertyEditors/DropDownValueEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/PublishValueValueEditor.cs @@ -9,23 +9,23 @@ using Umbraco.Core.Services; namespace Umbraco.Web.PropertyEditors { /// - /// A custom value editor for the drop down so that we can ensure that the 'value' not the ID get's put into cache + /// A custom value editor for any property editor that stores a pre-value int id so that we can ensure that the 'value' not the ID get's put into cache /// /// /// This is required for legacy/backwards compatibility, otherwise we'd just store the string version and cache the string version without /// needing additional lookups. /// - internal class DropDownValueEditor : ValueEditorWrapper + internal class PublishValueValueEditor : ValueEditorWrapper { private readonly IDataTypeService _dataTypeService; - internal DropDownValueEditor(IDataTypeService dataTypeService, ValueEditor wrapped) + internal PublishValueValueEditor(IDataTypeService dataTypeService, ValueEditor wrapped) : base(wrapped) { _dataTypeService = dataTypeService; } - public DropDownValueEditor(ValueEditor wrapped) + public PublishValueValueEditor(ValueEditor wrapped) : this(ApplicationContext.Current.Services.DataTypeService, wrapped) { } @@ -49,7 +49,7 @@ namespace Umbraco.Web.PropertyEditors return preVals.Single(x => x.Value.Id == preValId).Value.Value; } - LogHelper.Warn("Could not find a pre value with ID " + preValId + " for property alias " + property.Alias); + LogHelper.Warn("Could not find a pre value with ID " + preValId + " for property alias " + property.Alias); } } diff --git a/src/Umbraco.Web/PropertyEditors/DropDownMultipleValueEditor.cs b/src/Umbraco.Web/PropertyEditors/PublishValuesMultipleValueEditor.cs similarity index 83% rename from src/Umbraco.Web/PropertyEditors/DropDownMultipleValueEditor.cs rename to src/Umbraco.Web/PropertyEditors/PublishValuesMultipleValueEditor.cs index 696b4b1035..3118527117 100644 --- a/src/Umbraco.Web/PropertyEditors/DropDownMultipleValueEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/PublishValuesMultipleValueEditor.cs @@ -9,19 +9,23 @@ using Umbraco.Core.Services; namespace Umbraco.Web.PropertyEditors { /// - /// Custom value editor to handle posted json data and to return json data for the multiple selected items. + /// Custom value editor to handle posted json data and to return json data for the multiple selected items as well as ensuring + /// that the multiple selected int IDs are published to cache as a delimited string (values) /// - internal class DropDownMultipleValueEditor : DropDownValueEditor + /// + /// This is re-used by editors such as the multiple drop down list or check box list + /// + internal class PublishValuesMultipleValueEditor : PublishValueValueEditor { private readonly bool _publishIds; - internal DropDownMultipleValueEditor(bool publishIds, IDataTypeService dataTypeService, ValueEditor wrapped) + internal PublishValuesMultipleValueEditor(bool publishIds, IDataTypeService dataTypeService, ValueEditor wrapped) : base(dataTypeService, wrapped) { _publishIds = publishIds; } - public DropDownMultipleValueEditor(bool publishIds, ValueEditor wrapped) + public PublishValuesMultipleValueEditor(bool publishIds, ValueEditor wrapped) : this(publishIds, ApplicationContext.Current.Services.DataTypeService, wrapped) { } diff --git a/src/Umbraco.Web/PropertyEditors/DropDownPreValueEditor.cs b/src/Umbraco.Web/PropertyEditors/ValueListPreValueEditor.cs similarity index 89% rename from src/Umbraco.Web/PropertyEditors/DropDownPreValueEditor.cs rename to src/Umbraco.Web/PropertyEditors/ValueListPreValueEditor.cs index b9d6f3fd67..f815338066 100644 --- a/src/Umbraco.Web/PropertyEditors/DropDownPreValueEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ValueListPreValueEditor.cs @@ -11,10 +11,16 @@ using umbraco; namespace Umbraco.Web.PropertyEditors { - internal class DropDownPreValueEditor : PreValueEditor + /// + /// Pre-value editor used to create a list of items + /// + /// + /// This pre-value editor is shared with editors like drop down, checkbox list, etc.... + /// + internal class ValueListPreValueEditor : PreValueEditor { - public DropDownPreValueEditor() + public ValueListPreValueEditor() { Fields = CreatePreValueFields(); } @@ -29,7 +35,7 @@ namespace Umbraco.Web.PropertyEditors { new PreValueField { - Description = "Add and remove values for the drop down list", + Description = "Add and remove values for the list", //we're going to call this 'items' because we are going to override the //serialization of the pre-values to ensure that each one gets saved with it's own key //(new db row per pre-value, thus to maintain backwards compatibility) @@ -91,7 +97,7 @@ namespace Umbraco.Web.PropertyEditors } catch (Exception ex) { - LogHelper.Error("Could not deserialize the posted value: " + val, ex); + LogHelper.Error("Could not deserialize the posted value: " + val, ex); } return result; diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 9607c8add1..538ca92263 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -315,15 +315,16 @@ + - + - + - +