|
| 1 | +(function( $ ) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + /** |
| 5 | + * All of the code for your admin-facing JavaScript source |
| 6 | + * should reside in this file. |
| 7 | + * |
| 8 | + * Note: It has been assumed you will write jQuery code here, so the |
| 9 | + * $ function reference has been prepared for usage within the scope |
| 10 | + * of this function. |
| 11 | + * |
| 12 | + * This enables you to define handlers, for when the DOM is ready: |
| 13 | + * |
| 14 | + * $(function() { |
| 15 | + * |
| 16 | + * }); |
| 17 | + * |
| 18 | + * When the window is loaded: |
| 19 | + * |
| 20 | + * $( window ).load(function() { |
| 21 | + * |
| 22 | + * }); |
| 23 | + * |
| 24 | + * ...and/or other possibilities. |
| 25 | + * |
| 26 | + * Ideally, it is not considered best practise to attach more than a |
| 27 | + * single DOM-ready or window-load handler for a particular page. |
| 28 | + * Although scripts in the WordPress core, Plugins and Themes may be |
| 29 | + * practising this, we should strive to set a better example in our own work. |
| 30 | + */ |
| 31 | + |
| 32 | +})( jQuery ); |
| 33 | + |
| 34 | +let currentVal = 0 |
| 35 | + |
| 36 | +function progressBar(iValue, bIncremental = true) { |
| 37 | + if (iValue > 100) { |
| 38 | + iValue = 100 |
| 39 | + } else if (iValue < 0) { |
| 40 | + iValue = 0 |
| 41 | + } |
| 42 | + |
| 43 | + oProgressBar = jQuery('#olakai-performance-progress-bar') |
| 44 | + |
| 45 | + total = iValue |
| 46 | + if (bIncremental) { |
| 47 | + total += currentVal |
| 48 | + } |
| 49 | + console.log(`Setting progress bar from ${currentVal} by ${iValue} to ${total}%`) |
| 50 | + |
| 51 | + oProgressBar.css('width', `${total}%`) |
| 52 | + currentVal = total |
| 53 | + |
| 54 | + if (total > 0) { |
| 55 | + jQuery('#olakai-performance-progress-bar-container').removeClass('hidden') |
| 56 | + } else { |
| 57 | + jQuery('#olakai-performance-progress-bar-container').addClass('hidden') |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Check whether the URL exists |
| 63 | + * |
| 64 | + * @param {String} url |
| 65 | + */ |
| 66 | +function _head() { |
| 67 | + progressBar(8) |
| 68 | + jQuery.ajax({ |
| 69 | + data: { |
| 70 | + 'action': _olakai.action_report, |
| 71 | + 'html': _olakai.query_args.html, |
| 72 | + 'json': _olakai.query_args.json, |
| 73 | + }, |
| 74 | + type: 'head', |
| 75 | + url: _olakai.admin, |
| 76 | + success: function(data) { |
| 77 | + window.history.pushState({}, null, `${_olakai.tools}?page=${encodeURIComponent(_olakai.action_lighthouse_run)}&url=${encodeURIComponent(_olakai.query_args.url)}&html=${encodeURIComponent(_olakai.query_args.html)}&json=${encodeURIComponent(_olakai.query_args.json)}&preset=${encodeURIComponent(_olakai.query_args.preset)}&local=${encodeURIComponent(_olakai.query_args.local)}`); |
| 78 | + window.location.reload(); |
| 79 | + }, |
| 80 | + error: function(xhr, ajaxOptions, thrownError) { |
| 81 | + if (thrownError == "Not Found") { |
| 82 | + setTimeout(function() { |
| 83 | + _head(); |
| 84 | + }, 1000); |
| 85 | + } else { |
| 86 | + alert('there was an error'); |
| 87 | + console.log('error', thrownError) |
| 88 | + reset(true, false); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +function prepareReport(response) { |
| 95 | + _head(); |
| 96 | + let src = `${_olakai.admin}?action=${_olakai.action_report}&html=${response.html}&json=${response.json}`; |
| 97 | + _olakai.query_args.local = src; |
| 98 | + console.log(`Changing iframe src to ${src}`); |
| 99 | + |
| 100 | + jQuery('#_olakai_report_content').attr('src', src); |
| 101 | +} |
| 102 | + |
| 103 | +function reset(hideReport, disableGoButton) { |
| 104 | + if (hideReport) { |
| 105 | + report = { } |
| 106 | + } |
| 107 | + progressBar(disableGoButton ? 1 : 0, false) |
| 108 | + jQuery('#olakai-performance-form input[type=submit]').prop('disabled', disableGoButton); |
| 109 | + jQuery('#_olakai_report').toggleClass('hidden', hideReport); |
| 110 | + |
| 111 | + jQuery('#olakai-consulting-card-body').toggleClass('hidden', !hideReport); |
| 112 | +} |
| 113 | + |
| 114 | +function _olakai_form() { |
| 115 | + reset(true, false); |
| 116 | + |
| 117 | + // Check whether the form has already been submitted and this is a reload |
| 118 | + if (_olakai.query_args.html) { |
| 119 | + reset(false, false); |
| 120 | + } |
| 121 | + |
| 122 | + var form = jQuery('#olakai-performance-form'); |
| 123 | + if (!form) { |
| 124 | + return ; |
| 125 | + } |
| 126 | + |
| 127 | + form.submit(function(e) { |
| 128 | + reset(true, true); |
| 129 | + e.preventDefault(); |
| 130 | + var inputs = form.serializeArray(); |
| 131 | + _olakai.query_args.url = inputs[1].value |
| 132 | + _olakai.query_args.preset = inputs[2].value |
| 133 | + console.log(inputs); |
| 134 | + jQuery.ajax({ |
| 135 | + data: form.serialize(), |
| 136 | + type: 'post', |
| 137 | + url: form.attr("action"), |
| 138 | + success: function(data) { |
| 139 | + var content = JSON.parse(data); |
| 140 | + console.log(content); |
| 141 | + _olakai.query_args.html = content.html |
| 142 | + _olakai.query_args.json = content.json |
| 143 | + jQuery('#_open_report').attr("href", content.html); |
| 144 | + progressBar(10); |
| 145 | + prepareReport(content); |
| 146 | + }, |
| 147 | + error: function (xhr, ajaxOptions, thrownError) { |
| 148 | + alert('There was an error'); |
| 149 | + console.log('error', thrownError) |
| 150 | + // Toggle states |
| 151 | + reset(true, false); |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + return false; |
| 156 | + }); |
| 157 | + |
| 158 | + return false; |
| 159 | +} |
| 160 | + |
| 161 | +jQuery(document).ready(_olakai_form); |
0 commit comments