Your IP : 3.138.61.163


Current Path : /home/church/www/offwins.com/wp-content/plugins/adunblocker/admin/js/
Upload File :
Current File : /home/church/www/offwins.com/wp-content/plugins/adunblocker/admin/js/adunblocker-admin-license.js

(function( $ ) {
    
    'use strict';

    var notify = false;
    var processing = false;
    var events = {
        '.validate-license': {
            'action'    : 'click',
            'callback'  : validate_license
        }
    };

    var submit_button;
    
    document.addEventListener( 'DOMContentLoaded', function() {
        submit_button = $( '#submit' );
        initEvents();
        verify();

    });

    function verify() {

        submit_request( {
            action: get_action( 'verify' ),
            nonce: daau_license_app.nonces.validate_license
        } );

    }

    function initEvents() {
        
        for( var selector in events ) {
            if( $( selector ).length ) {
                $( selector ).on( events[selector]['action'], events[selector]['callback'] );
            }
        }

    }

    function disable_button() {
        
        submit_button.disabled = true;

    }

    function enable_button() {

        submit_button.disabled = false;

    }

    function add_spinner() {

        var spinner = $( '.daabd-admin-body .spinner' );

        if( ! spinner.length ) {
            var span = document.createElement( "span" );
            span.classList.add( 'spinner' );
            span.classList.add( 'is-active' );
            submit_button.after( span );
        }

    }

    function remove_spinner() {

        var spinner = $( '.daabd-admin-body .spinner' );

        if( spinner.length ) {

            spinner.remove();

        }

    }

    function show_notice( type, message ) {

            var admin_body = document.querySelector( '.daabd-admin-body' );

            if( ! admin_body.length ) {

                var div = document.createElement( 'div' );
                div.classList.add( 'notice' );
                
                if( type == 'error') {

                    div.classList.add( 'notice-error' );

                }

                if( type == 'success') {
                    
                    div.classList.add( 'notice-success' );

                }

                div.classList.add( 'is-dismissible' );

                var p = document.createElement( 'p' );
                p.textContent = message;
                
                if( notify ) {
                    
                    div.appendChild( p );
                    admin_body.appendChild( div );

                }

            }

    }

    function set_processing( value ) {
        processing = value;
    }

    function validate_license( e ) {
        
        e.preventDefault();

        disable_button();
        add_spinner();

        // show notices in admin
        notify = true;

        var email = '';
        var key = '';
        var submit = true;

        if( $( '#adunblocker-license-email').length ) {
            email = $( '#adunblocker-license-email' ).val();
        } else {
            submit = false;
        }

        if( $( '#adunblocker-license-key').length ) {
            key = $( '#adunblocker-license-key' ).val();
        } else {
            submit = false;
        }

        if( submit ) {

            submit_request( {
                license: {
                    email: email,
                    key: key
                },
                action: get_action( 'validate' ),
                nonce: daau_license_app.nonces.validate_license
            } );

        }
    }

    function submit_request( payload ) {
        console.log( payload );
        if( ! processing ) {
            
            set_processing( true );
        
            $.ajax( {
                url: ajaxurl,
                type: 'POST',
                dataType: 'json',
                cache: false,
                data: payload,
                    error: function( jqXHR, textStatus, errorThrown ) {
                        
                        console.log( errorThrown );
                        set_processing( false );
                        remove_spinner();
                        enable_button();
                        show_notice( "error", errorThrown );

                    },
                    success: function( data ) {
                        
                        set_processing( false );
                        remove_spinner();
                        enable_button();

                        if( data !== null ) {
                            console.log( data );
                            show_notice( data.status, data.message );                        

                            if( data.redirect == "true" ) {

                                redirect();
                                
                            }

                        }
                        
                    }

            } );

        }

    }

    function redirect() {
        
        setTimeout(function(){
            
            window.location.replace("?page=adunblocker-general-settings-page");

        }, 3000 );

    }

    function get_action( action_name ) {
        switch ( action_name ) {
            case 'validate':
                return 'daau_validate_license';
                break;
        
            default:
                return 'daau_verify_token';
                break;
        }
    }

})( jQuery );