1 <?php
2 3 4 5 6 7 8 9 10
11 namespace uix\ui\control;
12
13 14 15 16 17
18 class toggle extends \uix\ui\control{
19
20 21 22 23 24 25 26
27 public $type = 'toggle';
28
29 30 31 32 33 34 35
36 public $icons = array( 'on' => 'dashicons-yes', 'off' => '' );
37
38
39 40 41 42 43 44 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 63 64 65 66
67 public function set_assets() {
68
69
70 $this->assets['style']['toggle'] = $this->url . 'assets/controls/toggle/css/toggle' . UIX_ASSET_DEBUG . '.css';
71
72
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 84 85 86 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 104 105 106 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 118 119 120 121 122 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 143 144 145 146 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 }