| 1 |
// noinspection JSUnresolvedReference,CommaExpressionJS,SpellCheckingInspection |
| 2 |
|
| 3 |
jQuery(window).on('elementor:init', (function () { |
| 4 |
"use strict"; |
| 5 |
let kingaddonsAjaxSelect2 = elementor.modules.controls.BaseData.extend({ |
| 6 |
onReady: function () { |
| 7 |
let $this = this, |
| 8 |
$select = $this.ui.select, |
| 9 |
restUrl = $select.attr('data-rest-url'), |
| 10 |
nonce = window.wpApiSettings.nonce; |
| 11 |
|
| 12 |
let querySlug = '' !== $select.attr('data-query-slug') ? $select.attr('data-query-slug') : ''; |
| 13 |
|
| 14 |
$select.select2({ |
| 15 |
ajax: { |
| 16 |
url: restUrl, |
| 17 |
dataType: 'json', |
| 18 |
headers: { |
| 19 |
'X-WP-Nonce': nonce |
| 20 |
}, |
| 21 |
data: function (response) { |
| 22 |
return { |
| 23 |
s: response.term, |
| 24 |
query_slug: querySlug |
| 25 |
} |
| 26 |
} |
| 27 |
}, |
| 28 |
cache: !0 |
| 29 |
}); |
| 30 |
|
| 31 |
let controlValue = void 0 !== $this.getControlValue() ? $this.getControlValue() : ''; |
| 32 |
|
| 33 |
controlValue.isArray && (controlValue = $this.getControlValue().join()), jQuery.ajax({ |
| 34 |
url: restUrl, |
| 35 |
dataType: 'json', |
| 36 |
headers: { |
| 37 |
'X-WP-Nonce': nonce |
| 38 |
}, |
| 39 |
data: { |
| 40 |
ids: String(controlValue), |
| 41 |
query_slug: querySlug |
| 42 |
} |
| 43 |
}).then((function (response) { |
| 44 |
if ('' !== controlValue) { |
| 45 |
null !== response && response.results.length > 0 && (jQuery.each(response.results, (function (index, element) { |
| 46 |
let option = new Option(element.text, element.id, !0, !0); |
| 47 |
$select.append(option); |
| 48 |
$select.append(option).trigger('change.select2'); |
| 49 |
$select.val(controlValue).trigger('change.select2'); |
| 50 |
})), $select.trigger({ |
| 51 |
type: 'select2:select', |
| 52 |
params: { |
| 53 |
data: response |
| 54 |
} |
| 55 |
})) |
| 56 |
} |
| 57 |
})) |
| 58 |
}, |
| 59 |
onBeforeDestroy: function () { |
| 60 |
this.ui.select.data('select2') && this.ui.select.select2('destroy'), this.el.remove() |
| 61 |
} |
| 62 |
}); |
| 63 |
|
| 64 |
elementor.addControlView('king-addons-ajax-select2', kingaddonsAjaxSelect2) |
| 65 |
})); |