1 // Copyright 2004-2007 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
;
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";
38 /// Gets or sets the paginated page instance.
40 /// <value>The page.</value>
41 [ViewComponentParam(Required
= true)]
42 public IPaginatedPage Page
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; }
60 /// Gets or sets a value indicating whether the component should output inline styles.
62 /// <value><c>true</c> if it should use inline styles; otherwise, <c>false</c>.</value>
64 public bool UseInlineStyle
66 get { return useInlineStyle; }
67 set { useInlineStyle = value; }
74 public bool UsePathInfo
76 get { return usePathInfo; }
77 set { usePathInfo = value; }
81 /// Gets or sets the paginate function name.
83 /// A paginate function is a javascript fuction
84 /// that receives the page index as the only argument.
87 /// <value>The paginate function.</value>
89 public string PaginateFunction
91 get { return paginatefunction; }
92 set { paginatefunction = value; }
96 /// Gets or sets the URL to be used when generating links
98 /// <value>The URL.</value>
102 get { return urlParam; }
103 set { urlParam = value; }
107 /// Called by the framework once the component instance
110 public override void Initialize()
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();
127 /// <param name="writer">The writer.</param>
128 protected virtual void StartBlock(StringWriter writer
)
130 if (Context
.HasSection(StartSection
))
132 Context
.RenderSection(StartSection
, writer
);
138 writer
.Write("<div style=\"padding: 3px; margin: 3px; text-align: right; \">\r\n");
142 writer
.Write("<div class=\"pagination\">\r\n");
150 /// <param name="writer">The writer.</param>
151 protected virtual void EndBlock(StringWriter writer
)
153 if (Context
.HasSection(EndSection
))
155 Context
.RenderSection(EndSection
, writer
);
159 writer
.Write("\r\n</div>\r\n");
167 /// <param name="pageIndex">The page index.</param>
168 /// <returns></returns>
169 protected string CreateUrlForPage(int pageIndex
)
173 urlPartsBuilder
.PathInfoDict
[pageParamName
] = pageIndex
.ToString();
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
);
197 if (urlParam
!= null)
199 urlPartsBuilder
= UrlPartsBuilder
.Parse(urlParam
.ToString());
203 urlPartsBuilder
= new UrlPartsBuilder(RailsContext
.Request
.FilePath
);