Skip to content

Commit

Permalink
feature(views): Users can jump directly to content via prev/next links
Browse files Browse the repository at this point in the history
This adds an option to add a URL fragment to pagination links, jumping
the user directly to the paginated content on pages 2 and after. When
returning to the first page, the fragment is not included, assuming
that the user may actually want to see the full first page.

Should the base_url option contain a fragment, the url_fragment value will
not replace it.
  • Loading branch information
mrclay committed Apr 20, 2015
1 parent be6fb37 commit f90466c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
29 changes: 21 additions & 8 deletions views/default/navigation/pagination.php
Expand Up @@ -5,11 +5,12 @@
* @package Elgg
* @subpackage Core
*
* @uses int $vars['offset'] The offset in the list
* @uses int $vars['limit'] Number of items per page
* @uses int $vars['count'] Number of items in list
* @uses string $vars['base_url'] Base URL to use in links
* @uses string $vars['offset_key'] The string to use for offet in the URL
* @uses int $vars['offset'] The offset in the list
* @uses int $vars['limit'] Number of items per page
* @uses int $vars['count'] Number of items in list
* @uses string $vars['base_url'] Base URL to use in links
* @uses string $vars['url_fragment'] URL fragment to add to links if not present in base_url (optional)
* @uses string $vars['offset_key'] The string to use for offet in the URL
*/

if (elgg_in_context('widget')) {
Expand All @@ -29,6 +30,8 @@
}

$offset_key = elgg_extract('offset_key', $vars, 'offset');
$url_fragment = elgg_extract('url_fragment', $vars, '');

// some views pass an empty string for base_url
if (isset($vars['base_url']) && $vars['base_url']) {
$base_url = $vars['base_url'];
Expand All @@ -41,6 +44,16 @@
$base_url = current_page_url();
}

$base_url_has_fragment = preg_match('~#.~', $base_url);

$get_href = function ($offset) use ($base_url, $base_url_has_fragment, $offset_key, $url_fragment) {
$link = elgg_http_add_url_query_elements($base_url, array($offset_key => $offset));
if (!$base_url_has_fragment && $offset) {
$link .= "#$url_fragment";
}
return $link;
};

if ($count <= $limit && $offset == 0) {
// no need for pagination
return;
Expand All @@ -63,7 +76,7 @@

$pages['prev'] = [
'text' => elgg_echo('previous'),
'href' => elgg_http_add_url_query_elements($base_url, array($offset_key => $prev_offset))
'href' => $get_href($prev_offset),
];

if ($current_page == 1) {
Expand Down Expand Up @@ -111,7 +124,7 @@

$pages['next'] = [
'text' => elgg_echo('next'),
'href' => elgg_http_add_url_query_elements($base_url, array($offset_key => $next_offset))
'href' => $get_href($next_offset),
];

if ($current_page == $total_pages) {
Expand All @@ -133,7 +146,7 @@
// don't include offset=0
$page_offset = null;
}
$href = elgg_http_add_url_query_elements($base_url, array($offset_key => $page_offset));
$href = $get_href($page_offset);
}

if ($href && !$disabled) {
Expand Down
25 changes: 13 additions & 12 deletions views/default/page/components/list.php
Expand Up @@ -5,18 +5,19 @@
*
* @package Elgg
*
* @uses $vars['items'] Array of ElggEntity, ElggAnnotation or ElggRiverItem objects
* @uses $vars['offset'] Index of the first list item in complete list
* @uses $vars['limit'] Number of items per page. Only used as input to pagination.
* @uses $vars['count'] Number of items in the complete list
* @uses $vars['base_url'] Base URL of list (optional)
* @uses $vars['pagination'] Show pagination? (default: true)
* @uses $vars['position'] Position of the pagination: before, after, or both
* @uses $vars['full_view'] Show the full view of the items (default: false)
* @uses $vars['list_class'] Additional CSS class for the <ul> element
* @uses $vars['item_class'] Additional CSS class for the <li> elements
* @uses $vars['item_view'] Alternative view to render list items
* @uses $vars['no_results'] Message to display if no results (string|Closure)
* @uses $vars['items'] Array of ElggEntity, ElggAnnotation or ElggRiverItem objects
* @uses $vars['offset'] Index of the first list item in complete list
* @uses $vars['limit'] Number of items per page. Only used as input to pagination.
* @uses $vars['count'] Number of items in the complete list
* @uses $vars['base_url'] Base URL of list (optional)
* @uses $vars['url_fragment'] URL fragment to add to links if not present in base_url (optional)
* @uses $vars['pagination'] Show pagination? (default: true)
* @uses $vars['position'] Position of the pagination: before, after, or both
* @uses $vars['full_view'] Show the full view of the items (default: false)
* @uses $vars['list_class'] Additional CSS class for the <ul> element
* @uses $vars['item_class'] Additional CSS class for the <li> elements
* @uses $vars['item_view'] Alternative view to render list items
* @uses $vars['no_results'] Message to display if no results (string|Closure)
*/
$items = $vars['items'];
$count = elgg_extract('count', $vars);
Expand Down

0 comments on commit f90466c

Please sign in to comment.