| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## Copyright (c) 2013-2022 Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | ## <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 6 | ## http://cade.noxrun.com | ||
| 7 | ## | ||
| 8 | ## LICENSE: GPLv2 | ||
| 9 | ## https://github.com/cade-vs/perl-web-reactor | ||
| 10 | ## | ||
| 11 | ############################################################################## | ||
| 12 | ## | ||
| 13 | ## HTML Tabs | ||
| 14 | ## | ||
| 15 | ############################################################################## | ||
| 16 | package Web::Reactor::HTML::Tab; | ||
| 17 | use strict; | ||
| 18 | use Exception::Sink; | ||
| 19 | use Web::Reactor::HTML::Utils; | ||
| 20 | |||
| 21 | use parent 'Web::Reactor::Base'; | ||
| 22 | |||
| 23 | sub new | ||
| 24 | { | ||
| 25 | my $class = shift; | ||
| 26 | my %env = @_; | ||
| 27 | |||
| 28 | $class = ref( $class ) || $class; | ||
| 29 | |||
| 30 | my $self = { | ||
| 31 | 'ENV' => \%env, | ||
| 32 | }; | ||
| 33 | |||
| 34 | bless $self, $class; | ||
| 35 | |||
| 36 | # FIXME: move as argument, not env option | ||
| 37 | $self->__set_reo( $env{ 'REO_REACTOR' } ); | ||
| 38 | my $reo = $self->get_reo(); | ||
| 39 | |||
| 40 | $self->{ 'TABS_LIST' } = []; # contain tab IDs | ||
| 41 | $self->{ 'TAB_CONTROLLER_ID' } = join '_', ( 'RE_TAB', $reo->get_page_session_id(), ( $env{ 'NAME' } || $reo->create_uniq_id() ) ); | ||
| 42 | $self->{ 'TAB_COUNTER' } = 0; | ||
| 43 | |||
| 44 | $reo->html_content_accumulator_js( "js/reactor.js" ); | ||
| 45 | |||
| 46 | $self->{ 'OPT' } = { @_ }; | ||
| 47 | |||
| 48 | #use Data::Dumper; | ||
| 49 | #print STDERR Dumper( $self ); | ||
| 50 | |||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | # returns ( 'handle' code, html text ) handle code to be put inside HTML tag to activate this TAB | ||
| 55 | sub add | ||
| 56 | { | ||
| 57 | my $self = shift; | ||
| 58 | my $content = shift; | ||
| 59 | my %opt = @_; | ||
| 60 | |||
| 61 | my $et = uc $opt{ 'TYPE' }; # html element type TD, TR, DIV | ||
| 62 | my $on = $opt{ 'ON' }; # is visible? | ||
| 63 | |||
| 64 | my $class = $opt{ 'CLASS' } || 'reactor_tab'; | ||
| 65 | my $args = $opt{ 'ARGS' }; | ||
| 66 | |||
| 67 | boom "TYPE can be only one of DIV|TR|TD" unless $et =~ /^(DIV|TR|TD)$/i; | ||
| 68 | |||
| 69 | my $tab_controller_id = $self->{ 'TAB_CONTROLLER_ID' }; | ||
| 70 | my $tab_counter = ++ $self->{ 'TAB_COUNTER' }; | ||
| 71 | |||
| 72 | my $handle_id = $opt{ 'HANDLE_ID' } || "${tab_controller_id}_HANDLE_$tab_counter"; | ||
| 73 | my $tab_id = $opt{ 'TAB_ID' } || "${tab_controller_id}_CONTENT_$tab_counter"; | ||
| 74 | |||
| 75 | push @{ $self->{ 'TABS_LIST' } }, $tab_id; | ||
| 76 | |||
| 77 | my $class_on = $self->{ 'OPT' }{ 'CLASS_ON' }; | ||
| 78 | my $class_off = $self->{ 'OPT' }{ 'CLASS_OFF' }; | ||
| 79 | |||
| 80 | my $handle; | ||
| 81 | my $text; | ||
| 82 | |||
| 83 | my $display = $on ? '' : "style='display: none;'"; | ||
| 84 | my $handle_class = $on ? $class_on : $class_off; | ||
| 85 | |||
| 86 | $handle = qq{ class='$handle_class' ID=$handle_id onclick='reactor_tab_activate_id( "$tab_id" )' }; | ||
| 87 | $text = qq{ <$et id=$tab_id class='$class' data-controller-id='$tab_controller_id' data-handle-id='$handle_id' $display $args >$content</$et> }; | ||
| 88 | |||
| 89 | return ( $handle, $text ); | ||
| 90 | } | ||
| 91 | |||
| 92 | # puts tab controller inside html accumulator | ||
| 93 | |||
| 94 | sub finish | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | my $html; | ||
| 99 | |||
| 100 | my $tab_controller_id = $self->{ 'TAB_CONTROLLER_ID' }; | ||
| 101 | my $tabs_list = join ',', @{ $self->{ 'TABS_LIST' } }; | ||
| 102 | |||
| 103 | my $class_on = $self->{ 'OPT' }{ 'CLASS_ON' }; | ||
| 104 | my $class_off = $self->{ 'OPT' }{ 'CLASS_OFF' }; | ||
| 105 | |||
| 106 | # FIXME: <input hidden> active tab element keeper to be optionally outside element (by id) | ||
| 107 | $html = qq{ | ||
| 108 | <DIV class='reactor_tab_controller' id=$tab_controller_id style='display: none;' data-tabs-list='$tabs_list' data-class-on='$class_on' data-class-off='$class_off'> | ||
| 109 | |||
| 110 | <script type="text/javascript"> | ||
| 111 | |||
| 112 | reactor_tab_activate_id( sessionStorage.getItem( 'TABSET_ACTIVE_$tab_controller_id' ) ); | ||
| 113 | |||
| 114 | </script> | ||
| 115 | |||
| 116 | </DIV> | ||
| 117 | }; | ||
| 118 | |||
| 119 | my $reo = $self->get_reo(); | ||
| 120 | $reo->html_content_accumulator( 'ACCUMULATOR_HTML', $html ); | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; |