Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / ViewComponents / AbstractPaginationViewComponent.cs
blob646bf3628be373e3e0a4352aab3dd54d19fdf998
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.Collections;
18 using System.IO;
19 using Helpers;
21 /// <summary>
22 /// Pendent
23 /// </summary>
24 public abstract class AbstractPaginationViewComponent : ViewComponent
26 private const string StartSection = "startblock";
27 private const string EndSection = "endblock";
29 private string paginatefunction;
30 private object urlParam;
31 private IPaginatedPage page;
32 private UrlPartsBuilder urlPartsBuilder;
33 private bool usePathInfo;
34 private bool useInlineStyle = true;
35 private string pageParamName = "page";
37 /// <summary>
38 /// Gets or sets the paginated page instance.
39 /// </summary>
40 /// <value>The page.</value>
41 [ViewComponentParam(Required = true)]
42 public IPaginatedPage Page
44 get { return page; }
45 set { page = value; }
48 /// <summary>
49 /// Pendent
50 /// </summary>
51 /// <value>The name of the page param.</value>
52 [ViewComponentParam(Required = true)]
53 public string PageParamName
55 get { return pageParamName; }
56 set { pageParamName = value; }
59 /// <summary>
60 /// Gets or sets a value indicating whether the component should output inline styles.
61 /// </summary>
62 /// <value><c>true</c> if it should use inline styles; otherwise, <c>false</c>.</value>
63 [ViewComponentParam]
64 public bool UseInlineStyle
66 get { return useInlineStyle; }
67 set { useInlineStyle = value; }
70 /// <summary>
71 /// Pendent
72 /// </summary>
73 [ViewComponentParam]
74 public bool UsePathInfo
76 get { return usePathInfo; }
77 set { usePathInfo = value; }
80 /// <summary>
81 /// Gets or sets the paginate function name.
82 /// <para>
83 /// A paginate function is a javascript fuction
84 /// that receives the page index as the only argument.
85 /// </para>
86 /// </summary>
87 /// <value>The paginate function.</value>
88 [ViewComponentParam]
89 public string PaginateFunction
91 get { return paginatefunction; }
92 set { paginatefunction = value; }
95 /// <summary>
96 /// Gets or sets the URL to be used when generating links
97 /// </summary>
98 /// <value>The URL.</value>
99 [ViewComponentParam]
100 public object Url
102 get { return urlParam; }
103 set { urlParam = value; }
106 /// <summary>
107 /// Called by the framework once the component instance
108 /// is initialized
109 /// </summary>
110 public override void Initialize()
112 if (page == null)
114 throw new ViewComponentException("The DiggStylePagination requires a view component " +
115 "parameter named 'page' which should contain 'IPaginatedPage' instance");
118 // So when we render the blocks, the user might access the page
119 PropertyBag["page"] = page;
121 CreateUrlPartBuilder();
124 /// <summary>
125 /// Pendent
126 /// </summary>
127 /// <param name="writer">The writer.</param>
128 protected virtual void StartBlock(StringWriter writer)
130 if (Context.HasSection(StartSection))
132 Context.RenderSection(StartSection, writer);
134 else
136 if (useInlineStyle)
138 writer.Write("<div style=\"padding: 3px; margin: 3px; text-align: right; \">\r\n");
140 else
142 writer.Write("<div class=\"pagination\">\r\n");
147 /// <summary>
148 /// Pendent
149 /// </summary>
150 /// <param name="writer">The writer.</param>
151 protected virtual void EndBlock(StringWriter writer)
153 if (Context.HasSection(EndSection))
155 Context.RenderSection(EndSection, writer);
157 else
159 writer.Write("\r\n</div>\r\n");
164 /// <summary>
165 /// Pendent
166 /// </summary>
167 /// <param name="pageIndex">The page index.</param>
168 /// <returns></returns>
169 protected string CreateUrlForPage(int pageIndex)
171 if (usePathInfo)
173 urlPartsBuilder.PathInfoDict[pageParamName] = pageIndex.ToString();
175 else
177 urlPartsBuilder.QueryString.Remove(pageParamName);
178 urlPartsBuilder.QueryString[pageParamName] = pageIndex.ToString();
181 return urlPartsBuilder.BuildPathForLink(RailsContext.Server);
184 private void CreateUrlPartBuilder()
186 IDictionary urlParams = urlParam as IDictionary;
188 if (urlParams != null)
190 urlParams["encode"] = "true";
192 IUrlBuilder urlBuilder = RailsContext.GetService<IUrlBuilder>();
193 urlPartsBuilder = urlBuilder.CreateUrlPartsBuilder(RailsContext.UrlInfo, urlParams);
195 else
197 if (urlParam != null)
199 urlPartsBuilder = UrlPartsBuilder.Parse(urlParam.ToString());
201 else
203 urlPartsBuilder = new UrlPartsBuilder(RailsContext.Request.FilePath);