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); })); }
public ActionResult ApplyTemplate(int?templateId = null, string layoutData = null, int?contentId = null, string contentType = null) { var template = templateId != null?_layoutManager.GetLayout(templateId.Value) : null; var templateElements = template != null?_layoutManager.LoadElements(template).ToList() : default(IEnumerable <Element>); var describeContext = CreateDescribeElementsContext(contentId, contentType); var elementInstances = _mapper.ToLayoutModel(layoutData, describeContext).ToList(); var updatedLayout = templateElements != null ? _layoutManager.ApplyTemplate(elementInstances, templateElements) : _layoutManager.DetachTemplate(elementInstances); var canvas = updatedLayout.Single(x => x is Canvas); var editorModel = _mapper.ToEditorModel(canvas, describeContext); var json = JsonConvert.SerializeObject(editorModel, Formatting.None); return(Content(json, "application/json")); }
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); })); }