From d13a5b1b869e6290482ee53e057cd65ee7d5137d Mon Sep 17 00:00:00 2001 From: Kevin Giszewski Date: Tue, 29 Apr 2014 12:31:02 -0400 Subject: [PATCH] Remove RenderModel - Use server side converters for model cleanup --- .../Extensions/ArchetypeHelper.cs | 4 +- .../ArchetypeValueConverter.cs | 1 - .../ArcheTypePropertyEditor.cs | 22 ++- app/controllers/controller.js | 136 +++++------------- app/views/archetype.html | 15 +- 5 files changed, 54 insertions(+), 124 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeHelper.cs b/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeHelper.cs index e6c0607..6517af7 100644 --- a/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeHelper.cs +++ b/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeHelper.cs @@ -22,7 +22,6 @@ namespace Archetype.Umbraco.Extensions _app = ApplicationContext.Current; } - internal Models.Archetype DeserializeJsonToArchetype(string sourceJson, PreValueCollection dataTypePreValues) { try @@ -44,8 +43,7 @@ namespace Archetype.Umbraco.Extensions catch { return new Models.Archetype(); - } - + } } internal Models.Archetype DeserializeJsonToArchetype(string sourceJson, int dataTypeId) diff --git a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs index 58da5eb..2f096d3 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs @@ -44,6 +44,5 @@ namespace Archetype.Umbraco.PropertyConverters return archetype; } - } } \ No newline at end of file diff --git a/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs b/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs index d790e64..81414dc 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyEditors/ArcheTypePropertyEditor.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.Linq; using Archetype.Umbraco.Extensions; using ClientDependency.Core; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Models.Editors; @@ -53,7 +55,7 @@ namespace Archetype.Umbraco.PropertyEditors public override string ConvertDbToString(Property property, PropertyType propertyType, IDataTypeService dataTypeService) { - if (property.Value == null) + if (property.Value == null || property.Value.ToString() == "") return string.Empty; var archetype = new ArchetypeHelper().DeserializeJsonToArchetype(property.Value.ToString(), propertyType.DataTypeDefinitionId); @@ -70,17 +72,23 @@ namespace Archetype.Umbraco.PropertyEditors } } - property.Value = JsonConvert.SerializeObject(archetype); + var json = JObject.Parse(JsonConvert.SerializeObject(archetype)); + var propertiesToRemove = new String[] { "propertyEditorAlias", "dataTypeId", "dataTypeGuid" }; - return base.ConvertDbToString(property, propertyType, dataTypeService); + json.Descendants().OfType() + .Where(p => propertiesToRemove.Contains(p.Name)) + .ToList() + .ForEach(x => x.Remove()); + + return json.ToString(); } public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService) { - if (property.Value == null) + if (property.Value == null || property.Value.ToString() == "") return string.Empty; - var archetype = JsonConvert.DeserializeObject(property.Value.ToString(), _jsonSettings); + var archetype = JsonConvert.DeserializeObject(property.Value.ToString(), _jsonSettings); foreach (var fieldset in archetype.Fieldsets) { @@ -98,7 +106,7 @@ namespace Archetype.Umbraco.PropertyEditors } public override object ConvertEditorToDb(ContentPropertyData editorValue, object currentValue) { - if (editorValue.Value == null) + if (editorValue.Value == null || editorValue.Value.ToString() == "") return string.Empty; var archetype = new ArchetypeHelper().DeserializeJsonToArchetype(editorValue.Value.ToString(), editorValue.PreValues); @@ -115,7 +123,7 @@ namespace Archetype.Umbraco.PropertyEditors } } - return JsonConvert.SerializeObject(archetype); + return JsonConvert.SerializeObject(archetype); } } diff --git a/app/controllers/controller.js b/app/controllers/controller.js index 9ceb769..e0f1939 100644 --- a/app/controllers/controller.js +++ b/app/controllers/controller.js @@ -11,14 +11,11 @@ //ini the model $scope.model.value = $scope.model.value || getDefaultModel($scope.model.config); - - //ini the render model - $scope.archetypeRenderModel = {}; - initArchetypeRenderModel(); + init(); //helper to get $eval the labelTemplate $scope.getFieldsetTitle = function(fieldsetConfigModel, fieldsetIndex) { - var fieldset = $scope.archetypeRenderModel.fieldsets[fieldsetIndex]; + var fieldset = $scope.model.value.fieldsets[fieldsetIndex]; var fieldsetConfig = $scope.getConfigFieldsetByAlias(fieldset.alias); var template = fieldsetConfigModel.labelTemplate; @@ -44,7 +41,7 @@ cursor: "move", handle: ".handle", update: function (ev, ui) { - + }, stop: function (ev, ui) { @@ -61,11 +58,11 @@ if (typeof $index != 'undefined') { - $scope.archetypeRenderModel.fieldsets.splice($index + 1, 0, newFieldset); + $scope.model.value.fieldsets.splice($index + 1, 0, newFieldset); } else { - $scope.archetypeRenderModel.fieldsets.push(newFieldset); + $scope.model.value.fieldsets.push(newFieldset); } } @@ -74,15 +71,10 @@ } } - //rather than splice the archetypeRenderModel, we're hiding this and cleaning onFormSubmitting $scope.removeRow = function ($index) { if ($scope.canRemove()) { if (confirm('Are you sure you want to remove this?')) { - $scope.archetypeRenderModel.fieldsets[$index].remove = true; - /* - Touches the model. Not sure why this is needed but without it the Digest doesn't run on a remove row. - */ - $scope.archetypeRenderModel = JSON.parse(JSON.stringify($scope.archetypeRenderModel)); + $scope.model.value.fieldsets.splice($index, 1); } } } @@ -118,12 +110,15 @@ } //helper, ini the render model from the server (model.value) - function initArchetypeRenderModel() { - $scope.archetypeRenderModel = removeNulls($scope.model.value); + function init() { + $scope.model.value = removeNulls($scope.model.value); + addDefaultProperties($scope.model.value.fieldsets); + } - _.each($scope.archetypeRenderModel.fieldsets, function (fieldset) + function addDefaultProperties(fieldsets) + { + _.each(fieldsets, function (fieldset) { - fieldset.remove = false; fieldset.collapse = false; fieldset.isValid = true; }); @@ -144,6 +139,15 @@ return (typeof property !== 'undefined') ? property.value : ''; }; + $scope.isCollapsed = function(fieldset) + { + if(typeof fieldset.collapse === "undefined") + { + fieldset.collapse = true; + } + return fieldset.collapse; + } + //helper for expanding/collapsing fieldsets $scope.focusFieldset = function(fieldset){ fixDisableSelection(); @@ -159,13 +163,13 @@ iniState = fieldset.collapse; } - _.each($scope.archetypeRenderModel.fieldsets, function(fieldset){ + _.each($scope.model.value.fieldsets, function(fieldset){ fieldset.collapse = true; }); - if(!fieldset && $scope.archetypeRenderModel.fieldsets.length == 1 && $scope.archetypeRenderModel.fieldsets[0].remove == false) + if(!fieldset && $scope.model.value.fieldsets.length == 1) { - $scope.archetypeRenderModel.fieldsets[0].collapse = false; + $scope.model.value.fieldsets[0].collapse = false; return; } @@ -179,7 +183,7 @@ $scope.focusFieldset(); //developerMode helpers - $scope.archetypeRenderModel.toString = stringify; + $scope.model.value.toString = stringify; //encapsulate stringify (should be built into browsers, not sure of IE support) function stringify() { @@ -187,12 +191,12 @@ } //watch for changes - $scope.$watch('archetypeRenderModel', function (v) { + $scope.$watch('model.value', function (v) { if ($scope.model.config.developerMode) { console.log(v); if (typeof v === 'string') { - $scope.archetypeRenderModel = JSON.parse(v); - $scope.archetypeRenderModel.toString = stringify; + $scope.model.value = JSON.parse(v); + $scope.model.value.toString = stringify; } } }); @@ -200,64 +204,7 @@ //helper to count what is visible function countVisible() { - var count = 0; - - _.each($scope.archetypeRenderModel.fieldsets, function(fieldset){ - if (fieldset.remove == false) { - count++; - } - }); - - return count; - } - - //helper to sync the model to the renderModel - function syncModelToRenderModel() - { - $scope.model.value = { fieldsets: [] }; - - _.each($scope.archetypeRenderModel.fieldsets, function(fieldset){ - var cleanedFieldset = cleanFieldset(fieldset); - - if(cleanedFieldset){ - $scope.model.value.fieldsets.push(cleanedFieldset); - } - }); - } - - //helper to remove properties only used during editing that we don't want in the saved data - //also removes properties that are no longer in the config - function cleanFieldset(fieldset) - { - if (typeof fieldset != 'function' && !fieldset.remove){ - - var fieldsetConfig = $scope.getConfigFieldsetByAlias(fieldset.alias); - - //clone and clean - var tempFieldset = JSON.parse(JSON.stringify(fieldset)); - delete tempFieldset.remove; - delete tempFieldset.isValid; - delete tempFieldset.collapse; - - _.each(tempFieldset.properties, function(property, index){ - var propertyConfig = _.find(fieldsetConfig.properties, function(p){ - return property.alias == p.alias; - }); - - //just prune the property - if(propertyConfig){ - delete property.isValid; - } - else - { - //need to remove the whole property - tempFieldset.properties.splice(index, 1); - } - - }); - - return tempFieldset; - } + return $scope.model.value.fieldsets.length; } // helper to get initial model if none was provided @@ -270,7 +217,7 @@ //helper to add an empty fieldset to the render model function getEmptyRenderFieldset (fieldsetModel) { - return {alias: fieldsetModel.alias, remove: false, isValid: true, properties: []}; + return {alias: fieldsetModel.alias, collapse: false, isValid: true, properties: []}; } //helper to ensure no nulls make it into the model @@ -300,9 +247,9 @@ //helper to lookup validity when given a fieldsetIndex and property alias $scope.getPropertyValidity = function(fieldsetIndex, alias) { - if($scope.archetypeRenderModel.fieldsets[fieldsetIndex]) + if($scope.model.value.fieldsets[fieldsetIndex]) { - var property = _.find($scope.archetypeRenderModel.fieldsets[fieldsetIndex].properties, function(property){ + var property = _.find($scope.model.value.fieldsets[fieldsetIndex].properties, function(property){ return property.alias == alias; }); } @@ -310,23 +257,6 @@ return (typeof property == 'undefined') ? true : property.isValid; } - //sync things up on save - $scope.$on("formSubmitting", function (ev, args) { - - //test for form; may have to do this differently for nested archetypes - if(!form) - return; - - if(form.$invalid) - { - notificationsService.warning("Cannot Save Document", "The document could not be saved because of missing required fields.") - } - else - { - syncModelToRenderModel(); - } - }); - //custom js if ($scope.model.config.customJsPath) { assetsService.loadJs($scope.model.config.customJsPath); diff --git a/app/views/archetype.html b/app/views/archetype.html index bf31438..5108680 100644 --- a/app/views/archetype.html +++ b/app/views/archetype.html @@ -1,5 +1,5 @@
- +
  • @@ -8,29 +8,25 @@
- -
    -
  • +
      +
    • -
      -
      -
      -
      +
      - +
      @@ -49,7 +45,6 @@
    -