Updated CMS PAGINATION with the last Jocelyn's suggestion.
Added a node pagination helper, to build the html links and header related to pagination.
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
note
|
||||
description: "Paginator builder for CMS nodes."
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
CMS_NODE_PAGINATION_BUILDER
|
||||
|
||||
inherit
|
||||
|
||||
CMS_PAGINATION_BUILDER [CMS_NODE]
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make (a_api: CMS_NODE_API)
|
||||
-- Create an object.
|
||||
do
|
||||
node_api := a_api
|
||||
count := 5
|
||||
offset := 0
|
||||
ensure
|
||||
node_api_set: node_api = a_api
|
||||
limit_set: count = 5
|
||||
offset_set: offset = 0
|
||||
end
|
||||
|
||||
node_api: CMS_NODE_API
|
||||
-- CMS API.
|
||||
|
||||
feature -- Pager
|
||||
|
||||
items: ITERABLE [CMS_NODE]
|
||||
-- <Precursor>.
|
||||
do
|
||||
--NOTE: the current implementation does not use
|
||||
-- order by and ordering.
|
||||
create {ARRAYED_LIST [CMS_NODE]} Result.make (0)
|
||||
Result := node_api.recent_nodes (offset.as_integer_32, count.as_integer_32)
|
||||
end
|
||||
|
||||
end
|
||||
138
modules/node/handler/cms_node_pagination_helper.e
Normal file
138
modules/node/handler/cms_node_pagination_helper.e
Normal file
@@ -0,0 +1,138 @@
|
||||
note
|
||||
description: "Helper class to build the html pagination links and header summary"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
CMS_NODE_PAGINATION_HELPER
|
||||
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make (a_req: WSF_REQUEST; a_response: CMS_RESPONSE; a_nodes_count: INTEGER_64)
|
||||
-- Create an object with default values.
|
||||
do
|
||||
make_size (a_req, a_response, a_nodes_count, 5)
|
||||
end
|
||||
|
||||
make_size (a_req: WSF_REQUEST; a_response: CMS_RESPONSE; a_nodes_count: INTEGER_64; a_size: NATURAL)
|
||||
-- Create an object with a pages of size `a_size'.
|
||||
do
|
||||
req := a_req
|
||||
number_of_nodes := a_nodes_count
|
||||
create pager
|
||||
pager.set_count (a_size)
|
||||
response := a_response
|
||||
process
|
||||
end
|
||||
|
||||
req: WSF_REQUEST
|
||||
-- Current request.
|
||||
|
||||
process
|
||||
-- Process request query paraments to build the pager.
|
||||
do
|
||||
--TODO: at the moment the code looks for hardcoded parameters
|
||||
-- sie and page, maybe we can parametrize these names.
|
||||
-- Size:limit.
|
||||
if
|
||||
attached {WSF_STRING} req.query_parameter ("size") as l_size and then
|
||||
l_size.is_integer
|
||||
then
|
||||
pager.set_count (l_size.integer_value.to_natural_32)
|
||||
end
|
||||
|
||||
--Page:offset
|
||||
if
|
||||
attached {WSF_STRING} req.query_parameter ("page") as l_page and then
|
||||
l_page.is_integer
|
||||
then
|
||||
current_page := l_page.integer_value
|
||||
if current_page > 1 then
|
||||
pager.set_offset (((current_page-1)*(pager.count.to_integer_32)).to_natural_32)
|
||||
end
|
||||
else
|
||||
current_page := 1
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
pager: CMS_PAGINATION_BUILDER
|
||||
-- Paginator.
|
||||
|
||||
current_page: INTEGER
|
||||
-- current page.
|
||||
|
||||
number_of_pages: INTEGER_64
|
||||
-- total number of pages.
|
||||
do
|
||||
Result := (number_of_nodes // pager.count.as_integer_32) + 1
|
||||
end
|
||||
|
||||
number_of_nodes: INTEGER_64
|
||||
-- number of nodes.
|
||||
|
||||
response: CMS_RESPONSE
|
||||
-- cms response
|
||||
|
||||
feature -- Paginator
|
||||
|
||||
paging_summary: STRING
|
||||
-- Header summary
|
||||
-- Current page 1 of n pages.
|
||||
do
|
||||
create Result.make_from_string ("<p>Items:</p>")
|
||||
|
||||
Result.append ("<p>Current Page:")
|
||||
Result.append_integer (current_page)
|
||||
Result.append (" of ")
|
||||
Result.append_integer_64 (number_of_pages)
|
||||
Result.append (" pages</p>")
|
||||
end
|
||||
|
||||
|
||||
build_html_node_paging: STRING
|
||||
-- First, [Prev], [Next], Last.
|
||||
local
|
||||
s: STRING
|
||||
lnk: CMS_LOCAL_LINK
|
||||
|
||||
do
|
||||
-- NOTE: for development purposes we have the following hardcode output.
|
||||
create s.make_empty
|
||||
|
||||
-- pager
|
||||
s.append ("<div class=%"col-xs-12%">%N")
|
||||
s.append ("<ul class=%"pager%">%N")
|
||||
create lnk.make ("First", "nodes/?page=1&size="+pager.count.out)
|
||||
s.append ("<li>")
|
||||
s.append (response.link (lnk.title, lnk.location, Void))
|
||||
s.append ("</li>")
|
||||
if (current_page - 1) > 1 then
|
||||
create lnk.make ("Prev", "nodes/?page="+ (current_page-1).out +"&size="+pager.count.out)
|
||||
s.append ("<li>")
|
||||
s.append (response.link (lnk.title, lnk.location, Void))
|
||||
s.append ("</li>")
|
||||
end
|
||||
|
||||
if (current_page + 1) < number_of_pages then
|
||||
create lnk.make ("Next", "nodes/?page="+ (current_page+1).out +"&size="+pager.count.out)
|
||||
s.append ("<li>")
|
||||
s.append (response.link (lnk.title, lnk.location, Void))
|
||||
s.append ("</li>")
|
||||
end
|
||||
create lnk.make ("Last", "nodes/?page="+ number_of_pages.out +"&size="+pager.count.out)
|
||||
s.append ("<li>")
|
||||
s.append (response.link (lnk.title, lnk.location, Void))
|
||||
s.append ("</li>")
|
||||
|
||||
|
||||
s.append ("</ul>%N")
|
||||
s.append ("<div>%N")
|
||||
Result := s
|
||||
end
|
||||
end
|
||||
@@ -3,8 +3,26 @@ note
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
deferred class
|
||||
CMS_PAGINATION_BUILDER [G]
|
||||
class
|
||||
CMS_PAGINATION_BUILDER
|
||||
|
||||
inherit
|
||||
|
||||
ANY
|
||||
redefine
|
||||
default_create
|
||||
end
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
default_create
|
||||
do
|
||||
count := 5
|
||||
offset := 0
|
||||
ensure then
|
||||
limit_set: count = 5
|
||||
offset_set: offset = 0
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
@@ -44,12 +62,6 @@ feature -- Access
|
||||
asc_fasle: not order_ascending
|
||||
end
|
||||
|
||||
feature -- Pager
|
||||
|
||||
items: ITERABLE [G]
|
||||
-- Iterable of G with filters.
|
||||
deferred
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
|
||||
@@ -41,84 +41,20 @@ feature -- HTTP Methods
|
||||
s: STRING
|
||||
n: CMS_NODE
|
||||
lnk: CMS_LOCAL_LINK
|
||||
pager: CMS_NODE_PAGINATION_BUILDER
|
||||
number_of_pages: INTEGER_64
|
||||
current_page: INTEGER
|
||||
l_page_helper: CMS_NODE_PAGINATION_HELPER
|
||||
do
|
||||
-- At the moment the template is hardcoded, but we can
|
||||
-- get them from the configuration file and load them into
|
||||
-- the setup class.
|
||||
|
||||
create {GENERIC_VIEW_CMS_RESPONSE} l_response.make (req, res, api)
|
||||
l_response.add_variable (node_api.nodes, "nodes")
|
||||
create l_page_helper.make (req, l_response, node_api.nodes_count)
|
||||
|
||||
create pager.make (node_api)
|
||||
number_of_pages := (node_api.nodes_count // pager.count) + 1
|
||||
create s.make_empty
|
||||
s.append (l_page_helper.paging_summary)
|
||||
s.append (l_page_helper.build_html_node_paging)
|
||||
|
||||
-- Size:limit
|
||||
if
|
||||
attached {WSF_STRING} req.query_parameter ("size") as l_size and then
|
||||
l_size.is_integer
|
||||
then
|
||||
pager.set_count (l_size.integer_value.to_natural_32)
|
||||
end
|
||||
|
||||
|
||||
|
||||
--Page:offset
|
||||
if
|
||||
attached {WSF_STRING} req.query_parameter ("page") as l_page and then
|
||||
l_page.is_integer
|
||||
then
|
||||
current_page := l_page.integer_value
|
||||
if current_page > 1 then
|
||||
pager.set_offset (((current_page-1)*(pager.count.to_integer_32)).to_natural_32)
|
||||
end
|
||||
else
|
||||
current_page := 1
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- NOTE: for development purposes we have the following hardcode output.
|
||||
create s.make_from_string ("<p>Items:</p>")
|
||||
|
||||
s.append ("<p>Current Page:")
|
||||
s.append_integer (current_page)
|
||||
s.append (" of ")
|
||||
s.append_integer_64 (number_of_pages)
|
||||
s.append (" pages</p>")
|
||||
|
||||
-- pager
|
||||
s.append ("<div class=%"col-xs-12%">%N")
|
||||
s.append ("<ul class=%"pager%">%N")
|
||||
create lnk.make ("First", "nodes/?page=1&size="+pager.count.out)
|
||||
s.append ("<li>")
|
||||
s.append (l_response.link (lnk.title, lnk.location, Void))
|
||||
s.append ("</li>")
|
||||
if (current_page - 1) > 1 then
|
||||
create lnk.make ("Prev", "nodes/?page="+ (current_page-1).out +"&size="+pager.count.out)
|
||||
s.append ("<li>")
|
||||
s.append (l_response.link (lnk.title, lnk.location, Void))
|
||||
s.append ("</li>")
|
||||
end
|
||||
|
||||
if (current_page + 1) < number_of_pages then
|
||||
create lnk.make ("Next", "nodes/?page="+ (current_page+1).out +"&size="+pager.count.out)
|
||||
s.append ("<li>")
|
||||
s.append (l_response.link (lnk.title, lnk.location, Void))
|
||||
s.append ("</li>")
|
||||
end
|
||||
create lnk.make ("Last", "nodes/?page="+ number_of_pages.out +"&size="+pager.count.out)
|
||||
s.append ("<li>")
|
||||
s.append (l_response.link (lnk.title, lnk.location, Void))
|
||||
s.append ("</li>")
|
||||
|
||||
|
||||
s.append ("</ul>%N")
|
||||
s.append ("<div>%N")
|
||||
|
||||
if attached pager.items as lst then
|
||||
if attached node_api.recent_nodes (l_page_helper.pager) as lst then
|
||||
s.append ("<ul class=%"cms-nodes%">%N")
|
||||
across
|
||||
lst as ic
|
||||
@@ -132,6 +68,8 @@ feature -- HTTP Methods
|
||||
s.append ("</ul>%N")
|
||||
end
|
||||
|
||||
s.append (l_page_helper.build_html_node_paging)
|
||||
|
||||
l_response.set_main_content (s)
|
||||
l_response.add_block (create {CMS_CONTENT_BLOCK}.make ("nodes_warning", Void, "/nodes/ is not yet fully implemented<br/>", Void), "highlighted")
|
||||
l_response.execute
|
||||
|
||||
Reference in New Issue
Block a user