Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions assets/admin/scanner.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* FrontConsent Scanner: settings tab and dashboard widget. */

.frcn-scanner__summary {
display: flex;
gap: 16px;
align-items: center;
margin: 16px 0;
}

.frcn-scanner--widget .frcn-scanner__summary {
margin: 0 0 8px;
}

.frcn-scanner__grade {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-width: 72px;
height: 72px;
border-radius: 8px;
color: #fff;
background: #646970;
}

.frcn-scanner__grade--a { background: #00a32a; }
.frcn-scanner__grade--b { background: #4f8a10; }
.frcn-scanner__grade--c { background: #996800; }
.frcn-scanner__grade--d { background: #c9480a; }
.frcn-scanner__grade--e,
.frcn-scanner__grade--f { background: #d63638; }

.frcn-scanner__grade-letter {
font-size: 32px;
font-weight: 700;
line-height: 1;
}

.frcn-scanner__score {
font-size: 12px;
}

.frcn-scanner__headline {
margin: 0 0 4px;
font-size: 14px;
font-weight: 600;
}

.frcn-scanner__counts,
.frcn-scanner__date {
margin: 0 0 4px;
color: #50575e;
}

.frcn-scanner__count {
margin-right: 12px;
white-space: nowrap;
}

.frcn-scanner__checks {
max-width: 760px;
margin: 0;
padding: 0;
list-style: none;
}

.frcn-scanner__check {
display: flex;
gap: 10px;
padding: 10px 0;
border-top: 1px solid #dcdcde;
}

.frcn-scanner__check p {
margin: 4px 0 0;
}

.frcn-scanner__check-status {
color: #50575e;
}

/* Status is never conveyed by colour alone: icon + text label. */
.frcn-scanner__icon::before {
display: inline-block;
width: 1.4em;
font-weight: 700;
text-align: center;
}

.frcn-scanner__status--pass .frcn-scanner__icon::before { content: "\2713"; color: #00a32a; }
.frcn-scanner__status--warn .frcn-scanner__icon::before { content: "!"; color: #996800; }
.frcn-scanner__status--fail .frcn-scanner__icon::before { content: "\2715"; color: #d63638; }
.frcn-scanner__status--not_applicable .frcn-scanner__icon::before,
.frcn-scanner__status--in_report .frcn-scanner__icon::before { content: "\2013"; color: #646970; }

.frcn-scanner__pending .spinner {
float: none;
margin: 0 8px 0 0;
}

.frcn-scanner__message--error,
.frcn-scanner__error {
color: #d63638;
}

.frcn-scanner__widget-link {
margin-bottom: 0;
}
97 changes: 97 additions & 0 deletions assets/admin/scanner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* FrontConsent Scanner: starts a scan and follows it until it finishes.
* Used by the Scanner settings tab and the dashboard widget.
*
* @package FrontConsent
*/

(function () {
'use strict';

var config = window.frcnScanner;

if (!config) {
return;
}

function post(action) {
var body = new FormData();
body.append('action', action);
body.append('nonce', config.nonce);

return fetch(config.ajaxUrl, { method: 'POST', credentials: 'same-origin', body: body }).then(function (response) {
return response.json().then(function (json) {
if (!json || !json.success) {
throw new Error(json && json.data && json.data.message ? json.data.message : config.i18n.error);
}
return json.data;
}, function () {
throw new Error(config.i18n.error);
});
});
}

function initScanner(root) {
var button = root.querySelector('[data-frcn-scanner-start]');
var result = root.querySelector('[data-frcn-scanner-result]');
var message = root.querySelector('[data-frcn-scanner-message]');
var compact = root.classList.contains('frcn-scanner--widget');
var polls = 0;

function setMessage(text, isError) {
if (!message) {
return;
}
message.textContent = text || '';
message.classList.toggle('frcn-scanner__message--error', !!isError);
}

function render(data) {
if (result) {
result.innerHTML = compact ? data.compactHtml : data.html;
}
if (button) {
button.disabled = !!data.pending;
}
// The rendered result shows its own "Analyzing…" line while pending.
setMessage('');
if (!data.pending) {
return;
}
if (polls >= config.maxPolls) {
setMessage(config.i18n.timeout, true);
return;
}
window.setTimeout(poll, Math.max(3, data.pollAfterSeconds || 5) * 1000);
}

function fail(error) {
if (button) {
button.disabled = false;
}
setMessage(error.message, true);
}

function poll() {
polls++;
post('frcn_scanner_status').then(render, fail);
}

if (button) {
button.addEventListener('click', function () {
button.disabled = true;
polls = 0;
setMessage(config.i18n.running);
post('frcn_scanner_start').then(render, fail);
});
}

if (result && result.hasAttribute('data-frcn-scanner-pending')) {
poll();
}
}

document.addEventListener('DOMContentLoaded', function () {
Array.prototype.forEach.call(document.querySelectorAll('[data-frcn-scanner]'), initScanner);
});
})();
6 changes: 4 additions & 2 deletions bin/build-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ const CleanCSS = require( 'clean-css' );

/**
* Plugin-relative paths of every plain asset this script knows how to
* minify. Keep in sync with the enqueue calls in includes/Frontend/CookieNotice.php
* and includes/Admin/Settings.php.
* minify. Keep in sync with the enqueue calls in includes/Frontend/CookieNotice.php,
* includes/Admin/Settings.php and includes/Admin/Scanner.php.
*/
const ASSETS = [
{ relativePath: 'assets/cookie-notice/frontconsent-cookie-notice.js', type: 'js' },
{ relativePath: 'assets/cookie-notice/frontconsent-cookie-notice.css', type: 'css' },
{ relativePath: 'assets/admin/settings.js', type: 'js' },
{ relativePath: 'assets/admin/settings.css', type: 'css' },
{ relativePath: 'assets/admin/scanner.js', type: 'js' },
{ relativePath: 'assets/admin/scanner.css', type: 'css' },
];

/**
Expand Down
Loading
Loading