public static CanvasArea GetAreaByAlias(List <CanvasArea> areas, string alias) { AreaResult = null; foreach (var area in areas) { FindArea(area, alias); } return(AreaResult); }
public static CanvasArea GetAreaByAlias(List<CanvasArea> areas, string alias) { AreaResult = null; foreach (var area in areas) { FindArea(area, alias); } return AreaResult; }
private static void FindArea(Canvas.Models.CanvasArea area, string alias) { if (area.Alias == alias) { AreaResult = area; } if (AreaResult == null) { foreach (var c in area.Controls.Where(x => x.Type == "Grid" || x.Type == "Section")) { foreach (var a in c.Areas) { FindArea(a, alias); } } } }
public static CanvasView Get(string alias, bool isAuthenticated) { try { var View = new CanvasView(); int pageId = UmbracoContext.Current.PageId.Value; if (isAuthenticated) { var cs = ApplicationContext.Current.Services.ContentService; var page = cs.GetById(pageId); var model = Repository.GetObjectById(pageId); // Find area recurcivly if (model.Areas.All(x => x.Alias != alias)) { // Did not find area // Check if there is a root area, create it if not. Log.Info("Canvas - Did not find area, try to create it: " + alias); var area = new CanvasArea(); area.Alias = alias; if (model.Areas.Any()) { // Append new model.Areas.Add(area); Repository.SaveJson(model, pageId); page = cs.GetById(pageId); } else { // Create new var areaModel = new CanvasModel(); areaModel.Areas.Add(area); Repository.SaveJson(areaModel, pageId); page = cs.GetById(pageId); } } View.page = page; View.viewName = "/Views/Canvas/Backend.cshtml"; } else { View.node = UmbracoContext.Current.ContentCache.GetById(pageId); View.viewName = "/Views/Canvas/FrontEnd.cshtml"; } View.alias = alias; return View; } catch (Exception ex) { Log.Error("Canvas error on Get in ViewHelper.", ex); return null; } }
private void FindAreaWithControl(CanvasArea area, Guid controlId) { var control = area.Controls.FirstOrDefault(x => x.ControlID == controlId); if (control != null) { AreaControlResult = area; } if (AreaControlResult == null) { foreach (var c in area.Controls.Where(x => x.Type == "Grid" || x.Type == "Section")) { foreach (var a in c.Areas) { FindAreaWithControl(a, controlId); } } } }
public JsonResult AddControl(string areaAlias, string controlType, int pageId) { var controlId = Guid.NewGuid(); string columns = "6:medium:;6:medium:"; var model = Repository.GetObjectById(pageId); if (model != null && !string.IsNullOrEmpty(controlType) && !string.IsNullOrEmpty(areaAlias)) { // Find Area In Grid/Section or Root foreach (var area in model.Areas) { FindArea(area, areaAlias); } if (AreaResult != null) { var control = new CanvasControl { ControlID = controlId, Type = controlType }; // If control is grid then create area inside it if (controlType == "Grid") { control.Columns = columns; int p = 0; foreach (var column in columns.Split(';').Where(x => !string.IsNullOrEmpty(x))) { string col = column.Split(':')[0]; var area = new CanvasArea {Alias = "grid-" + col + "-" + controlId.ToString() + "-" + p}; control.Areas.Add(area); p++; } } // If control is section then create area inside it if (controlType == "Section") { var area = new CanvasArea {Alias = "section-" + controlId.ToString()}; control.Areas.Add(area); } AreaResult.Controls.Add(control); } // Convert model back to json for saving Repository.SaveJson(model, pageId); } else { // If no area is found, create it var m = new CanvasModel(); var area = new CanvasArea {Alias = areaAlias}; var control = new CanvasControl { ControlID = controlId, Type = controlType }; area.Controls.Add(control); m.Areas.Add(area); Repository.SaveJson(m, pageId); } return Json(new { success = true, controlType = controlType, controlId = controlId.ToString(), pageId = pageId }); }
public JsonResult PostEditControl(FormCollection form) { var controlId = new Guid(form["controlId"]); var pageId = form["pageId"]; var controlType = form["controlType"]; var model = Repository.GetObjectById(Convert.ToInt32(pageId)); if (model != null) { // Finna Area hvort sem það sé inn í Grid/section eða rótar svæði sem á undir sér Control foreach (var area in model.Areas) { FindAreaWithControl(area, controlId); } if (AreaControlResult != null) { var control = AreaControlResult.Controls.FirstOrDefault(x => x.ControlID == controlId); if (control != null) { var controlProperties = control.GetType().GetProperties().ToDictionary(x => x.Name, x => x.GetValue(control, null) ?? "").ToList(); foreach (var property in controlProperties) { if (property.Key != "Type" && property.Key != "ControlID") { var value = form[property.Key]; // If Macro control // Replace single quote with double quote if (controlType == "Macro" && property.Key == "Macro") { value = value.Replace("'", "\""); } PropertyInfo prop = control.GetType().GetProperty(property.Key, BindingFlags.Public | BindingFlags.Instance); if (null != prop && prop.CanWrite) { prop.SetValue(control, HttpContext.Server.HtmlEncode(value), null); } } } // Ef Control er grid þá þarf að eyða út gömlum svæðum og búa til ný if (controlType == "Grid") { var columns = form["columns"].Split(';'); var ListOfAreasThatHaveNotChanges = new List<CanvasArea>(); // Finna öll svæði sem eru undir Controli var AreasInsideGrid = control.Areas; bool update = true; // Athuga hvort það eigi að uppfæra grid eða búa til nýtt area undir þessu controli. t.d. col-4-12345 í stað col-6-12345 var p = 0; foreach (var col in columns) { string[] colValue = col.Split(':'); var colAreaAlis = "grid-" + colValue[0] + "-" + controlId.ToString() + "-" + p; p++; var AreaInsideGrid = AreasInsideGrid.FirstOrDefault(x => x.Alias == colAreaAlis); if (AreaInsideGrid == null) { update = false; } else { ListOfAreasThatHaveNotChanges.Add(AreaInsideGrid); } } if (!update) { // Eyða út öllum svæðum sem hafa breyst var AreasToRemove = AreasInsideGrid.Intersect(ListOfAreasThatHaveNotChanges).ToList(); foreach (var a in AreasToRemove.ToList()) { AreasInsideGrid.Remove(a); } p = 0; foreach (var column in columns) { string col = column.Split(':')[0]; var area = new CanvasArea { Alias = "grid-" + col + "-" + controlId.ToString() + "-" + p }; control.Areas.Insert(p, area); p++; } } } } } } var saveStatus = Repository.SaveJson(model, Convert.ToInt32(pageId)); return Json(new { success = saveStatus }); }