lib/Mojolicious/Plugin/TagHelpers/BootstrapPagination.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Fri, 08 Aug 2014 23:02:44 +0200
changeset 40 373c7f784bc2
parent 29 006e82a1bcd0
permissions -rw-r--r--
Set columns size
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     1
# Based on Mojolicious-Plugin-TagHelpers-Pagination and slightly modified for bootstrap
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     2
# Under Artistic License 2.0
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     3
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     4
package Mojolicious::Plugin::TagHelpers::BootstrapPagination;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     5
use Mojo::Base 'Mojolicious::Plugin';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     6
use Mojo::ByteStream 'b';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     7
use Scalar::Util 'blessed';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     8
use POSIX 'ceil';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     9
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    10
our $VERSION = 0.01;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    11
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    12
our @value_list =
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    13
  qw/prev
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    14
     next
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    15
     current_start
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    16
     current_end
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    17
     page_start
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    18
     page_end
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    19
     separator
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    20
     ellipsis
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    21
     placeholder/;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    22
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    23
# Register plugin
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    24
sub register {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    25
  my ($plugin, $mojo, $param) = @_;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    26
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    27
  $param ||= {};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    28
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    29
  # Load parameter from Config file
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    30
  if (my $config_param = $mojo->config('TagHelpers-Pagination')) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    31
    $param = { %$config_param, %$param };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    32
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    33
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    34
  foreach (@value_list) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    35
    $plugin->{$_} = $param->{$_} if defined $param->{$_};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    36
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    37
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    38
  # Set 'current_start' and 'current_end' symbols,
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    39
  # if 'current' template is available.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    40
  # Same for 'page'.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    41
  foreach (qw/page current/) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    42
    if (defined $param->{$_}) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    43
      @{$plugin}{$_ . '_start', $_ . '_end'} = split("{$_}", $param->{$_});
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    44
      $plugin->{$_ . '_end'} ||= '';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    45
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    46
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    47
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    48
  # Default current start and current end symbols
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    49
  for ($plugin) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    50
    $_->{current_start} //= '';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    51
    $_->{current_end}   //= '';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    52
    $_->{page_start}    //= '';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    53
    $_->{page_end}      //= '';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    54
    $_->{prev}          //= '&lt;';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    55
    $_->{next}          //= '&gt;';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    56
    $_->{separator}     //= '';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    57
    $_->{ellipsis}      //= '<li><span>...</span></li>';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    58
    $_->{placeholder}   //= 'page';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    59
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    60
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    61
  # Establish pagination helper
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    62
  $mojo->helper(
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    63
    pagination => sub {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    64
      shift; # Controller
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    65
      return b( $plugin->pagination( @_ ) );
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    66
    });
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    67
};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    68
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    69
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    70
# Pagination helper
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    71
sub pagination {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    72
  my $self = shift;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    73
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    74
  # $_[0] = current page
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    75
  # $_[1] = page count
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    76
  # $_[2] = template or Mojo::URL
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    77
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    78
  return '' unless $_[0] || $_[1];
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    79
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    80
  # No valid count given
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    81
  local $_[1] = !$_[1] ? 1 : ceil($_[1]);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    82
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    83
  # New parameter hash
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    84
  my %values =
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    85
    map { $_ => $self->{$_} } @value_list;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    86
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    87
  # Overwrite plugin defaults
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    88
  if ($_[3] && ref $_[3] eq 'HASH') {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    89
    my $overwrite = $_[3];
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    90
    foreach (@value_list) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    91
      $values{$_}  = $overwrite->{$_} if defined $overwrite->{$_};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    92
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    93
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    94
    foreach (qw/page current/) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    95
      if (defined $overwrite->{$_}) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    96
	@values{$_ . '_start', $_ . '_end'} = split("{$_}", $overwrite->{$_});
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    97
	$values{$_ . '_end'} ||= '';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    98
      };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    99
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   100
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   101
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   102
  # Establish string variables
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   103
  my ($p, $n, $cs, $ce, $ps, $pe, $s, $el, $ph) = @values{@value_list};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   104
  # prev next current_start current_end
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   105
  # page_start page_end separator ellipsis placeholder
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   106
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   107
  # Template
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   108
  my $t = $_[2];
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   109
  if (blessed $t && blessed $t eq 'Mojo::URL') {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   110
    $t = $t->to_string;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   111
    $t =~ s/\%7[bB]$ph\%7[dD]/{$ph}/g;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   112
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   113
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   114
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   115
  my $sub = sublink_gen($t,$ps,$pe,$ph);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   116
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   117
  # Pagination string
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   118
  my $e;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   119
  my $counter = 1;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   120
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   121
  if ($_[1] >= 7){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   122
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   123
    # < [1] #2 #3
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   124
    if ($_[0] == 1){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   125
      $e .= $sub->(undef, [$p, 'prev']) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   126
	    $sub->(undef, [$cs . 1  . $ce, 'self']) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   127
	    $sub->('2') . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   128
	    $sub->('3') . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   129
    }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   130
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   131
    # < #1 #2 #3
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   132
    elsif (!$_[0]) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   133
      $e .= $sub->(undef, [$p, 'prev']) . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   134
      $e .= $sub->($_) . $s foreach (1 .. 3);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   135
    }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   136
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   137
    # #< #1
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   138
    else {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   139
      $e .= $sub->(($_[0] - 1), [$p, 'prev']) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   140
            $sub->('1') . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   141
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   142
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   143
    # [2] #3
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   144
    if ($_[0] == 2){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   145
      $e .= $sub->(undef, [$cs . 2 . $ce, 'self']) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   146
	    $sub->('3') . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   147
    }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   148
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   149
    # ...
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   150
    elsif ($_[0] > 3){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   151
      $e .= $el . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   152
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   153
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   154
    # #x-1 [x] #x+1
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   155
    if (($_[0] >= 3) && ($_[0] <= ($_[1] - 2))){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   156
      $e .= $sub->($_[0] - 1) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   157
	    $sub->(undef, [$cs .$_[0] . $ce, 'self']) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   158
	    $sub->($_[0] + 1) . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   159
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   160
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   161
    # ...
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   162
    if ($_[0] < ($_[1] - 2)){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   163
      $e .= $el . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   164
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   165
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   166
    # number is prefinal
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   167
    if ($_[0] == ($_[1] - 1)){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   168
      $e .= $sub->($_[1] - 2) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   169
	    $sub->(undef, [$cs . $_[0] . $ce, 'self']) . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   170
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   171
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   172
    # Number is final
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   173
    if ($_[0] == $_[1]){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   174
      $e .= $sub->($_[1] - 1) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   175
            $sub->(undef, [$cs . $_[1] . $ce, 'self']) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   176
	    $sub->(undef, [$n, 'next']);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   177
    }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   178
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   179
    # Number is anywhere in between
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   180
    else {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   181
      $e .= $sub->($_[1]) . $s .
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   182
            $sub->(($_[0] + 1), [$n,'next']);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   183
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   184
  }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   185
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   186
  # Counter < 7
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   187
  else {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   188
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   189
    # Previous
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   190
    if ($_[0] > 1){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   191
      $e .= $sub->(($_[0] - 1), [$p, 'prev']) . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   192
    } else {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   193
      $e .= $sub->(undef, [$p, 'prev']) . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   194
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   195
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   196
    # All numbers in between
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   197
    while ($counter <= $_[1]){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   198
      if ($_[0] != $counter) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   199
        $e .= $sub->($counter) . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   200
      }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   201
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   202
      # Current
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   203
      else {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   204
        $e .= $sub->(undef, [$cs . $counter . $ce, 'self']) . $s;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   205
      };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   206
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   207
      $counter++;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   208
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   209
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   210
    # Next
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   211
    if ($_[0] != $_[1]){
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   212
      $e .= $sub->(($_[0] + 1), [$n, 'next']);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   213
    }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   214
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   215
    else {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   216
      $e .= $sub->(undef, [$n, 'next']);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   217
    };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   218
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   219
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   220
  # Pagination string
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   221
  $e;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   222
};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   223
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   224
# Sublink function generator
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   225
sub sublink_gen {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   226
  my ($url, $ps, $pe, $ph) = @_;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   227
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   228
  my $s = 'sub {';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   229
  # $_[0] = number
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   230
  # $_[1] = number_shown
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   231
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   232
  # Url is template
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   233
  if ($url) {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   234
    $s .= 'my $url=' . b($url)->quote . ';';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   235
    $s .= 'if($_[0]){$url=~s/\{' . $ph . '\}/$_[0]/g}else{$url=undef};';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   236
  }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   237
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   238
  # No template given
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   239
  else {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   240
    $s .= 'my $url = $_[0];';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   241
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   242
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   243
  $s .= 'my$n=$_[1]||' . b($ps)->quote . '.$_[0].' . b($pe)->quote . ';';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   244
  $s .= q{my $liclass='';};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   245
  $s .= q{if(ref $n && $n->[1] eq "self"){$liclass = "active"};};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   246
  $s .= q{my $rel='';};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   247
  $s .= q{if(ref $n){$rel=' rel="'.$n->[1].'"';$n=$n->[0]};};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   248
  $s .= q!if($url){$url=~s/&/&amp;/g;!;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   249
  $s .= q{$url=~s/</&lt;/g;};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   250
  $s .= q{$url=~s/>/&gt;/g;};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   251
  $s .= q{$url=~s/"/&quot;/g;};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   252
  $s .= q!$url=~s/'/&#39;/g;};!;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   253
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   254
  # Create sublink
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   255
  $s .= q{return '<li'.($liclass?' class="'.$liclass.'"':'').'><a'.($url?' href="'.$url.'"':'').$rel.'>' . $n . '</a></li>';};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   256
  $s .= '}';
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   257
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   258
  my $x = eval($s);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   259
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   260
  warn $@ if $@;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   261
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   262
  $x;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   263
};
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   264
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   265
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   266
1;
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   267
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   268
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   269
__END__
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   270
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   271
=pod
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   272
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   273
=head1 NAME
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   274
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   275
Mojolicious::Plugin::TagHelpers::Pagination - Pagination Helper for Mojolicious
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   276
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   277
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   278
=head1 SYNOPSIS
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   279
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   280
  # Mojolicious
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   281
  $app->plugin('TagHelpers::Pagination' => {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   282
    separator => ' ',
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   283
    current => '<strong>{current}</strong>'
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   284
  });
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   285
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   286
  # Mojolicious::Lite
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   287
  plugin 'TagHelpers::Pagination' => {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   288
    separator => ' ',
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   289
    current   =>  '<strong>{current}</strong>'
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   290
  };
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   291
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   292
  # In Templates
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   293
  %= pagination(2, 4, '?page={page}' => { separator => "\n" })
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   294
  # <a href="?page=1" rel="prev">&lt;</a>
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   295
  # <a href="?page=1">1</a>
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   296
  # <a rel="self"><strong>2</strong></a>
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   297
  # <a href="?page=3">3</a>
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   298
  # <a href="?page=4">4</a>
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   299
  # <a href="?page=3" rel="next">&gt;</a>
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   300
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   301
=head1 DESCRIPTION
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   302
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   303
L<Mojolicious::Plugin::TagHelpers::Pagination> helps you to create
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   304
pagination elements in your templates, like this:
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   305
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   306
L<E<lt>|/#> L<1|/#> ... L<5|/#> B<[6]> L<7|/#> ... L<18|/#> L<E<gt>|/#>
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   307
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   308
=head1 METHODS
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   309
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   310
L<Mojolicious::Plugin::TagHelpers::Pagination> inherits all methods from
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   311
L<Mojolicious::Plugin> and implements the following new one.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   312
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   313
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   314
=head2 register
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   315
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   316
  # Mojolicious
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   317
  $app->plugin('TagHelpers::Pagination' => {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   318
    separator => ' ',
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   319
    current => '<strong>{current}</strong>'
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   320
  });
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   321
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   322
  # Or in your config file
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   323
  {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   324
    'TagHelpers-Pagination' => {
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   325
      separator => ' ',
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   326
      current => '<strong>{current}</strong>'
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   327
    }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   328
  }
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   329
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   330
Called when registering the plugin.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   331
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   332
All L<parameters|/PARAMETERS> can be set either on registration or
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   333
as part of the configuration file with the key C<TagHelpers-Pagination>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   334
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   335
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   336
=head1 HELPERS
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   337
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   338
=head2 pagination
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   339
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   340
  # In Templates:
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   341
  %= pagination(4, 6 => '/page-{page}.html');
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   342
  % my $url = Mojo::URL->new->query({ page => '{page}'});
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   343
  %= pagination(4, 6 => $url);
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   344
  %= pagination(4, 6 => '/page/{page}.html', { current => '<b>{current}</b>' });
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   345
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   346
Generates a pagination string.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   347
Expects at least two numeric values: the current page number and
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   348
the total count of pages.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   349
Additionally it accepts a link pattern and a hash reference
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   350
with parameters overwriting the default plugin parameters for
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   351
pagination.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   352
The link pattern can be a string using a placeholder in curly brackets
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   353
(defaults to C<page>) for the page number it should link to.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   354
It's also possible to give a
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   355
L<Mojo::URL> object containing the placeholder.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   356
The placeholder can be used multiple times.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   357
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   358
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   359
=head1 PARAMETERS
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   360
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   361
For the layout of the pagination string, the plugin accepts the
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   362
following parameters, that are able to overwrite the default
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   363
layout elements. These parameters can again be overwritten in
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   364
the pagination helper.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   365
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   366
=over 2
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   367
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   368
=item current
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   369
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   370
Pattern for current page number. The C<{current}> is a
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   371
placeholder for the current number.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   372
Defaults to C<[{current}]>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   373
Instead of a pattern, both sides of the current number
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   374
can be defined with C<current_start> and C<current_end>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   375
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   376
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   377
=item ellipsis
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   378
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   379
Placeholder symbol for hidden pages. Defaults to C<...>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   380
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   381
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   382
=item next
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   383
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   384
Symbol for next pages. Defaults to C<&gt;>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   385
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   386
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   387
=item page
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   388
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   389
Pattern for page number. The C<{page}> is a
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   390
placeholder for the page number.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   391
Defaults to C<{page}>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   392
Instead of a pattern, both sides of the page number
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   393
can be defined with C<page_start> and C<page_end>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   394
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   395
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   396
=item placeholder
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   397
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   398
String representing the placeholder for the page number in the URL
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   399
pattern. Defaults to C<page>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   400
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   401
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   402
=item prev
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   403
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   404
Symbol for previous pages. Defaults to C<&lt;>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   405
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   406
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   407
=item separator
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   408
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   409
Symbol for the separation of pagination elements.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   410
Defaults to C<&nbsp;>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   411
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   412
=back
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   413
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   414
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   415
=head1 DEPENDENCIES
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   416
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   417
L<Mojolicious>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   418
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   419
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   420
=head1 AVAILABILITY
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   421
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   422
  https://github.com/Akron/Mojolicious-Plugin-TagHelpers-Pagination
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   423
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   424
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   425
=head1 COPYRIGHT AND LICENSE
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   426
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   427
Copyright (C) 2012-2014, L<Nils Diewald|http://nils-diewald.de/>.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   428
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   429
This program is free software, you can redistribute it
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   430
and/or modify it under the terms of the Artistic License version 2.0.
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   431
006e82a1bcd0 Add pagination
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
   432
=cut