selectize.js
5.54 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* JavaScript for interacting with Selectize instances.
*
* @since 1.0.0
*
* @package Media_Library_Organizer
* @author Media Library Organizer
*/
/**
* Initializes selectize instances
*
* @since 1.0.7
*
* @param string container Only initialize selectize instances within the given DOM element ID/class (optional)
*/
function mediaLibraryOrganizerSelectizeInit( container ) {
var media_library_organizer_selectize_container = ( typeof container != 'undefined' ? container : 'body' );
( function( $ ) {
/**
* Selectize Instances: Simple
*/
$( media_library_organizer_selectize.selectors.simple.join( ', ' ), $( media_library_organizer_selectize_container ) ).each(
function() {
var delimiter = ',';
if ( typeof $( this ).data( 'delimiter' ) !== 'undefined' ) {
delimiter = $( this ).data( 'delimiter' );
}
$( this ).selectize(
{
delimiter: delimiter
}
);
}
);
/**
* Selectize Instances: Multiple Values
*/
$( media_library_organizer_selectize.selectors.multiple.join( ', ' ), $( media_library_organizer_selectize_container ) ).each(
function() {
var delimiter = ( typeof $( this ).data( 'delimiter' ) !== 'undefined' ? $( this ).data( 'delimiter' ) : ',' );
$( this ).selectize( {
plugins: [ 'drag_drop', 'remove_button' ],
delimiter: delimiter,
persist: false,
create: function( input ) {
return {
value: input,
text: input
}
}
} );
}
);
/**
* Selectize Instances: WordPress AJAX Search
*/
$( media_library_organizer_selectize.selectors.ajax.join( ', ' ), $( media_library_organizer_selectize_container ) ).each(
function() {
var action = $( this ).data( 'action' ),
nonce = $( this ).data( 'nonce' ),
args = $( this ).data( 'args' ),
name = $( this ).attr( 'name' ),
name_field = $( this ).data( 'name-field' ),
value_field = $( this ).data( 'value-field' ),
method = $( this ).data( 'method' ),
output_fields = $( this ).data( 'output-fields' ).split( ',' ),
plugins = $( this ).data( 'plugins' ).split( ',' );
$( this ).selectize(
{
plugins: plugins,
delimiter: ',',
valueField: value_field, // The value to store in the select when the form is submitted.
labelField: name_field, // What to display on the output?
searchField: name_field, // For some reason, this has to be specified.
options: [],
create: false,
render: {
option: function( item, escape ) {
// Build string.
var output_string = [],
output_fields_length = output_fields.length;
for ( var i = 0; i < output_fields_length; i++ ) {
output_string.push( item[ output_fields[ i ] ] );
}
// Return output.
return '<div>' + output_string.join( ', ' ) + '</div>';
}
},
load: function( query, callback ) {
// Bail if the query is too short.
if ( ! query.length || query.length < 3 ) {
return callback();
}
// Send request to Plugin's AJAX endpoint.
$.ajax(
{
url: ajaxurl,
type: method,
dataType: 'json',
data: {
'action': action,
'nonce': nonce,
'query': query,
'args': args
},
error: function() {
callback();
},
success: function( result ) {
// If an error occured from the request, output it now.
if ( ! result.success ) {
alert( result.data );
}
callback( result.data );
}
}
);
},
/**
* Copy values to hidden field as a comma separated string, which might be used
*/
onChange: function( value ) {
// Bail if no hidden field.
if ( ! $( 'input[type=hidden]', this.$input.parent() ).length ) {
return;
}
if ( value === null || ! value.length ) {
$( 'input[type=hidden]', this.$input.parent() ).val( '' );
return;
}
// Implode into comma separated string.
$( 'input[type=hidden]', this.$input.parent() ).val( value.join() );
}
}
);
}
);
} )( jQuery );
}
/**
* Destroys selectize instances
*
* @since 1.0.7
*
* @param string container Destroy selectize instances within the given DOM element ID/class (optional)
*/
function mediaLibraryOrganizerSelectizeDestroy( container ) {
var media_library_organizer_selectize_container = ( typeof container != 'undefined' ? container : 'body' );
( function( $ ) {
/**
* Selectize Instances: Simple
*/
$( media_library_organizer_selectize.selectors.simple.join( ', ' ), $( media_library_organizer_selectize_container ) ).each(
function() {
if ( typeof this.selectize !== 'undefined' ) {
this.selectize.destroy();
}
}
);
/**
* Selectize Instances: Multiple Values
*/
$( media_library_organizer_selectize.selectors.multiple.join( ', ' ), $( media_library_organizer_selectize_container ) ).each(
function() {
if ( typeof this.selectize !== 'undefined' ) {
this.selectize.destroy();
}
}
);
/**
* Selectize Instances: WordPress AJAX Search
*/
$( media_library_organizer_selectize.selectors.ajax.join( ', ' ), $( media_library_organizer_selectize_container ) ).each(
function() {
if ( typeof this.selectize !== 'undefined' ) {
this.selectize.destroy();
}
}
);
} )( jQuery );
}