public void Delete(ApiCall call, Guid[] ids) { var sitedb = call.WebSite.SiteDb(); List <CmsCssRule> rules = new List <CmsCssRule>(); foreach (var item in ids) { var rule = sitedb.CssRules.Get(item); if (rule != null) { rules.Add(rule); } } foreach (var item in rules.GroupBy(o => o.ParentStyleId)) { var rulelist = item.ToList(); List <CmsCssRuleChanges> changes = new List <CmsCssRuleChanges>(); foreach (var delRule in rulelist) { CmsCssRuleChanges change = new CmsCssRuleChanges(); change.CssRuleId = delRule.Id; change.ChangeType = ChangeType.Delete; changes.Add(change); } sitedb.CssRules.UpdateStyle(changes, item.Key); } }
public void Delete(Guid id, bool updatesource = true) { var cssrule = Get(id); if (cssrule == null) { return; } if (updatesource) { if (cssrule.IsInline) { _RemoveInlineCss(cssrule); } else { ///remove the css rule... CmsCssRuleChanges change = new CmsCssRuleChanges(); change.ChangeType = ChangeType.Delete; change.CssText = cssrule.CssText; change.CssRuleId = cssrule.Id; List <CmsCssRuleChanges> list = new List <CmsCssRuleChanges>(); list.Add(change); UpdateStyle(list, cssrule.ParentStyleId); } } RaiseBeforeEvent(cssrule, ChangeType.Delete); Store.delete(id, false); RaiseEvent(cssrule, ChangeType.Delete); }
public bool AddOrUpdate(Models.CmsCssRule value, Guid UserId = default(Guid), bool updateSource = false) { var old = Get(value.Id); if (old == null) { RaiseBeforeEvent(value, ChangeType.Add); bool ok = Store.add(value.Id, value, false); if (ok) { if (updateSource) { if (value.IsInline) { AddInlineCss(value.OwnerObjectId, value.OwnerObjectConstType, value.KoobooId, value.RuleText); } else { AddNewCssRule(value.ParentStyleId, value.CssText); } } RaiseEvent(value, ChangeType.Add); } } else { if (!IsEqual(value, old)) { RaiseBeforeEvent(value, ChangeType.Update, old); Store.update(value.Id, value, false); if (updateSource) { if (value.IsInline) { UpdateInlineCss(value.Id, value.CssText); } else { CmsCssRuleChanges change = new CmsCssRuleChanges(); change.ChangeType = ChangeType.Update; change.CssText = value.CssText; change.CssRuleId = value.Id; List <CmsCssRuleChanges> list = new List <CmsCssRuleChanges>(); list.Add(change); UpdateStyle(list, value.ParentStyleId); } } RaiseEvent(value, ChangeType.Update, old); } } return(true); }
public void AddNewCssRule(Guid ParentStyleId, string cssText) { CmsCssRuleChanges change = new CmsCssRuleChanges(); change.CssText = cssText; change.ChangeType = ChangeType.Add; List <CmsCssRuleChanges> list = new List <CmsCssRuleChanges>(); list.Add(change); UpdateStyle(list, ParentStyleId); }
public void AddNewCssRule(Guid ParentStyleId, string selector, string declarationText) { CmsCssRuleChanges change = new CmsCssRuleChanges(); change.selectorText = selector; change.ChangeType = ChangeType.Add; change.DeclarationText = declarationText; List <CmsCssRuleChanges> list = new List <CmsCssRuleChanges>(); list.Add(change); UpdateStyle(list, ParentStyleId); }
private static CmsCssRuleChanges GetStyleRuleChangeItem(CssRuleViewModel rule) { List <CmsCssDeclaration> declarations = new List <CmsCssDeclaration>(); if (rule.Declarations != null) { foreach (var item in rule.Declarations) { declarations.Add(new CmsCssDeclaration() { PropertyName = item.Name, Value = item.Value, Important = item.Important }); } } var changeitem = new CmsCssRuleChanges() { ChangeType = ChangeType.Add, CssRuleId = rule.Id, selectorText = rule.Selector, Declarations = declarations }; return(changeitem); }
private static CmsCssRuleChanges GetMediaRuleAdded(CssRuleViewModel rule) { CmsCssRuleChanges change = new CmsCssRuleChanges(); if (rule.Selector.IndexOf("@media", StringComparison.OrdinalIgnoreCase) == -1) { change.selectorText = "@media " + rule.Selector; } else { change.selectorText = rule.Selector; } string ruletext = string.Empty; foreach (var item in rule.Rules) { ruletext += "\r\n" + item.Selector + "\r\n{\r\n"; List <CmsCssDeclaration> declarations = new List <CmsCssDeclaration>(); if (item.Declarations != null) { foreach (var decl in item.Declarations) { declarations.Add(new CmsCssDeclaration() { PropertyName = decl.Name, Value = decl.Value, Important = decl.Important }); } ruletext += CssService.SerializeCmsCssDeclaration(declarations); } ruletext += "\r\n}"; } change.DeclarationText = ruletext; return(change); }
public static void DeleteUrl(SiteDb sitedb, IRepository repo, Guid ObjectId, string OldUrl) { var UseObject = repo.Get(ObjectId); if (UseObject != null) { if (UseObject is IDomObject) { var domobject = UseObject as IDomObject; if (domobject != null) { string body = domobject.Body; string newbody = Service.DomService.DeleteUrl(body, OldUrl); domobject.Body = newbody; repo.AddOrUpdate(domobject); } } else if (UseObject is Style) { var style = UseObject as Style; style.Body = Lib.Helper.StringHelper.ReplaceIgnoreCase(style.Body, OldUrl, ""); sitedb.Styles.AddOrUpdate(style); } else if (UseObject is CmsCssRule) { var rule = UseObject as CmsCssRule; if (rule.IsInline) { var rulerepo = sitedb.GetRepository(rule.OwnerObjectConstType); if (rulerepo != null) { var ownerobject = rulerepo.Get(rule.OwnerObjectId); if (ownerobject != null && ownerobject is IDomObject) { var domobject = ownerobject as IDomObject; var node = Service.DomService.GetElementByKoobooId(domobject.Dom, rule.KoobooId); if (node != null && node.nodeType == Dom.enumNodeType.ELEMENT) { var el = node as Kooboo.Dom.Element; string style = el.getAttribute("style"); if (!string.IsNullOrEmpty(style)) { string[] seperators = new string[1]; seperators[0] = ";"; string[] parts = style.Split(seperators, StringSplitOptions.RemoveEmptyEntries); int count = parts.Length; for (int i = 0; i < count; i++) { if (parts[i].IndexOf(OldUrl, StringComparison.OrdinalIgnoreCase) > -1) { parts[i] = ""; } } style = string.Join(";", parts); } if (string.IsNullOrWhiteSpace(style)) { el.removeAttribute("style"); } else { el.setAttribute("style", style); } string newhtml = Service.DomService.ReSerializeElement(el); List <SourceUpdate> updates = new List <SourceUpdate>(); updates.Add(new SourceUpdate() { StartIndex = el.location.openTokenStartIndex, EndIndex = el.location.endTokenEndIndex, NewValue = newhtml }); domobject.Body = Service.DomService.UpdateSource(domobject.Body, updates); rulerepo.AddOrUpdate(domobject); } } } } else if (rule.ruleType == RuleType.ImportRule) { List <CmsCssRuleChanges> changelist = new List <CmsCssRuleChanges>(); CmsCssRuleChanges changes = new CmsCssRuleChanges(); changes.CssRuleId = rule.Id; changes.selectorText = rule.SelectorText; changes.DeclarationText = ""; changes.ChangeType = ChangeType.Delete; changelist.Add(changes); Guid parentstyle = rule.ParentStyleId; if (parentstyle == default(Guid)) { parentstyle = rule.OwnerObjectId; } sitedb.CssRules.UpdateStyle(changelist, parentstyle); } else { var declaration = Kooboo.Dom.CSS.CSSSerializer.deserializeDeclarationBlock(rule.CssText); var found = declaration.item.FindAll(o => !string.IsNullOrEmpty(o.value) && o.value.IndexOf(OldUrl, StringComparison.OrdinalIgnoreCase) > -1); if (found != null && found.Count > 0) { foreach (var founditem in found) { declaration.item.Remove(founditem); } } string newdecltext = Kooboo.Dom.CSS.CSSSerializer.serializeDeclarationBlock(declaration); List <CmsCssRuleChanges> changelist = new List <CmsCssRuleChanges>(); CmsCssRuleChanges changes = new CmsCssRuleChanges(); changes.CssRuleId = rule.Id; changes.selectorText = rule.SelectorText; changes.DeclarationText = newdecltext; changes.ChangeType = ChangeType.Update; changelist.Add(changes); Guid parentstyle = rule.ParentStyleId; if (parentstyle == default(Guid)) { parentstyle = rule.OwnerObjectId; } sitedb.CssRules.UpdateStyle(changelist, parentstyle); } } else if (UseObject is ResourceGroup) { var oldid = Data.IDGenerator.GetRouteId(OldUrl); var group = UseObject as ResourceGroup; group.Children.Remove(oldid); repo.AddOrUpdate(group); } } }
public static void ChangeUrl(SiteDb sitedb, IRepository repo, Guid ObjectId, string OldUrl, string NewUrl) { var UseObject = repo.Get(ObjectId); if (UseObject != null) { if (UseObject is IDomObject) { var domobject = UseObject as IDomObject; if (domobject != null) { string body = domobject.Body; string newbody = Service.DomService.UpdateUrl(body, OldUrl, NewUrl); domobject.Body = newbody; repo.AddOrUpdate(domobject); } } else if (UseObject is Style) { var style = UseObject as Style; style.Body = Lib.Helper.StringHelper.ReplaceIgnoreCase(style.Body, OldUrl, NewUrl); sitedb.Styles.AddOrUpdate(style); } else if (UseObject is CmsCssRule) { var rule = UseObject as CmsCssRule; if (rule.IsInline) { var rulerepo = sitedb.GetRepository(rule.OwnerObjectConstType); if (rulerepo != null) { var ownerobject = rulerepo.Get(rule.OwnerObjectId); if (ownerobject != null && ownerobject is IDomObject) { var domobject = ownerobject as IDomObject; var node = Service.DomService.GetElementByKoobooId(domobject.Dom, rule.KoobooId); if (node != null && node.nodeType == Dom.enumNodeType.ELEMENT) { var el = node as Kooboo.Dom.Element; string style = el.getAttribute("style"); if (!string.IsNullOrEmpty(style)) { style = Lib.Helper.StringHelper.ReplaceIgnoreCase(style, OldUrl, NewUrl); } el.setAttribute("style", style); string newhtml = Service.DomService.ReSerializeElement(el); List <SourceUpdate> updates = new List <SourceUpdate>(); updates.Add(new SourceUpdate() { StartIndex = el.location.openTokenStartIndex, EndIndex = el.location.endTokenEndIndex, NewValue = newhtml }); domobject.Body = Service.DomService.UpdateSource(domobject.Body, updates); rulerepo.AddOrUpdate(domobject); } } } } else if (rule.ruleType == RuleType.ImportRule) { string newdecltext = Lib.Helper.StringHelper.ReplaceIgnoreCase(rule.RuleText, OldUrl, NewUrl); List <CmsCssRuleChanges> changelist = new List <CmsCssRuleChanges>(); CmsCssRuleChanges changes = new CmsCssRuleChanges(); changes.CssRuleId = rule.Id; changes.selectorText = newdecltext; // rule.SelectorText; changes.DeclarationText = newdecltext; changes.ChangeType = ChangeType.Update; changelist.Add(changes); Guid parentstyle = rule.ParentStyleId; if (parentstyle == default(Guid)) { parentstyle = rule.OwnerObjectId; } sitedb.CssRules.UpdateStyle(changelist, parentstyle); } else { var declaration = Kooboo.Dom.CSS.CSSSerializer.deserializeDeclarationBlock(rule.CssText); foreach (var item in declaration.item) { if (item.value.IndexOf(OldUrl, StringComparison.OrdinalIgnoreCase) > -1) { item.value = Lib.Helper.StringHelper.ReplaceIgnoreCase(item.value, OldUrl, NewUrl); } } string newdecltext = Kooboo.Dom.CSS.CSSSerializer.serializeDeclarationBlock(declaration); List <CmsCssRuleChanges> changelist = new List <CmsCssRuleChanges>(); CmsCssRuleChanges changes = new CmsCssRuleChanges(); changes.CssRuleId = rule.Id; changes.selectorText = rule.SelectorText; changes.DeclarationText = newdecltext; changes.ChangeType = ChangeType.Update; changelist.Add(changes); Guid parentstyle = rule.ParentStyleId; if (parentstyle == default(Guid)) { parentstyle = rule.OwnerObjectId; } sitedb.CssRules.UpdateStyle(changelist, parentstyle); } } else if (UseObject is ResourceGroup) { var oldid = Data.IDGenerator.GetRouteId(OldUrl); var group = UseObject as ResourceGroup; int neworder = 0; if (group.Children.ContainsKey(oldid)) { neworder = group.Children[oldid]; } group.Children.Remove(oldid); if (NewUrl != null && !NewUrl.ToLower().StartsWith("http://") && !NewUrl.ToLower().StartsWith("https://")) { var newid = Data.IDGenerator.GetRouteId(NewUrl); group.Children[newid] = neworder; sitedb.ResourceGroups.AddOrUpdate(group); } } } }
public void UpdateRules(ApiCall call) { Guid StyleId = call.ObjectId; var json = call.Context.Request.Body; if (string.IsNullOrEmpty(json) || StyleId == default(Guid)) { return; } UpdateStyleRuleViewModel model; try { model = Lib.Helper.JsonHelper.Deserialize <UpdateStyleRuleViewModel>(json); } catch (Exception ex) { throw; } List <CmsCssRuleChanges> changes = new List <CmsCssRuleChanges>(); if (model.Added != null && model.Added.Count > 0) { foreach (var rule in model.Added.OrderBy(o => o.Key)) { if (rule.Value.RuleType == RuleType.StyleRule) { CmsCssRuleChanges changeitem = GetStyleRuleChangeItem(rule.Value); changes.Add(changeitem); } else if (rule.Value.RuleType == RuleType.MediaRule) { CmsCssRuleChanges changeitem = GetMediaRuleAdded(rule.Value); changes.Add(changeitem); } } } if (model.Modified != null && model.Modified.Count > 0) { foreach (var rule in model.Modified) { List <CmsCssDeclaration> declarations = new List <CmsCssDeclaration>(); if (rule.Value.Declarations != null) { foreach (var item in rule.Value.Declarations) { declarations.Add(new CmsCssDeclaration() { PropertyName = item.Name, Value = item.Value, Important = item.Important }); } } changes.Add(new CmsCssRuleChanges() { ChangeType = ChangeType.Update, CssRuleId = rule.Value.Id, selectorText = rule.Value.Selector, Declarations = declarations }); } } if (model.Removed != null && model.Removed.Count > 0) { foreach (var guid in model.Removed) { changes.Add(new CmsCssRuleChanges() { ChangeType = ChangeType.Delete, CssRuleId = guid }); } } if (changes.Count > 0) { call.WebSite.SiteDb().CssRules.UpdateStyle(changes, StyleId); } }
public void ProcessStyleRules(RenderContext context, List <Model.StyleModel> changes, Guid StyleId) { if (!changes.Any()) { return; } var prechanges = new List <RuleChange>(); RuleChange current = null; foreach (var item in changes) { current = null; if (item.RuleId != default(Guid)) { current = prechanges.Find(o => o.RuleId == item.RuleId); if (current == null) { current = new RuleChange(); var cmscssrule = context.WebSite.SiteDb().CssRules.Get(item.RuleId); if (cmscssrule == null) { current.RuleId = default(Guid); current.Declarations = new Dom.CSS.CSSDeclarationBlock(); } else { current.RuleId = item.RuleId; current.Declarations = Service.CssService.ParseStyleDeclaration(cmscssrule); if (current.Declarations == null) { current.Declarations = new Dom.CSS.CSSDeclarationBlock(); } } if (current != null) { prechanges.Add(current); } } if (current != null) { current.Selector = item.Selector; } } else { current = prechanges.Find(o => o.RuleId == default(Guid) && o.Selector == item.Selector); if (current == null) { current = new RuleChange(); current.Selector = item.Selector; current.Declarations = new Dom.CSS.CSSDeclarationBlock(); prechanges.Add(current); } } if (current != null) { if (!string.IsNullOrEmpty(item.Value)) { current.Declarations.setProperty(item.Property, item.Value, item.Important); var prop = current.Declarations.item.FirstOrDefault(f => f != null && f.propertyname == item.Property); if (prop != null) { current.Declarations.item.Remove(prop); current.Declarations.item.Add(prop); } } else { current.Declarations.removeProperty(item.Property); } } } var changelist = new List <CmsCssRuleChanges>(); foreach (var item in prechanges) { var onechange = new CmsCssRuleChanges(); onechange.CssRuleId = item.RuleId; onechange.selectorText = item.Selector; onechange.DeclarationText = item.Declarations.GenerateCssText(); if (item.Declarations.item.Count() == 0) { onechange.ChangeType = ChangeType.Delete; } else { if (item.RuleId == default(Guid)) { onechange.ChangeType = ChangeType.Add; } else { onechange.ChangeType = ChangeType.Update; } } changelist.Add(onechange); } context.WebSite.SiteDb().CssRules.UpdateStyle(changelist, StyleId); }
internal void UpdateStyleSheet(RenderContext context, Guid?styleId) { if (!styleId.HasValue) { return; } var rules = context.WebSite.SiteDb().CssRules.Query.Where(o => o.ParentStyleId == styleId).SelectAll(); var changelist = new List <CmsCssRuleChanges>(); if (!string.IsNullOrWhiteSpace(MediaRuleList)) { var mediaRuleId = rules.FirstOrDefault(f => CssSelectorComparer.IsEqual(f.SelectorText, MediaRuleList))?.Id; if (!mediaRuleId.HasValue) { rules = rules.Where(w => w.ParentCssRuleId == mediaRuleId).ToList(); } } rules = rules.Where(w => CssSelectorComparer.IsEqual(w.SelectorText, Selector)).ToList(); foreach (var rule in rules) { CSSDeclarationBlock declarations = Service.CssService.ParseStyleDeclaration(rule); if (declarations == null) { declarations = new CSSDeclarationBlock(); } if (!string.IsNullOrEmpty(Value)) { declarations.setProperty(Property, Value, Important); var prop = declarations.item.FirstOrDefault(f => f != null && f.propertyname == Property); if (prop != null) { declarations.item.Remove(prop); declarations.item.Add(prop); } } else { declarations.removeProperty(Property); } var onechange = new CmsCssRuleChanges { CssRuleId = rule.Id, selectorText = rule.SelectorText, DeclarationText = declarations.GenerateCssText() }; if (declarations.item.Count() == 0) { onechange.ChangeType = ChangeType.Delete; } else { if (rule.Id == default(Guid)) { onechange.ChangeType = ChangeType.Add; } else { onechange.ChangeType = ChangeType.Update; } } changelist.Add(onechange); } context.WebSite.SiteDb().CssRules.UpdateStyle(changelist, styleId.Value); }