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 TColor.php

Documentation is available at TColor.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Widget\Form\AdiantiWidgetInterface;
  5. use Adianti\Widget\Base\TElement;
  6. use Adianti\Widget\Base\TScript;
  7. use Adianti\Widget\Form\TEntry;
  8. use Adianti\Control\TAction;
  9. use Adianti\Core\AdiantiCoreTranslator;
  10. use Exception;
  11.  
  12. /**
  13.  * Color Widget
  14.  *
  15.  * @version    7.4
  16.  * @package    widget
  17.  * @subpackage form
  18.  * @author     Pablo Dall'Oglio
  19.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  20.  * @license    http://www.adianti.com.br/framework-license
  21.  */
  22. class TColor extends TEntry implements AdiantiWidgetInterface
  23. {
  24.     const THEME_CLASSIC  = 'classic';
  25.     const THEME_NANO     = 'nano';
  26.     const THEME_MONOLITH = 'monolith';
  27.  
  28.     protected $formName;
  29.     protected $name;
  30.     protected $id;
  31.     protected $size;
  32.     protected $changeFunction;
  33.     protected $changeAction;
  34.     protected $theme;
  35.     protected $options;
  36.     
  37.     /**
  38.      * Class Constructor
  39.      * @param $name Name of the widget
  40.      */
  41.     public function __construct($name)
  42.     {
  43.         parent::__construct($name);
  44.         $this->id = 'tcolor_'.mt_rand(10000000001999999999);
  45.         $this->tag->{'widget''tcolor';
  46.         $this->tag->{'autocomplete''off';
  47.         $this->setSize('100%');
  48.  
  49.         $this->theme self::THEME_CLASSIC;
  50.         $this->options [
  51.             'swatches' => [
  52.                 '#F44336''#E91E63''#9C27B0''#673AB7''#3F51B5''#2196F3''#03A9F4',
  53.                 '#00BCD4''#009688''#4CAF50''#8BC34A''#CDDC39''#ffe821''#FFC107',
  54.                 '#FF9800''#FF5722''#795548''#9E9E9E''#607D8B''#000000''#ffffff',
  55.             ],
  56.             'components' => [
  57.                 'preview' => true,
  58.                 'opacity' => true,
  59.                 'hue' => true,
  60.                 'interaction' => [
  61.                     'hex' => false,
  62.                     'rgba' => false,
  63.                     'hsla' => false,
  64.                     'hsva' => false,
  65.                     'cmyk' => false,
  66.                     'input' => false,
  67.                     'clear' => true,
  68.                     'save' => true
  69.                 ]
  70.             ],
  71.         ];
  72.     }
  73.  
  74.     /**
  75.      * Set extra option TColor
  76.      *
  77.      * @see Component documentation https://github.com/Simonwep/pickr#options
  78.      *
  79.      * @param $option Key name option
  80.      * @param $value Option value
  81.      */
  82.     public function setOption($option$value)
  83.     {
  84.         if (is_array($value))
  85.         {
  86.             $oldOptions $this->options[$option]??[];
  87.  
  88.             $value array_merge($oldOptions$value);
  89.         }
  90.  
  91.         $this->options[$option$value;
  92.     }
  93.  
  94.     /**
  95.      * Get options TColor
  96.      */
  97.     public function getOptions()
  98.     {
  99.         return $this->options;
  100.     }
  101.  
  102.     /**
  103.      * Get option TColor
  104.      * 
  105.      */
  106.     public function getOption($option)
  107.     {
  108.         if (empty($this->options[$option]))
  109.         {
  110.             return null;
  111.         }
  112.  
  113.         return $this->options[$option];
  114.     }
  115.  
  116.     /**
  117.      * Set theme
  118.      */
  119.     public function setTheme($theme)
  120.     {
  121.         if (in_array($theme[self::THEME_CLASSICself::THEME_NANOself::THEME_MONOLITH]) )
  122.         {
  123.             $theme self::THEME_CLASSIC;
  124.         }
  125.  
  126.         $this->theme $theme;
  127.     }
  128.     
  129.     /**
  130.      * Enable the field
  131.      * @param $form_name Form name
  132.      * @param $field Field name
  133.      */
  134.     public static function enableField($form_name$field)
  135.     {
  136.         TScript::create" tcolor_enable_field('{$form_name}', '{$field}'); );
  137.     }
  138.     
  139.     /**
  140.      * Disable the field
  141.      * @param $form_name Form name
  142.      * @param $field Field name
  143.      */
  144.     public static function disableField($form_name$field)
  145.     {
  146.         TScript::create" tcolor_disable_field('{$form_name}', '{$field}'); );
  147.     }
  148.     
  149.     /**
  150.      * Set change function
  151.      */
  152.     public function setChangeFunction($function)
  153.     {
  154.         $this->changeFunction $function;
  155.     }
  156.     
  157.     /**
  158.      * Define the action to be executed when the user changes the content
  159.      * @param $action TAction object
  160.      */
  161.     public function setChangeAction(TAction $action)
  162.     {
  163.         $this->changeAction $action;
  164.     }
  165.     
  166.     /**
  167.      * Shows the widget at the screen
  168.      */
  169.     public function show()
  170.     {
  171.         $wrapper new TElement('div');
  172.         $wrapper->{'class''input-group color-div colorpicker-component';
  173.         $wrapper->{'style''float:inherit';
  174.         
  175.         $span new TElement('span');
  176.         $span->{'class''input-group-addon tcolor';
  177.         
  178.         $outer_size 'undefined';
  179.         if (strstr((string) $this->size'%'!== FALSE)
  180.         {
  181.             $outer_size $this->size;
  182.             $this->size '100%';
  183.         }
  184.         
  185.         if ($this->changeAction)
  186.         {
  187.             if (!TForm::getFormByName($this->formNameinstanceof TForm)
  188.             {
  189.                 throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  190.             }
  191.             
  192.             $string_action $this->changeAction->serialize(FALSE);
  193.             $this->setProperty('changeaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback')");
  194.             $this->changeFunction $this->getProperty('changeaction');
  195.         }
  196.         
  197.         $i new TElement('i');
  198.         $i->{'class''tcolor-icon';
  199.         $span->add($i);
  200.         ob_start();
  201.         parent::show();
  202.         $child ob_get_contents();
  203.         ob_end_clean();
  204.         $wrapper->add($child);
  205.         $wrapper->add($span);
  206.         $wrapper->show();
  207.         
  208.         $options json_encode($this->options);
  209.  
  210.         TScript::create("tcolor_start('{$this->id}', '{$outer_size}', '{$this->theme}', function(color) { {$this->changeFunction} }, {$options}); ");
  211.         
  212.         if (!parent::getEditable())
  213.         {
  214.             self::disableField($this->formName$this->name);
  215.         }
  216.     }
  217. }