1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

migrate napalm/device config to typescript

This commit is contained in:
checktheroads
2021-04-20 01:43:36 -07:00
parent 98829b209a
commit 0aa8fc2fc2
15 changed files with 234 additions and 64 deletions

View File

@@ -0,0 +1,41 @@
import { createToast } from '../toast';
import { apiGetBase, getNetboxData, hasError, toggleLoader } from '../util';
/**
* Initialize device config elements.
*/
function initConfig() {
toggleLoader('show');
const url = getNetboxData('data-object-url');
if (url !== null) {
apiGetBase<DeviceConfig>(url)
.then(data => {
if (hasError(data)) {
createToast('danger', 'Error Fetching Device Config', data.error).show();
} else {
const configTypes = [
'running',
'startup',
'candidate',
] as (keyof DeviceConfig['get_config'])[];
for (const configType of configTypes) {
const element = document.getElementById(`${configType}_config`);
if (element !== null) {
element.innerHTML = data.get_config[configType];
}
}
}
})
.finally(() => {
toggleLoader('hide');
});
}
}
if (document.readyState !== 'loading') {
initConfig();
} else {
document.addEventListener('DOMContentLoaded', initConfig);
}

View File

@@ -1,19 +1,5 @@
import { createToast } from './toast';
import { getNetboxData, apiGetBase, hasError, isTruthy } from './util';
/**
* Toggle visibility of card loader.
*/
function toggleLoader(action: 'show' | 'hide') {
const spinnerContainer = document.querySelector('div.card-overlay');
if (spinnerContainer !== null) {
if (action === 'show') {
spinnerContainer.classList.remove('d-none');
} else {
spinnerContainer.classList.add('d-none');
}
}
}
import { createToast } from '../toast';
import { getNetboxData, apiGetBase, hasError, isTruthy, toggleLoader } from '../util';
/**
* Get an attribute from a row's cell.

View File

@@ -113,6 +113,14 @@ type LLDPNeighborDetail = {
get_lldp_neighbors_detail: { [interface: string]: LLDPInterface[] };
};
type DeviceConfig = {
get_config: {
candidate: string;
running: string;
startup: string;
};
};
interface ObjectWithGroup extends APIObjectBase {
group: Nullable<APIReference>;
}

View File

@@ -172,3 +172,17 @@ export function getNetboxData(key: string): string | null {
}
return null;
}
/**
* Toggle visibility of card loader.
*/
export function toggleLoader(action: 'show' | 'hide') {
const spinnerContainer = document.querySelector('div.card-overlay');
if (spinnerContainer !== null) {
if (action === 'show') {
spinnerContainer.classList.remove('d-none');
} else {
spinnerContainer.classList.add('d-none');
}
}
}