var a_w_f = {};
var awf_expanded_filters = [];
var awf_url_params = false;
jQuery( document ).ready( function( $ ) {
'use strict';
$( '#toplevel_page_annasta-filters, #toplevel_page_annasta-filters > a' ).addClass( 'wp-has-current-submenu' ).removeClass( 'wp-not-current-submenu' );
$( '#toplevel_page_annasta-filters .wp-first-item' ).addClass( 'current' );
a_w_f.get_filter_id = function ( el ) {
var filter = $( el ).closest( '.awf-filter-wrapper' ).attr( 'id' );
filter = filter.substring( 11 );
var filter_id = filter.split( '-' );
if( $.isArray( filter_id ) ) {
filter_id = filter_id.pop();
} else {
return '';
}
return filter_id;
};
a_w_f.display_admin_notice = function( msg, type ) {
if( 'undefined' === typeof( type ) || 0 === type.length ) { type = 'error'; }
$('
' ).insertBefore('#awf-settings');
$( 'html, body' ).animate({
scrollTop: $('#mainform').offset().top
}, 1000);
};
a_w_f.confirm_deletion = function() {
if ( 'undefined' !== typeof showNotice ) { return showNotice.warn(); }
else {
return !! confirm( 'Are you sure you want to proceed with this deletion?' );
}
return true;
};
a_w_f.rebuild_filter_style_options = function( $select ) {
var filter_id = a_w_f.get_filter_id( $select );
var preset_id = $( '#awf-preset-id' ).val();
var $style_container = $( '#awf-filter-' + preset_id + '-' + filter_id + '-style-container' );
var $style_options_cnt = $( '#awf_filter_' + preset_id + '-' + filter_id + '_style_options_container' );
$style_container.block({ message: '' });
$.ajax({
type: "post",
url: "admin-ajax.php",
dataType: "html",
data: {
action: 'awf_admin',
awf_action: 'rebuild-style-options',
awf_preset: preset_id,
awf_filter: filter_id,
awf_filter_type: $( '#awf_filter_' + preset_id + '-' + filter_id + '_type' ).val(),
awf_filter_style: $select.val(),
awf_ajax_referer: awf_js_data.awf_ajax_referer
},
success: function( response ) {
$style_container.unblock();
$style_options_cnt.html( response );
awf_set_style_events( $style_container );
},
error: function( response ) { console.log( response ); }
});
};
a_w_f.copy_to_clipboard = function( el ) {
var $temp_input = $("");
$( 'body' ).append( $temp_input );
$temp_input.val( $( el ).text() ).select();
document.execCommand( 'copy' );
$temp_input.remove();
var tip = $('').addClass('awf-tip-msg').text( $( el ).parent().data( 'tip' ) );
tip.appendTo( el ).fadeIn( 100 ).delay( 1000 ).fadeOut( 1000 ).queue(function() { $(this).remove(); });
};
a_w_f.set_new_url = function() {
var new_url = window.location.href.toString().replace( window.location.search.toString(), '?' + awf_url_params.toString() );
window.history.pushState( '', '', new_url );
};
a_w_f.change_icon_weight = function( $container, $checkbox ) {
var $input = $checkbox.closest( 'tr' ).find( '.awf-filter-icon' );
$input.toggleClass( 'awf-solid' );
if( $input.hasClass( 'awf-unselected-icon' ) ) {
$container.find( '.awf-unselected-icon-preview > span' ).toggleClass( 'awf-solid' );
} else if( $input.hasClass( 'awf-selected-icon' ) ) {
$container.find( '.awf-selected-icon-preview > span' ).toggleClass( 'awf-solid' );
}
};
if( 'undefined' !== typeof $.blockUI ) {
$.blockUI.defaults.overlayCSS = { backgroundColor: '#fff', opacity: 0.5 };
$.blockUI.defaults.css = { border: 'none' };
}
if ( 'URLSearchParams' in window ) {
awf_url_params = new URLSearchParams( window.location.search );
}
$('#awf-settings table.awf-preset-filters-table > tbody').sortable({
items: '> tr',
containment: 'parent',
handle: '.awf-filter-priority',
tolerance: 'pointer',
axis: 'y',
update: function() {
awf_update_filters_positions();
}
});
$( '#awf-add-association-btn' ).on( 'click', function() { awf_add_association(); });
$( '.awf-delete-association-btn' ).on( 'click', function() {
if( a_w_f.confirm_deletion() ) { awf_delete_association( $( this ) ); }
});
$( '#awf-add-filter' ).on( 'click', function() { awf_add_filter(); });
awf_set_filter_events( $( '.awf-preset-filters-table' ) );
$( '.awf-filter-style-container' ).each( function( i, el ) { awf_set_style_events( $( el ) ); });
if( false !== awf_url_params ) {
if( awf_url_params.has( 'awf-expanded-filters' ) ) {
awf_expanded_filters = awf_url_params.get( 'awf-expanded-filters' ).split('-');
awf_url_params.delete( 'awf-expanded-filters' );
awf_expanded_filters.forEach( function( filter_id ) {
$( '#awf-filter-' + $( '#awf-preset-id' ).val() + '-' + filter_id + ' .awf-filter-toggle-btn' ).trigger( 'click' );
});
}
if( awf_url_params.has( 'awf-goto' ) ) {
$( [document.documentElement, document.body] ).animate( { scrollTop: $( '#' + awf_url_params.get( 'awf-goto' ) ).offset().top - 25 }, 750, 'swing' );
awf_url_params.delete( 'awf-goto' );
a_w_f.set_new_url();
}
}
function awf_set_filter_events( $container ) {
$container.find( '.awf-delete-filter-btn' ).on( 'click', function() { if( a_w_f.confirm_deletion() ) { awf_delete_filter( $( this ) ); } } );
$container.find( '.awf-filter-toggle-btn' ).on( 'click', function() { awf_toggle_filter( $( this ) ); } );
$container.find( '.awf-collapse-filter-btn' ).on( 'click', function() { awf_toggle_filter( $( this ) ); } );
$container.find( '.awf-show-title' ).on( 'change', function() { $( this ).closest( 'tr' ).siblings( '.awf-is-collapsible-container' ).toggle(); });
$container.find( '.awf-exclude-filter-item-btn' ).on( 'click', function() { awf_exclude_filter_item( $( this ) ); } );
$container.find( '.awf-remove-from-excluded-btn' ).on( 'click', function() { awf_remove_from_excluded( $( this ) ); } );
$container.find( '.awf-filter-type-select' ).on( 'change', function() {
awf_rebuild_filter_type_and_styles( $( this ).closest( '.awf-filter-options' ) );
} );
$container.find( '.awf-filter-style-select' ).on( 'change', function() { a_w_f.rebuild_filter_style_options( $( this ) ); } );
$container.find( '.awf-style-options-btn' ).on( 'click', function() { $( this ).parents( '.awf-filter-style-container' ).toggleClass( 'awf-style-options-collapsed' ); });
$container.find( '.awf-add-ppp-value-btn' ).on( 'click', function() { awf_add_ppp_value( $( this ) ); });
$container.find( '.awf-remove-ppp-value-btn' ).on( 'click', function() { awf_remove_ppp_value( $( this ) ); });
if( 'premium' in a_w_f ) { a_w_f.set_premium_filter_events( $container ); }
}
function awf_set_style_events( $container ) {
var $filter_container = $container.closest( '.awf-filter-options' );
var type = $filter_container.find( '.awf-filter-type-select' ).val();
var style = $container.find( '.awf-filter-style-select' ).val();
var style_options_content = $.trim( $container.find( '.awf-style-options-container' ).first().html() );
if( style_options_content.length > 0 ) {
$container.removeClass( 'awf-style-options-collapsed' );
$container.removeClass( 'awf-hide-style-options-btn' );
} else {
$container.addClass( 'awf-style-options-collapsed' );
$container.addClass( 'awf-hide-style-options-btn' );
}
if( 'range' === type ) {
$filter_container.find('.awf-range-type-select').on( 'change', function() {
awf_rebuild_filter_type_and_styles( $filter_container );
});
$filter_container.find('.awf-add-custom-range-value-btn').on( 'click', function() { awf_add_custom_range_value( $( this ) ); });
$filter_container.find('.awf-delete-custom-range-value-btn').on( 'click', function() { awf_delete_custom_range_value( $( this ) ); });
}
if( 'icons' === style ) {
$container.find( '.awf-solid-icon' ).on( 'change', function() { a_w_f.change_icon_weight( $container, $( this ) ); });
$container.find( 'input.awf-unselected-icon' ).on( 'change', function() { awf_update_icon_preview( $container, $( this ) ); });
$container.find( '.awf-unselected-icon-preview' ).hover(
function() { awf_icon_preview_hover_in( $( this ), 'unselected' ); },
function() { awf_icon_preview_hover_out( $( this ), 'unselected' ); }
);
$container.find( 'input.awf-selected-icon' ).on( 'change', function() { awf_update_icon_preview( $container, $( this ) ); });
$container.find( '.awf-selected-icon-preview' ).hover(
function() { awf_icon_preview_hover_in( $( this ), 'selected' ); },
function() { awf_icon_preview_hover_out( $( this ), 'selected' ); }
);
$container.find( '.awf-icon-example' ).on( 'click', function() { a_w_f.copy_to_clipboard( $( this ) ); });
} else if( 'colours' === style ) {
$container.find('.awf-colorpicker').each( function( i, el ) {
$( el ).wpColorPicker();
} );
}
if( 'range-slider' === style ) {
console.log('in');
$filter_container.closest( '.awf-filter-wrapper' ).addClass( 'awf-range-slider-filter' );
awf_build_range_slider_previews();
// if( 'range-slider' === $filter_container.find( '.awf-filter-style-select' ).first().val() ) {
// $filter_container.closest( '.awf-filter-wrapper' ).addClass( 'awf-range-slider-filter' );
// } else {
// $filter_container.closest( '.awf-filter-wrapper' ).removeClass( 'awf-range-slider-filter' );
// }
} else {
$filter_container.closest( '.awf-filter-wrapper' ).removeClass( 'awf-range-slider-filter' );
}
awf_refresh_wc_help_tips( $container );
if( 'premium' in a_w_f ) { a_w_f.set_premium_style_events( style, $container ); }
}
function awf_add_association() {
$('.awf-spinner-overlay').show();
var new_association = $('#awf-associations-select').val();
$.ajax({
type: "post",
url: "admin-ajax.php",
dataType: "html",
data: {
action: 'awf_admin',
awf_action: 'add-preset-association',
awf_preset: $( '#awf-preset-id' ).val(),
awf_association: new_association,
awf_ajax_referer: awf_js_data.awf_ajax_referer,
},
success: function( response ) {
response = $.trim( response );
if( response ) {
if( response.startsWith( 'ERROR: ' ) ) {
response = response.replace( 'ERROR: ', '' );
a_w_f.display_admin_notice( response, 'error' );
} else {
awf_reload_associations( response );
}
$('.awf-spinner-overlay').hide();
}
},
error: function( response ) { console.log( response ); }
});
}
function awf_delete_association( $delete_btn ) {
$('.awf-spinner-overlay').show();
var association_id = $delete_btn.attr( 'data-association' );
$.ajax({
type: "post",
url: "admin-ajax.php",
data: {
action: 'awf_admin',
awf_action: 'delete-preset-association',
awf_ajax_referer: awf_js_data.awf_ajax_referer,
awf_preset: $( '#awf-preset-id' ).val(),
awf_association: association_id
},
success: function( response ) {
response = $.trim( response );
if( response ) {
if( response.startsWith( 'ERROR: ' ) ) {
response = response.replace( 'ERROR: ', '' );
a_w_f.display_admin_notice( response, 'error' );
} else {
awf_reload_associations( response );
}
$('.awf-spinner-overlay').hide();
}
},
error: function( response ) { console.log( response ); }
} );
}
function awf_reload_associations( html ) {
$( '.awf-associations-table' ).replaceWith( html );
$( '#awf-add-association-btn' ).on( 'click', function() { awf_add_association(); });
$( '.awf-delete-association-btn' ).on( 'click', function() {
if( a_w_f.confirm_deletion() ) { awf_delete_association( $( this ) ); }
});
}
function awf_update_filters_positions() {
$('.awf-spinner-overlay').show();
var positions = [];
$( '#awf-settings table.awf-preset-filters-table > tbody .awf-filter-container' ).each( function() {
positions.push( a_w_f.get_filter_id( this ) );
});
$.ajax({
type: "post",
url: "admin-ajax.php",
data: {
action: 'awf_admin',
awf_action: 'update_filters_positions',
awf_preset: $( '#awf-preset-id' ).val(),
awf_filters_positions: positions,
awf_ajax_referer: awf_js_data.awf_ajax_referer
},
success: function( response ) {
response = $.trim( response );
if( response ) {
a_w_f.display_admin_notice( response, 'error' );
} else {
var priority = 1;
$( '#awf-settings table.awf-preset-filters-table .awf-filter-priority' ).each( function() {
$( this ).html( priority++ );
});
$('.awf-spinner-overlay').hide();
}
},
error: function( response ) { console.log( response ); }
});
}
function awf_add_filter() {
var $filters_wrapper = $( '#awf-settings .awf-preset-filters-table' ).first();
$filters_wrapper.block({ message: '' });
var new_filter = $('#awf_filters_select').val();
$.ajax({
type: "post",
url: "admin-ajax.php",
dataType: "html",
data: {
action: 'awf_admin',
awf_action: 'add-filter',
awf_preset: $( '#awf-preset-id' ).val(),
awf_filter: new_filter,
awf_ajax_referer: awf_js_data.awf_ajax_referer
},
success: function( response ) {
response = $.trim( response );
if( response.startsWith( 'ERROR: ' ) ) {
response = response.replace( 'ERROR: ', '' );
a_w_f.display_admin_notice( response, 'error' );
} else {
var $new_filter_row = $( response ).appendTo( '.awf-preset-filters-table > tbody' );
awf_set_filter_events( $new_filter_row );
awf_set_style_events( $new_filter_row.find( '.awf-filter-style-container' ).first() );
$new_filter_row.find( '.awf-filter-toggle-btn' ).trigger( 'click' );
$new_filter_row.find( '.tips, .help_tip, .woocommerce-help-tip' ).tipTip({
attribute: "data-tip",
fadeIn: 50,
fadeOut: 50,
delay: 200
});
$( [document.documentElement, document.body] ).animate( { scrollTop: $new_filter_row.offset().top - 30 }, 1000);
$('#awf_filters_select option[value="' + new_filter + '"]').remove();
}
$filters_wrapper.unblock();
},
error: function( response ) { console.log( response ); }
});
}
function awf_delete_filter( $delete_btn ) {
var $filters_wrapper = $( '#awf-settings .awf-preset-filters-table' ).first();
$filters_wrapper.block({ message: '' });
var filter_id = a_w_f.get_filter_id( $delete_btn );
$.ajax({
type: "post",
url: "admin-ajax.php",
dataType: "json",
data: {
action: 'awf_admin',
awf_action: 'delete-filter',
awf_preset: $( '#awf-preset-id' ).val(),
awf_filter: filter_id,
awf_ajax_referer: awf_js_data.awf_ajax_referer
},
success: function( response ) {
if( response.hasOwnProperty('error') ) {
a_w_f.display_admin_notice( response.error );
} else {
$delete_btn.closest( '.awf-filter-wrapper' ).remove();
var new_option_atts = {
value: response.option_value,
text: response.option_label,
};
$( '#awf_filters_select' ).append( $( '