| 1 | use Renard::Incunabula::Common::Setup; | ||
| 2 | package Renard::Curie::Model::View::Role::SubviewPageable; | ||
| 3 | # ABSTRACT: Role for view models that are paged | ||
| 4 | $Renard::Curie::Model::View::Role::SubviewPageable::VERSION = '0.005'; | ||
| 5 | use Moo::Role; | ||
| 6 | use MooX::HandlesVia; | ||
| 7 | use Renard::Incunabula::Common::Types qw(Bool PositiveOrZeroInt ArrayRef); | ||
| 8 | |||
| 9 | has _subviews => ( | ||
| 10 | is => 'lazy', # _build__subviews | ||
| 11 | isa => ArrayRef, | ||
| 12 | clearer => 1, # _clear_subviews | ||
| 13 | handles_via => 'Array', | ||
| 14 | handles => { | ||
| 15 | _number_of_subviews => 'count', | ||
| 16 | }, | ||
| 17 | ); | ||
| 18 | requires '_build__subviews'; | ||
| 19 | |||
| 20 | has _subview_idx => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => PositiveOrZeroInt, | ||
| 23 | default => sub { 0 }, | ||
| 24 | trigger => 1 # _trigger__subview_idx | ||
| 25 | ); | ||
| 26 | |||
| 27 | requires '_trigger__subview_idx'; | ||
| 28 | |||
| 29 | method set_current_subview_to_first() { | ||
| 30 | $self->_subview_idx( 0 ); | ||
| 31 | } | ||
| 32 | |||
| 33 | method set_current_subview_to_last() { | ||
| 34 | $self->_subview_idx( $self->_number_of_subviews - 1 ); | ||
| 35 | } | ||
| 36 | |||
| 37 | method can_move_to_previous_subview() :ReturnType(Bool) { | ||
| 38 | $self->_subview_idx > 0; | ||
| 39 | } | ||
| 40 | |||
| 41 | method can_move_to_next_subview() :ReturnType(Bool) { | ||
| 42 | $self->_subview_idx < $self->_number_of_subviews - 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | method set_current_subview_forward() { | ||
| 46 | if( $self->can_move_to_next_subview ) { | ||
| 47 | $self->_subview_idx( $self->_subview_idx + 1 ); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | method set_current_subview_back() { | ||
| 52 | if( $self->can_move_to_previous_subview ) { | ||
| 53 | $self->_subview_idx( $self->_subview_idx - 1 ); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||
| 59 | __END__ |