Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / ViewComponents / SelectStylePagination.cs
blob67a821c4b6efcbb35ce51c9cf5031222c61785b9
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.ViewComponents
17 using System;
18 using System.Collections;
19 using System.IO;
21 /// <summary>
22 /// Pendent
23 /// </summary>
24 [ViewComponentDetails("SelectStylePagination", Sections="startblock,endblock,first,last,next,prev,link,select")]
25 public class SelectStylePagination : AbstractPaginationViewComponent
27 private const string NextSection = "next";
28 private const string PrevSection = "prev";
29 private const string FirstSection = "first";
30 private const string LastSection = "last";
31 private const string LinkSection = "link";
32 private const string SelectSection = "select";
34 /// <summary>
35 /// Called by the framework so the component can
36 /// render its content
37 /// </summary>
38 public override void Render()
40 StringWriter writer = new StringWriter();
42 StartBlock(writer);
43 WriteFirst(writer);
44 WritePrev(writer);
46 WriteSelect(writer);
48 WriteNext(writer);
49 WriteLast(writer);
50 EndBlock(writer);
52 RenderText(writer.ToString());
55 private void WriteSelect(StringWriter writer)
57 if (Context.HasSection(SelectSection))
59 Context.RenderSection(SelectSection, writer);
61 else
63 writer.WriteLine("<select onchange=\"window.location.href = this.options[this.selectedIndex].value;\">");
65 for (int i = 1; i <= Page.LastIndex; i++)
67 string addition = Page.CurrentIndex == i ? " selected=\"true\"" : "";
68 writer.WriteLine("\t<option value=\"" + CreateUrlForPage(i) + "\"" + addition +">Page " + i + "</option>");
71 writer.WriteLine("</select>");
75 private void WriteFirst(StringWriter writer)
77 string caption = "&laquo;&laquo;";
79 if (Context.HasSection(FirstSection))
81 TextWriter capWriter = new StringWriter();
82 Context.RenderSection(FirstSection, capWriter);
83 caption = capWriter.ToString().Trim();
86 WriteLink(writer, Page.FirstIndex, caption, !Page.HasFirst);
89 private void WriteLast(StringWriter writer)
91 string caption = "&raquo;&raquo;";
93 if (Context.HasSection(LastSection))
95 TextWriter capWriter = new StringWriter();
96 Context.RenderSection(LastSection, capWriter);
97 caption = capWriter.ToString().Trim();
100 WriteLink(writer, Page.LastIndex, caption, !Page.HasLast);
103 private void WritePrev(StringWriter writer)
105 string caption = "&laquo;";
107 if (Context.HasSection(PrevSection))
109 TextWriter capWriter = new StringWriter();
110 Context.RenderSection(PrevSection, capWriter);
111 caption = capWriter.ToString().Trim();
114 WriteLink(writer, Page.PreviousIndex, caption, !Page.HasPrevious);
117 private void WriteNext(StringWriter writer)
119 string caption = "&raquo;";
121 if (Context.HasSection(NextSection))
123 TextWriter capWriter = new StringWriter();
124 Context.RenderSection(NextSection, capWriter);
125 caption = capWriter.ToString().Trim();
128 WriteLink(writer, Page.NextIndex, caption, !Page.HasNext);
131 private void WriteLink(TextWriter writer, int index, string text, bool disabled)
133 if (disabled)
135 if (UseInlineStyle)
137 writer.Write(String.Format("<span style=\"color: #DDD; padding: 2px 5px 2px 5px; margin: 2px; border: 1px solid #EEE;\">{0}</span>", text));
139 else
141 writer.Write(String.Format("<span class=\"disabled\">{0}</span>", text));
144 else
146 WritePageLink(writer, index, text, null);
150 /// <summary>
151 /// Writes the Page link.
152 /// </summary>
153 /// <param name="writer">The writer.</param>
154 /// <param name="pageIndex">Index of the page.</param>
155 /// <param name="text">The text.</param>
156 /// <param name="htmlAttributes">The HTML attributes.</param>
157 protected void WritePageLink(TextWriter writer, int pageIndex, String text, IDictionary htmlAttributes)
159 string url = CreateUrlForPage(pageIndex);
161 if (Context.HasSection(LinkSection))
163 PropertyBag["pageIndex"] = pageIndex;
164 PropertyBag["text"] = text;
165 PropertyBag["url"] = url;
167 Context.RenderSection(LinkSection, writer);
169 else
171 String href;
173 if (PaginateFunction != null)
175 href = "javascript:" + PaginateFunction + "(" + pageIndex + ");void(0);";
177 else
179 href = url;
182 if (UseInlineStyle)
184 writer.Write(String.Format("<a style=\"color: #000099;text-decoration: none;padding: 2px 5px 2px 5px;margin: 2px;border: 1px solid #AAAFEE;\" href=\"{0}\">{1}</a>\r\n", href, text));
186 else
188 writer.Write(String.Format("<a href=\"{0}\">{1}</a>\r\n", href, text));