setting all protected methods on WizardStepPage as virtual
[castle.git] / MonoRail / Castle.MonoRail.Framework / ViewComponents / AbstractPaginationViewComponent.cs
blob421b0607a07b0186d6c5fda160573e0c523c95e0
1 // Copyright 2004-2008 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;
20 using Services;
22 /// <summary>
23 /// Pendent
24 /// </summary>
25 public abstract class AbstractPaginationViewComponent : ViewComponent
27 private const string StartSection = "startblock";
28 private const string EndSection = "endblock";
30 private string paginatefunction;
31 private string paginatefunctionFixedArgs;
32 private object urlParam;
33 private IPaginatedPage page;
34 private UrlParts urlParts;
35 private bool usePathInfo;
36 private bool useInlineStyle = true;
37 private bool preserveQueryString = false;
38 private string pageParamName = "page";
40 /// <summary>
41 /// Gets or sets the paginated page instance.
42 /// </summary>
43 /// <value>The page.</value>
44 [ViewComponentParam(Required = true)]
45 public IPaginatedPage Page
47 get { return page; }
48 set { page = value; }
51 /// <summary>
52 /// Pendent
53 /// </summary>
54 /// <value>The name of the page param.</value>
55 [ViewComponentParam(Required = true)]
56 public string PageParamName
58 get { return pageParamName; }
59 set { pageParamName = value; }
62 /// <summary>
63 /// Gets or sets a value indicating whether the component should output inline styles.
64 /// </summary>
65 /// <value><c>true</c> if it should use inline styles; otherwise, <c>false</c>.</value>
66 [ViewComponentParam]
67 public bool UseInlineStyle
69 get { return useInlineStyle; }
70 set { useInlineStyle = value; }
73 /// <summary>
74 /// Pendent
75 /// </summary>
76 [ViewComponentParam]
77 public bool UsePathInfo
79 get { return usePathInfo; }
80 set { usePathInfo = value; }
83 /// <summary>
84 /// Gets or sets the paginate function name.
85 /// <para>
86 /// A paginate function is a javascript fuction
87 /// that receives the page index as the only argument.
88 /// </para>
89 /// </summary>
90 /// <value>The paginate function.</value>
91 [ViewComponentParam]
92 public string PaginateFunction
94 get { return paginatefunction; }
95 set { paginatefunction = value; }
98 /// <summary>
99 /// Gets or sets the js paginate function fixed args.
100 /// </summary>
101 /// <value>The paginate function fixed args.</value>
102 [ViewComponentParam]
103 public string PaginatefunctionFixedArgs
105 get { return paginatefunctionFixedArgs; }
106 set { paginatefunctionFixedArgs = value; }
109 /// <summary>
110 /// Gets or sets the URL to be used when generating links
111 /// </summary>
112 /// <value>The URL.</value>
113 [ViewComponentParam]
114 public object Url
116 get { return urlParam; }
117 set { urlParam = value; }
121 /// <summary>
122 /// Gets or sets a value indicating whether the component should render existing query string arguments and overwrite page parameter
123 /// </summary>
124 /// <remarks>
125 /// <para>
126 /// Default value is <c>false</c>
127 /// </para>
128 /// <para>
129 /// Only has effect when <see cref="Url"/> or <see cref="PaginateFunction"/> are not defined
130 /// </para>
131 /// </remarks>
132 /// <value>
133 /// <c>true</c> if it should preserve existing query string arguments; otherwise <c>false</c>.
134 /// </value>
135 [ViewComponentParam]
136 public bool PreserveQueryString
138 get { return preserveQueryString; }
139 set { preserveQueryString = value; }
142 /// <summary>
143 /// Called by the framework once the component instance
144 /// is initialized
145 /// </summary>
146 public override void Initialize()
148 if (page == null)
150 throw new ViewComponentException("The DiggStylePagination requires a view component " +
151 "parameter named 'page' which should contain 'IPaginatedPage' instance");
154 // So when we render the blocks, the user might access the page
155 PropertyBag["page"] = page;
157 CreateUrlPartBuilder();
160 /// <summary>
161 /// Pendent
162 /// </summary>
163 /// <param name="writer">The writer.</param>
164 protected virtual void StartBlock(StringWriter writer)
166 if (Context.HasSection(StartSection))
168 Context.RenderSection(StartSection, writer);
170 else
172 if (useInlineStyle)
174 writer.Write("<div style=\"padding: 3px; margin: 3px; text-align: right; \">\r\n");
176 else
178 writer.Write("<div class=\"pagination\">\r\n");
183 /// <summary>
184 /// Pendent
185 /// </summary>
186 /// <param name="writer">The writer.</param>
187 protected virtual void EndBlock(StringWriter writer)
189 if (Context.HasSection(EndSection))
191 Context.RenderSection(EndSection, writer);
193 else
195 writer.Write("\r\n</div>\r\n");
200 /// <summary>
201 /// Compute url for given page index.
202 /// </summary>
203 /// <param name="pageIndex">The page index.</param>
204 /// <returns>return the computed url for given page index.</returns>
205 protected virtual string CreateUrlForPage(int pageIndex)
207 if (usePathInfo)
209 urlParts.PathInfoDict[pageParamName] = pageIndex.ToString();
211 else
213 urlParts.QueryString.Remove(pageParamName);
214 urlParts.QueryString[pageParamName] = pageIndex.ToString();
217 return urlParts.BuildPathForLink(EngineContext.Server);
220 private void CreateUrlPartBuilder()
222 IDictionary urlParams = urlParam as IDictionary;
224 if (urlParams != null)
226 urlParams["encode"] = "true";
228 IUrlBuilder urlBuilder = EngineContext.Services.GetService<IUrlBuilder>();
229 urlParts = urlBuilder.CreateUrlPartsBuilder(EngineContext.UrlInfo, urlParams);
231 else
233 if (urlParam != null)
235 urlParts = UrlParts.Parse(urlParam.ToString());
237 else
239 if (!PreserveQueryString)
241 urlParts = new UrlParts(EngineContext.Request.FilePath);
243 else
245 urlParts = UrlParts.Parse(EngineContext.Request.Url);