Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sky/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sky/Tabs.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library\View\Sky;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use TechFry\Library\TResource;

class Tabs extends Bootstrap
{
    public $type;

    public $orientation;

    public function display()
    {
        // config - type (tabs/pills/underline), orientation (x/y), tab_class, tab_nav_class, tab_content_class

        if (!$this->data)
        {
            return;
        }
        
        if ($this->framework == 'amp')
        {
            return $this->display_amp();
        }

        $this->type = isset($this->config['type']) ? $this->config['type'] : 'tabs';
        $this->orientation = isset($this->config['orientation']) ? $this->config['orientation'] : 'x';

        $tab_class = isset($this->config['tab_class']) ? $this->config['tab_class'] : '';
        if ($this->orientation == 'y')
        {
            $tab_class .= ' d-flex align-items-start';
        }

        $output = '<div class="' . $tab_class . '">';

            // Buttons - Tab navigation
            $output .= $this->tab_buttons();

            // Tab content
            $output .= $this->tab_content();

        $output .= '</div>';

        // Add JavaScript
        $this->include_bsjs('tab');

        return $output;
    }

    public function tab_buttons()
    {
        $tab_nav_class = isset($this->config['tab_nav_class']) ? $this->config['tab_nav_class'] : '';
        if ($this->orientation == 'y')
        {
            $tab_nav_class .= ' flex-column';
        }
        
        $output = '<ul class="nav nav-' . $this->type . ' ' . $tab_nav_class . '" role="tablist">';
        $i = 1;

        foreach ($this->data as $tab)
        {
            $tab = array_values((array) $tab);

            // First tab is active
            $active = ($i == 1) ? ' active' : '';

            $output .= '<li class="nav-item" role="presentation">';

            $output .= '<button class="nav-link ' . $active .'" data-bs-toggle="tab" data-bs-target="#tab-pane' . $i . '" type="button" role="tab"';
            $output .= ' aria-controls="tab-pane' . $i . '" aria-selected="true">';
            $output .= $tab[0];
            $output .= '</button>';

            $output .= '</li>';

            $i++;
        }
        $output .= '</ul>';

        return $output;
    }

    public function tab_content()
    {
        $tab_content_class = isset($this->config['tab_content_class']) ? $this->config['tab_content_class'] : '';
        
        $output = '<div class="tab-content ' . $tab_content_class . '">';
        $i = 1;

        foreach ($this->data as $tab)
        {
            $tab = array_values((array) $tab);

            $active = ($i == 1) ? ' show active' : '';

            $output .= '<div class="tab-pane fade' . $active . ' my-3" id="tab-pane' . $i .'" role="tabpanel" tabindex="0">';
                $output .= $tab[1];
            $output .= '</div>';

            $i++;
        }
        $output .= '</div>';

        return $output;
    }

    // On AMP, display Tabs as Accordion
    public function display_amp()
    {
        $this->items = $this->data;

        // Add AMP JS
        $js_url = 'https://cdn.ampproject.org/v0/amp-accordion-0.1.js';

        $r = new TResource();
        $r->add_resource('script', 'amp.accordion', $js_url, [], array('async' => 'async', 'custom-element' => 'amp-accordion'));

        $output = '';

        $output .= '<amp-accordion id="my-accordion" animate disable-session-states>';
        $n = 1;
        foreach ($this->items as $i => $item)
        {
            $item = (array) $item;
            $item = array_values($item);

            // First item should be expanded
            $expanded = ($n == 1) ? ' expanded' : '';

            $output .= '<section class=""' . $expanded . '>';
            $output .= '<h3 class="p-2">' . $item[0] . '</h3>';
            $output .= '<div class="p-2">' . $item[1] . '</div>';
            $output .= '</section>';

            $n++;
        }

        $output .= '</amp-accordion>';

        return $output; 
    }
}