PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Umbraco.Web/umbraco.presentation/umbraco/LiveEditing/Modules/SkinModule/SkinModule.cs

#
C# | 275 lines | 209 code | 61 blank | 5 comment | 31 complexity | 5d917edc6a3d915845615cf9f2de5819 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0, LGPL-2.1, MIT, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using umbraco.presentation.LiveEditing.Modules;
  6. using ClientDependency.Core;
  7. using System.Web.UI.WebControls;
  8. using umbraco.presentation.LiveEditing.Controls;
  9. using umbraco.IO;
  10. using System.Web.UI;
  11. using umbraco.cms.businesslogic.skinning;
  12. using ClientDependency.Core.Controls;
  13. using umbraco.presentation.umbraco.controls;
  14. using HtmlAgilityPack;
  15. using umbraco.cms.businesslogic.template;
  16. using System.Text;
  17. using System.IO;
  18. using System.Collections;
  19. namespace umbraco.presentation.umbraco.LiveEditing.Modules.SkinModule
  20. {
  21. [ClientDependency(200, ClientDependencyType.Javascript, "modal/modal.js", "UmbracoClient")]
  22. [ClientDependency(200, ClientDependencyType.Css, "modal/style.css", "UmbracoClient")]
  23. [ClientDependency(500, ClientDependencyType.Javascript, "LiveEditing/Modules/SkinModule/js/ModuleInjection.js", "UmbracoRoot")]
  24. [ClientDependency(800, ClientDependencyType.Javascript, "LiveEditing/Modules/SkinModule/js/disableInstallButtonsOnClick.js", "UmbracoRoot")]
  25. public class SkinModule : BaseModule
  26. {
  27. protected LabelButton m_SkinButton = new LabelButton();
  28. protected Panel m_SkinModal;
  29. protected LabelButton m_ModuleButton = new LabelButton();
  30. protected Panel m_ModuleModal;
  31. public SkinModule(LiveEditingManager manager)
  32. : base(manager)
  33. { }
  34. protected override void OnInit(EventArgs e)
  35. {
  36. base.OnInit(e);
  37. EnsureChildControls();
  38. }
  39. protected override void CreateChildControls()
  40. {
  41. base.CreateChildControls();
  42. Skin ActiveSkin = Skin.CreateFromAlias(Skinning.GetCurrentSkinAlias(NodeFactory.Node.GetCurrent().template));
  43. m_SkinModal = new Panel();
  44. m_SkinModal.ID = "LeSkinModal";
  45. m_SkinModal.Attributes.Add("style", "display: none");
  46. m_SkinModal.Controls.Add(new UserControl().LoadControl(String.Format("{0}/LiveEditing/Modules/SKinModule/SkinCustomizer.ascx", SystemDirectories.Umbraco)));
  47. Controls.Add(m_SkinModal);
  48. m_SkinButton.ID = "LeSkinButton";
  49. m_SkinButton.CssClass = "button";
  50. m_SkinButton.ToolTip = ActiveSkin != null && ActiveSkin.Dependencies.Count > 0 ? "Customize skin" : "Change skin";
  51. m_SkinButton.ImageUrl = String.Format("{0}/LiveEditing/Modules/SKinModule/images/skin.gif", SystemDirectories.Umbraco);
  52. string s = (ActiveSkin != null && ActiveSkin.Dependencies.Count > 0 ? "setTasksClientScripts();" : "") + "jQuery('#" + m_SkinModal.ClientID + @"').show();" + "jQuery('#" + m_SkinModal.ClientID + @"').ModalWindowShowWithoutBackground('" + ui.GetText("skin") + "',true,500,400,50,0, ['.modalbuton'], null);";
  53. m_SkinButton.OnClientClick = s +"return false;";
  54. Controls.Add(m_SkinButton);
  55. if (UmbracoContext.Current.LiveEditingContext.InSkinningMode && !string.IsNullOrEmpty(UmbracoContext.Current.Request["umbSkinningConfigurator"]))
  56. {
  57. ScriptManager.RegisterClientScriptBlock(
  58. this,
  59. this.GetType(),
  60. "ShowSkinModule",
  61. "function ShowSkinModule(){" + s + "}",
  62. true);
  63. ClientDependencyLoader.Instance.RegisterDependency(500, "LiveEditing/Modules/SkinModule/js/SkinModuleShowOnStartup.js", "UmbracoRoot", ClientDependencyType.Javascript);
  64. }
  65. // modules
  66. if (CanInsertModules(NodeFactory.Node.GetCurrent().template))
  67. {
  68. m_ModuleModal = new Panel();
  69. m_ModuleModal.ID = "LeModuleModal";
  70. m_ModuleModal.CssClass = "ModuleSelector";
  71. m_ModuleModal.Attributes.Add("style", "display: none");
  72. m_ModuleModal.Controls.Add(new UserControl().LoadControl(String.Format("{0}/LiveEditing/Modules/SKinModule/ModuleSelector.ascx", SystemDirectories.Umbraco)));
  73. Controls.Add(m_ModuleModal);
  74. m_ModuleButton.ID = "LeModuleButton";
  75. m_ModuleButton.CssClass = "button";
  76. m_ModuleButton.ToolTip = "Insert Module";
  77. m_ModuleButton.ImageUrl = String.Format("{0}/LiveEditing/Modules/SKinModule/images/module.gif", SystemDirectories.Umbraco);
  78. m_ModuleButton.OnClientClick = "umbShowModuleSelection();" + "return false;";
  79. Controls.Add(m_ModuleButton);
  80. }
  81. }
  82. private bool CanInsertModules(int template)
  83. {
  84. Template t = new Template(template);
  85. HtmlDocument doc = new HtmlDocument();
  86. doc.Load(t.MasterPageFile);
  87. if (doc.DocumentNode.SelectNodes(string.Format("//*[@class = '{0}']", "umbModuleContainer")) != null)
  88. return true;
  89. else
  90. {
  91. if (t.HasMasterTemplate)
  92. return CanInsertModules(t.MasterTemplate);
  93. else
  94. return false;
  95. }
  96. }
  97. protected override void Manager_MessageReceived(object sender, MesssageReceivedArgs e)
  98. {
  99. switch (e.Type)
  100. {
  101. case "injectmodule":
  102. //update template, insert macro tag
  103. InsertModule(NodeFactory.Node.GetCurrent().template, e.Message.Split(';')[0], e.Message.Split(';')[1], e.Message.Split(';')[2] == "prepend");
  104. break;
  105. case "movemodule":
  106. string moduleId = e.Message.Split(';')[0];
  107. string parentId = e.Message.Split(';')[1];
  108. int index = 0;
  109. int.TryParse(e.Message.Split(';')[2], out index);
  110. HtmlNode module = FindModule(NodeFactory.Node.GetCurrent().template, moduleId, false);
  111. if (module != null)
  112. {
  113. FindModule(NodeFactory.Node.GetCurrent().template, moduleId, true);
  114. MoveModule(NodeFactory.Node.GetCurrent().template, module, parentId, index);
  115. }
  116. break;
  117. case "removemodule":
  118. FindModule(NodeFactory.Node.GetCurrent().template, e.Message.Split(';')[0], true);
  119. break;
  120. }
  121. }
  122. private bool MoveModule(int template, HtmlNode module, string targetId, int index)
  123. {
  124. Template t = new Template(template);
  125. string TargetFile = t.MasterPageFile;
  126. string TargetID = targetId;
  127. HtmlDocument doc = new HtmlDocument();
  128. doc.Load(TargetFile);
  129. if (doc.DocumentNode.SelectNodes(string.Format("//*[@id = '{0}']", TargetID)) != null)
  130. {
  131. HtmlNode parent = doc.DocumentNode.SelectSingleNode(string.Format("//*[@id = '{0}']", TargetID));
  132. if (index > 0 && parent.ChildNodes.Count > 0)
  133. {
  134. parent.InsertAfter(module, parent.ChildNodes[index - 1]);
  135. }
  136. else if (index == 0 && parent.ChildNodes.Count > 0)
  137. {
  138. parent.InsertBefore(module, parent.ChildNodes[0]);
  139. }
  140. else
  141. {
  142. parent.AppendChild(module);
  143. //parent.ChildNodes.Add(module);
  144. }
  145. doc.Save(TargetFile);
  146. return true;
  147. }
  148. else
  149. {
  150. //might be on master template
  151. if (t.HasMasterTemplate)
  152. return MoveModule(template, module, targetId,index);
  153. else
  154. return false;
  155. }
  156. }
  157. private HtmlNode FindModule(int template, string id, bool remove)
  158. {
  159. Template t = new Template(template);
  160. string TargetFile = t.MasterPageFile;
  161. string TargetID = id;
  162. HtmlDocument doc = new HtmlDocument();
  163. doc.Load(TargetFile);
  164. if (doc.DocumentNode.SelectSingleNode(string.Format("//*[@id = '{0}']", TargetID)) != null)
  165. {
  166. if (!remove)
  167. return doc.DocumentNode.SelectSingleNode(string.Format("//*[@id = '{0}']", TargetID));
  168. else
  169. {
  170. HtmlNode r = doc.DocumentNode.SelectSingleNode(string.Format("//*[@id = '{0}']", TargetID)).Clone();
  171. doc.DocumentNode.SelectSingleNode(string.Format("//*[@id = '{0}']", TargetID)).RemoveAll();
  172. doc.Save(TargetFile);
  173. return r;
  174. }
  175. }
  176. else
  177. {
  178. if (t.HasMasterTemplate)
  179. return FindModule(template,id,remove);
  180. else
  181. return null;
  182. }
  183. }
  184. private bool InsertModule(int template, string targetId, string tag, bool prepend)
  185. {
  186. Template t = new Template(template);
  187. string TargetFile = t.MasterPageFile;
  188. string TargetID = targetId;
  189. HtmlDocument doc = new HtmlDocument();
  190. doc.Load(TargetFile);
  191. if (doc.DocumentNode.SelectNodes(string.Format("//*[@id = '{0}']", TargetID)) != null)
  192. {
  193. foreach (HtmlNode target in doc.DocumentNode.SelectNodes(string.Format("//*[@id = '{0}']", TargetID)))
  194. {
  195. HtmlNode macrotag = HtmlNode.CreateNode(tag);
  196. if (prepend)
  197. target.PrependChild(macrotag);
  198. else
  199. target.AppendChild(macrotag);
  200. }
  201. doc.Save(TargetFile);
  202. return true;
  203. }
  204. else
  205. {
  206. //might be on master template
  207. if (t.HasMasterTemplate)
  208. return InsertModule(t.MasterTemplate, targetId, tag, prepend);
  209. else
  210. return false;
  211. }
  212. }
  213. }
  214. }