1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
Files
checktheroads-hyperglass/hyperglass/static/hyperglass.es6

362 lines
12 KiB
Plaintext
Raw Normal View History

2019-08-06 01:09:55 -07:00
// Module Imports
global.jQuery = require('jquery');
const $ = jQuery;
const Popper = require('popper.js');
const bootstrap = require('bootstrap');
const selectpicker = require('bootstrap-select');
const animsition = require('animsition');
const ClipboardJS = require('clipboard');
2019-08-22 01:01:23 -07:00
const frontEndConfig = require('./frontend.json');
2019-08-06 01:09:55 -07:00
2019-08-22 01:01:23 -07:00
const inputMessages = frontEndConfig.messages;
2019-08-22 20:27:57 -07:00
const pageContainer = $('#hg-page-container');
const formContainer = $('#hg-form');
2019-08-13 00:49:27 -07:00
const queryLocation = $('#location');
const queryType = $('#query_type');
const queryTarget = $('#query_target');
2019-08-21 08:23:33 -07:00
const queryTargetAppend = $('#hg-target-append');
2019-08-22 01:01:23 -07:00
const submitIcon = $('#hg-submit-icon');
2019-08-13 00:49:27 -07:00
const resultsContainer = $('#hg-results');
const resultsAccordion = $('#hg-accordion');
2019-08-22 20:27:57 -07:00
const resultsColumn = resultsAccordion.parent();
2019-08-13 00:49:27 -07:00
const backButton = $('#hg-back-btn');
2019-08-20 18:52:29 -07:00
const footerHelpBtn = $('#hg-footer-help-btn');
const footerTermsBtn = $('#hg-footer-terms-btn');
const footerCreditBtn = $('#hg-footer-credit-btn');
const footerPopoverTemplate = '<div class="popover mw-sm-75 mw-md-50 mw-lg-25" role="tooltip"><div class="arrow"></div><h3 class="popover-header"></h3><div class="popover-body"></div></div>';
2019-08-13 00:49:27 -07:00
2019-08-22 01:01:23 -07:00
class InputInvalid extends Error {
constructor(validationMsg, invalidField, fieldContainer) {
super(validationMsg, invalidField, fieldContainer);
this.name = this.constructor.name;
this.message = validationMsg;
this.field = invalidField;
this.container = fieldContainer;
}
}
2019-08-22 20:27:57 -07:00
const swapSpacing = (goTo) => {
if (goTo === 'form') {
pageContainer.removeClass('px-0 px-md-3');
resultsColumn.removeClass('px-0');
} else if (goTo === 'results') {
pageContainer.addClass('px-0 px-md-3');
resultsColumn.addClass('px-0');
}
};
2019-08-13 00:49:27 -07:00
const resetResults = () => {
queryLocation.selectpicker('deselectAll');
queryLocation.selectpicker('val', '');
queryType.selectpicker('val', '');
queryTarget.val('');
resultsContainer.animsition('out', formContainer, '#');
resultsContainer.hide();
2019-08-22 01:01:23 -07:00
$('.hg-info-btn').remove();
2019-08-22 20:27:57 -07:00
swapSpacing('form');
2019-08-13 00:49:27 -07:00
formContainer.show();
formContainer.animsition('in');
2019-08-20 18:52:29 -07:00
backButton.addClass('d-none');
2019-08-13 00:49:27 -07:00
resultsAccordion.empty();
};
const reloadPage = () => {
queryLocation.selectpicker('deselectAll');
queryLocation.selectpicker('val', '');
queryType.selectpicker('val', '');
queryTarget.val('');
resultsAccordion.empty();
};
queryLocation.selectpicker({
2019-08-06 01:09:55 -07:00
liveSearchNormalize: true,
style: '',
styleBase: 'form-control',
iconBase: '',
tickIcon: 'remixicon-check-line',
});
2019-08-13 00:49:27 -07:00
queryType.selectpicker({
2019-08-06 01:09:55 -07:00
liveSearchNormalize: true,
style: '',
styleBase: 'form-control',
iconBase: '',
tickIcon: 'remixicon-check-line',
});
2019-08-20 18:52:29 -07:00
footerTermsBtn.popover({
html: true,
2019-08-21 08:37:57 -07:00
trigger: 'focus',
2019-08-20 18:52:29 -07:00
template: footerPopoverTemplate,
placement: 'top',
content: $('#hg-footer-terms-html').html(),
});
footerHelpBtn.popover({
html: true,
2019-08-21 08:37:57 -07:00
trigger: 'focus',
2019-08-20 18:52:29 -07:00
placement: 'top',
template: footerPopoverTemplate,
content: $('#hg-footer-help-html').html(),
});
footerCreditBtn.popover({
html: true,
2019-08-21 08:37:57 -07:00
trigger: 'focus',
2019-08-20 18:52:29 -07:00
placement: 'top',
title: $('#hg-footer-credit-title').html(),
content: $('#hg-footer-credit-content').html(),
template: footerPopoverTemplate,
});
2019-08-06 01:09:55 -07:00
$(document).ready(() => {
2019-08-13 00:49:27 -07:00
reloadPage();
resultsContainer.hide();
2019-08-07 11:09:40 -07:00
$('#hg-ratelimit-query').modal('hide');
2019-08-06 01:09:55 -07:00
$('.animsition').animsition({
inClass: 'fade-in',
outClass: 'fade-out',
inDuration: 800,
outDuration: 800,
transition: (url) => { window.location.href = url; },
});
2019-08-07 11:09:40 -07:00
formContainer.animsition('in');
2019-08-13 00:49:27 -07:00
});
2019-08-06 01:09:55 -07:00
2019-08-21 08:23:33 -07:00
const supportedBtn = qt => `<button class="btn btn-dark hg-info-btn" id="hg-info-btn-${qt}" data-hg-type="${qt}" type="button"><div id="hg-info-icon-${qt}"><i class="remixicon-information-line"></i></div></button>`;
queryType.on('changed.bs.select', () => {
const queryTypeId = queryType.val();
2019-08-22 20:27:57 -07:00
const queryTypeBtn = $('.hg-info-btn');
2019-08-21 08:23:33 -07:00
if ((queryTypeId === 'bgp_community') || (queryTypeId === 'bgp_aspath')) {
2019-08-22 20:27:57 -07:00
queryTypeBtn.remove();
2019-08-21 08:23:33 -07:00
queryTargetAppend.prepend(supportedBtn(queryTypeId));
2019-08-22 20:27:57 -07:00
} else {
queryTypeBtn.remove();
2019-08-21 08:23:33 -07:00
}
});
queryTargetAppend.on('click', '.hg-info-btn', () => {
const queryTypeId = $('.hg-info-btn').data('hg-type');
$(`#hg-info-${queryTypeId}`).modal('show');
});
2019-08-07 11:09:40 -07:00
const queryApp = (queryType, queryTypeName, locationList, queryTarget) => {
const resultsTitle = `${queryTypeName} Query for ${queryTarget}`;
2019-08-06 01:09:55 -07:00
2019-08-07 11:09:40 -07:00
$('#hg-results-title').html(resultsTitle);
2019-08-06 01:09:55 -07:00
2019-08-22 01:01:23 -07:00
submitIcon.empty().removeClass('hg-loading').html('<i class="remixicon-search-line"></i>');
2019-08-06 01:09:55 -07:00
$.each(locationList, (n, loc) => {
2019-08-07 11:09:40 -07:00
const locationName = $(`#${loc}`).data('display-name');
const networkName = $(`#${loc}`).data('netname');
2019-08-06 01:09:55 -07:00
2019-08-07 11:09:40 -07:00
const contentHtml = `
2019-08-06 01:09:55 -07:00
<div class="card" id="${loc}-output">
2019-08-14 09:36:33 -07:00
<div class="card-header bg-loading" id="${loc}-heading">
2019-08-06 01:09:55 -07:00
<div class="float-right hg-status-container" id="${loc}-status-container">
2019-08-14 09:36:33 -07:00
<button type="button" class="float-right btn btn-loading btn-lg hg-menu-btn hg-status-btn"
2019-08-06 01:09:55 -07:00
data-location="${loc}" id="${loc}-status-btn" disabled>
</button>
</div>
2019-08-14 09:36:33 -07:00
<button type="button" class="float-right btn btn-loading btn-lg hg-menu-btn hg-copy-btn"
2019-08-06 01:09:55 -07:00
data-clipboard-target="#${loc}-text" id="${loc}-copy-btn" disabled>
2019-08-13 00:49:27 -07:00
<i class="remixicon-checkbox-multiple-blank-line hg-menu-icon hg-copy hg-copy-icon"></i>
2019-08-06 01:09:55 -07:00
</button>
<h2 class="mb-0" id="${loc}-heading-container">
2019-08-14 09:36:33 -07:00
<button class="btn btn-link" type="button" data-toggle="collapse"
2019-08-06 01:09:55 -07:00
data-target="#${loc}-content" aria-expanded="true" aria-controls="${loc}-content"
id="${loc}-heading-text">
</button>
</h2>
</div>
<div class="collapse" id="${loc}-content" aria-labelledby="${loc}-heading" data-parent="#hg-accordion">
<div class="card-body" id="${loc}-text">
</div>
</div>
</div>
`;
if ($(`#${loc}-output`).length) {
$(`#${loc}-output`).replaceWith(contentHtml);
} else {
$('#hg-accordion').append(contentHtml);
}
2019-08-07 11:09:40 -07:00
const iconLoading = `<i id="${loc}-spinner" class="hg-menu-icon hg-status-icon remixicon-loader-4-line text-secondary"></i>`;
2019-08-06 01:09:55 -07:00
2019-08-07 11:09:40 -07:00
$(`#${loc}-heading-text`).text(locationName);
2019-08-06 01:09:55 -07:00
$(`#${loc}-status-container`)
.addClass('hg-loading')
.find('.hg-status-btn')
.empty()
2019-08-07 11:09:40 -07:00
.html(iconLoading);
const generateError = (errorClass, locError, text) => {
const iconError = '<i class="hg-menu-icon hg-status-icon remixicon-alert-line"></i>';
2019-08-14 09:36:33 -07:00
$(`#${locError}-heading`).removeClass('bg-loading').addClass(`bg-${errorClass}`);
2019-08-16 23:23:09 -07:00
$(`#${locError}-heading`).find('.hg-menu-btn').removeClass('btn-loading').addClass(`btn-${errorClass}`);
2019-08-07 11:09:40 -07:00
$(`#${locError}-status-container`)
2019-08-06 01:09:55 -07:00
.removeClass('hg-loading')
.find('.hg-status-btn')
.empty()
2019-08-07 11:09:40 -07:00
.html(iconError);
$(`#${locError}-text`).html(text);
2019-08-06 01:09:55 -07:00
}
$.ajax({
url: '/lg',
type: 'POST',
data: JSON.stringify({
location: loc,
query_type: queryType,
2019-08-07 11:09:40 -07:00
target: queryTarget,
2019-08-06 01:09:55 -07:00
}),
contentType: 'application/json; charset=utf-8',
context: document.body,
async: true,
timeout: 15000,
})
.done((data, textStatus, jqXHR) => {
2019-08-07 11:09:40 -07:00
const displayHtml = `<pre>${jqXHR.responseText}</pre>`;
const iconSuccess = '<i class="hg-menu-icon hg-status-icon remixicon-check-line"></i>';
2019-08-14 09:36:33 -07:00
$(`#${loc}-heading`).removeClass('bg-loading').addClass('bg-primary');
2019-08-16 23:23:09 -07:00
$(`#${loc}-heading`).find('.hg-menu-btn').removeClass('btn-loading').addClass('btn-primary');
2019-08-06 01:09:55 -07:00
$(`#${loc}-status-container`)
.removeClass('hg-loading')
.find('.hg-status-btn')
.empty()
2019-08-07 11:09:40 -07:00
.html(iconSuccess);
$(`#${loc}-text`).empty().html(displayHtml);
2019-08-06 01:09:55 -07:00
})
.fail((jqXHR, textStatus, errorThrown) => {
2019-08-07 11:09:40 -07:00
const codesDanger = [401, 415, 500, 501, 503];
const codesWarning = [405];
2019-08-06 01:09:55 -07:00
if (textStatus === 'timeout') {
2019-08-18 15:14:26 -07:00
const displayHtml = 'Request timed out.';
2019-08-07 11:09:40 -07:00
const iconTimeout = '<i class="remixicon-time-line"></i>';
2019-08-14 09:36:33 -07:00
$(`#${loc}-heading`).removeClass('bg-loading').addClass('bg-warning');
2019-08-16 23:23:09 -07:00
$(`#${loc}-heading`).find('.hg-menu-btn').removeClass('btn-loading').addClass('btn-warning');
2019-08-14 09:36:33 -07:00
$(`#${loc}-status-container`).removeClass('hg-loading').find('.hg-status-btn').empty().html(iconTimeout);
$(`#${loc}-text`).empty().html(displayHtml);
2019-08-07 11:09:40 -07:00
} else if (codesDanger.includes(jqXHR.status)) {
2019-08-06 01:09:55 -07:00
generateError('danger', loc, jqXHR.responseText);
2019-08-07 11:09:40 -07:00
} else if (codesWarning.includes(jqXHR.status)) {
2019-08-06 01:09:55 -07:00
generateError('warning', loc, jqXHR.responseText);
2019-08-07 11:09:40 -07:00
} else if (jqXHR.status === 429) {
resetResults();
$('#hg-ratelimit-query').modal('show');
2019-08-06 01:09:55 -07:00
}
})
.always(() => {
$(`#${loc}-status-btn`).removeAttr('disabled');
$(`#${loc}-copy-btn`).removeAttr('disabled');
});
$(`#${locationList[0]}-content`).collapse('show');
});
};
2019-08-22 01:01:23 -07:00
$(document).on('InvalidInputEvent', (e, domField) => {
console.log('event triggered');
const errorField = $(domField);
console.log(errorField);
if (errorField.hasClass('is-invalid')) {
console.log('has class');
errorField.on('keyup', () => {
console.log('keyup');
errorField.removeClass('is-invalid');
errorField.nextAll('.invalid-feedback').remove();
});
}
});
2019-08-06 01:09:55 -07:00
// Submit Form Action
$('#lgForm').submit((event) => {
event.preventDefault();
2019-08-22 01:01:23 -07:00
submitIcon.empty().html('<i class="remixicon-loader-4-line"></i>').addClass('hg-loading');
2019-08-07 11:09:40 -07:00
const queryType = $('#query_type').val();
const queryLocation = $('#location').val();
const queryTarget = $('#query_target').val();
2019-08-22 01:01:23 -07:00
try {
// message, thing to circle in red, place to put error text
if (!queryTarget) {
const queryTargetContainer = $('#query_target');
throw new InputInvalid(inputMessages.no_input, queryTargetContainer, queryTargetContainer.parent());
}
if (!queryType) {
const queryTypeContainer = $('#query_type').next('.dropdown-toggle');
throw new InputInvalid(inputMessages.no_query_type, queryTypeContainer, queryTypeContainer.parent());
}
if (queryLocation === undefined || queryLocation.length === 0) {
const queryLocationContainer = $('#location').next('.dropdown-toggle');
throw new InputInvalid(inputMessages.no_location, queryLocationContainer, queryLocationContainer.parent());
}
} catch (err) {
err.field.addClass('is-invalid');
err.container.append(`<div class="invalid-feedback px-1">${err.message}</div>`);
submitIcon.empty().removeClass('hg-loading').html('<i class="remixicon-search-line"></i>');
$(document).trigger('InvalidInputEvent', err.field);
return false;
}
const queryTypeTitle = $(`#${queryType}`).data('display-name');
2019-08-07 11:09:40 -07:00
queryApp(queryType, queryTypeTitle, queryLocation, queryTarget);
2019-08-06 01:09:55 -07:00
$('#hg-form').animsition('out', $('#hg-results'), '#');
$('#hg-form').hide();
2019-08-22 20:27:57 -07:00
swapSpacing('results');
2019-08-06 01:09:55 -07:00
$('#hg-results').show();
$('#hg-results').animsition('in');
$('#hg-submit-spinner').remove();
2019-08-20 18:52:29 -07:00
$('#hg-back-btn').removeClass('d-none');
2019-08-13 00:49:27 -07:00
$('#hg-back-btn').animsition('in');
2019-08-06 01:09:55 -07:00
});
2019-08-13 00:49:27 -07:00
$('#hg-back-btn').click(() => {
2019-08-07 11:09:40 -07:00
resetResults();
});
2019-08-06 01:09:55 -07:00
const clipboard = new ClipboardJS('.hg-copy-btn');
clipboard.on('success', (e) => {
2019-08-22 20:27:57 -07:00
const copyIcon = $(e.trigger).find('.hg-copy-icon');
copyIcon.removeClass('remixicon-checkbox-multiple-blank-line').addClass('remixicon-checkbox-multiple-line');
2019-08-06 01:09:55 -07:00
e.clearSelection();
setTimeout(() => {
2019-08-22 20:27:57 -07:00
copyIcon.removeClass('remixicon-checkbox-multiple-line').addClass('remixicon-checkbox-multiple-blank-line');
2019-08-06 01:09:55 -07:00
}, 800);
});
clipboard.on('error', (e) => {
console.log(e);
});
$('#hg-accordion').on('mouseenter', '.hg-status-btn', (e) => {
$(e.currentTarget)
.find('.hg-status-icon')
.addClass('remixicon-repeat-line');
});
$('#hg-accordion').on('mouseleave', '.hg-status-btn', (e) => {
$(e.currentTarget)
.find('.hg-status-icon')
.removeClass('remixicon-repeat-line');
});
$('#hg-accordion').on('click', '.hg-status-btn', (e) => {
2019-08-07 11:09:40 -07:00
const thisLocation = $(e.currentTarget).data('location');
console.log(`Refreshing ${thisLocation}`);
const queryType = $('#query_type').val();
const queryTypeTitle = $(`#${queryType}`).data('display-name');
const queryTarget = $('#query_target').val();
queryApp(queryType, queryTypeTitle, [thisLocation,], queryTarget);
});
$('#hg-ratelimit-query').on('shown.bs.modal', () => {
$('#hg-ratelimit-query').trigger('focus');
});
$('#hg-ratelimit-query').find('btn').on('click', () => {
$('#hg-ratelimit-query').modal('hide');
2019-08-06 01:09:55 -07:00
});