1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
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";
35 /// Called by the framework so the component can
36 /// render its content
38 public override void Render()
40 StringWriter writer
= new StringWriter();
52 RenderText(writer
.ToString());
55 private void WriteSelect(StringWriter writer
)
57 if (Context
.HasSection(SelectSection
))
59 Context
.RenderSection(SelectSection
, writer
);
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
= "««";
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
= "»»";
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
= "«";
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
= "»";
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
)
137 writer
.Write(String
.Format("<span style=\"color: #DDD; padding: 2px 5px 2px 5px; margin: 2px; border: 1px solid #EEE;\">{0}</span>", text
));
141 writer
.Write(String
.Format("<span class=\"disabled\">{0}</span>", text
));
146 WritePageLink(writer
, index
, text
, null);
151 /// Writes the Page link.
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
);
173 if (PaginateFunction
!= null)
175 href
= "javascript:" + PaginateFunction
+ "(" + pageIndex
+ ");void(0);";
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
));
188 writer
.Write(String
.Format("<a href=\"{0}\">{1}</a>\r\n", href
, text
));