UIX Documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • None
  • uix
    • data
    • ui
      • control

Classes

  • uix\data\data
  • uix\ui
  • uix\ui\box
  • uix\ui\control
  • uix\ui\control\autocomplete
  • uix\ui\control\button
  • uix\ui\control\checkbox
  • uix\ui\control\color
  • uix\ui\control\editor
  • uix\ui\control\email
  • uix\ui\control\file
  • uix\ui\control\hidden
  • uix\ui\control\number
  • uix\ui\control\post_relation
  • uix\ui\control\radio
  • uix\ui\control\select
  • uix\ui\control\separator
  • uix\ui\control\slider
  • uix\ui\control\template
  • uix\ui\control\text
  • uix\ui\control\textarea
  • uix\ui\control\toggle
  • uix\ui\footer
  • uix\ui\grid
  • uix\ui\header
  • uix\ui\help
  • uix\ui\metabox
  • uix\ui\modal
  • uix\ui\notice
  • uix\ui\page
  • uix\ui\panel
  • uix\ui\post_type
  • uix\ui\repeat
  • uix\ui\section
  • uix\ui\uix

Interfaces

  • uix\data\load
  • uix\data\save

Functions

  • uix
  • uix_autoload_class
  1 <?php
  2 /**
  3  * UIX Controls
  4  *
  5  * @package   controls
  6  * @author    David Cramer
  7  * @license   GPL-2.0+
  8  * @link
  9  * @copyright 2016 David Cramer
 10  */
 11 namespace uix\ui\control;
 12 
 13 /**
 14  * Pretty little on/off type toggle switch 
 15  *
 16  * @since 1.0.0
 17  */
 18 class toggle extends \uix\ui\control{
 19 
 20     /**
 21      * The type of object
 22      *
 23      * @since       1.0.0
 24      * @access public
 25      * @var         string
 26      */
 27     public $type = 'toggle';
 28 
 29     /**
 30      * The on and off icons to use
 31      *
 32      * @since       1.0.0
 33      * @access public
 34      * @var        array
 35      */
 36     public $icons = array( 'on' => 'dashicons-yes', 'off' => '' );
 37 
 38 
 39     /**
 40      * Gets the classes for the control input
 41      *
 42      * @since  1.0.0
 43      * @access public
 44      * @return array
 45      */
 46     public function classes() {
 47 
 48         $classes = array( 
 49             'toggle-checkbox'
 50         );
 51 
 52         if( !empty( $this->struct['on_icon'] ) )
 53             $this->icons['on'] = $this->struct['on_icon'];
 54 
 55         if( !empty( $this->struct['off_icon'] ) )
 56             $this->icons['off'] = $this->struct['off_icon'];
 57 
 58         return $classes;
 59     }
 60 
 61     /**
 62      * Define core UIX styles - override to register core ( common styles for uix type )
 63      *
 64      * @since 1.0.0
 65      * @access public
 66      */
 67     public function set_assets() {
 68 
 69         // Initilize core styles
 70         $this->assets['style']['toggle']    = $this->url . 'assets/controls/toggle/css/toggle' . UIX_ASSET_DEBUG . '.css';
 71 
 72         // Initilize core scripts
 73         $this->assets['script']['toggle-control-init']  = array(
 74             "src"       => $this->url . 'assets/controls/toggle/js/toggle' . UIX_ASSET_DEBUG . '.js',
 75             "in_footer" => true
 76         );
 77 
 78         parent::set_assets();
 79     }
 80 
 81 
 82     /**
 83      * Enqueues specific tabs assets for the active pages
 84      *
 85      * @since 1.0.0
 86      * @access protected
 87      */
 88     protected function enqueue_active_assets(){
 89 
 90         echo '<style type="text/css">';
 91 
 92         if( !empty( $this->struct['base_color'] ) )
 93             echo '.' . $this->id() . ' > .uix-control-input > .switch.active {background: ' . $this->struct['base_color'] . ';}';
 94 
 95         if( !empty( $this->struct['off_color'] ) )
 96             echo '.' . $this->id() . '> .uix-control-input > .switch { background: ' . $this->struct['off_color'] . ';}';
 97 
 98         echo '</style>';
 99 
100     }
101 
102     /**
103      * Gets the attributes for the control.
104      *
105      * @since  1.0.0
106      * @access public
107      */
108     public function set_attributes() {
109 
110         parent::set_attributes();
111         if( !empty( $this->struct['toggle_all'] ) )
112             $this->attributes['data-toggle-all'] = 'true';
113 
114     }
115 
116     /**
117      * Returns the main input field for rendering
118      *
119      * @since 1.0.0
120      * @see \uix\ui\uix
121      * @access public
122      * @return string 
123      */
124     public function input(){
125         
126         $value          = $this->get_value();
127         $status_class   = '';
128         if( !empty( $value ) )
129             $status_class = ' active';        
130 
131         $input = '<label class="switch setting_toggle_alert' . esc_attr( $status_class ) . '" data-for="' . esc_attr( $this->id() ) . '-control">';
132             $input .= '<input type="checkbox" value="1" ' . $this->build_attributes() . ' ' . checked( 1, $value, false ) . '>';
133             $input .= '<span class="toggle-on dashicons ' . esc_attr( $this->icons['on'] ) . '"></span>';
134             $input .= '<span class="toggle-off dashicons ' . esc_attr( $this->icons['off'] ) . '"></span>';
135             $input .= '<div class="box"></div>';
136         $input .= '</label>';
137 
138         return $input;
139     }  
140 
141     /**
142      * Returns the description for the control
143      *
144      * @since 1.0.0
145      * @access public
146      * @return string description string 
147      */
148     public function sdescription(){
149         $output = null;
150         if( isset( $this->struct['description'] ) )
151             $output .= '<span class="uix-toggle-description">' . esc_html( $this->struct['description'] ) . '</span>';
152 
153         return $output;
154     }
155 
156 }
UIX Documentation API documentation generated by ApiGen