protected override DriverResult Editor(LayoutPart part, IUpdateModel updater, dynamic shapeHelper) { return(ContentShape("Parts_Layout_Edit", () => { if (part.Id == 0 && String.IsNullOrWhiteSpace(part.LayoutData)) { var settings = part.TypePartDefinition.Settings.GetModel <LayoutTypePartSettings>(); // If the default layout setting is left empty, use the one from the service if (String.IsNullOrWhiteSpace(settings.DefaultLayoutData)) { var defaultData = _serializer.Serialize(_layoutManager.CreateDefaultLayout()); part.LayoutData = defaultData; } else { part.LayoutData = settings.DefaultLayoutData; } } var viewModel = new LayoutPartViewModel { LayoutEditor = _layoutEditorFactory.Create(part) }; if (updater != null) { updater.TryUpdateModel(viewModel, Prefix, null, new[] { "Part", "Templates" }); var describeContext = new DescribeElementsContext { Content = part }; var elementInstances = _mapper.ToLayoutModel(viewModel.LayoutEditor.Data, describeContext).ToArray(); var recycleBin = (RecycleBin)_mapper.ToLayoutModel(viewModel.LayoutEditor.RecycleBin, describeContext).SingleOrDefault(); var context = new LayoutSavingContext { Content = part, Updater = updater, Elements = elementInstances, RemovedElements = recycleBin != null ? recycleBin.Elements : Enumerable.Empty <Element>() }; _elementManager.Saving(context); _elementManager.Removing(context); part.LayoutData = _serializer.Serialize(elementInstances); part.TemplateId = viewModel.LayoutEditor.TemplateId; part.SessionKey = viewModel.LayoutEditor.SessionKey; viewModel.LayoutEditor.Data = _mapper.ToEditorModel(part.LayoutData, new DescribeElementsContext { Content = part }).ToJson(); } return shapeHelper.EditorTemplate(TemplateName: "Parts.Layout", Model: viewModel, Prefix: Prefix); })); }
protected override DriverResult Editor(LayoutPart part, IUpdateModel updater, dynamic shapeHelper) { return(ContentShape("Parts_Layout_Edit", () => { if (part.Id == 0 && String.IsNullOrWhiteSpace(part.LayoutData)) { part.LayoutData = part.TypePartDefinition.Settings.GetModel <LayoutTypePartSettings>().DefaultLayoutData; } var viewModel = new LayoutPartViewModel { LayoutEditor = _layoutEditorFactory.Create(part) }; if (updater != null) { updater.TryUpdateModel(viewModel, Prefix, null, new[] { "Part", "Templates" }); var describeContext = new DescribeElementsContext { Content = part }; var elementInstances = _mapper.ToLayoutModel(viewModel.LayoutEditor.Data, describeContext).ToArray(); var removedElementInstances = _serializer.Deserialize(viewModel.LayoutEditor.Trash, describeContext).ToArray(); var context = new LayoutSavingContext { Content = part, Updater = updater, Elements = elementInstances, RemovedElements = removedElementInstances }; _elementManager.Saving(context); _elementManager.Removing(context); part.LayoutData = _serializer.Serialize(elementInstances); part.TemplateId = viewModel.LayoutEditor.TemplateId; part.SessionKey = viewModel.LayoutEditor.SessionKey; viewModel.LayoutEditor.Data = _mapper.ToEditorModel(part.LayoutData, new DescribeElementsContext { Content = part }).ToJson(); } return shapeHelper.EditorTemplate(TemplateName: "Parts.Layout", Model: viewModel, Prefix: Prefix); })); }