jQuery(document).ready(function($) {
// Add standard filters
jQuery(".add-field-manipulation").on('click', function(){
var nonce = $('#_wpnonce').val();
var TrueRowCount = $('#woosea-ajax-table >tbody >tr').length-1;
var rowCount = Math.round(new Date().getTime() + (Math.random() * 100));
var plusCount = Math.round(new Date().getTime() + (Math.random() * 100));
jQuery.ajax({
method: "POST",
url: ajaxurl,
data: {
'action': 'woosea_ajax',
'security': nonce,
'rowCount': rowCount
}
})
.done(function( data ) {
data = JSON.parse( data );
if(TrueRowCount == 0){
$( '#woosea-ajax-table' ).find('tbody:first').append('
| | | | |
');
} else {
$(' | | | | |
').insertBefore(".rules-buttons");
}
// Check if user selected a data manipulation condition
jQuery(".field_manipulation_extra_" + rowCount).on("click", function(){
plusCount = Math.round(new Date().getTime() + (Math.random() * 100));
jQuery(".becomes_fields_" + rowCount).append('
');
});
})
.fail(function( data ) {
console.log('Failed AJAX Call :( /// Return Data: ' + data);
});
});
// Add extra fields to existing field manipulations
jQuery(".field_extra").on('click', function(){
var nonce = $('#_wpnonce').val();
var className = $(this).attr("class").split(' ')[3];
var rowCount = className.split("_")[3];
var plusCount = Math.round(new Date().getTime() + (Math.random() * 100));
jQuery.ajax({
method: "POST",
url: ajaxurl,
data: {
'action': 'woosea_ajax',
'security': nonce,
'rowCount': rowCount
}
})
.done(function( data ) {
data = JSON.parse( data );
jQuery(".becomes_fields_" + rowCount).append('
');
})
.fail(function( data ) {
console.log('Failed AJAX Call :( /// Return Data: ' + data);
});
});
// Add standard filters
jQuery(".add-filter").on('click',function(){
var nonce = $('#_wpnonce').val();
// Count amount of rows, used to create the form array field and values
var TrueRowCount = $('#woosea-ajax-table >tbody >tr').length-1;
var rowCount = Math.round(new Date().getTime() + (Math.random() * 100));
jQuery.ajax({
method: "POST",
url: ajaxurl,
data: {
'action': 'woosea_ajax',
'security': nonce,
'rowCount': rowCount
}
})
.done(function( data ) {
data = JSON.parse( data );
if(TrueRowCount == 0){
$( '#woosea-ajax-table' ).find('tbody:first').append(' | Filter: | | | | | | |
');
} else {
$(' | Filter: | | | | | | |
').insertBefore( ".rules-buttons");
}
// Check if user selected a data manipulation condition
jQuery("#rules_" + rowCount).on("change", function(){
if ($(this).val() == "categories") {
var checkNumeric = $.isNumeric(rowCount);
if(checkNumeric) {
jQuery.ajax({
method: "POST",
url: ajaxurl,
data: {
'action': 'woosea_categories_dropdown',
'rowCount': rowCount
}
})
.done(function( data ) {
data = JSON.parse( data );
jQuery("#criteria_" + rowCount).replaceWith('' + data.dropdown + '');
});
}
}
});
})
.fail(function( data ) {
console.log('Failed AJAX Call :( /// Return Data: ' + data);
});
});
// Add rules
jQuery(".add-rule").on('click',function(){
var nonce = $('#_wpnonce').val();
// Count amount of rows, used to create the form array field and values
var TrueRowCount = $('#woosea-ajax-table >tbody >tr').length-1;
var rowCount = Math.round(new Date().getTime() + (Math.random() * 100));
jQuery.ajax({
method: "POST",
url: ajaxurl,
data: {
'action': 'woosea_ajax',
'security': nonce,
'rowCount': rowCount
}
})
.done(function( data ) {
data = JSON.parse( data );
if(TrueRowCount == 0){
$( '#woosea-ajax-table' ).find('tbody:first').append(' | Rule: | | | | | | |
');
} else {
$(' | Rule: | | | | | | |
').insertBefore( ".rules-buttons");
}
// Check if user selected a data manipulation condition
jQuery("#condition_" + rowCount).on("change", function(){
var manipulators = ['multiply', 'divide', 'plus', 'minus'];
var cond = $(this).val();
// User selected a data manipulation value so remove some input fields
if(jQuery.inArray(cond, manipulators) != -1){
jQuery("#than_attribute_" + rowCount).remove();
jQuery("#is-field_" + rowCount).remove();
jQuery("#cs_" + rowCount).remove();
}
// Replace pieces of string
var modifiers = ['replace'];
if(jQuery.inArray(cond, modifiers) != -1){
jQuery("#than_attribute_" + rowCount).remove();
jQuery("#cs_" + rowCount).remove();
}
});
// Check if user created a Google category rule
jQuery("#than_attribute_" + rowCount).on("change", function(){
if ($(this).val() == "google_category") {
var rownr = $(this).closest("tr").prevAll("tr").length;
$("#is-field_" + rowCount).replaceWith('');
jQuery(".js-autosuggest").on('click', function(){
var rowCount = $(this).closest("tr").prevAll("tr").length;
jQuery( ".autocomplete_" + rowCount ).typeahead({
input: '.js-autosuggest',
source: google_taxonomy,
hint: true,
loadingAnimation: true,
items: 10,
minLength: 2,
alignWidth: false,
debug: true
});
jQuery( ".autocomplete_" + rowCount ).focus();
jQuery(this).keyup(function (){
var minimum = 5;
var len = jQuery(this).val().length;
if (len >= minimum){
jQuery(this).closest("input").removeClass("input-field-large");
jQuery(this).closest("input").addClass("input-field-large-active");
} else {
jQuery(this).closest("input").removeClass("input-field-large-active");
jQuery(this).closest("input").addClass("input-field-large");
}
});
jQuery(this).click(function (){
var len = jQuery(this).val().length;
if (len < 1){
jQuery(this).closest("input").removeClass("input-field-large-active");
jQuery(this).closest("input").addClass("input-field-large");
}
});
});
}
});
})
.fail(function( data ) {
console.log('Failed AJAX Call :( /// Return Data: ' + data);
});
});
// Find and remove selected table rows
jQuery(".delete-row").on('click',function(){
//$("table tbody").find('input[name="record"]').each(function(){
$(".woo-product-feed-pro-body").find('input[name="record"]').each(function(){
if($(this).is(":checked")){
$(this).parents("tr").remove();
}
});
});
});