/** * * jQuery Chosen AJAX Autocomplete Library * * https://github.com/meltingice/ajax-chosen * https://github.com/michaelperrin/ajax-chosen * * MIT License * * Customized by SPF * */ ;(function ($) { function SP_PCAjaxChosen (element, options) { this.element = $(element) this.options = options this.init() } SP_PCAjaxChosen.prototype.init = function () { this.element.chosen(this.options) this.container = this.element.next('.chosen-container') this.search_field = this.container.find('.chosen-search-input') this.is_multiple = this.container.hasClass('chosen-container-multi') this.is_typing = false this.chosenXhr = null this.events() } SP_PCAjaxChosen.prototype.events = function () { var _this = this this.search_field.on('compositionstart', function () { _this.is_typing = true }) this.search_field.on('compositionend', function () { _this.is_typing = false _this.update_list() }) this.search_field.on('keyup', function () { _this.update_list() }) this.search_field.on('focus', function () { _this.search_field_focused() }) } SP_PCAjaxChosen.prototype.search_field_focused = function () { this.search_welcome_message() if (this.options.min_length === 0 && this.search_field.val().length === 0) { this.update_list() } } SP_PCAjaxChosen.prototype.search_welcome_message = function () { var value = $.trim(this.search_field.val()) var results = this.container.find('.chosen-results') if (results.children().length === 0 && value.length === 0) { results.html( '
  • ' + this.options.typing_text.replace( '%s', this.options.min_length - value.length ) + '
  • ' ) } } SP_PCAjaxChosen.prototype.update_list = function () { var _this = this this.search_welcome_message() if (this.is_typing) { return } var value = $.trim(this.search_field.val()) var message = value.length < this.options.min_length ? this.options.typing_text.replace( '%s', this.options.min_length - value.length ) : this.options.searching_text this.container.find('.no-results').text(message) if (value === this.search_field.data('prevVal')) { return } this.search_field.data('prevVal', value) if (this.timer) { clearTimeout(this.timer) } if (value.length < this.options.min_length) { return } this.timer = setTimeout(function () { if (_this.chosenXhr) { _this.chosenXhr.abort() } _this.options.data['term'] = value _this.chosenXhr = window.wp.ajax .post('spf-chosen', _this.options.data) .done(function (response) { _this.show_results(response) }) .fail(function (response) { _this.container.find('.no-results').text(response.error) }) }, this.options.type_delay) } SP_PCAjaxChosen.prototype.show_results = function (items) { var _this = this if (this.is_typing || items === null) { return } if (items.length === 0) { this.element.data().chosen.no_results_clear() this.element.data().chosen.no_results(this.search_field.val()) return } var selected_values = [] this.element.find('option').each(function () { if ($(this).is(':selected')) { selected_values.push($(this).val() + '-' + $(this).text()) } else { if ($(this).attr('value').length) { $(this).remove() } } }) $.each(items, function (i, item) { if ($.inArray(item.value + '-' + item.text, selected_values) === -1) { $('