Files
nolimit-discounts/assets/js/admin.js
T

88 lines
3.1 KiB
JavaScript

/**
* WooCommerce Email Discount Manager — admin scripts.
*/
( function( $ ) {
'use strict';
$( function() {
// ---------------------------------------------------------------
// Drag & drop sorting (categories + shipping methods).
// ---------------------------------------------------------------
$( '.wc-edm-sortable' ).sortable( {
handle: '.wc-edm-drag-handle',
axis: 'y',
placeholder: 'wc-edm-sortable-placeholder',
forcePlaceholderSize: true
} );
// ---------------------------------------------------------------
// Group edit: add / remove categories.
// ---------------------------------------------------------------
var $categorySelect = $( '#wc-edm-category-select' );
var $categoryList = $( '#wc-edm-selected-categories' );
$( '#wc-edm-add-category' ).on( 'click', function() {
var $option = $categorySelect.find( 'option:selected' );
var id = $option.val();
var name = $option.text().trim();
if ( ! id || $option.prop( 'disabled' ) ) {
return;
}
var $item = $( '<li>' )
.attr( 'data-id', id )
.append( $( '<span>' ).addClass( 'dashicons dashicons-menu wc-edm-drag-handle' ) )
.append( $( '<span>' ).addClass( 'wc-edm-item-label' ).text( name ) )
.append( $( '<button>' ).attr( 'type', 'button' ).addClass( 'wc-edm-remove-category' ).html( '&times;' ) )
.append( $( '<input>' ).attr( { type: 'hidden', name: 'category_ids[]' } ).val( id ) );
$categoryList.append( $item );
$option.prop( 'disabled', true );
$categorySelect.val( '' );
} );
$categoryList.on( 'click', '.wc-edm-remove-category', function() {
var $item = $( this ).closest( 'li' );
var id = String( $item.data( 'id' ) );
$categorySelect.find( 'option[value="' + id + '"]' ).prop( 'disabled', false );
$item.remove();
} );
// ---------------------------------------------------------------
// Email edit: toggle fields for the selected assignment type.
// ---------------------------------------------------------------
function toggleAssignmentFields() {
var type = $( '#wc-edm-assignment-type input[name="assignment_type"]:checked' ).val();
$( '.wc-edm-type-fields' ).each( function() {
$( this ).toggle( $( this ).data( 'type' ) === type );
} );
}
if ( $( '#wc-edm-assignment-type' ).length ) {
toggleAssignmentFields();
$( '#wc-edm-assignment-type' ).on( 'change', 'input[name="assignment_type"]', toggleAssignmentFields );
}
// ---------------------------------------------------------------
// Confirm before deleting.
// ---------------------------------------------------------------
$( document ).on( 'click', '.wc-edm-delete', function( e ) {
if ( ! window.confirm( wcEdmAdmin.confirmDelete ) ) {
e.preventDefault();
}
} );
// ---------------------------------------------------------------
// Shipping tab: auto-submit the group selector on change.
// ---------------------------------------------------------------
$( '#wc-edm-shipping-group' ).on( 'change', function() {
$( this ).closest( 'form' ).trigger( 'submit' );
} );
} );
}( jQuery ) );