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
|
|
|
* Template file include. for including custom control html/php |
15
|
|
|
* |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class handlebars 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 = 'handlebars'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* set the object's data |
31
|
|
|
* @since 1.0.0 |
32
|
|
|
* @access public |
33
|
|
|
* |
34
|
|
|
* @param mixed $data the data to be set |
35
|
|
|
*/ |
36
|
1 |
|
public function set_data( $data ) { |
37
|
1 |
|
if ( isset( $data[ $this->slug ] ) ) { |
38
|
1 |
|
if ( is_string( $data[ $this->slug ] ) ) { |
39
|
1 |
|
$is_json = json_decode( $data[ $this->slug ], ARRAY_A ); |
40
|
1 |
|
if ( ! empty( $is_json ) ) { |
41
|
1 |
|
$data[ $this->slug ] = $is_json; |
42
|
|
|
} else { |
43
|
1 |
|
$data[ $this->slug ] = str_replace( '{{@root', '{{json @root', $data[ $this->slug ] ); |
44
|
|
|
} |
45
|
|
|
} |
46
|
1 |
|
$this->data[ $this->id() ][ $this->slug ] = apply_filters( 'uix_' . $this->slug . '_sanitize_' . $this->type, $data[ $this->slug ], $this ); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets the classes for the control input |
53
|
|
|
* |
54
|
|
|
* @since 1.0.0 |
55
|
|
|
* @access public |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function classes() { |
59
|
1 |
|
|
60
|
|
|
return [ |
61
|
1 |
|
'uix-tab-canvas', |
62
|
1 |
|
]; |
63
|
1 |
|
|
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
/** |
67
|
|
|
* Render the Control |
68
|
|
|
* |
69
|
|
|
* @since 1.0.0 |
70
|
|
|
* @see \uix\ui\uix |
71
|
|
|
* @access public |
72
|
|
|
* @return string HTML of rendered control |
73
|
|
|
*/ |
74
|
|
|
public function render() { |
75
|
|
|
|
76
|
1 |
|
$output = '<div data-app="' . esc_attr( $this->name() ) . '" ' . esc_attr( $this->build_attributes() ) . '></div>'; |
77
|
1 |
|
$output .= '<input type="hidden" name="' . esc_attr( $this->name() ) . '" value="' . esc_attr( $this->get_value() ) . '" data-data="' . esc_attr( $this->get_value() ) . '">'; |
78
|
|
|
add_action( 'admin_footer', array( $this, 'input' ) ); |
79
|
1 |
|
add_action( 'wp_footer', array( $this, 'input' ) ); |
80
|
|
|
|
81
|
|
|
return $output; |
82
|
|
|
} |
83
|
1 |
|
|
84
|
|
|
/** |
85
|
|
|
* get this controls value |
86
|
|
|
* |
87
|
|
|
* @since 1.0.0 |
88
|
|
|
* @access public |
89
|
|
|
* @return mixed the controls value |
90
|
|
|
*/ |
91
|
|
|
public function get_value() { |
92
|
|
|
$value = parent::get_value(); |
93
|
|
|
|
94
|
|
|
if ( is_array( $value ) ) { |
95
|
|
|
$value = json_encode( $value ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the main input field for rendering |
103
|
|
|
* |
104
|
|
|
* @since 1.0.0 |
105
|
|
|
* @see \uix\ui\uix |
106
|
|
|
* @access public |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function input() { |
110
|
|
|
$output = '<script type="text/html" data-template="' . esc_attr( $this->name() ) . '">'; |
111
|
|
|
if ( ! empty( $this->struct['template'] ) && file_exists( $this->struct['template'] ) ) { |
112
|
|
|
ob_start(); |
113
|
1 |
|
include $this->struct['template']; |
114
|
|
|
$output .= ob_get_clean(); |
115
|
|
|
} |
116
|
1 |
|
$output .= '</script>'; |
117
|
1 |
|
echo $output; |
118
|
|
|
|
119
|
|
|
return $output; |
120
|
1 |
|
} |
121
|
1 |
|
|
122
|
|
|
/** |
123
|
1 |
|
* register scritps and styles |
124
|
1 |
|
* |
125
|
|
|
* @since 1.0.0 |
126
|
|
|
* @access public |
127
|
1 |
|
*/ |
128
|
1 |
|
public function set_assets() { |
129
|
|
|
|
130
|
|
|
// Initilize core styles |
131
|
1 |
|
$this->assets['script']['baldrick'] = array( |
132
|
1 |
|
'src' => $this->url . 'assets/js/jquery.baldrick' . UIX_ASSET_DEBUG . '.js', |
133
|
|
|
'deps' => array( 'jquery' ), |
134
|
|
|
); |
135
|
|
|
$this->assets['script']['handlebars'] = array( |
136
|
|
|
'src' => $this->url . 'assets/js/handlebars-latest' . UIX_ASSET_DEBUG . '.js', |
137
|
|
|
); |
138
|
|
|
$this->assets['script']['handlebars-control'] = array( |
139
|
|
|
'src' => $this->url . 'assets/controls/handlebars/handlebars-control' . UIX_ASSET_DEBUG . '.js', |
140
|
|
|
'deps' => array( 'baldrick' ), |
141
|
|
|
); |
142
|
|
|
$this->assets['script']['baldrick-handlebars'] = array( |
143
|
|
|
'src' => $this->url . 'assets/js/handlebars.baldrick' . UIX_ASSET_DEBUG . '.js', |
144
|
|
|
'deps' => array( 'baldrick' ), |
145
|
|
|
); |
146
|
|
|
parent::set_assets(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|