add component support
This commit is contained in:
parent
23b83144a8
commit
7c5aa6e8f3
|
@ -42,6 +42,10 @@
|
|||
<tbody id="properties"></tbody>
|
||||
</table>
|
||||
<div class="hscroll" id="attachments"></div>
|
||||
<table>
|
||||
<thead><tr><th>component</th><th>quantity</th></tr></thead>
|
||||
<tbody id="components"></tbody>
|
||||
</table>
|
||||
<button type="button" onclick="delete_part()">delete part (including children)</button>
|
||||
<button type="button" onclick="update_part()">update part (or add if not selected)</button>
|
||||
</div>
|
||||
|
|
|
@ -11,7 +11,9 @@ var parts = null;
|
|||
// part field to populate
|
||||
const fields = ["name", "description", "details", "package", "manufacturer", "mpn", "family", "datasheet", "page", "location", "stock"];
|
||||
// URLs to set
|
||||
const urls = ["page","datasheet"];
|
||||
const urls = ["page", "datasheet"];
|
||||
// tables with key value information
|
||||
const tables = ["distributors", "properties", "components"];
|
||||
|
||||
function search()
|
||||
{
|
||||
|
@ -71,15 +73,11 @@ function clear()
|
|||
const a = document.getElementById('url_' + field);
|
||||
a.href = null;
|
||||
}
|
||||
// clear distributors
|
||||
const distributors = document.getElementById('distributors');
|
||||
distributors.innerHTML = null;
|
||||
// clear properties
|
||||
const properties = document.getElementById('properties');
|
||||
properties.innerHTML = null;
|
||||
// clear attachments
|
||||
const attachments = document.getElementById('attachments');
|
||||
attachments.innerHTML = null;
|
||||
// clear tables
|
||||
for (const table of tables) {
|
||||
const body = document.getElementById(table);
|
||||
body.innerHTML = null;
|
||||
}
|
||||
}
|
||||
|
||||
function select_part()
|
||||
|
@ -137,42 +135,42 @@ function select_part()
|
|||
tr.appendChild(td_url);
|
||||
distributors.appendChild(tr);
|
||||
}
|
||||
// add properties
|
||||
const properties = document.getElementById('properties');
|
||||
properties.innerHTML = null;
|
||||
for (const prop in part["properties"]) {
|
||||
const tr = document.createElement('tr');
|
||||
let td = document.createElement('td');
|
||||
let input = document.createElement('input');
|
||||
input.type = "text";
|
||||
input.style.width = "95%";
|
||||
input.value = prop;
|
||||
td.appendChild(input);
|
||||
tr.appendChild(td);
|
||||
td = document.createElement('td');
|
||||
input = document.createElement('input');
|
||||
input.type = "text";
|
||||
input.style.width = "95%";
|
||||
input.value = part["properties"][prop].join(",");
|
||||
td.appendChild(input);
|
||||
tr.appendChild(td);
|
||||
properties.appendChild(tr);
|
||||
// add properties and components
|
||||
const kv_tables = ['properties', 'components'];
|
||||
for (const kv_table of kv_tables) {
|
||||
const table = document.getElementById(kv_table);
|
||||
table.innerHTML = null;
|
||||
part[kv_table][""] = ""; // empty field to add
|
||||
for (const key in part[kv_table]) {
|
||||
const tr = document.createElement('tr');
|
||||
let td = document.createElement('td');
|
||||
let input = document.createElement('input');
|
||||
input.type = "text";
|
||||
input.style.width = "95%";
|
||||
input.value = key;
|
||||
td.appendChild(input);
|
||||
tr.appendChild(td);
|
||||
td = document.createElement('td');
|
||||
input = document.createElement('input');
|
||||
if ('components' == kv_table) {
|
||||
input.type = "number";
|
||||
input.min = "0";
|
||||
input.step = "1";
|
||||
input.value = part[kv_table][key]
|
||||
} else {
|
||||
input.type = "text";
|
||||
if ("object" == typeof part[kv_table][key]) {
|
||||
input.value = part[kv_table][key].join(",");
|
||||
} else {
|
||||
input.value = part[kv_table][key];
|
||||
}
|
||||
}
|
||||
input.style.width = "95%";
|
||||
td.appendChild(input);
|
||||
tr.appendChild(td);
|
||||
table.appendChild(tr);
|
||||
}
|
||||
}
|
||||
// add empty property field
|
||||
let tr = document.createElement('tr');
|
||||
let td = document.createElement('td');
|
||||
let input = document.createElement('input');
|
||||
input.type = "text";
|
||||
input.style.width = "95%";
|
||||
td.appendChild(input);
|
||||
tr.appendChild(td);
|
||||
td = document.createElement('td');
|
||||
input = document.createElement('input');
|
||||
input.type = "text";
|
||||
input.style.width = "95%";
|
||||
td.appendChild(input);
|
||||
tr.appendChild(td);
|
||||
properties.appendChild(tr);
|
||||
// add attachments
|
||||
const attachments = document.getElementById('attachments');
|
||||
attachments.innerHTML = null;
|
||||
|
@ -245,6 +243,16 @@ function update_part()
|
|||
part.properties[name] = value.split(",");
|
||||
}
|
||||
}
|
||||
// get components
|
||||
part.components = {};
|
||||
const components = document.getElementById('components');
|
||||
for (const component of components.rows) {
|
||||
const name = component.cells[0].firstChild.value;
|
||||
const quantity = component.cells[1].firstChild.value;
|
||||
if (name && name.length > 0) {
|
||||
part.components[name] = parseInt(quantity);
|
||||
}
|
||||
}
|
||||
console.log(part);
|
||||
|
||||
var post = new XMLHttpRequest();
|
||||
|
|
33
server.rb
33
server.rb
|
@ -139,6 +139,17 @@ def get_part_by_id(id)
|
|||
if parent then
|
||||
part["attachments"] += parent["attachments"]
|
||||
end
|
||||
# add components for assembly
|
||||
part["components"] = {}
|
||||
if parent then
|
||||
parent["components"].each do |k,v|
|
||||
part["components"][k] ||= v
|
||||
end
|
||||
end
|
||||
statement = @db.prepare("SELECT part.name AS name, assembly.quantity AS quantity FROM assembly JOIN part ON part.id = assembly.component WHERE assembly.assembled = ?")
|
||||
statement.execute(id).each do |row|
|
||||
part["components"][row["name"]] = row["quantity"]
|
||||
end
|
||||
# clean up
|
||||
delete = ["parent"]
|
||||
delete.each do |k|
|
||||
|
@ -297,7 +308,6 @@ def add_part(part)
|
|||
# update inventory
|
||||
field = "location"
|
||||
part[field] = nil if part[field] and 0 == part[field].length
|
||||
part["location"] = nil if part["stock"] and 0 == part["stock"].length
|
||||
if part[field] then
|
||||
statement = @db.prepare("SELECT id FROM #{field} WHERE LOWER(name) = ?")
|
||||
ref = statement.execute(part[field].downcase).to_a[0]
|
||||
|
@ -336,10 +346,9 @@ def add_part(part)
|
|||
end
|
||||
# update properties
|
||||
field = "properties"
|
||||
part[field] = nil if part[field] and 0 == part[field].length
|
||||
delete = @db.prepare("DELETE FROM property_value WHERE part = ?")
|
||||
delete.execute(part["id"])
|
||||
if part[field] then
|
||||
delete = @db.prepare("DELETE FROM property_value WHERE part = ?")
|
||||
delete.execute(part["id"])
|
||||
part[field].each do |name,values|
|
||||
next unless values and !values.empty?
|
||||
statement = @db.prepare("SELECT id FROM property WHERE LOWER(name) = ?")
|
||||
|
@ -356,6 +365,22 @@ def add_part(part)
|
|||
end
|
||||
end
|
||||
end
|
||||
# update components
|
||||
field = "components"
|
||||
if part[field] then
|
||||
delete = @db.prepare("DELETE FROM assembly WHERE assembled = ?")
|
||||
delete.execute(part["id"])
|
||||
part[field].each do |name,quantity|
|
||||
next unless name
|
||||
statement = @db.prepare("SELECT id FROM part WHERE LOWER(name) = ?")
|
||||
ref = statement.execute(name.downcase).to_a[0]
|
||||
#raise StandardError.new("component #{name} does not exist") unless ref
|
||||
next unless ref
|
||||
quantity ||= 0
|
||||
insert = @db.prepare("INSERT INTO assembly (assembled,component,quantity) VALUES (?,?,?)");
|
||||
insert.execute(part["id"], ref["id"], quantity)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
post '/part' do
|
||||
|
|
Loading…
Reference in New Issue