PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Website/Internal/Usercontrols/BlockDemo2.ascx.cs

https://github.com/WouterBos/graphite
C# | 395 lines | 286 code | 29 blank | 80 comment | 19 complexity | 18621f3640886ab2d64d9ead0152ee17 MD5 | raw file
  1. using Graphite;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Text.RegularExpressions;
  11. using System.Reflection;
  12. using System.Collections;
  13. public partial class GraphiteInternal_BlockDemo2 : System.Web.UI.UserControl
  14. {
  15. private Graphite.Internal.Config config;
  16. Dictionary<string, Boolean> dicFiles;
  17. private string strMenuItemName = "";
  18. private string path = "";
  19. protected void Page_Load(object sender, EventArgs e)
  20. {
  21. path = Request.ServerVariables["SCRIPT_PATH"];
  22. if (String.IsNullOrEmpty(Request.QueryString["type"]) == false)
  23. {
  24. strMenuItemName = Request.QueryString["type"];
  25. }
  26. CreateDemo();
  27. }
  28. private void CreateDemo()
  29. {
  30. // Getting configuration settings from XML
  31. config = new Graphite.Internal.Config(Graphite.Tools.GetXmlPath("demo"));
  32. dicFiles = config.Files(GetActiveIndex());
  33. // Run methods
  34. CreateMenu();
  35. GetDemoHTML();
  36. GetDemoCss();
  37. GetDemoJavaScript();
  38. GetDemoDescription();
  39. CreateSupportedBrowsersList();
  40. SetStageDimension();
  41. }
  42. private void SetStageDimension()
  43. {
  44. Dictionary<string, string> stageDimension = config.GetStageDimension(GetActiveIndex());
  45. if (stageDimension["width"] == "auto")
  46. {
  47. stageDimension["width"] = "100%";
  48. }
  49. iframe.Attributes["style"] += "; width: " + stageDimension["width"];
  50. iframe.Attributes["style"] += "; height: " + stageDimension["height"];
  51. iframe.Attributes["src"] = "/Internal/Pages/Demos/IframeDemo.aspx?demopath=" + Request.Url.LocalPath;
  52. }
  53. // Finds out which tab in the demo menu is selected. If no menu is visible above the demo, the index is always 0.
  54. private int GetActiveIndex()
  55. {
  56. int intMenuItemActive = 0;
  57. if (String.IsNullOrEmpty(Request.QueryString["type"]) == false)
  58. {
  59. intMenuItemActive = config.Index(strMenuItemName.ToLower());
  60. }
  61. if (intMenuItemActive == -1)
  62. {
  63. intMenuItemActive = 0;
  64. if (strMenuItemName.Length > 0)
  65. {
  66. litMessage.Text = @"<div class='gp_text'><span class='gp_textAlert'>" +
  67. @"Cannot find a type called &ldquo;" + strMenuItemName + @"&rdquo;, showing default type instead." +
  68. @"</span></div>";
  69. }
  70. }
  71. else
  72. {
  73. litMessage.Text = "";
  74. }
  75. return intMenuItemActive;
  76. }
  77. // Create tabs above demo.
  78. private void CreateMenu()
  79. {
  80. int intMenuItemActive = GetActiveIndex();
  81. ArrayList types = config.Types();
  82. if (types.Count > 0)
  83. {
  84. for (int i = 0; i < types.Count; i++)
  85. {
  86. HyperLink hlMenuLink = new HyperLink();
  87. ArrayList type = types[i] as ArrayList;
  88. hlMenuLink.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(type[1].ToString());
  89. hlMenuLink.NavigateUrl = Request.ServerVariables["SCRIPT_NAME"] + "?type=" + Server.HtmlEncode(type[0].ToString());
  90. if (strMenuItemName.ToLower() == type[0].ToString().ToLower())
  91. {
  92. hlMenuLink.CssClass = "active";
  93. }
  94. Types.Controls.Add(hlMenuLink);
  95. }
  96. }
  97. }
  98. // Returns source code of a demo file
  99. //private string GetSourceCode(string suffix, bool dicFiles)
  100. //{
  101. // string strFileName = "default";
  102. // if (dicFiles == false)
  103. // {
  104. // int intMenuItemActive = GetActiveIndex();
  105. // strFileName = config.Type(intMenuItemActive);
  106. // }
  107. // string strRoot = Server.MapPath(Request.ServerVariables["SCRIPT_PATH"]) + "\\";
  108. // StringBuilder sbCode = new StringBuilder();
  109. // try
  110. // {
  111. // System.IO.StreamReader sr = new System.IO.StreamReader(strRoot + strFileName + suffix);
  112. // string line;
  113. // while (sr.Peek() != -1)
  114. // {
  115. // line = sr.ReadLine();
  116. // sbCode.AppendLine(line);
  117. // }
  118. // sr.Close();
  119. // sr.Dispose();
  120. // }
  121. // catch (Exception exp)
  122. // {
  123. // //
  124. // }
  125. // return sbCode.ToString();
  126. //}
  127. // Gets description from file and prints it below demo block.
  128. private void GetDemoDescription()
  129. {
  130. if (dicFiles.ContainsKey("description") == true)
  131. {
  132. string strDescription = Graphite.Internal.Demo.GetSourceCode("-description.html", dicFiles["description"], GetActiveIndex(), path, config);
  133. litDescription.Text = strDescription;
  134. }
  135. else
  136. {
  137. litDescription.Text = "<p>No description available.</p>";
  138. }
  139. }
  140. // Gets codebehind of ASCX if any available.
  141. //private string GetCodeBehind(string ascxSource, string cssClass)
  142. //{
  143. // string strCode = "";
  144. // // Get URL to Codebehind
  145. // string strCodeBehindURL = "";
  146. // int intCodeFileStart = ascxSource.IndexOf("CodeFile=", StringComparison.OrdinalIgnoreCase);
  147. // int intIgnoreCodeFile = ascxSource.IndexOf("<!-- Graphite: Ignore Codefile -->", StringComparison.OrdinalIgnoreCase);
  148. // int intStartQuote = ascxSource.IndexOf("\"", intCodeFileStart);
  149. // int intEndQuote = ascxSource.IndexOf("\"", (intStartQuote + 1));
  150. // if (intStartQuote >= 0)
  151. // {
  152. // strCodeBehindURL = ascxSource.Substring((intStartQuote + 1), (intEndQuote - intStartQuote - 1));
  153. // }
  154. // string strRoot = Server.MapPath(Request.ServerVariables["SCRIPT_PATH"]) + "\\";
  155. // if (intIgnoreCodeFile >= 0)
  156. // {
  157. // // Get Codebehind code
  158. // try
  159. // {
  160. // StringBuilder sbCode = new StringBuilder();
  161. // System.IO.StreamReader sr = new System.IO.StreamReader(strRoot + strCodeBehindURL);
  162. // while (sr.Peek() != -1)
  163. // {
  164. // string line = sr.ReadLine();
  165. // sbCode.AppendLine(line);
  166. // }
  167. // sr.Close();
  168. // sr.Dispose();
  169. // strCode = sbCode.ToString();
  170. // // Insert class into private variable _strRootClass
  171. // int rootClassStart = strCode.IndexOf("_strRootClass", StringComparison.OrdinalIgnoreCase);
  172. // int rootClassValueStart = strCode.IndexOf("\"\"", rootClassStart);
  173. // strCode = strCode.Insert(rootClassValueStart + 1, config.CssClass(GetActiveIndex()));
  174. // }
  175. // catch (Exception exp)
  176. // {
  177. // // No Codebehind available
  178. // }
  179. // }
  180. // return strCode;
  181. //}
  182. // Change string in order to store it as a string in JavaScript
  183. private string WrapInJsString(string str) {
  184. string strReturn = str;
  185. strReturn = strReturn.Replace("'", "\\'");
  186. strReturn = strReturn.Replace("\n", "\\n");
  187. strReturn = strReturn.Replace("\r", "\\r");
  188. strReturn = strReturn.Replace("/", "\\/");
  189. return strReturn;
  190. }
  191. // Gets demo HTML which is either an HTML or an ASCX file (depends on config file).
  192. private void GetDemoHTML()
  193. {
  194. string strFileName = "default";
  195. if (dicFiles.ContainsKey("ascx") == true)
  196. {
  197. // File is ASCX
  198. if (dicFiles["ascx"] == false)
  199. {
  200. int intMenuItemActive = GetActiveIndex();
  201. strFileName = config.Type(intMenuItemActive);
  202. }
  203. // Load control
  204. Control ctrlControl = LoadControl(this.Parent.Page.TemplateSourceDirectory + "\\" + strFileName + ".ascx");
  205. PropertyInfo[] info = ctrlControl.GetType().GetProperties();
  206. // Set strRootClass property if available
  207. foreach (PropertyInfo item in info)
  208. {
  209. if (item.CanWrite)
  210. {
  211. switch (item.Name)
  212. {
  213. case "strRootClass":
  214. item.SetValue(ctrlControl, config.CssClass(GetActiveIndex()), null);
  215. break;
  216. }
  217. }
  218. }
  219. // Add ASCX control
  220. string strHtmlCode = Graphite.Internal.Demo.GetSourceCode(".ascx", dicFiles["ascx"], GetActiveIndex(), path, config);
  221. litDemoAscx.Text = WrapInJsString(strHtmlCode);
  222. // Find codebehind
  223. string strCodeBehind = Graphite.Internal.Demo.GetCodeBehind(strHtmlCode, config.CssClass(GetActiveIndex()), path, config, GetActiveIndex());
  224. litDemoCodeBehind.Text = WrapInJsString(strCodeBehind);
  225. if (strCodeBehind == "") {
  226. phCodeLinksCodeBehind.Visible = false;
  227. }
  228. // Hide HTML placeholder
  229. phCodeLinksHtml.Visible = false;
  230. }
  231. else if (dicFiles.ContainsKey("html") == true)
  232. {
  233. // File is HTML
  234. string strHtmlCode = Graphite.Internal.Demo.GetSourceCode(".html", dicFiles["html"], GetActiveIndex(), path, config);
  235. strHtmlCode = strHtmlCode.Replace("###GP_BLOCK_TYPE###", config.CssClass(GetActiveIndex())); // Set HTML CSS class
  236. Literal litControl = new Literal();
  237. litControl.Text = strHtmlCode;
  238. // Add HTML to demo block
  239. litDemoHtml.Text = WrapInJsString(strHtmlCode);
  240. // Get demo code for copy/paste
  241. if (dicFiles["externalDemo"] == true)
  242. {
  243. strHtmlCode = Graphite.Internal.Demo.GetSourceCode("-external.html", dicFiles["html"], GetActiveIndex(), path, config);
  244. }
  245. // Hide ASCX and Codebehind block
  246. phCodeLinksAscx.Visible = false;
  247. phCodeLinksCodeBehind.Visible = false;
  248. }
  249. else
  250. {
  251. // There's no HTML. Hide all HTML-related blocks
  252. phCodeLinksHtml.Visible = false;
  253. phCodeLinksAscx.Visible = false;
  254. phCodeLinksCodeBehind.Visible = false;
  255. }
  256. }
  257. // Gets CSS code and prints it in demo if available
  258. private void GetDemoCss()
  259. {
  260. if (dicFiles.ContainsKey("css") == true)
  261. {
  262. string strCssCode = Graphite.Internal.Demo.GetSourceCode(".scss", dicFiles["css"], GetActiveIndex(), path, config);
  263. if (dicFiles["externalDemo"] == false)
  264. {
  265. string strCssLink;
  266. if (dicFiles["css"] == true)
  267. {
  268. strCssLink = "default.css";
  269. }
  270. else
  271. {
  272. strCssLink = config.Type(GetActiveIndex()) + ".scss";
  273. }
  274. CSSLink.Attributes["href"] = strCssLink;
  275. }
  276. string strStartBaseComment = "/* Start base */";
  277. string strEndBaseComment = "/* End base */";
  278. int intStartBase = strCssCode.IndexOf(strStartBaseComment);
  279. int intEndbase = strCssCode.IndexOf(strEndBaseComment);
  280. if (intStartBase >= 0 && intEndbase >= 0)
  281. {
  282. strCssCode = strCssCode.Substring(intEndbase + strStartBaseComment.Length);
  283. }
  284. litDemoCss.Text = WrapInJsString(strCssCode);
  285. }
  286. else
  287. {
  288. phCodeLinksLess.Visible = false;
  289. }
  290. }
  291. // Gets JavaScript code and prints it in demo if available
  292. private void GetDemoJavaScript()
  293. {
  294. if (dicFiles.ContainsKey("javascript") == true)
  295. {
  296. string strJsCode = Graphite.Internal.Demo.GetSourceCode("-js.html", dicFiles["javascript"], GetActiveIndex(), path, config);
  297. strJsCode = WrapInJsString(strJsCode);
  298. strJsCode = strJsCode.Replace("<script", "###GP###SCRIPT");
  299. strJsCode = strJsCode.Replace("</script", "###GP###/SCRIPT");
  300. litDemoJavaScript.Text = strJsCode;
  301. }
  302. else
  303. {
  304. phCodeLinksJs.Visible = false;
  305. }
  306. }
  307. // Creates list of browsers the block supports
  308. private void CreateSupportedBrowsersList()
  309. {
  310. Dictionary<string, string> dicSupportedBrowsers = config.SupportedBrowsers(GetActiveIndex());
  311. string[] strAllBrowsers = new string[] {
  312. "msie",
  313. "firefox",
  314. "chrome",
  315. "safari",
  316. "opera",
  317. "ipad",
  318. "iphone",
  319. "android",
  320. "windowsphone",
  321. "blackberry"
  322. };
  323. StringBuilder sbBrowserList = new StringBuilder();
  324. sbBrowserList.AppendLine("<ul class='graphite_browser'>");
  325. for (int i = 0; i <= strAllBrowsers.GetUpperBound(0); i++)
  326. {
  327. string strBrowserVersion = "";
  328. string strUnsupported = " class='graphite_browserUnsupported'";
  329. // ES_TODO: Remove this ugly "try"
  330. try
  331. {
  332. strBrowserVersion = dicSupportedBrowsers[strAllBrowsers[i].ToString()];
  333. strUnsupported = "";
  334. }
  335. catch (Exception exp)
  336. {
  337. }
  338. string browserName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(strAllBrowsers[i]);
  339. sbBrowserList.AppendLine(" <li" + strUnsupported + ">");
  340. sbBrowserList.AppendLine(" <strong class='graphite_browserIcon graphite_browser" + browserName + "'>" + browserName + "</strong>");
  341. sbBrowserList.AppendLine(" <span class='graphite_browserVersion'>" + strBrowserVersion + "</span>");
  342. sbBrowserList.AppendLine(" </li>");
  343. }
  344. sbBrowserList.AppendLine("</ul>");
  345. litBrowserList.Text = sbBrowserList.ToString();
  346. }
  347. }