autocomplete.js
3.18 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
(function( $ ) { 'use strict';
/* https://autocomplete.trevoreyre.com/#/javascript-component?id=getresultvalue */
if ( typeof( mclocations ) !== 'undefined' ) {
new Autocomplete( '#mc-locations-autocomplete', {
search: input => {
const url = mclocations.ajaxurl;
return new Promise( resolve => {
if (input.length < 3) {
return resolve([])
}
var data = new FormData();
data.append( 'action', mclocations.action );
data.append( 'security', mclocations.security );
data.append( 'data', input );
const response = fetch(url, {
method: 'POST',
credentials: 'same-origin',
body: data
}).then(response => response.json())
.then(data => {
resolve(data.response)
})
})
},
onSubmit: result => {
var location_field = document.getElementById( 'mc_event_location_value' );
location_field.value = result.location_id;
$( location_field ).trigger( 'change' );
},
getResultValue: result => ( '' !== result.location_label ) ? result.location_label : '#' + result.location_id
});
}
if ( typeof( mcpages ) !== 'undefined' ) {
/* https://autocomplete.trevoreyre.com/#/javascript-component?id=getresultvalue */
new Autocomplete( '#mc-pages-autocomplete', {
search: input => {
const url = mcpages.ajaxurl;
return new Promise( resolve => {
if (input.length < 3) {
return resolve([])
}
var data = new FormData();
data.append( 'action', mcpages.action );
data.append( 'security', mcpages.security );
data.append( 'data', input );
const response = fetch(url, {
method: 'POST',
credentials: 'same-origin',
body: data
}).then(response => response.json())
.then(data => {
resolve(data.response)
})
})
},
onSubmit: result => {
var pages_field = document.getElementById( 'mc_uri_id' );
pages_field.value = result.post_id;
$( pages_field ).trigger( 'change' );
},
getResultValue: result => ( '' !== result.post_title ) ? result.post_title : '#' + result.post_id
});
}
if ( typeof( mcicons ) !== 'undefined' ) {
/* https://autocomplete.trevoreyre.com/#/javascript-component?id=getresultvalue */
new Autocomplete( '#mc-icons-autocomplete', {
search: input => {
const url = mcicons.ajaxurl;
return new Promise( resolve => {
if (input.length < 2) {
return resolve([])
}
var data = new FormData();
data.append( 'action', mcicons.action );
data.append( 'security', mcicons.security );
data.append( 'data', input );
const response = fetch(url, {
method: 'POST',
credentials: 'same-origin',
body: data
}).then(response => response.json())
.then(data => {
resolve(data.response)
})
})
},
onSubmit: result => {
var icon_field = document.getElementById( 'mc_category_icon' );
icon_field.value = result.filename;
$( icon_field ).trigger( 'change' );
},
renderResult: (result, props) => `
<li ${props}>${result.svg} ${result.filename}</li>
`,
getResultValue: result => result.filename
});
}
}(jQuery));