');
});
result_object.append('');
// If the user clicks on a tag then add it to the box
$('.SO_result').click(function () {
// Check if the tag is already in the list
if (elem_parent.children('#tag_' + $(this).children('.SO_result_id').html()).length === 0) {
// It doesn't exist
// Add the tag
elem.parent('.inputbox').before('' + $(this).children('.SO_result_title').html() + 'x');
// Hide the results box
result_object = $('.SO_results');
result_object.hide();
result_object.html('');
// Reset the search bar
$(elem).val('');
$(elem).focus();
}
// Delete Button
delete_tag();
});
// Various keys
$(elem).keyup(function (e) {
if ($.inArray(e.keyCode, settings.break_keycodes) > -1) {
console.log(131);
// We need to assign this as we didnt click on it
var new_elem = ($('.SO_results').children(":first"));
// Check if the tag is already in the list
if ($('#tag_' + $(new_elem).children('.SO_result_id').html()).length === 0) {
// It doesn't exist
// Add the tag
elem.prev('.selected_tags').append('' + $(new_elem).children('.SO_result_title').html() + 'x');
// Hide the results box
result_object = elem.parent().next('.SO_results');
result_object.hide();
result_object.html('');
// Reset the search bar
$(elem).val('');
}
// Delete Button
delete_tag();
} else if (e.keyCode === 8) {
// Backspace so remove the last tag
if (elem.closest('.tag').hasClass('tag-highlight-delete')) {
// Delete the tag
elem.closest('.tag').remove();
} else {
// Add the delete class
elem.closest('.tag').addClass('tag-highlight-delete');
}
}
});
});
};
this.SO_init = function () {
var tags, form,
submitted = false;
if (elem.val().length > 0) {
// Convert default tags to actual tags
tags = elem.val();
// Split them up, accounting for all types
tags = tags.replace(", ", ",");
tags = tags.replace("; ", ",");
tags = tags.replace(";", ",");
tags = tags.replace(" ", "");
tags = tags.replace(" ", "");
// CSV to array
tags = tags.split(",");
tags.forEach(function (tag) {
// Get id and name
tag = tag.split("_");
var tag_id = tag[0],
tag_name = tag[1];
// Add the tag
elem.parent('.inputbox').before('' + tag_name + 'x');
});
// Reset the search bar
$(elem).val('');
}
form = $(elem).parents('form');
$(form).submit(function (e) {
//## TODO: For each input, create a seperate hidden field
// Select all the inputs
var elements = form.find('input[data-sotag!=""]'),
// Selected options holds the options while being processed
selected_options = [];
// Enter each loop
elements.each(function () {
elem.parents('.tag_input').children('.tag').each(function () { selected_options.push($(this).attr('id')); });
selected_options = selected_options.join(',');
$(form).append('');
// Reset the array
selected_options = [];
});
// Submit the form
if (submitted === false) {
$(form).submit();
}
submitted = true;
e.preventDefault();
});
// Now if the user starts typing show results
elem.bind('keyup', function () {
so_update_results();
});
delete_tag();
};
this.SO_init();
};
$.fn.sotag = function (options) {
return this.each(function () {
var element = $(this),
element_id = element.attr("name"),
selected_options = [],
element_parent,
form,
sotag;
// Prepare the html of the input
element.wrap('');
element.addClass('tag_input_text');
element.wrap('');
// Default CSS of the element
element.attr('autocomplete', 'off');
element_parent = element.parents('.tag_input');
element_parent.after('');
// Submitting forms
form = $(this).closest('form');
$(form).submit(function (e) {
element.children('.tag').each(function () { selected_options.push($(this).attr('id')); });
selected_options = selected_options.join(',');
$(form).append('');
// Empty the selected_options for the next one
selected_options.length = 0;
$(form).unbind().submit();
e.preventDefault();
});
// Return early if this element already has a plugin instance
if (element.data('sotag')) { return; }
// pass options to plugin constructor
sotag = new SoTag(this, element_parent, options);
// Store plugin object in this element's data
element.data('sotag', sotag);
});
};
}(jQuery));
/* jQuery(document).ready(function(){
jQuery('#tags').sotag({
autocomplete_URL:ajaxurl,
description : true
});
}); */