aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/FocusStack.pm
blob: cd218daa5449d767d77f46e6c05a81a135dbe997 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package Reaction::UI::FocusStack;

use Reaction::Class;

use namespace::clean -except => [ qw(meta) ];


has vp_head => (
  isa => 'Reaction::UI::ViewPort', is => 'rw',
  clearer => 'clear_vp_head',
);
has vp_tail => (
  isa => 'Reaction::UI::ViewPort', is => 'rw',
  clearer => 'clear_vp_tail',
);
has vp_count => (
  isa => 'Int', is => 'rw', required => 1, default => sub { 0 }
);
has loc_prefix => (isa => 'Str', is => 'rw', predicate => 'has_loc_prefix');
sub push_viewport {
  my ($self, $class, %create) = @_;
  my $tail = $self->vp_tail;
  my $loc = $self->vp_count;
  if ($self->has_loc_prefix) {
    $loc = join('-', $self->loc_prefix, $loc);
  }
  my $vp = $class->new(
    location => $loc,
    %create,
    focus_stack => $self,
    (defined $tail ? ( outer => $tail ) : ()), # XXX possibly a bug in immutable?
  );
  if ($tail) {           # if we already have a tail (non-empty vp stack)
    $tail->inner($vp);     # set the current tail's inner vp to the new vp
  } else {               # else we're currently an empty stack
    $self->vp_head($vp);   # so set the head to the new vp
  }
  $self->vp_count($self->vp_count + 1);
  $self->vp_tail($vp);
  return $vp;
};

sub pop_viewport {
  my ($self) = @_;
  my $head = $self->vp_head;
  confess "Can't pop from empty focus stack" unless defined($head);
  my $vp = $self->vp_tail;
  if ($vp eq $head) {
    $self->clear_vp_head;
    $self->clear_vp_tail;
  } else {
    $self->vp_tail($vp->outer);
  }
  $self->vp_count($self->vp_count - 1);
  return $vp;
};

sub pop_viewports_to {
  my ($self, $vp) = @_;
  1 while ($self->pop_viewport ne $vp);
  return $vp;
};

sub apply_events {
  my $self = shift;
  my $all_events = shift;
  my $vp = $self->vp_tail;

  while (defined $vp && keys %$all_events) {
    my $loc = $vp->location;
    my %vp_events = map { $_ => delete $all_events->{$_} }
      grep { /^${loc}[-:]/ } keys %$all_events;
    $vp->apply_events(\%vp_events);
    $vp = $vp->outer;
  }
};


__PACKAGE__->meta->make_immutable;


1;

=head1 NAME

Reaction::UI::FocusStack - A linked list of ViewPort-based objects

=head1 SYNOPSIS

  my $stack = Reaction::UI::FocusStack->new();

  # Or more commonly, in a Reaction::UI::RootController based
  # Catalyst Controller:
  my $stack = $ctx->focus_stack;

  # Add a new basic viewport inside the last viewport on the stack:
  my $vp = $stack->push_viewport('Reaction::UI::ViewPort' => 
                                  layout => 'xhtml'
                                );

  # Fetch the innermost viewport from the stack:
  my $vp = $stack->pop_viewport();

  # Remove all viewports inside a given viewport:
  $stack->pop_viewports_to($vp);

  # Create a named stack as a tangent to an existing viewport:
  my $newstack = $vp->create_tangent('somename');

  # Resolve current events using your stack:
  # This is called by Reaction::UI::RootController in the end action.
  $stack->apply_events($ctx, $param_hash);

=head1 DESCRIPTION

A FocusStack represents a list of related L<ViewPort|Reaction::UI::ViewPort>
objects. The L<Reaction::UI::RootController> creates an empty stack for you in
it's begin action, which represents the main thread/container of the page.
Typically you add new ViewPorts to this stack as the main parts of your page.
To add multiple parallel page subparts, create a tangent from the outer
viewport, and add more viewports as normal.

=head1 METHODS

=head2 new

=over

=item Arguments: none

=back

Create a new empty FocusStack. This is done for you in
L<Reaction::UI::RootController>.

=head2 push_viewport

=over

=item Arguments: $class, %options

=back

Creates a new L<Reaction::UI::ViewPort> based object and adds it to the stack.

The following attributes of the new ViewPort are set:

=over 

=item outer

Is set to the preceding ViewPort in the stack.

=item focus_stack

Is set to the FocusStack object that created the ViewPort.

=item location

Is set to the location of the ViewPort in the stack.

=back

=head2 pop_viewport

=over 

=item Arguments: none

=back

Removes the last/innermost ViewPort from the stack and returns it.

=head2 pop_viewports_to

=over 

=item Arguments: $viewport

=back

Pops all ViewPorts off the stack until the given ViewPort object
remains as the last item. If passed a $viewport not on the stack, this
will empty the stack completely (and then die complainingly).

TODO: Should pop_viewports_to check $vp->focus_stack eq $self first?

=head2 vp_head

=over

=item Arguments: none

=back

Retrieve the first ViewPort in this stack. Useful for calling
L<Reaction::UI::Window/render_viewport> on a
L<Reaction::UI::ViewPort/focus_tangent>.

=head2 vp_head

=over 

=item Arguments: none

=back

Retrieve the first ViewPort in this stack. Useful for calling
L<Reaction::UI::Window/render_viewport> on a
L<Reaction::UI::ViewPort/focus_tangent>.

=head2 vp_tail

=over 

=item Arguments: none

=back

Retrieve the last ViewPort in this stack. Useful for calling
L<Reaction::UI::Window/render_viewport> on a
L<Reaction::UI::ViewPort/focus_tangent>.

=head2 vp_count

=over 

=item Arguments: none

=back

=head2 loc_prefix

=head2 apply_events

=over 

=item Arguments: $ctx, $params_hashref

=back

Instruct each of the ViewPorts in the stack to apply the given events
to each of it's tangent stacks, and then to itself. These are applied
starting with the last/innermost ViewPort first.

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut