partdb/public/partdb.js

62 lines
1.4 KiB
JavaScript

"use strict";
// the last search query
var last_search = null;
// the collection of parts
var parts = null;
function search()
{
const terms = document.getElementById('terms');
if (terms && terms.value && terms.value.length >= 3) {
} else {
return;
}
last_search = '/search/' + terms.value;
let xhr = new XMLHttpRequest();
xhr.open('GET', last_search, true);
xhr.onload = function() {
if (this.responseURL.endsWith(last_search)) {
parts = JSON.parse(this.response)
results();
}
};
xhr.onerror = function() {
console.log("search call failed");
};
xhr.send();
}
function results()
{
const select = document.getElementById('results');
select.innerHTML = null;
for (const part of parts) {
const option = document.createElement('option');
option.setAttribute('value', part.id);
option.innerHTML = part.name + " (" + part.description + ")";
select.appendChild(option);
}
}
function select()
{
const results = document.getElementById('results');
const variants_select = document.getElementById('variants');
const pinout_preview = document.getElementById('pinout_preview');
if (results.selectedIndex >= 0) {
let part_selected = parseInt(results.options[results.selectedIndex].value);
for (const part of parts) {
if (part.id == part_selected) {
const fields = ["name", "description"];
for (const field of fields) {
const input = document.getElementById('part_' + field);
input.value = part[field];
}
}
}
}
}