| 1 |
/** |
| 2 |
* Creates tribute.js autocompleters. |
| 3 |
* |
| 4 |
* @package WPZincDashboardWidget |
| 5 |
* @author WP Zinc |
| 6 |
*/ |
| 7 |
|
| 8 |
var wpzinc_autocompleters = []; |
| 9 |
var wpzinc_autocomplete_listeners = new Map(); |
| 10 |
|
| 11 |
/** |
| 12 |
* Sets up tribute.js autocompleters based on the |
| 13 |
* configuration stored in the localized wpzinc_autocomplete |
| 14 |
* global variable. |
| 15 |
* |
| 16 |
* Once setup, wp_zinc_autocomplete_initialize() can be used to initialize |
| 17 |
* them. They don't need to be setup again. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
function wp_zinc_autocomplete_setup() { |
| 22 |
|
| 23 |
wpzinc_autocompleters = []; |
| 24 |
|
| 25 |
wpzinc_autocomplete.forEach( |
| 26 |
function ( autocompleter, i ) { |
| 27 |
|
| 28 |
// Build collection. |
| 29 |
var collection = []; |
| 30 |
autocompleter.triggers.forEach( |
| 31 |
function ( trigger, j ) { |
| 32 |
// Don't include the opening trigger in the return value when selected. |
| 33 |
// This prevents e.g. {{something} when { is the trigger. |
| 34 |
trigger.selectTemplate = function ( item ) { |
| 35 |
return item.original.value; |
| 36 |
}; |
| 37 |
|
| 38 |
// Check where values are sourced from for this trigger. |
| 39 |
if ( 'url' in trigger ) { |
| 40 |
// Configure remote datasource. |
| 41 |
trigger.values = function ( text, cb ) { |
| 42 |
|
| 43 |
// Build form data. |
| 44 |
data = new FormData(); |
| 45 |
data.append( 'action', trigger.action ); |
| 46 |
data.append( 'nonce', trigger.nonce ); |
| 47 |
data.append( 'search', text ); |
| 48 |
|
| 49 |
// Send AJAX request. |
| 50 |
fetch( |
| 51 |
trigger.url, |
| 52 |
{ |
| 53 |
method: trigger.method, |
| 54 |
credentials: 'same-origin', |
| 55 |
body: data |
| 56 |
} |
| 57 |
).then( |
| 58 |
function ( response ) { |
| 59 |
return response.json(); |
| 60 |
} |
| 61 |
).then( |
| 62 |
function ( result ) { |
| 63 |
cb( result.data ); |
| 64 |
} |
| 65 |
).catch( |
| 66 |
function ( error ) { |
| 67 |
console.error( error ); |
| 68 |
} |
| 69 |
); |
| 70 |
|
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Add to collection. |
| 75 |
collection.push( trigger ); |
| 76 |
} |
| 77 |
); |
| 78 |
|
| 79 |
// Initialize autocompleter. |
| 80 |
var tribute = new Tribute( |
| 81 |
{ |
| 82 |
collection: collection |
| 83 |
} |
| 84 |
); |
| 85 |
|
| 86 |
// Store in array. |
| 87 |
wpzinc_autocompleters.push( |
| 88 |
{ |
| 89 |
fields: autocompleter.fields, |
| 90 |
instance: tribute |
| 91 |
} |
| 92 |
); |
| 93 |
|
| 94 |
} |
| 95 |
); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Attaches all registered tribute.js autocompleters. |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* |
| 104 |
* @param string container Only attach within the given container element. |
| 105 |
*/ |
| 106 |
function wp_zinc_autocomplete_initialize( container ) { |
| 107 |
|
| 108 |
wpzinc_autocompleters.forEach( |
| 109 |
function ( autocompleter, i ) { |
| 110 |
|
| 111 |
autocompleter.fields.forEach( |
| 112 |
function ( field, j ) { |
| 113 |
|
| 114 |
// If a container is supplied, only attach the autocompleter to the field within |
| 115 |
// the container. |
| 116 |
if ( typeof container !== 'undefined' ) { |
| 117 |
field = container + ' ' + field; |
| 118 |
} |
| 119 |
|
| 120 |
const elements = document.querySelectorAll( field ); |
| 121 |
|
| 122 |
// Attach Tribute to the elements. |
| 123 |
autocompleter.instance.attach( elements ); |
| 124 |
|
| 125 |
// Add event listener handlers to each attached element |
| 126 |
// which triggers a change event when an autocomplete suggestion |
| 127 |
// is inserted to the field. |
| 128 |
// This ensures page builders e.g. Elementor update their underlying |
| 129 |
// model and reflect the input value's change. |
| 130 |
elements.forEach( |
| 131 |
function (el) { |
| 132 |
const handler = function (e) { |
| 133 |
if (typeof jQuery !== 'undefined') { |
| 134 |
jQuery( e.target ).trigger( 'input' ); |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
el.addEventListener( 'tribute-replaced', handler ); |
| 139 |
|
| 140 |
// Store the handler for cleanup. |
| 141 |
wpzinc_autocomplete_listeners.set( el, handler ); |
| 142 |
} |
| 143 |
); |
| 144 |
|
| 145 |
} |
| 146 |
); |
| 147 |
|
| 148 |
} |
| 149 |
); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Detaches all tribute.js autocompleters |
| 155 |
* |
| 156 |
* @since 1.0.0 |
| 157 |
*/ |
| 158 |
function wp_zinc_autocomplete_destroy() { |
| 159 |
|
| 160 |
wpzinc_autocompleters.forEach( |
| 161 |
function ( autocompleter, i ) { |
| 162 |
|
| 163 |
autocompleter.fields.forEach( |
| 164 |
function ( field, j ) { |
| 165 |
|
| 166 |
const elements = document.querySelectorAll( field ); |
| 167 |
|
| 168 |
// Detach Tribute. |
| 169 |
autocompleter.instance.detach( elements ); |
| 170 |
|
| 171 |
// Remove our event listeners that might have been registered earlier. |
| 172 |
elements.forEach( |
| 173 |
function (el) { |
| 174 |
const handler = wpzinc_autocomplete_listeners.get( el ); |
| 175 |
if (handler) { |
| 176 |
el.removeEventListener( 'tribute-replaced', handler ); |
| 177 |
wpzinc_autocomplete_listeners.delete( el ); |
| 178 |
} |
| 179 |
} |
| 180 |
); |
| 181 |
|
| 182 |
} |
| 183 |
); |
| 184 |
|
| 185 |
} |
| 186 |
); |
| 187 |
|
| 188 |
wpzinc_autocompleters = []; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
// Setup and initialize. |
| 193 |
wp_zinc_autocomplete_setup(); |
| 194 |
wp_zinc_autocomplete_initialize(); |
| 195 |
|