-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathclass-fieldmanager-select.php
More file actions
208 lines (181 loc) · 4.91 KB
/
class-fieldmanager-select.php
File metadata and controls
208 lines (181 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* Class file for Fieldmanager_Select
*
* @package Fieldmanager
*/
/**
* Select dropdown or multi-select field.
*
* This class extends {@link Fieldmanager_Options}, which allows you to define
* options (values) via an array or via a dynamic
* {@link Fieldmanager_Datasource}, like {@link Fieldmanager_Datasource_Post},
* {@link Fieldmanager_Datasource_Term}, or {@link Fieldmanager_Datasource_User}.
*/
class Fieldmanager_Select extends Fieldmanager_Options {
/**
* Override $field_class.
*
* @var string
*/
public $field_class = 'select';
/**
* Should we support type-ahead? i.e. use chosen.js or not.
*
* @var bool
*/
public $type_ahead = false;
/**
* Send an empty element first.
*
* @var bool
*/
public $first_empty = false;
/**
* String to use for first empty element.
*
* @var string
*/
public $first_empty_label = ' ';
/**
* Tell FM to save multiple values.
*
* @var bool
*/
public $multiple = false;
/**
* Override constructor to add chosen.js maybe.
*
* @param string $label The form label.
* @param array $options The form options.
*/
public function __construct( $label = '', $options = array() ) {
$this->attributes = array(
'size' => '1',
);
// Add the Fieldmanager Select Javascript library.
fm_add_script(
'fm_select_js',
'js/fieldmanager-select.js',
array( 'fm_loader' ),
FM_VERSION,
true,
'fm_select',
array(
'nonce' => wp_create_nonce( 'fm_search_terms_nonce' ),
)
);
parent::__construct( $label, $options );
// You can make a select field multi-select either by setting the attribute
// or by setting `'multiple' => true`. If you opt for the latter, the
// attribute will be set for you.
if ( array_key_exists( 'multiple', $this->attributes ) ) {
$this->multiple = true;
} elseif ( $this->multiple ) {
$this->attributes['multiple'] = 'multiple';
}
// Add the chosen library for type-ahead capabilities.
if ( $this->type_ahead ) {
fm_add_script( 'fm_chosen', 'js/chosen/chosen.jquery.min.js', array( 'jquery' ), '1.8.2' );
fm_add_style( 'fm_chosen_css', 'js/chosen/chosen.min.css', array(), '1.8.2' );
}
}
/**
* Form element.
*
* @param array $value The current value.
* @return string HTML string.
*/
public function form_element( $value = array() ) {
$select_classes = array( 'fm-element' );
// If this is a multiple select, need to handle differently.
$do_multiple = '';
if ( $this->multiple ) {
$do_multiple = '[]';
}
// Handle type-ahead based fields using the chosen library.
if ( $this->type_ahead ) {
$select_classes[] = 'chosen-select';
if ( ! isset( $GLOBALS['fm_chosen_initialized'] ) ) {
add_action( 'admin_footer', array( $this, 'chosen_init' ) );
$GLOBALS['fm_chosen_initialized'] = true;
}
if ( $this->grouped ) {
$select_classes[] = 'fm-options-grouped';
} else {
$select_classes[] = 'fm-options';
}
}
$opts = '';
if ( $this->is_repeatable() || $this->first_empty ) {
$opts .= '<option value="">' . esc_html( $this->first_empty_label ) . '</option>';
}
$opts .= $this->form_data_elements( $value );
return sprintf(
'<select class="%s" name="%s" id="%s" %s>%s</select>',
esc_attr( implode( ' ', $select_classes ) ),
esc_attr( $this->get_form_name( $do_multiple ) ),
esc_attr( $this->get_element_id() ),
$this->get_element_attributes(),
$opts
);
}
/**
* Single data element (<option>).
*
* @param array $data_row The data row.
* @param array $value The option value.
* @return string HTML string.
*/
public function form_data_element( $data_row, $value = array() ) {
// For taxonomy-based selects, only return selected options if taxonomy preload is disabled.
// Additional terms will be provided by Ajax for typeahead to avoid overpopulating the select for large taxonomies.
$option_selected = $this->option_selected( $data_row['value'], $value, 'selected' );
return sprintf(
'<option value="%s" %s>%s</option>',
esc_attr( $data_row['value'] ),
$option_selected,
esc_html( $data_row['name'] )
);
}
/**
* Start an <optgroup>.
*
* @param string $label Optgroup label.
* @return string HTML string.
*/
public function form_data_start_group( $label ) {
return sprintf(
'<optgroup label="%s">',
esc_attr( $label )
);
}
/**
* End an <optgroup>.
*
* @return string HTML string.
*/
public function form_data_end_group() {
return '</optgroup>';
}
/**
* Init chosen.js.
*/
public function chosen_init() {
?>
<script type="text/javascript">
jQuery(function($){
var chosenOpts = {
allow_single_deselect: true,
disable_search_threshold: -1,
width: '350px'
}
$('.fm-wrapper').on("fm_added_element fm_collapsible_toggle fm_activate_tab",".fm-item",function(){
$(".chosen-select:visible",this).chosen(chosenOpts)
});
$(".chosen-select:visible").chosen(chosenOpts);
});
</script>
<?php
}
}