public ViewResult Create(ElementDataViewModel model, string session) { var sessionState = _objectStore.Get <ElementSessionState>(session); var contentId = sessionState.ContentId; var contentType = sessionState.ContentType; var describeContext = CreateDescribeContext(contentId, contentType); var descriptor = _elementManager.GetElementDescriptorByTypeName(describeContext, model.TypeName); var data = Request.Form.ToDictionary(); var element = _elementManager.ActivateElement(descriptor, e => e.Data = data); var context = CreateEditorContext(session, describeContext.Content, element, postedElementData: data, updater: this); var editorResult = _elementManager.UpdateEditor(context); var viewModel = new EditElementViewModel { SessionKey = session, Layout = describeContext.Content.As <ILayoutAspect>(), EditorResult = editorResult, TypeName = model.TypeName, DisplayText = descriptor.DisplayText, ElementData = element.Data.Serialize(), Tabs = editorResult.CollectTabs().ToArray(), }; if (!ModelState.IsValid) { _transactionManager.Cancel(); } else { viewModel.Submitted = true; viewModel.ElementEditorModel = _mapper.ToEditorModel(element, describeContext); } return(View(viewModel)); }
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 LayoutEditor Create(string layoutData, string sessionKey, int?templateId = null, IContent content = null) { return(new LayoutEditor { Data = _mapper.ToEditorModel(layoutData, new DescribeElementsContext { Content = content }).ToJson(), ConfigurationData = GetConfigurationData(content), TemplateId = templateId, Content = content, SessionKey = sessionKey, Templates = GetTemplates(content) }); }
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); })); }
public ViewResult Update(string session) { var sessionState = _objectStore.Get <ElementSessionState>(session); var contentId = sessionState.ContentId; var contentType = sessionState.ContentType; var typeName = sessionState.TypeName; var elementData = ElementDataHelper.Deserialize(sessionState.ElementData); var describeContext = CreateDescribeContext(contentId, contentType); var descriptor = _elementManager.GetElementDescriptorByTypeName(describeContext, typeName); var data = elementData.Combine(ElementDataHelper.Deserialize(sessionState.ElementEditorData)); var element = _elementManager.ActivateElement(descriptor, e => e.Data = data); var context = CreateEditorContext(session, describeContext.Content, element, elementData, this); var editorResult = _elementManager.UpdateEditor(context); var viewModel = new EditElementViewModel { Layout = describeContext.Content.As <ILayoutAspect>(), EditorResult = editorResult, TypeName = typeName, DisplayText = descriptor.DisplayText, ElementData = element.Data.Serialize(), Tabs = editorResult.CollectTabs().ToArray(), SessionKey = session }; if (!ModelState.IsValid) { _transactionManager.Cancel(); } else { viewModel.ElementHtml = RenderElement(element, describeContext); viewModel.Submitted = true; viewModel.ElementEditorModel = _mapper.ToEditorModel(element, describeContext); } return(View("Edit", viewModel)); }