'use strict'; jQuery( function( $ ) { var AAL = { $wrapper: {}, $container: {}, conter: 0, init: function () { var _this = this; AAL.$wrapper = $( ".aal-notifier-settings" ); AAL.$container = $( "ul", AAL.$wrapper ); AAL.counter = AAL.$container.children().length; // check if there's only one option if ( 1 === AAL.counter ) { var $temp_el = AAL.$container.children().first(); // check if the "value" select box has no options if ( 0 === $temp_el.find( ".aal-value option" ).length ) { // click the button with a timeout. Note that this is a hack that will need // to be solved server-side setTimeout( function () { $temp_el.find( ".aal-category" ).change(); }, 300 ); } } // when the "add" button is clicked AAL.$container.on( 'click', '.aal-new-rule', function ( e ) { e.preventDefault(); _this.addRule( $( this ).closest( 'li' ) ); }); AAL.$container.on( 'click', '.aal-delete-rule', function ( e ) { e.preventDefault(); // do not delete item if it's the only one left in the list if ( 1 === AAL.$container.children().length ) { return; } _this.deleteRule( $( this ).closest( 'li' ) ); }); // handle change on action category selectbox AAL.$container.on( 'change', '.aal-category', function ( e ) { e.preventDefault(); var $select = $( this ), $siblings = $select.siblings( "select" ); // disable all selectboxes to prevent multiple calls $siblings.filter( "select" ).prop( 'disabled', true ); // grab live data via AJAX var data = _this.getData( $select.val(), function ( d ) { var $target = $siblings.filter( '.aal-value' ); $target.empty(); // clear so we can insert fresh data $.each( d.data, function ( k, v ) { $target.append( $( "