(function ($) { 'use strict'; /** * Global doc.ready function */ $(document).ready(function () { $('.amp-settings-form').submit(function (e) { e.preventDefault(); $.post(amp_data.wp_post, $(this).serialize(), function (d) { UIkit.notify(" Your settings have been saved.", {pos: 'bottom-right', status: "success"}); }); }); // Set the selected option of selects $('select').each(function () { var attr = $(this).attr('data-value'); if (typeof attr !== typeof undefined && attr !== false && attr !== '') { $(this).val(attr); } }); $('#register_for_amp').submit(function(){ var result = false; var email = $.trim($('#register_aamp_email').val()); var name = $.trim($('#register_aamp_name').val()); if(name == ''){ $('.register_aamp_container .uk-alert').removeClass('uk-alert-success').addClass('uk-alert-danger').text('Name is required!').slideDown(); return false; } if(email == '' || !validateEmail(email)){ $('.register_aamp_container .uk-alert').removeClass('uk-alert-success').addClass('uk-alert-danger').text('Email Address Invalid').slideDown(); return false; } var data = {action: 'register_advanced_amp',email: email}; jQuery.post(amp_data.wp_get, data, function (response) { response = JSON.parse(response); if(response.status === true){ return true; } else if(response.status === false) { return false; } }); }); // Header Color Picker $('.spectrum').each(function () { var element = $(this); var value = element.val(); element.spectrum({ showInput: true, showAlpha: true, color: value, change: function (color) { element.val(color.toHexString()); } }); }); // Select Images $('.imageSelect').click(function (e) { e.preventDefault(); var target = $(this).data('target'); var image = wp.media({ title: 'Select Image', multiple: false }).open() .on('select', function (e) { // This will return the selected image from the Media Uploader, the result is an object var uploaded_image = image.state().get('selection').first(); // We convert uploaded_image to a JSON object to make accessing it easier // Output to the console uploaded_image var image_url = uploaded_image.toJSON().url; // Let's assign the url value to the input field $('#' + target).val(image_url); }); }); $('#yes_handle_non_secure').click(function () { $('.yes-options').fadeIn(); $('.options_to_deal_with_non_secure_content').prop('disabled', false); }); $('#no_handle_non_secure').click(function () { $('.yes-options').hide(); $('.options_to_deal_with_non_secure_content').prop('disabled', true); }); $('body').on('click', '#disable_menu_display', function(){ if($(this).is(':checked')){ $('#select_amp_menu_block').slideUp(); } else { $('#select_amp_menu').val('default'); $('#select_amp_menu_block').slideDown(); } }); $('body').on('click', '#disable_recent_posts', function(){ if($(this).is(':checked')){ $('#relevancy_basis').slideUp(); } else { $('#relevancy_basis').slideDown(); } }); // Run Mobile Test $('.uk-button-run-test').click(function () { var button = $(this); button.attr('disabled', 'disabled'); button.find('i').removeClass('fa-play').addClass('fa-spin').addClass('fa-refresh'); var website = $('#mobile-test-url').val(); if (website == "") { UIkit.notify(" URL cannot be empty!", {pos: 'bottom-right', status: "error"}); button.removeAttr('disabled'); button.find('i').addClass('fa-play').removeClass('fa-spin').removeClass('fa-refresh'); return null; } var data = [{ name: 'action', value: 'amp_mobile_test' }, { name: 'website', value: website }]; $.post(amp_data.wp_post, data, function (d) { button.removeAttr('disabled'); button.find('i').addClass('fa-play').removeClass('fa-spin').removeClass('fa-refresh'); if (d.hasOwnProperty('status')) { if (d.status == 'ERROR') { UIkit.notify(" " + d.message, { pos: 'bottom-right', status: "error" }); } } else { d = JSON.parse(d); if (d.hasOwnProperty('responseCode')) { if (d.responseCode == 200) { // Give the link to screenshot button $('.mobile-screenshot-button').attr('href', website); // Apply the logic here var container = $('.mobile-results'); // Apply the Passed/Not Passed Status container.find('.mobile-status').removeClass('uk-text-danger').removeClass('uk-text-success'); if (d.ruleGroups.USABILITY.pass == true) { container.find('.mobile-status').addClass('uk-text-success').html(' Your URL has passed the Mobile Friendly Test!'); } else { container.find('.mobile-status').addClass('uk-text-danger').html(' Your URL did not pass the Mobile Friendly Test!'); } // Apply the progress bar changes container.find('.uk-progress-bar').css('width', d.ruleGroups.USABILITY.score + '%').html('Score: ' + d.ruleGroups.USABILITY.score + ' of 100'); // Show the problems if any var problems = $('.mobile-problems'); var problem_list = $('.mobile-problems-list'); if (d.ruleGroups.USABILITY.score < 100) { problems.slideDown(); problem_list.empty(); problem_list.append('
Failed Tests:
'); for (var key in d.formattedResults.ruleResults) { var result = d.formattedResults.ruleResults[key]; if (result.ruleImpact > 0) { problem_list.append('
' + result.localizedRuleName + ' (-' + parseInt(result.ruleImpact) + ')
'); } } } else { problems.slideUp(); } // Show the things that are okay var success_list = $('.mobile-success-list'); success_list.empty(); success_list.append('
Verified Tests:
'); for (var key in d.formattedResults.ruleResults) { var result = d.formattedResults.ruleResults[key]; if (result.ruleImpact == 0) { success_list.append('
' + result.localizedRuleName + '
'); } } // Display the screenshot var screenshot = $('.mobile-screenshot'); screenshot.attr('src', 'data:image/jpeg;charset=utf-8;base64,' + d.screenshot.data.replace(/\_/g, '/').replace(/\-/g, '+')); container.slideDown(); } else { UIkit.notify(" An unknown problem occurred. Please try again later.", {pos: 'bottom-right', status: "error"}); } } } }); }); }); })(jQuery); function validateEmail(email) { var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); }