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
17 using System
.Collections
;
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";
41 /// Gets or sets the paginated page instance.
43 /// <value>The page.</value>
44 [ViewComponentParam(Required
= true)]
45 public IPaginatedPage Page
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; }
63 /// Gets or sets a value indicating whether the component should output inline styles.
65 /// <value><c>true</c> if it should use inline styles; otherwise, <c>false</c>.</value>
67 public bool UseInlineStyle
69 get { return useInlineStyle; }
70 set { useInlineStyle = value; }
77 public bool UsePathInfo
79 get { return usePathInfo; }
80 set { usePathInfo = value; }
84 /// Gets or sets the paginate function name.
86 /// A paginate function is a javascript fuction
87 /// that receives the page index as the only argument.
90 /// <value>The paginate function.</value>
92 public string PaginateFunction
94 get { return paginatefunction; }
95 set { paginatefunction = value; }
99 /// Gets or sets the js paginate function fixed args.
101 /// <value>The paginate function fixed args.</value>
103 public string PaginatefunctionFixedArgs
105 get { return paginatefunctionFixedArgs; }
106 set { paginatefunctionFixedArgs = value; }
110 /// Gets or sets the URL to be used when generating links
112 /// <value>The URL.</value>
116 get { return urlParam; }
117 set { urlParam = value; }
122 /// Gets or sets a value indicating whether the component should render existing query string arguments and overwrite page parameter
126 /// Default value is <c>false</c>
129 /// Only has effect when <see cref="Url"/> or <see cref="PaginateFunction"/> are not defined
133 /// <c>true</c> if it should preserve existing query string arguments; otherwise <c>false</c>.
136 public bool PreserveQueryString
138 get { return preserveQueryString; }
139 set { preserveQueryString = value; }
143 /// Called by the framework once the component instance
146 public override void Initialize()
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();
163 /// <param name="writer">The writer.</param>
164 protected virtual void StartBlock(StringWriter writer
)
166 if (Context
.HasSection(StartSection
))
168 Context
.RenderSection(StartSection
, writer
);
174 writer
.Write("<div style=\"padding: 3px; margin: 3px; text-align: right; \">\r\n");
178 writer
.Write("<div class=\"pagination\">\r\n");
186 /// <param name="writer">The writer.</param>
187 protected virtual void EndBlock(StringWriter writer
)
189 if (Context
.HasSection(EndSection
))
191 Context
.RenderSection(EndSection
, writer
);
195 writer
.Write("\r\n</div>\r\n");
201 /// Compute url for given page index.
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
)
209 urlParts
.PathInfoDict
[pageParamName
] = pageIndex
.ToString();
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
);
233 if (urlParam
!= null)
235 urlParts
= UrlParts
.Parse(urlParam
.ToString());
239 if (!PreserveQueryString
)
241 urlParts
= new UrlParts(EngineContext
.Request
.FilePath
);
245 urlParts
= UrlParts
.Parse(EngineContext
.Request
.Url
);