partdb/public/partdb.js

307 lines
8.3 KiB
JavaScript
Raw Normal View History

2023-01-28 05:56:17 +01:00
/* frontend for the part database
* Copyright (C) 2023 King Kévin <kingkevin@cuvoodoo.info>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
2023-01-23 23:58:59 +01:00
"use strict";
// the last search query
var last_search = null;
2023-02-26 12:11:57 +01:00
var next_search = null;
2023-01-25 00:20:59 +01:00
// the collection of parts
var parts = null;
2023-01-30 12:09:51 +01:00
// last selected part
var part_id = null;
2023-01-26 05:44:11 +01:00
// part field to populate
2023-01-29 10:09:42 +01:00
const fields = ["name", "description", "details", "package", "manufacturer", "mpn", "family", "datasheet", "page", "location", "stock"];
2023-01-26 05:44:11 +01:00
// URLs to set
2023-01-30 04:18:56 +01:00
const urls = ["page", "datasheet"];
// tables with key value information
const tables = ["distributors", "properties", "components"];
2023-01-23 23:58:59 +01:00
function search()
{
const terms = document.getElementById('terms');
if (terms && terms.value && terms.value.length >= 3) {
2023-02-26 12:11:57 +01:00
const current_search = '/search?terms=' + terms.value;
do_search(current_search);
2023-01-23 23:58:59 +01:00
}
2023-02-26 12:11:57 +01:00
}
2023-01-23 23:58:59 +01:00
2023-02-26 12:11:57 +01:00
function do_search(current_search)
{
if (null == last_search) { // no request running
last_search = current_search;
let xhr = new XMLHttpRequest();
xhr.open('GET', last_search, true);
xhr.onload = function() {
if (decodeURI(this.responseURL).endsWith(last_search)) {
parts = JSON.parse(this.response);
results();
}
last_search = null;
if (next_search) {
do_search(next_search);
next_search = null;
}
};
xhr.onerror = function() {
console.log("search call failed");
};
xhr.send();
} else {
next_search = current_search; // save for next search
}
2023-01-23 23:58:59 +01:00
}
2023-01-25 00:20:59 +01:00
function results()
2023-01-23 23:58:59 +01:00
{
2023-01-27 02:15:01 +01:00
const results = document.getElementById('results');
results.innerHTML = null;
2023-01-23 23:58:59 +01:00
for (const part of parts) {
2023-01-25 00:20:59 +01:00
const option = document.createElement('option');
option.setAttribute('value', part.id);
2023-01-28 02:41:32 +01:00
option.innerHTML = part.name;
if (part.description) {
option.innerHTML += " (" + part.description + ")";
2023-01-30 12:09:51 +01:00
}
if (part_id == part.id) {
option.selected = "selected";
2023-01-27 02:15:01 +01:00
}
results.appendChild(option);
2023-01-25 00:20:59 +01:00
}
2023-01-27 02:15:01 +01:00
select_part(); // in case we reselected
2023-01-25 00:20:59 +01:00
}
2023-01-26 05:44:11 +01:00
function clear()
{
// clear part fields
for (const field of fields) {
const input = document.getElementById('part_' + field);
2023-01-30 01:14:04 +01:00
input.value = "";
2023-01-26 05:44:11 +01:00
}
// clear URLs
for (const field of urls) {
2023-01-30 12:11:00 +01:00
const span = document.getElementById('url_' + field);
span.innerHTML = span.innerText;
2023-01-26 05:44:11 +01:00
}
2023-01-30 04:18:56 +01:00
// clear tables
for (const table of tables) {
const body = document.getElementById(table);
body.innerHTML = null;
}
2023-01-31 03:43:41 +01:00
// cleat attachment
const input = document.getElementById('attachment');
input.value = "";
2023-01-26 05:44:11 +01:00
}
2023-01-27 02:15:01 +01:00
function select_part()
2023-01-25 00:20:59 +01:00
{
const results = document.getElementById('results');
if (results.selectedIndex >= 0) {
2023-01-30 12:09:51 +01:00
part_id = parseInt(results.options[results.selectedIndex].value);
2023-01-25 00:20:59 +01:00
for (const part of parts) {
2023-01-30 12:09:51 +01:00
if (part.id == part_id) {
2023-01-27 02:15:34 +01:00
//console.log(part);
2023-01-25 05:15:15 +01:00
// populate part fields
2023-01-25 00:20:59 +01:00
for (const field of fields) {
const input = document.getElementById('part_' + field);
2023-01-30 01:16:05 +01:00
if (undefined === part[field]) {
input.value = "";
} else {
input.value = part[field];
2023-01-25 05:15:15 +01:00
}
}
// set URLs
for (const field of urls) {
2023-01-28 05:00:13 +01:00
const span = document.getElementById('url_' + field);
const text = span.innerText;
span.innerHTML = null;
2023-01-30 23:51:41 +01:00
if (undefined === part[field] || null == part[field] || 0 == part[field].length) {
2023-01-28 05:00:13 +01:00
span.innerHTML = text;
2023-01-25 05:15:15 +01:00
} else {
2023-01-28 05:00:13 +01:00
const a = document.createElement('a');
2023-01-25 05:15:15 +01:00
a.href = part[field];
2023-01-28 05:00:13 +01:00
a.innerText = text;
span.appendChild(a);
2023-01-25 05:15:15 +01:00
}
2023-01-25 00:20:59 +01:00
}
// add distributors, properties and components
for (const table of tables) {
const tbody = document.getElementById(table);
tbody.innerHTML = null;
2023-01-30 23:52:27 +01:00
const content = part[table].slice();
2023-01-30 12:37:55 +01:00
if ("components" == table) {
2023-01-30 23:52:27 +01:00
content.push({name: "", quantity: ""}); // empty field to add
2023-01-30 12:37:55 +01:00
} else {
2023-01-30 23:52:27 +01:00
content.push({name: "", value: ""}); // empty field to add
2023-01-30 12:37:55 +01:00
}
2023-01-30 23:52:27 +01:00
for (const row of content) {
2023-01-30 04:18:56 +01:00
const tr = document.createElement('tr');
let td = document.createElement('td');
let input = document.createElement('input');
input.type = "text";
input.style.width = "95%";
input.value = row.name;
2023-01-30 04:18:56 +01:00
td.appendChild(input);
tr.appendChild(td);
if (null != row.value) {
td = document.createElement('td');
input = document.createElement('input');
input.type = "text";
input.value = row.value;
input.style.width = "95%";
td.appendChild(input);
tr.appendChild(td);
}
if (row.sku) {
td = document.createElement('td');
input = document.createElement('input');
input.type = "text";
input.value = row.sku;
input.style.width = "95%";
td.appendChild(input);
tr.appendChild(td);
}
if (null != row.quantity) {
td = document.createElement('td');
input = document.createElement('input');
2023-01-30 04:18:56 +01:00
input.type = "number";
input.min = "0";
input.step = "1";
input.value = row.quantity;
2023-01-30 12:37:55 +01:00
input.size = "4";
td.appendChild(input);
tr.appendChild(td);
2023-01-30 04:18:56 +01:00
}
2023-01-30 12:30:11 +01:00
if (row.description) {
td = document.createElement('td');
td.innerText = row.description;
tr.appendChild(td);
}
if (row.url) {
td = document.createElement('td');
td.innerHTML = "<a href='" + row.url + "'>link</a>";
tr.appendChild(td);
}
tbody.appendChild(tr);
2023-01-30 04:18:56 +01:00
}
2023-01-25 11:22:26 +01:00
}
2023-01-26 11:55:56 +01:00
// add attachments
const attachments = document.getElementById('attachments');
attachments.innerHTML = null;
for (const attachment of part["attachments"]) {
const a = document.createElement('a');
a.href = attachment;
const img = document.createElement('img');
img.alt = attachment.split("/").slice(-1);
img.src = attachment;
a.appendChild(img);
attachments.appendChild(a);
}
2023-01-25 00:20:59 +01:00
}
2023-01-23 23:58:59 +01:00
}
}
}
2023-01-26 00:42:54 +01:00
function delete_part()
{
if (null == parts) {
return;
}
const results = document.getElementById('results');
if (results.selectedIndex < 0) {
return;
}
const part_selected = parseInt(results.options[results.selectedIndex].value);
const xhr = new XMLHttpRequest();
xhr.open('GET', '/delete/' + part_selected, true);
xhr.onload = function() {
2023-01-26 05:44:11 +01:00
clear();
2023-01-26 00:42:54 +01:00
search(); // refresh search
};
xhr.onerror = function() {
console.log("delete part failed");
};
xhr.send();
}
2023-01-27 02:04:03 +01:00
function update_part()
{
// the part to update
let part = {};
// its id
const results = document.getElementById('results');
if (results.selectedIndex >= 0) {
part.id = parseInt(results.options[results.selectedIndex].value);
}
// the fields
for (const field of fields) {
const input = document.getElementById('part_' + field);
2023-01-30 01:00:48 +01:00
part[field] = input.value;
2023-01-27 02:04:03 +01:00
}
if (part["name"].length == 0) {
return;
}
// get distributors
part.distributors = [];
2023-01-27 02:04:03 +01:00
const distributors = document.getElementById('distributors');
for (const distributor of distributors.rows) {
part.distributors.push({name: distributor.cells[0].firstChild.value, sku: distributor.cells[1].firstChild.value});
2023-01-27 02:04:03 +01:00
}
// get properties
part.properties = [];
2023-01-27 02:04:03 +01:00
const properties = document.getElementById('properties');
for (const prop of properties.rows) {
part.properties.push({name: prop.cells[0].firstChild.value, value: prop.cells[1].firstChild.value});
2023-01-27 02:04:03 +01:00
}
2023-01-30 04:18:56 +01:00
// get components
part.components = [];
2023-01-30 04:18:56 +01:00
const components = document.getElementById('components');
for (const component of components.rows) {
const nam = component.cells[0].firstChild.value;
const q = component.cells[1].firstChild.value;
if (nam && nam.length > 0) {
part.components.push({name: nam, quantity: parseInt(q)});
2023-01-30 04:18:56 +01:00
}
}
2023-01-30 12:30:37 +01:00
//console.log(part);
2023-01-27 02:04:03 +01:00
var post = new XMLHttpRequest();
post.open("POST", "part");
post.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
post.onload = function() {
clear();
search(); // refresh search
};
post.onerror = function() {
console.log("update part failed");
};
post.send(JSON.stringify(part));
}
2023-01-31 03:37:49 +01:00
function attach()
{
const results = document.getElementById('results');
if (results.selectedIndex >= 0) {
part_id = parseInt(results.options[results.selectedIndex].value);
} else {
part_id = null;
return;
}
const attachment = document.getElementById('attachment');
if (null == attachment.value || 0 == attachment.value.length) {
return;
}
const xhr = new XMLHttpRequest();
xhr.open('GET', '/attach?id=' + part_id + "&url=" + encodeURI(attachment.value), true);
xhr.onload = function() {
clear();
search(); // refresh search
};
xhr.onerror = function() {
console.log("part attachment failed");
};
xhr.send();
}