Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TTreeView.php

Documentation is available at TTreeView.php

  1. <?php
  2. namespace Adianti\Widget\Util;
  3.  
  4. use Adianti\Widget\Base\TElement;
  5. use Adianti\Widget\Base\TScript;
  6.  
  7. /**
  8.  * TreeView
  9.  * 
  10.  * @version    7.4
  11.  * @package    widget
  12.  * @subpackage util
  13.  * @author     Pablo Dall'Oglio
  14.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  15.  * @license    http://www.adianti.com.br/framework-license
  16.  */
  17. class TTreeView extends TElement
  18. {
  19.     private $itemIcon;
  20.     private $itemAction;
  21.     private $collapsed;
  22.     private $callback;
  23.     
  24.     /**
  25.      * Class Constructor
  26.      */
  27.     public function __construct()
  28.     {
  29.         parent::__construct('ul');
  30.         $this->{'id''ttreeview_'.mt_rand(10000000001999999999);
  31.         $this->collapsed FALSE;
  32.     }
  33.     
  34.     /**
  35.      * Set node transformer
  36.      */
  37.     public function setTransformer($callback)
  38.     {
  39.         $this->callback $callback;
  40.     }
  41.     
  42.     /**
  43.      * Set size
  44.      * @param $size width
  45.      */
  46.     public function setSize($width)
  47.     {
  48.         $this->{'style'"width: {$width}px";
  49.     }
  50.     
  51.     /**
  52.      * Set item icon
  53.      * @param $icon icon location
  54.      */
  55.     public function setItemIcon($icon)
  56.     {
  57.         $this->itemIcon $icon;
  58.     }
  59.     
  60.     /**
  61.      * Set item action
  62.      * @param $action icon action
  63.      */
  64.     public function setItemAction($action)
  65.     {
  66.         $this->itemAction $action;
  67.     
  68.     
  69.     /**
  70.      * Collapse the Tree
  71.      */
  72.     public function collapse()
  73.     {
  74.         $this->collapsed TRUE;
  75.     }
  76.     
  77.     /**
  78.      * Expand to Tree Node
  79.      * @param $key Node key
  80.      */
  81.     public function expandTo($key)
  82.     {
  83.         $objectId $this->{'id'};
  84.         $id md5($key);
  85.         $script new TElement('script');
  86.         $script->{'type''text/javascript';
  87.         $script->add("setTimeout(function(){ \$('#{$objectId}_{$id}').parents('ul').show()  },1);");
  88.         $script->show();
  89.     }
  90.     
  91.     /**
  92.      * Fill treeview from an multi-dimensional array
  93.      * @param multi-dimensional array
  94.      */
  95.     public function fromArray($array)
  96.     {
  97.         if (is_array($array))
  98.         {
  99.             foreach ($array as $key => $option)
  100.             {
  101.                 if (is_scalar($option))
  102.                 {
  103.                     $element new TElement('li');
  104.                     $span new TElement('span');
  105.                     $span->{'class''file';
  106.                     $span->add($option);
  107.                     if ($this->itemIcon)
  108.                     {
  109.                         $element->{'style'"background-image:url(app/images/{$this->itemIcon})";
  110.                     }
  111.                     
  112.                     if ($this->itemAction)
  113.                     {
  114.                         $this->itemAction->setParameter('key'$key);
  115.                         $this->itemAction->setParameter('value'$option);
  116.                         $string_action $this->itemAction->serialize(FALSE);
  117.                         $element->{'onClick'"__adianti_ajax_exec('{$string_action}')";
  118.                         $element->{'id'$this->{'id''_' md5($key);
  119.                     }
  120.                     $span->{'key'$key;
  121.                     
  122.                     if (is_callable($this->callback))
  123.                     {
  124.                         $span call_user_func($this->callback$span);
  125.                     }
  126.  
  127.                     $element->add($span);
  128.                     
  129.                     parent::add($element);
  130.                 }
  131.                 else if (is_array($option))
  132.                 {
  133.                     $element new TElement('li');
  134.                     $span new TElement('span');
  135.                     $span->{'class''folder';
  136.                     $span->add($key);
  137.                     $element->add($span);
  138.                     $element->add($this->fromOptions($option));
  139.                     parent::add($element);
  140.                 }
  141.             }
  142.         }
  143.     }
  144.     
  145.     /**
  146.      * Fill one level of the treeview
  147.      * @param $options array of options
  148.      * @ignore-autocomplete on
  149.      */
  150.     private function fromOptions($options)
  151.     {
  152.         if (is_array($options))
  153.         {
  154.             $ul new TElement('ul');
  155.             foreach ($options as $key => $option)
  156.             {
  157.                 if (is_scalar($option))
  158.                 {
  159.                     $element new TElement('li');
  160.                     $span new TElement('span');
  161.                     $span->{'class''file';
  162.                     $span->add($option);
  163.                     if ($this->itemIcon)
  164.                     {
  165.                         $element->{'style'"background-image:url(app/images/{$this->itemIcon})";
  166.                     }
  167.                     
  168.                     if ($this->itemAction)
  169.                     {
  170.                         $this->itemAction->setParameter('key'$key);
  171.                         $this->itemAction->setParameter('value'$option);
  172.                         $string_action $this->itemAction->serialize(FALSE);
  173.                         $element->{'onClick'"__adianti_ajax_exec('{$string_action}')";
  174.                         $element->{'id'$this->{'id''_' md5($key);
  175.                     }
  176.                     $span->{'key'$key;
  177.                     
  178.                     if (is_callable($this->callback))
  179.                     {
  180.                         $span call_user_func($this->callback$span);
  181.                     }
  182.  
  183.                     $element->add($span);
  184.                 }
  185.                 else if (is_array($option))
  186.                 {
  187.                     $element new TElement('li');
  188.                     $span new TElement('span');
  189.                     $span->{'class''folder';
  190.                     $span->add($key);
  191.                     $element->add($span);
  192.                     $element->add($this->fromOptions($option));
  193.                 }
  194.                 else if (is_object($option))
  195.                 {
  196.                     $element new TElement('li');
  197.                     $element->add($option);
  198.                 }
  199.                 $ul->add($element);
  200.             }
  201.             return $ul;
  202.         }
  203.     }
  204.     
  205.     /**
  206.      * Shows the tag
  207.      */
  208.     public function show()
  209.     {
  210.         $objectId $this->{'id'};
  211.         $collapsed $this->collapsed 'true' 'false';
  212.         
  213.         parent::add(TScript::create(" ttreeview_start( '#{$objectId}', {$collapsed} ); "FALSE));
  214.         parent::show();
  215.     }
  216. }