diff --git a/.gitignore b/.gitignore index afa2379..982452b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ NOTES credentials.json *.sql +public/attachments/ diff --git a/public/index.css b/public/index.css index 32296fc..88b7693 100644 --- a/public/index.css +++ b/public/index.css @@ -40,3 +40,11 @@ div.justify { td, th { vertical-align:top; } +.hscroll { + width: 100%; + overflow-x: auto; + white-space: nowrap; +} +img { + height: 200px; +} diff --git a/public/index.html b/public/index.html index 45e3d2a..a45f5fa 100644 --- a/public/index.html +++ b/public/index.html @@ -40,6 +40,7 @@ propertyvalue(s) +
diff --git a/public/partdb.js b/public/partdb.js index 49a6ea4..b11298e 100644 --- a/public/partdb.js +++ b/public/partdb.js @@ -165,6 +165,19 @@ function select() td.appendChild(input); tr.appendChild(td); properties.appendChild(tr); + // 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); + console.log(attachments); + } } } } diff --git a/schema.sql b/schema.sql index 45c64bb..dfaba99 100644 --- a/schema.sql +++ b/schema.sql @@ -65,18 +65,6 @@ CREATE TABLE IF NOT EXISTS distribution ( FOREIGN KEY (distributor) REFERENCES distributor (id) ); --- part attachments -CREATE TABLE IF NOT EXISTS attachment ( - id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index - part INTEGER NOT NULL, -- the part - mime TEXT NOT NULL, -- attachment document type - path TEXT NOT NULL, -- the path to the attachment - priority INTEGER NOT NULL, -- in which place the attachment should be displayed - FOREIGN KEY (part) REFERENCES part (id), - FOREIGN KEY (type) REFERENCES document (id), - UNIQUE (part, priority) -); - -- part property CREATE TABLE IF NOT EXISTS property ( id INTEGER AUTO_INCREMENT PRIMARY KEY, -- index diff --git a/server.rb b/server.rb index 62914f9..3e1025b 100755 --- a/server.rb +++ b/server.rb @@ -13,9 +13,17 @@ require 'mysql2' require 'json' require 'sinatra' +# allow dumping crashes in browser DEBUG = true +# maximum number of parts returned PARTS_LIMIT = 100 +# credentials for database CREDENTIALS = "credentials.json" +# folder name for served pages +PUBLIC = "public" +# folder name for part attachments (in PUBLIC) +ATTACHMENTS = "attachments" + raise "database information #{CREDENTIALS} do not exist" unless File.file? CREDENTIALS # open server @@ -109,6 +117,17 @@ def get_part_by_id(id) part["properties"][k] ||= v end end + # add attachments + part["attachments"] = [] + dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"] + Dir.entries(dir).each do |file| + path = dir + "/" + file + next unless File.file? path + part["attachments"] << ATTACHMENTS + "/" + part["name"] + "/" + file + end + if parent then + part["attachments"] += parent["attachments"] + end # clean up delete = ["parent"] delete.each do |k|