From 7070fbf8ba4c800815696bb8225c0af3161b5bf9 Mon Sep 17 00:00:00 2001 From: "DK\\ala" Date: Thu, 30 Mar 2017 15:52:49 +0300 Subject: [PATCH] first commit --- src/Sitecore.Support.95158.sln | 22 ++ .../Include/zzz/Sitecore.Support.95158.config | 9 + .../Processors/EvaluateSorting.cs | 113 ++++++ .../Properties/AssemblyInfo.cs | 6 + .../Sitecore.Support.95158.csproj | 90 +++++ src/Sitecore.Support.95158/packages.config | 4 + .../Page Modes/LayoutDefinition.js | 321 ++++++++++++++++++ src/Sitecore.Support.95158/web.config | 4 + 8 files changed, 569 insertions(+) create mode 100644 src/Sitecore.Support.95158.sln create mode 100644 src/Sitecore.Support.95158/App_Config/Include/zzz/Sitecore.Support.95158.config create mode 100644 src/Sitecore.Support.95158/Pipelines/InsertRenderings/Processors/EvaluateSorting.cs create mode 100644 src/Sitecore.Support.95158/Properties/AssemblyInfo.cs create mode 100644 src/Sitecore.Support.95158/Sitecore.Support.95158.csproj create mode 100644 src/Sitecore.Support.95158/packages.config create mode 100644 src/Sitecore.Support.95158/sitecore/shell/Applications/Page Modes/LayoutDefinition.js create mode 100644 src/Sitecore.Support.95158/web.config diff --git a/src/Sitecore.Support.95158.sln b/src/Sitecore.Support.95158.sln new file mode 100644 index 0000000..01b1b3f --- /dev/null +++ b/src/Sitecore.Support.95158.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25123.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{CE871B41-FB3B-4F88-91C2-B7F4C3B0105D}") = "Sitecore.Support.95158", "Sitecore.Support.95158\Sitecore.Support.95158.csproj", "{1941A4FF-2693-4906-9F3C-120F6A2C83A2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1941A4FF-2693-4906-9F3C-120F6A2C83A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1941A4FF-2693-4906-9F3C-120F6A2C83A2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1941A4FF-2693-4906-9F3C-120F6A2C83A2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1941A4FF-2693-4906-9F3C-120F6A2C83A2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/Sitecore.Support.95158/App_Config/Include/zzz/Sitecore.Support.95158.config b/src/Sitecore.Support.95158/App_Config/Include/zzz/Sitecore.Support.95158.config new file mode 100644 index 0000000..a3b1273 --- /dev/null +++ b/src/Sitecore.Support.95158/App_Config/Include/zzz/Sitecore.Support.95158.config @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/src/Sitecore.Support.95158/Pipelines/InsertRenderings/Processors/EvaluateSorting.cs b/src/Sitecore.Support.95158/Pipelines/InsertRenderings/Processors/EvaluateSorting.cs new file mode 100644 index 0000000..efec4e1 --- /dev/null +++ b/src/Sitecore.Support.95158/Pipelines/InsertRenderings/Processors/EvaluateSorting.cs @@ -0,0 +1,113 @@ +using Sitecore.Data.Fields; +using Sitecore.Data.Items; +using Sitecore.Diagnostics; +using Sitecore.Layouts; +using Sitecore.Pipelines.InsertRenderings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml; + +namespace Sitecore.Support.Pipelines.InsertRenderings.Processors +{ + internal class EvaluateSorting : InsertRenderingsProcessor + { + protected virtual void Evaluate(InsertRenderingsArgs args, Item item) + { + new List(args.Renderings); + if (item == null) + { + return; + } + LayoutField layoutField = item.Fields[FieldIDs.LayoutField]; + if (!layoutField.InnerField.ContainsStandardValue && !string.IsNullOrEmpty(layoutField.InnerField.Value)) + { + XmlDocument xmlDocument = new XmlDocument(); + xmlDocument.LoadXml(layoutField.InnerField.Value); + string deviceId = Context.Device.ID.ToString(); + XmlNode xmlNode = null; + this.GetDevice(xmlDocument, deviceId, ref xmlNode); + if (xmlNode != null) + { + List> sorting = this.GetSorting(xmlNode); + sorting.Sort(new Comparison>(this.CompareByKey)); + Dictionary sortingDictionary = sorting.ToDictionary((KeyValuePair keyItem) => keyItem.Key, (KeyValuePair valueItem) => valueItem.Value); + using (Dictionary.KeyCollection.Enumerator enumerator = sortingDictionary.Keys.GetEnumerator()) + { + while (enumerator.MoveNext()) + { + string key = enumerator.Current; + RenderingReference renderingReference = args.Renderings.Find((RenderingReference x) => x.UniqueId == key); + RenderingReference renderingReference2 = args.Renderings.Find((RenderingReference x) => x.UniqueId == sortingDictionary[key]); + if (renderingReference != null && renderingReference2 != null) + { + args.Renderings.Remove(renderingReference); + int index = args.Renderings.IndexOf(renderingReference2); + args.Renderings.Insert(index, renderingReference); + } + } + } + } + } + } + + private int CompareByKey(KeyValuePair x, KeyValuePair y) + { + if (!(x.Key == y.Value)) + { + return 0; + } + return 1; + } + + protected virtual void GetDevice(XmlNode parentNode, string deviceId, ref XmlNode device) + { + if (parentNode.Name == "d") + { + device = parentNode; + return; + } + foreach (XmlNode parentNode2 in parentNode.ChildNodes) + { + this.GetDevice(parentNode2, deviceId, ref device); + } + } + + protected virtual List> GetSorting(XmlNode device) + { + List> list = new List>(); + foreach (XmlNode xmlNode in device.ChildNodes) + { + if (xmlNode.Attributes != null) + { + string text = (xmlNode.Attributes["p:before"] != null) ? xmlNode.Attributes["p:before"].Value : null; + string text2 = (xmlNode.Attributes["uid"] != null) ? xmlNode.Attributes["uid"].Value : null; + if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2)) + { + int num = text.IndexOf('{'); + int num2 = text.IndexOf('}'); + if (num > 0 && num2 > num) + { + text = text.Substring(num, num2 - num + 1); + list.Add(new KeyValuePair(text2, text)); + } + } + } + } + return list; + } + + public override void Process(InsertRenderingsArgs args) + { + Assert.ArgumentNotNull(args, "args"); + if (args.HasRenderings && Context.Database != Client.CoreDatabase) + { + Item contextItem = args.ContextItem; + if (contextItem != null) + { + this.Evaluate(args, contextItem); + } + } + } + } +} diff --git a/src/Sitecore.Support.95158/Properties/AssemblyInfo.cs b/src/Sitecore.Support.95158/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..890819e --- /dev/null +++ b/src/Sitecore.Support.95158/Properties/AssemblyInfo.cs @@ -0,0 +1,6 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Sitecore.Support.95158")] +[assembly: AssemblyProduct("Sitecore.Support.95158")] +[assembly: ComVisible(false)] diff --git a/src/Sitecore.Support.95158/Sitecore.Support.95158.csproj b/src/Sitecore.Support.95158/Sitecore.Support.95158.csproj new file mode 100644 index 0000000..d867ad0 --- /dev/null +++ b/src/Sitecore.Support.95158/Sitecore.Support.95158.csproj @@ -0,0 +1,90 @@ + + + + + Debug + AnyCPU + + + 2.0 + {1941A4FF-2693-4906-9F3C-120F6A2C83A2} + {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + Sitecore.Support + Sitecore.Support.95158 + v4.5 + true + + + + + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + ..\packages\SC.Sitecore.Kernel.7.2.5\lib\Sitecore.Kernel.dll + False + + + + + + + + + + + + + + + + + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + + True + True + 0 + / + http://localhost:49605/ + False + False + + + False + + + + + \ No newline at end of file diff --git a/src/Sitecore.Support.95158/packages.config b/src/Sitecore.Support.95158/packages.config new file mode 100644 index 0000000..c4085c8 --- /dev/null +++ b/src/Sitecore.Support.95158/packages.config @@ -0,0 +1,4 @@ + + + + diff --git a/src/Sitecore.Support.95158/sitecore/shell/Applications/Page Modes/LayoutDefinition.js b/src/Sitecore.Support.95158/sitecore/shell/Applications/Page Modes/LayoutDefinition.js new file mode 100644 index 0000000..26e6fc3 --- /dev/null +++ b/src/Sitecore.Support.95158/sitecore/shell/Applications/Page Modes/LayoutDefinition.js @@ -0,0 +1,321 @@ +Sitecore.LayoutDefinition = new function () { +}; + +Sitecore.LayoutDefinition.insert = function (placeholderKey, id) { + var layoutDefinition = this.getLayoutDefinition(); + var device = this.getDevice(layoutDefinition); + + var r = new Object(); + r["@id"] = id; + r["@ph"] = placeholderKey; + + device.r.splice(0, 0, r); + + this.setLayoutDefinition(layoutDefinition); +}; + +Sitecore.LayoutDefinition.getRendering = function (uid) { + var layoutDefinition = this.getLayoutDefinition(); + var device = this.getDevice(layoutDefinition); + if (!device) { + return null; + } + + for (var n = 0; n < device.r.length; n++) { + if (this.getShortID(device.r[n]["@uid"]) == uid) { + return device.r[n]; + } + } +}; + +Sitecore.LayoutDefinition.remove = function (uid) { + var layoutDefinition = this.getLayoutDefinition(); + var device = this.getDevice(layoutDefinition); + + this.removeRendering(device, uid); + this.setLayoutDefinition(layoutDefinition); +}; + +Sitecore.LayoutDefinition.removeRendering = function (device, uid) { + for (n = 0; n < device.r.length; n++) { + if (this.getShortID(device.r[n]["@uid"]) == uid) { + var r = device.r[n]; + device.r.splice(n, 1); + return r; + } + } + return null; +}; + +Sitecore.LayoutDefinition.moveToPosition = function (uid, placeholderKey, position) { + var layoutDefinition = this.getLayoutDefinition(); + var device = this.getDevice(layoutDefinition); + var originalPosition = this._getRenderingPositionInPlaceholder(device, placeholderKey, uid, placeholderKey); + var r = this.removeRendering(device, uid); + if (r == null) { + return; + } + + if (position == 0) { + Sitecore.LayoutDefinition.handleRelatedRenderings(placeholderKey, r, device.r); + if (originalPosition == -1) { + r["@ph"] = placeholderKey; + } + + device.r.splice(0, 0, r); + this.setLayoutDefinition(layoutDefinition); + return; + } + // Rendering is moving down inside the same placeholder. Decrement the real position, because rendering itself is removed + // from his original position. + if (originalPosition > -1 && originalPosition < position) { + position--; + } + + var placeholderWiseCount = 0; + var placeholderWiseCountEmpty = 0; + for (var totalCount = 0; totalCount < device.r.length; totalCount++) { + var rendering = device.r[totalCount]; + var renderingPlaceholder = rendering["@ph"]; + if (renderingPlaceholder == '' && placeholderWiseCount > 0) { + placeholderWiseCountEmpty++; + } + + if (Sitecore.PageModes.Utility.areEqualPlaceholders(renderingPlaceholder, placeholderKey)) { + placeholderWiseCount++; + } + + if (placeholderWiseCountEmpty + placeholderWiseCount == position) { + r["@ph"] = placeholderKey; // patch 414405 + Sitecore.LayoutDefinition.handleRelatedRenderings(placeholderKey, rendering, device.r); + device.r.splice(totalCount + 1, 0, r); + break; + } + } + + this.setLayoutDefinition(layoutDefinition); +}; + +Sitecore.LayoutDefinition.handleRelatedRenderings = function (newPlaceholder, renderingToMove, renderings) { + if (!renderingToMove) { + return; + } + + if (!renderings || renderings.length == 0) { + return; + } + + if (newPlaceholder == "") { + return; + } + + var chrome = $sc.first(Sitecore.PageModes.ChromeManager.chromes(), function () { + if (!this.type || typeof this.type.uniqueId != 'function') { + return false; + } + + return this.type.uniqueId() === $sc.toShortId(renderingToMove["@uid"]); + }); + + if (!chrome) { + return; + } + + var relatedRenderings = Sitecore.LayoutDefinition.getRelatedRenderingsUID(chrome); + + for (var i = 0; i < renderings.length; i++) { + var r = renderings[i]; + if (relatedRenderings.indexOf($sc.toShortId(r["@uid"])) == -1) { + continue; + } + + var renderingPlaceholder = Sitecore.LayoutDefinition.checkPlaceholderPath(r["@ph"]); + var movedRenderingPlaceholder = Sitecore.LayoutDefinition.checkPlaceholderPath(renderingToMove["@ph"]); + + if (renderingPlaceholder.match("^" + movedRenderingPlaceholder)) { + r["@ph"] = r["@ph"].replace(renderingToMove["@ph"], newPlaceholder) + } + } +}; + +Sitecore.LayoutDefinition.checkPlaceholderPath = function (placeholder) { + if (placeholder.indexOf('/') != 0) { + return '/' + placeholder; + } + + return placeholder; +}; + +Sitecore.LayoutDefinition.getRelatedRenderingsUID = function (chrome) { + var renderingsUid = []; + var childChromes = chrome.getChildChromes(function () { + return this.type && typeof this.type.uniqueId == 'function'; + }, true); + + $sc.each(childChromes, function () { + renderingsUid.push(this.type.uniqueId().toString()); + renderingsUid = renderingsUid.concat(Sitecore.LayoutDefinition.getRelatedRenderingsUID(this)); + }); + + return renderingsUid; +}; + +Sitecore.LayoutDefinition.getRenderingConditions = function (renderingUid) { + if (!Sitecore.PageModes.Personalization) { + return []; + } + + var layoutDefinition = this.getLayoutDefinition(); + var device = this.getDevice(layoutDefinition); + var conditions = []; + for (var i = 0; i < device.r.length; i++) { + if (this.getShortID(device.r[i]["@uid"]) == renderingUid && device.r[i].rls) { + var rules = device.r[i].rls.ruleset; + if (rules && rules.rule) { + if (!$sc.isArray(rules.rule)) { + rules.rule = [rules.rule]; + } + + for (var j = 0; j < rules.rule.length; j++) { + var conditionId = rules.rule[j]["@uid"]; + var isActive = Sitecore.PageModes.Personalization.ConditionStateStorage.isConditionActive(renderingUid, conditionId); + conditions.push(new Sitecore.PageModes.Personalization.Condition( + conditionId, + rules.rule[j]["@name"], + isActive + )); + } + } + } + } + + return conditions; +}; + +Sitecore.LayoutDefinition.GetConditionById = function (conditionId) { + var layoutDefinition = this.getLayoutDefinition(); + var device = this.getDevice(layoutDefinition); + for (var i = 0; i < device.r.length; i++) { + var rules = device.r[i].rls ? device.r[i].rls.ruleset : null; + if (rules && rules.rule) { + if (!$sc.isArray(rules.rule)) { + rules.rule = [rules.rule]; + } + + for (var j = 0; j < rules.rule.length; j++) { + if (rules.rule[j]["@uid"] == conditionId) { + return { rule: rules.rule[j] }; + } + } + } + } + + return {}; +}; + +Sitecore.LayoutDefinition.getRenderingIndex = function (placeholderKey, index) { + var layoutDefinition = this.getLayoutDefinition(); + var device = this.getDevice(layoutDefinition); + + var i = 0; + + for (n = 0; n < device.r.length; n++) { + if (device.r[n]["@ph"] == placeholderKey) { + if (i == index) { + return n; + } + + i++; + } + } + + return -1; +}; + +Sitecore.LayoutDefinition.getRenderingPositionInPlaceholder = function (placeholderKey, uid) { + var layoutDefinition = this.getLayoutDefinition(); + var device = this.getDevice(layoutDefinition); + return this._getRenderingPositionInPlaceholder(device, placeholderKey, uid); +}; + +Sitecore.LayoutDefinition.getLayoutDefinition = function () { + return JSON.parse($sc("#scLayout").val()); +}; + +Sitecore.LayoutDefinition.setLayoutDefinition = function (layoutDefinition) { + var newValue = $sc.type(layoutDefinition) === "string" ? layoutDefinition : JSON.stringify(layoutDefinition); + if ($sc("#scLayout").val() != newValue) { + $sc("#scLayout").val(newValue); + Sitecore.PageModes.PageEditor.setModified(true); + } +}; + +Sitecore.LayoutDefinition.getDeviceID = function () { + return $sc("#scDeviceID").val(); +}; + +Sitecore.LayoutDefinition.getDevice = function (layoutDefinition) { + var deviceID = this.getDeviceID(); + + if (!layoutDefinition.r.d) { + return null; + } + + //By serialization behaivour. If there is single element- it would not be serialized as array + if (!layoutDefinition.r.d.length) { + layoutDefinition.r.d = [layoutDefinition.r.d]; + } + + var list = layoutDefinition.r.d; + + for (var n = 0; n < list.length; n++) { + var d = list[n]; + + var id = this.getShortID(d["@id"]); + + if (id == deviceID) { + //By serialization behaivour. If there is single element- it would not be serialized as array + if (d.r && !d.r.length) { + d.r = [d.r]; + } + return d; + } + } + + return null; +}; + +Sitecore.LayoutDefinition.getShortID = function (id) { + return id.substr(1, 8) + id.substr(10, 4) + id.substr(15, 4) + id.substr(20, 4) + id.substr(25, 12); +}; + +Sitecore.LayoutDefinition.readLayoutFromRibbon = function () { + var layout = Sitecore.PageModes.PageEditor.ribbon().contentWindow.$("scLayoutDefinition").value; + if (layout && layout.length > 0) { + this.setLayoutDefinition(layout); + return true; + } + + return false; +}; + +Sitecore.LayoutDefinition._getRenderingPositionInPlaceholder = function (device, placeholderKey, uid, defaultPlaceholderKey) { + var counter = 0; + for (var i = 0; i < device.r.length; i++) { + var devicePlaceholder = device.r[i]["@ph"]; + if (devicePlaceholder == '' && defaultPlaceholderKey) { + devicePlaceholder = defaultPlaceholderKey; + } + + if (device.r[i]["@ph"] == "" || Sitecore.PageModes.Utility.areEqualPlaceholders(devicePlaceholder, placeholderKey)) { + if (this.getShortID(device.r[i]["@uid"]) == uid) { + return counter; + } + + counter++; + } + } + + return -1; +}; + diff --git a/src/Sitecore.Support.95158/web.config b/src/Sitecore.Support.95158/web.config new file mode 100644 index 0000000..7e1d425 --- /dev/null +++ b/src/Sitecore.Support.95158/web.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file