/**
*
* jQuery Chosen AJAX Autocomplete Library
*
* https://github.com/meltingice/ajax-chosen
* https://github.com/michaelperrin/ajax-chosen
*
* MIT License
*
* Customized by Codestar
*
*/
(function($) {
function CSFAjaxChosen(element, options) {
this.element = $(element);
this.options = options;
this.init();
};
CSFAjaxChosen.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();
};
CSFAjaxChosen.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();
});
};
CSFAjaxChosen.prototype.search_field_focused = function() {
this.search_welcome_message();
if ( this.options.min_length === 0 && this.search_field.val().length === 0 ) {
this.update_list();
}
};
CSFAjaxChosen.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) + '');
}
};
CSFAjaxChosen.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('csf-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 );
};
CSFAjaxChosen.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 ) {
$('').attr('value', item.value).html(item.text).appendTo(_this.element);
}
});
var value_before_trigger = this.search_field.val();
var width_before_trigger = this.search_field.innerWidth();
this.element.trigger('chosen:updated');
if( this.is_multiple ) {
var $hidden_select = this.element.parent().find('.csf-hide-select');
var $hidden_value = $hidden_select.val() || [];
this.element.CSFChosenOrder($hidden_value, true);
this.search_field.css('width', width_before_trigger);
}
this.search_field.val(value_before_trigger);
if ( this.chosenXhr.done !== null ) {
this.chosenXhr.done(items);
}
};
$.fn.CSFAjaxChosen = function(chosenOptions) {
return this.each(function() {
new CSFAjaxChosen(this, chosenOptions);
});
};
})(jQuery);
;// Chosen Order v1.2.1
// This plugin allows you to handle the order of the selection for Chosen multiple