| 1 |
/** |
| 2 |
* Register an AutocompleterControl that can be used in place of |
| 3 |
* a TextControl when a dropdown list of optional choices are required |
| 4 |
* |
| 5 |
* @since 1.0.0 |
| 6 |
* |
| 7 |
* @param object atts TextControl compatible attributes that would be passed to a TextControl, |
| 8 |
* plus an 'options' property comprising of an array of values to display in |
| 9 |
* the autocomplete dropdown |
| 10 |
* { |
| 11 |
* label: field.label, |
| 12 |
* help: field.description, |
| 13 |
* value: props.attributes[ attribute ], |
| 14 |
* list: <unique string> |
| 15 |
* options: [ |
| 16 |
* 'value', |
| 17 |
* 'value2' |
| 18 |
* ] |
| 19 |
* } |
| 20 |
* @param array options Autocomplete Choices |
| 21 |
* |
| 22 |
* @package WPZincDashboardWidget |
| 23 |
* @author WP Zinc |
| 24 |
*/ |
| 25 |
|
| 26 |
var WPZincAutocompleterControl = function ( atts ) { |
| 27 |
|
| 28 |
// Define some constants for the various items we'll use. |
| 29 |
const el = window.wp.element.createElement; |
| 30 |
const { |
| 31 |
TextControl |
| 32 |
} = window.wp.components; |
| 33 |
|
| 34 |
// Build options. |
| 35 |
var options = []; |
| 36 |
for ( var i in atts.options ) { |
| 37 |
options.push( |
| 38 |
el( |
| 39 |
'option', |
| 40 |
{ |
| 41 |
value: atts.options[ i ], |
| 42 |
key: atts.id + '_' + i |
| 43 |
}, |
| 44 |
atts.options[ i ] |
| 45 |
) |
| 46 |
) |
| 47 |
} |
| 48 |
|
| 49 |
// Unset options from attributes. |
| 50 |
delete atts.options; |
| 51 |
|
| 52 |
// Return the TextControl with a <datalist>, which will |
| 53 |
// provide the autocomplete functionality required. |
| 54 |
return [ |
| 55 |
el( |
| 56 |
TextControl, |
| 57 |
atts |
| 58 |
), |
| 59 |
el( |
| 60 |
'datalist', |
| 61 |
{ |
| 62 |
id: atts.list |
| 63 |
}, |
| 64 |
options |
| 65 |
) |
| 66 |
]; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Append an Autocompleter to Gutenberg Blocks' Autocompleters, |
| 72 |
* using the global `wpzinc_autocomplete` array. |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
* |
| 76 |
* @param array completers Completers. |
| 77 |
* @param string blockName Block Name. |
| 78 |
*/ |
| 79 |
function wp_zinc_auto_complete_gutenberg_register( completers, blockName ) { |
| 80 |
|
| 81 |
wpzinc_autocomplete_gutenberg.forEach( |
| 82 |
function ( autocompleter, i ) { |
| 83 |
|
| 84 |
autocompleter.triggers.forEach( |
| 85 |
function ( trigger, j ) { |
| 86 |
|
| 87 |
// Skip if this uses a remote data source. |
| 88 |
if ( 'url' in trigger ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Add to Gutenberg's Autocompleters. |
| 93 |
completers.push( |
| 94 |
{ |
| 95 |
name: trigger.name, |
| 96 |
triggerPrefix: trigger.trigger, |
| 97 |
options: trigger.values, |
| 98 |
|
| 99 |
/** |
| 100 |
* How options should be matched |
| 101 |
*/ |
| 102 |
getOptionKeywords: function ( option ) { |
| 103 |
|
| 104 |
return option.value; |
| 105 |
|
| 106 |
}, |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns the option label to display in the autocomplete |
| 110 |
* drop down. |
| 111 |
* |
| 112 |
* @since 1.0.0 |
| 113 |
* |
| 114 |
* @param object option Autocomplete Option. |
| 115 |
* @return string Label |
| 116 |
*/ |
| 117 |
getOptionLabel: function ( option ) { |
| 118 |
|
| 119 |
return option.value; |
| 120 |
|
| 121 |
}, |
| 122 |
|
| 123 |
/** |
| 124 |
* Appends the returned content to the current block |
| 125 |
* that the user is editing, when the user clicks |
| 126 |
* the autocomplete option. |
| 127 |
* |
| 128 |
* @since 1.0.0 |
| 129 |
* |
| 130 |
* @param object option Chosen Autocomplete Option. |
| 131 |
* @return string Value |
| 132 |
*/ |
| 133 |
getOptionCompletion: function ( option ) { |
| 134 |
|
| 135 |
return option.key; |
| 136 |
|
| 137 |
}, |
| 138 |
|
| 139 |
} |
| 140 |
); |
| 141 |
|
| 142 |
} |
| 143 |
); |
| 144 |
|
| 145 |
} |
| 146 |
); |
| 147 |
|
| 148 |
return completers; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Registers our Autocomplete Gutenberg Block Filter |
| 154 |
* for the Post Editor. |
| 155 |
* |
| 156 |
* @since 1.0.0 |
| 157 |
*/ |
| 158 |
if ( typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined' && typeof wpzinc_autocomplete_gutenberg !== 'undefined' ) { |
| 159 |
|
| 160 |
wpzinc_autocomplete_gutenberg.forEach( |
| 161 |
function ( autocompleter, i ) { |
| 162 |
|
| 163 |
autocompleter.triggers.forEach( |
| 164 |
function ( trigger, j ) { |
| 165 |
|
| 166 |
wp.hooks.addFilter( |
| 167 |
'editor.Autocomplete.completers', |
| 168 |
'wp-zinc/autocompleters/' + trigger.name, |
| 169 |
wp_zinc_auto_complete_gutenberg_register |
| 170 |
); |
| 171 |
|
| 172 |
} |
| 173 |
); |
| 174 |
|
| 175 |
} |
| 176 |
); |
| 177 |
|
| 178 |
} |
| 179 |
|