111 lines
2.9 KiB
JavaScript
111 lines
2.9 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');
|
|
if (results.selectedIndex >= 0) {
|
|
let part_selected = parseInt(results.options[results.selectedIndex].value);
|
|
for (const part of parts) {
|
|
if (part.id == part_selected) {
|
|
console.log(part);
|
|
// populate part fields
|
|
const fields = ["name", "description", "details", "package", "pincount", "manufacturer", "family", "datasheet", "page", "location", "stock"];
|
|
for (const field of fields) {
|
|
const input = document.getElementById('part_' + field);
|
|
if (input.tagName == "INPUT") {
|
|
if (undefined === part[field]) {
|
|
input.value = "";
|
|
} else {
|
|
input.value = part[field];
|
|
}
|
|
} else if (input.tagName == "TEXTAREA") {
|
|
if (undefined === part[field]) {
|
|
input.innerHTML = null;
|
|
} else {
|
|
input.innerHTML = part[field];
|
|
}
|
|
}
|
|
}
|
|
// set URLs
|
|
const urls = ["page","datasheet"];
|
|
for (const field of urls) {
|
|
const a = document.getElementById('url_' + field);
|
|
if (null == a) {
|
|
continue;
|
|
}
|
|
if (undefined === part[field]) {
|
|
a.href = null;
|
|
} else {
|
|
a.href = part[field];
|
|
}
|
|
}
|
|
// set distributors
|
|
const distributors = document.getElementById('distributors');
|
|
distributors.innerHTML = null;
|
|
for (const distributor of part["distributors"]) {
|
|
const tr = document.createElement('tr');
|
|
const td_name = document.createElement('td');
|
|
td_name.innerText = distributor.name;
|
|
tr.appendChild(td_name);
|
|
const td_sku = document.createElement('td');
|
|
const input = document.createElement('input');
|
|
input.type = "text";
|
|
input.style.width = "95%";
|
|
if (distributor.sku) {
|
|
input.value = distributor.sku;
|
|
}
|
|
td_sku.appendChild(input);
|
|
tr.appendChild(td_sku);
|
|
const td_url = document.createElement('td');
|
|
if (distributor.url) {
|
|
td_url.innerHTML = "<a href='" + distributor.url + "'>link</a>";
|
|
}
|
|
tr.appendChild(td_url);
|
|
distributors.appendChild(tr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|