PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Controls/PostCalendar.cs

#
C# | 299 lines | 202 code | 40 blank | 57 comment | 27 complexity | 1de1685c8d6654360c672fd6192c2e46 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <summary>
  3. // The Post Calendar
  4. // </summary>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace App_Code.Controls
  7. {
  8. using System;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Web;
  13. using System.Web.UI;
  14. using System.Web.UI.HtmlControls;
  15. using System.Web.UI.WebControls;
  16. using BlogEngine.Core;
  17. /// <summary>
  18. /// The Post Calendar
  19. /// </summary>
  20. public class PostCalendar : Calendar, ICallbackEventHandler
  21. {
  22. #region Constants and Fields
  23. /// <summary>
  24. /// The callback.
  25. /// </summary>
  26. private string callback;
  27. #endregion
  28. #region Properties
  29. /// <summary>
  30. /// Gets or sets a value indicating whether ShowPostTitles.
  31. /// </summary>
  32. public bool ShowPostTitles
  33. {
  34. get
  35. {
  36. return (bool)(this.ViewState["ShowPostTitles"] ?? default(bool));
  37. }
  38. set
  39. {
  40. this.ViewState["ShowPostTitles"] = value;
  41. }
  42. }
  43. #endregion
  44. #region Implemented Interfaces
  45. #region ICallbackEventHandler
  46. /// <summary>
  47. /// Returns the results of a callback event that targets a control.
  48. /// </summary>
  49. /// <returns>The result of the callback.</returns>
  50. public string GetCallbackResult()
  51. {
  52. return this.callback;
  53. }
  54. /// <summary>
  55. /// Processes a callback event that targets a control.
  56. /// </summary>
  57. /// <param name="eventArgument">A string that represents an event argument to pass to the event handler.</param>
  58. public void RaiseCallbackEvent(string eventArgument)
  59. {
  60. DateTime date;
  61. if (!DateTime.TryParse(eventArgument, out date))
  62. {
  63. return;
  64. }
  65. this.VisibleDate = date;
  66. this.ShowTitle = false;
  67. using (var sw = new StringWriter())
  68. {
  69. this.RenderControl(new HtmlTextWriter(sw));
  70. this.callback = sw.ToString();
  71. }
  72. }
  73. #endregion
  74. #endregion
  75. #region Methods
  76. /// <summary>
  77. /// Raises the <see cref="E:System.Web.UI.WebControls.Calendar.DayRender"/> event of the <see cref="T:System.Web.UI.WebControls.Calendar"/> control and allows you to provide a custom handler for the <see cref="E:System.Web.UI.WebControls.Calendar.DayRender"/> event.
  78. /// </summary>
  79. /// <param name="cell">A <see cref="T:System.Web.UI.WebControls.TableCell"/> that contains information about the cell to render.</param>
  80. /// <param name="day">A <see cref="T:System.Web.UI.WebControls.CalendarDay"/> that contains information about the day to render.</param>
  81. protected override void OnDayRender(TableCell cell, CalendarDay day)
  82. {
  83. if (day.IsToday)
  84. {
  85. cell.Attributes["id"] += "today";
  86. }
  87. var list = Post.GetPostsByDate(day.Date, day.Date);
  88. if (list.Count > 0)
  89. {
  90. cell.Controls.Clear();
  91. if (this.ShowPostTitles)
  92. {
  93. cell.Controls.Add(new LiteralControl(day.DayNumberText));
  94. foreach (var a in
  95. list.Where(post => post.IsVisible).Select(post => new HtmlAnchor { InnerHtml = string.Format("<br /><br />{0}", post.Title), HRef = post.RelativeLink }))
  96. {
  97. cell.Controls.Add(a);
  98. }
  99. }
  100. else
  101. {
  102. if (list[0].IsVisible)
  103. {
  104. var a = new HtmlAnchor
  105. {
  106. InnerHtml = day.DayNumberText,
  107. HRef =
  108. string.Format("{0}{1}/{2}/{3}/default{4}", Utils.RelativeWebRoot, day.Date.Year, day.Date.ToString("MM"), day.Date.ToString("dd"), BlogConfig.FileExtension)
  109. };
  110. a.Attributes["class"] = "exist";
  111. cell.Controls.Add(a);
  112. }
  113. else
  114. {
  115. cell.Text = day.DayNumberText;
  116. }
  117. }
  118. }
  119. else
  120. {
  121. cell.Controls.Clear();
  122. cell.Text = day.DayNumberText;
  123. }
  124. }
  125. /// <summary>
  126. /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
  127. /// </summary>
  128. /// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
  129. protected override void OnLoad(EventArgs e)
  130. {
  131. this.Page.ClientScript.GetCallbackEventReference(this, "arg", null, "context");
  132. base.OnLoad(e);
  133. }
  134. /// <summary>
  135. /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
  136. /// </summary>
  137. /// <param name="e">An <see cref="T:System.EventArgs"/> that contains event data.</param>
  138. protected override void OnPreRender(EventArgs e)
  139. {
  140. if (!this.Page.IsCallback && !this.Page.IsPostBack)
  141. {
  142. this.VisibleDate = DateTime.Now;
  143. }
  144. if (!this.Page.IsPostBack && this.Context.Request.QueryString["date"] != null)
  145. {
  146. DateTime date;
  147. if (DateTime.TryParse(this.Context.Request.QueryString["date"], out date))
  148. {
  149. this.VisibleDate = date;
  150. }
  151. }
  152. base.OnPreRender(e);
  153. if (!this.ShowPostTitles)
  154. {
  155. this.ShowTitle = false;
  156. }
  157. }
  158. /// <summary>
  159. /// Displays the <see cref="T:System.Web.UI.WebControls.Calendar"/> control on the client.
  160. /// </summary>
  161. /// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter"/> that contains the output stream for rendering on the client.</param>
  162. protected override void Render(HtmlTextWriter writer)
  163. {
  164. if (this.ShowPostTitles && !this.Page.IsCallback)
  165. {
  166. base.Render(writer);
  167. }
  168. else
  169. {
  170. if ((this.Page.IsPostBack && !this.Page.IsCallback) || this.VisibleDate == DateTime.MinValue)
  171. {
  172. this.VisibleDate = DateTime.Now;
  173. }
  174. writer.Write("<div id=\"calendarContainer\">");
  175. writer.Write("<table class=\"calendar\" summary=\"\" style=\";border-collapse:collapse;\">");
  176. writer.Write("<tr><td>");
  177. var oldest = GetOldestPostDate();
  178. if (this.VisibleDate.Year != oldest.Year || this.VisibleDate.Month != oldest.Month)
  179. {
  180. writer.Write("<a href=\"javascript:BlogEngine.Calendar.nav('{0}')\">{1}</a>&nbsp;&nbsp;", this.VisibleDate.AddMonths(-1).ToString("yyyy-MM-dd"), HttpUtility.HtmlEncode(this.PrevMonthText));
  181. }
  182. else
  183. {
  184. writer.Write("{0}&nbsp;&nbsp;", HttpUtility.HtmlEncode(this.PrevMonthText));
  185. }
  186. writer.Write("</td><td style=\"text-align:center;width:100px\">{0}</td><td align=\"right\">", this.VisibleDate.ToString("MMMM yyyy"));
  187. if (this.VisibleDate.Year != DateTime.Now.Year || this.VisibleDate.Month != DateTime.Now.Month)
  188. {
  189. writer.Write("&nbsp;&nbsp;<a href=\"javascript:BlogEngine.Calendar.nav('{0}')\">{1}</a>", this.VisibleDate.AddMonths(1).ToString("yyyy-MM-dd"), HttpUtility.HtmlEncode(this.NextMonthText));
  190. }
  191. else
  192. {
  193. writer.Write("&nbsp;&nbsp;{0}", HttpUtility.HtmlEncode(this.NextMonthText));
  194. }
  195. writer.Write("</td></tr>");
  196. writer.Write("</table>");
  197. this.Attributes.Add("summary", "Post calendar");
  198. try
  199. {
  200. base.Render(writer);
  201. }
  202. catch (Exception)
  203. {
  204. writer.Write("<a href=\"javascript:void(location.reload(true))\">Reload page</a>");
  205. }
  206. writer.Write("</div>");
  207. if (!this.Page.IsCallback)
  208. {
  209. writer.Write(this.Script());
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// Gets the oldest post date.
  215. /// </summary>
  216. /// <returns>The oldest post date.</returns>
  217. private static DateTime GetOldestPostDate()
  218. {
  219. return Post.Posts.Count > 0 ? Post.Posts[Post.Posts.Count - 1].DateCreated : DateTime.Now;
  220. }
  221. /// <summary>
  222. /// Scripts this instance.
  223. /// </summary>
  224. /// <returns>The script.</returns>
  225. private string Script()
  226. {
  227. var sb = new StringBuilder();
  228. sb.AppendFormat(
  229. @"<script type=""text/javascript"">
  230. function setupBlogEngineCalendar() {{
  231. BlogEngine.Calendar = {{
  232. months: {{}},
  233. nav: function(date) {{
  234. var m = BlogEngine.Calendar.months;
  235. if (m[date] == null || m[date] == 'undefined') {{
  236. {0}
  237. }} else {{
  238. BlogEngine.updateCalendar(m[date], date);
  239. }}
  240. }}
  241. }};
  242. }}
  243. </script>",
  244. this.Page.ClientScript.GetCallbackEventReference(this, "date", "BlogEngine.updateCalendar", "date"));
  245. /*
  246. ");
  247. sb.Append("var months = new Object();");
  248. sb.Append("function CalNav(date){");
  249. sb.Append("if (months[date] == null || months[date] == 'undefined')");
  250. sb.Append("{" + Page.ClientScript.GetCallbackEventReference(this, "date", "BlogEngine.updateCalendar", "date") + "}");
  251. sb.Append("else {BlogEngine.updateCalendar(months[date], date)}");
  252. sb.Append("}");
  253. sb.Append("</script>");
  254. */
  255. return sb.ToString();
  256. }
  257. #endregion
  258. }
  259. }