From 1100720b03b352afd4fb39f61c1fd21ccf38c9fe Mon Sep 17 00:00:00 2001 From: mikael-lovqvists-claude-agent Date: Sun, 17 May 2026 18:05:30 +0000 Subject: [PATCH] Initial project setup for task-inventory Node.js + Express 5 backend with flat NDJSON key-value store (borrowed from electronics-inventory). Sequential integer IDs per namespace. Vanilla JS SPA with filters for status, priority, and tags. Co-Authored-By: Claude Sonnet 4.6 --- .gitignore | 3 + lib/ids.mjs | 18 + lib/kv-store.mjs | 203 ++++++++++ lib/storage.mjs | 34 ++ package-lock.json | 846 ++++++++++++++++++++++++++++++++++++++++++ package.json | 14 + public/app.mjs | 267 +++++++++++++ public/index.html | 19 + public/lib/api.mjs | 16 + public/lib/dom.mjs | 16 + public/style.css | 204 ++++++++++ public/templates.html | 51 +++ server.mjs | 82 ++++ 13 files changed, 1773 insertions(+) create mode 100644 .gitignore create mode 100644 lib/ids.mjs create mode 100644 lib/kv-store.mjs create mode 100644 lib/storage.mjs create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 public/app.mjs create mode 100644 public/index.html create mode 100644 public/lib/api.mjs create mode 100644 public/lib/dom.mjs create mode 100644 public/style.css create mode 100644 public/templates.html create mode 100644 server.mjs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d8fc6d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +data/ +*.tmp diff --git a/lib/ids.mjs b/lib/ids.mjs new file mode 100644 index 0000000..31f346b --- /dev/null +++ b/lib/ids.mjs @@ -0,0 +1,18 @@ +import { Simple_KeyValue_Store } from './kv-store.mjs'; +import { mkdirSync } from 'node:fs'; + +mkdirSync('./data', { recursive: true }); + +const id_store = new Simple_KeyValue_Store('./data/ids.ndjson', { + auto_load: true, + auto_store: true, + debounce_flush_timeout: 1000, +}); + +export function next_id(namespace) { + const key = `seq:${namespace}`; + const current = id_store.get(key) ?? 0; + const next = current + 1; + id_store.set(key, next); + return next; +} diff --git a/lib/kv-store.mjs b/lib/kv-store.mjs new file mode 100644 index 0000000..a2d096c --- /dev/null +++ b/lib/kv-store.mjs @@ -0,0 +1,203 @@ +import fs from 'node:fs'; + +function DEFAULT_SERIALIZER_FACTORY() { + const s = new Serializer(); + + //TODO - buffers and possibly typed arrays + s.register_constructor(BigInt, 'Bi', (raw) => raw.toString(), (stored) => BigInt(stored)); + s.register_constructor(Buffer, 'Bu', (raw) => raw.toString('base64'), (stored) => Buffer.from(stored, 'base64')); + s.register_default('P', (raw) => (JSON.stringify(raw), raw), (stored) => stored); + + return s; +} + +class Serializer { + constructor(constructor_lut=new Map(), identity_lut=new Map(), type_lut=new Map()) { + Object.assign(this, { constructor_lut, identity_lut, type_lut, default: null }); + } + + serialize(value) { + const { constructor_lut, identity_lut } = this; + + const by_identity = identity_lut.get(value); + if (by_identity) { + const { type_id, serialize_value } = by_identity; + return [ type_id, serialize_value(value) ]; + } + + const by_constructor = constructor_lut.get(value.constructor); + if (by_constructor) { + const { type_id, serialize_value } = by_constructor; + return [ type_id, serialize_value(value) ]; + } + + return [this.default.type_id, this.default.serialize_value(value)]; + } + + deserialize(value) { + if (value === undefined) { + throw new Error(`Deserialize called without value`); + } + const [ type_id, raw_value ] = value; + const { type_lut } = this; + + const operation = type_lut.get(type_id); + if (!operation) { + throw new Error(`Unknown type_id: ${type_id}`); + } + const { deserialize_value } = operation; + return deserialize_value(raw_value); + } + + register_default(type_id, serialize_value, deserialize_value) { + const { type_lut } = this; + const operation = { selector: 'default', type_id, serialize_value, deserialize_value }; + type_lut.set(type_id, operation); + this.default = operation; + } + + register_constructor(constructor, type_id, serialize_value, deserialize_value) { + const { constructor_lut, type_lut } = this; + const operation = { selector: 'constructor', constructor, type_id, serialize_value, deserialize_value }; + constructor_lut.set(constructor, operation); + type_lut.set(type_id, operation); + } + + register_identity(identity, type_id, serialize_value, deserialize_value) { + const { identity_lut, type_lut } = this; + const operation = { selector: 'identity', identity, type_id, serialize_value, deserialize_value }; + identity_lut.set(identity, operation); + type_lut.set(type_id, operation); + } +} + + + +const DEFAULT_SETTINGS = { + auto_load: false, + auto_store: false, + auto_store_events: [ + 'SIGINT', + 'SIGTERM', + 'exit', + ], + debounce_flush_timeout: null, +} + + +export class Simple_KeyValue_Store { + #flush_debounce_timer = null + + constructor(storage_path, settings=DEFAULT_SETTINGS, data=new Map(), serializer=DEFAULT_SERIALIZER_FACTORY()) { + + const actual_settings = { ...DEFAULT_SETTINGS, ...settings }; + Object.assign(this, { storage_path, data, serializer, ...actual_settings }); + + if (this.auto_load) { + this.load(); + } + + if (this.auto_store) { + for (const event of this.auto_store_events) { + process.on(event, () => (this.store(), process.exit()) ); + } + } + + } + + trigger_flush_debouncer() { + if (this.#flush_debounce_timer) { + clearTimeout(this.#flush_debounce_timer); + this.#flush_debounce_timer = null; + } + + const { debounce_flush_timeout } = this; + if (debounce_flush_timeout) { + this.#flush_debounce_timer = setTimeout(() => { + this.#flush_debounce_timer = null; + this.store(); + }, debounce_flush_timeout); + } + } + + + create_pending_filename() { + const suffix = process.hrtime.bigint().toString(16); + const { storage_path } = this; + return `${storage_path}-${suffix}.tmp`; + } + + get(key) { + const { serializer } = this; + const raw_entry = this.data.get(key); + if (raw_entry !== undefined) { + return serializer.deserialize(raw_entry); + } + } + + set(key, value) { + const { serializer } = this; + this.data.set(key, serializer.serialize(value)); + this.trigger_flush_debouncer(); + } + + delete(key) { + const result = this.data.delete(key); + if (result) { + this.trigger_flush_debouncer(); + } + return result; + } + + load() { + const { data, storage_path } = this; + let file_contents; + try { + file_contents = fs.readFileSync(storage_path, 'utf-8'); + } catch (e) { + if (e.code === 'ENOENT') { return; } + throw e; + } + + for (const line of file_contents.split('\n')) { + if (!line) continue; + const [key, value] = JSON.parse(line); + data.set(key, value); + } + + } + + store() { + if (this.#flush_debounce_timer) { + clearTimeout(this.#flush_debounce_timer); + this.#flush_debounce_timer = null; + } + const { data, storage_path } = this; + const pending_out_path = this.create_pending_filename(); + const out_fd = fs.openSync(pending_out_path, 'w'); + try { + for (const [key, value] of data.entries()) { + fs.writeSync(out_fd, JSON.stringify([key, value]) + '\n'); + } + fs.closeSync(out_fd); + fs.renameSync(pending_out_path, storage_path); + } finally { + try { fs.closeSync(out_fd); } catch {} + try { fs.unlinkSync(pending_out_path); } catch {} + } + } + +} + + +/* +const kvs = new Simple_KeyValue_Store('./data.ndjson', { auto_load: true, auto_store: true, debounce_flush_timeout: 10_000 }); + +console.log(kvs.get('hello'), kvs.get('shello'), kvs.get('Path')); +kvs.set('hello', 123456789n) + +console.log(Buffer.from([123, 10, 20])); +kvs.set('Path', Buffer.from([123, 10, 20])); + +process.exit() //Exit immediately instead of waiting for debounce_timer (this also prevents double store - we should document this properly) +*/ \ No newline at end of file diff --git a/lib/storage.mjs b/lib/storage.mjs new file mode 100644 index 0000000..dbe0f36 --- /dev/null +++ b/lib/storage.mjs @@ -0,0 +1,34 @@ +import { mkdirSync } from 'node:fs'; +import { Simple_KeyValue_Store } from './kv-store.mjs'; + +mkdirSync('./data', { recursive: true }); + +const store = new Simple_KeyValue_Store('./data/tasks.ndjson', { + auto_load: true, + auto_store: true, + debounce_flush_timeout: 5000, +}); + +// --- Tasks --- + +export function list_tasks() { + const result = []; + for (const [key] of store.data.entries()) { + if (key.startsWith('task:')) { + result.push(store.get(key)); + } + } + return result.sort((a, b) => b.created_at - a.created_at); +} + +export function get_task(id) { + return store.get(`task:${id}`) ?? null; +} + +export function set_task(task) { + store.set(`task:${task.id}`, task); +} + +export function delete_task(id) { + return store.delete(`task:${id}`); +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e1bba2c --- /dev/null +++ b/package-lock.json @@ -0,0 +1,846 @@ +{ + "name": "task-inventory", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "task-inventory", + "version": "0.1.0", + "dependencies": { + "express": "^5.2.1" + }, + "engines": { + "node": ">=25" + } + }, + "node_modules/accepts": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", + "integrity": "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==", + "license": "MIT", + "dependencies": { + "mime-types": "^3.0.0", + "negotiator": "^1.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/body-parser": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.2.tgz", + "integrity": "sha512-oP5VkATKlNwcgvxi0vM0p/D3n2C3EReYVX+DNYs5TjZFn/oQt2j+4sVJtSMr18pdRr8wjTcBl6LoV+FUwzPmNA==", + "license": "MIT", + "dependencies": { + "bytes": "^3.1.2", + "content-type": "^1.0.5", + "debug": "^4.4.3", + "http-errors": "^2.0.0", + "iconv-lite": "^0.7.0", + "on-finished": "^2.4.1", + "qs": "^6.14.1", + "raw-body": "^3.0.1", + "type-is": "^2.0.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/content-disposition": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-1.1.0.tgz", + "integrity": "sha512-5jRCH9Z/+DRP7rkvY83B+yGIGX96OYdJmzngqnw2SBSxqCFPd0w2km3s5iawpGX8krnwSGmF0FW5Nhr0Hfai3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz", + "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.2.2.tgz", + "integrity": "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==", + "license": "MIT", + "engines": { + "node": ">=6.6.0" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==", + "license": "MIT" + }, + "node_modules/encodeurl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", + "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", + "license": "MIT" + }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/express": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/express/-/express-5.2.1.tgz", + "integrity": "sha512-hIS4idWWai69NezIdRt2xFVofaF4j+6INOpJlVOLDO8zXGpUVEVzIYk12UUi2JzjEzWL3IOAxcTubgz9Po0yXw==", + "license": "MIT", + "dependencies": { + "accepts": "^2.0.0", + "body-parser": "^2.2.1", + "content-disposition": "^1.0.0", + "content-type": "^1.0.5", + "cookie": "^0.7.1", + "cookie-signature": "^1.2.1", + "debug": "^4.4.0", + "depd": "^2.0.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "finalhandler": "^2.1.0", + "fresh": "^2.0.0", + "http-errors": "^2.0.0", + "merge-descriptors": "^2.0.0", + "mime-types": "^3.0.0", + "on-finished": "^2.4.1", + "once": "^1.4.0", + "parseurl": "^1.3.3", + "proxy-addr": "^2.0.7", + "qs": "^6.14.0", + "range-parser": "^1.2.1", + "router": "^2.2.0", + "send": "^1.1.0", + "serve-static": "^2.2.0", + "statuses": "^2.0.1", + "type-is": "^2.0.1", + "vary": "^1.1.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/finalhandler": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-2.1.1.tgz", + "integrity": "sha512-S8KoZgRZN+a5rNwqTxlZZePjT/4cnm0ROV70LedRHZ0p8u9fRID0hJUZQpkKLzro8LfmC8sx23bY6tVNxv8pQA==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "on-finished": "^2.4.1", + "parseurl": "^1.3.3", + "statuses": "^2.0.1" + }, + "engines": { + "node": ">= 18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-2.0.0.tgz", + "integrity": "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.3.tgz", + "integrity": "sha512-ej4AhfhfL2Q2zpMmLo7U1Uv9+PyhIZpgQLGT1F9miIGmiCJIoCgSmczFdrc97mWT4kVY72KA+WnnhJ5pghSvSg==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/http-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz", + "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==", + "license": "MIT", + "dependencies": { + "depd": "~2.0.0", + "inherits": "~2.0.4", + "setprototypeof": "~1.2.0", + "statuses": "~2.0.2", + "toidentifier": "~1.0.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/iconv-lite": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.2.tgz", + "integrity": "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/is-promise": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz", + "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==", + "license": "MIT" + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/media-typer": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.0.tgz", + "integrity": "sha512-aisnrDP4GNe06UcKFnV5bfMNPBUw4jsLGaWwWfnH3v02GnBuXX2MCVn5RbrWo0j3pczUilYblq7fQ7Nw2t5XKw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/merge-descriptors": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz", + "integrity": "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/mime-db": { + "version": "1.54.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz", + "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.2.tgz", + "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==", + "license": "MIT", + "dependencies": { + "mime-db": "^1.54.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/negotiator": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.0.0.tgz", + "integrity": "sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/object-inspect": { + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "license": "ISC", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/path-to-regexp": { + "version": "8.4.2", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.4.2.tgz", + "integrity": "sha512-qRcuIdP69NPm4qbACK+aDogI5CBDMi1jKe0ry5rSQJz8JVLsC7jV8XpiJjGRLLol3N+R5ihGYcrPLTno6pAdBA==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "license": "MIT", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/qs": { + "version": "6.15.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.15.2.tgz", + "integrity": "sha512-Rzq0KEyX/w/tEybncDgdkZrJgVUsUMk3xjh3t5bv3S1HTAtg+uOYt72+ZfwiQwKdysThkTBdL/rTi6HDmX9Ddw==", + "license": "BSD-3-Clause", + "dependencies": { + "side-channel": "^1.1.0" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/raw-body": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.2.tgz", + "integrity": "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "http-errors": "~2.0.1", + "iconv-lite": "~0.7.0", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/router": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/router/-/router-2.2.0.tgz", + "integrity": "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "depd": "^2.0.0", + "is-promise": "^4.0.0", + "parseurl": "^1.3.3", + "path-to-regexp": "^8.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, + "node_modules/send": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/send/-/send-1.2.1.tgz", + "integrity": "sha512-1gnZf7DFcoIcajTjTwjwuDjzuz4PPcY2StKPlsGAQ1+YH20IRVrBaXSWmdjowTJ6u8Rc01PoYOGHXfP1mYcZNQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "fresh": "^2.0.0", + "http-errors": "^2.0.1", + "mime-types": "^3.0.2", + "ms": "^2.1.3", + "on-finished": "^2.4.1", + "range-parser": "^1.2.1", + "statuses": "^2.0.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/serve-static": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.1.tgz", + "integrity": "sha512-xRXBn0pPqQTVQiC8wyQrKs2MOlX24zQ0POGaj0kultvoOCstBQM5yvOhAVSUwOMjQtTvsPWoNCHfPGwaaQJhTw==", + "license": "MIT", + "dependencies": { + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "parseurl": "^1.3.3", + "send": "^1.2.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==", + "license": "ISC" + }, + "node_modules/side-channel": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz", + "integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.3", + "side-channel-list": "^1.0.0", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz", + "integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/statuses": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", + "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "license": "MIT", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/type-is": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.1.0.tgz", + "integrity": "sha512-faYHw0anBbc/kWF3zFTEnxSFOAGUX9GFbOBthvDdLsIlEoWOFOtS0zgCiQYwIskL9iGXZL3kAXD8OoZ4GmMATA==", + "license": "MIT", + "dependencies": { + "content-type": "^2.0.0", + "media-typer": "^1.1.0", + "mime-types": "^3.0.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/type-is/node_modules/content-type": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.0.0.tgz", + "integrity": "sha512-j/O/d7GcZCyNl7/hwZAb606rzqkyvaDctLmckbxLzHvFBzTJHuGEdodATcP3yIRoDrLHkIATJuvzbFlp/ki2cQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "license": "ISC" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..3feaf3c --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "task-inventory", + "version": "0.1.0", + "type": "module", + "engines": { + "node": ">=25" + }, + "scripts": { + "start": "node server.mjs" + }, + "dependencies": { + "express": "^5.2.1" + } +} diff --git a/public/app.mjs b/public/app.mjs new file mode 100644 index 0000000..67c1c80 --- /dev/null +++ b/public/app.mjs @@ -0,0 +1,267 @@ +import * as api from './lib/api.mjs'; +import { qs, clone, set_text, show, hide } from './lib/dom.mjs'; + +// --------------------------------------------------------------------------- +// State +// --------------------------------------------------------------------------- + +class App_State { + constructor() { + this.tasks = []; + this.filter_status = 'open'; + this.filter_priority = ''; + this.filter_tag = ''; + this.search = ''; + } +} + +const state = new App_State(); + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +function filtered_tasks() { + return state.tasks.filter(t => { + if (state.filter_status && t.status !== state.filter_status) { return false; } + if (state.filter_priority && t.priority !== state.filter_priority) { return false; } + if (state.filter_tag && !t.tags.includes(state.filter_tag)) { return false; } + if (state.search) { + const q = state.search.toLowerCase(); + if (!t.title.toLowerCase().includes(q) && !t.body.toLowerCase().includes(q)) { return false; } + } + return true; + }); +} + +function all_tags() { + const tags = new Set(); + for (const t of state.tasks) { + for (const tag of t.tags) { tags.add(tag); } + } + return [...tags].sort(); +} + +// --------------------------------------------------------------------------- +// Task dialog +// --------------------------------------------------------------------------- + +class Task_Dialog { + constructor() { + const el = clone('t-task-dialog'); + document.body.appendChild(el); + this.dialog = el; + this.form = el.querySelector('form'); + this.on_save = null; + + el.querySelector('.btn-cancel').addEventListener('click', () => el.close()); + this.form.addEventListener('submit', (e) => { + e.preventDefault(); + if (this.on_save) { this.on_save(this._read_form()); } + el.close(); + }); + } + + _read_form() { + const fd = new FormData(this.form); + const tags_raw = fd.get('tags').trim(); + return { + title: fd.get('title').trim(), + body: fd.get('body').trim(), + status: fd.get('status'), + priority: fd.get('priority'), + tags: tags_raw ? tags_raw.split(',').map(s => s.trim()).filter(Boolean) : [], + }; + } + + open(title_text, initial = {}, on_save) { + set_text(this.dialog, '.dialog-title', title_text); + this.form.elements['title'].value = initial.title ?? ''; + this.form.elements['body'].value = initial.body ?? ''; + this.form.elements['status'].value = initial.status ?? 'open'; + this.form.elements['priority'].value = initial.priority ?? 'normal'; + this.form.elements['tags'].value = (initial.tags ?? []).join(', '); + this.on_save = on_save; + this.dialog.showModal(); + } +} + +const task_dialog = new Task_Dialog(); + +// --------------------------------------------------------------------------- +// Render +// --------------------------------------------------------------------------- + +function render_tasks(container) { + container.innerHTML = ''; + + // Toolbar + const toolbar = document.createElement('div'); + toolbar.className = 'toolbar'; + toolbar.innerHTML = '

Tasks

'; + const add_btn = document.createElement('button'); + add_btn.className = 'btn-primary'; + add_btn.textContent = '+ New task'; + add_btn.addEventListener('click', () => open_add_dialog()); + toolbar.appendChild(add_btn); + container.appendChild(toolbar); + + // Filter bar + const filter_bar = document.createElement('div'); + filter_bar.className = 'filter-bar'; + + const status_sel = document.createElement('select'); + for (const [val, label] of [['', 'All statuses'], ['open', 'Open'], ['deferred', 'Deferred'], ['done', 'Done'], ['cancelled', 'Cancelled']]) { + const opt = document.createElement('option'); + opt.value = val; opt.textContent = label; + if (val === state.filter_status) { opt.selected = true; } + status_sel.appendChild(opt); + } + status_sel.addEventListener('change', () => { state.filter_status = status_sel.value; render(); }); + filter_bar.appendChild(status_sel); + + const priority_sel = document.createElement('select'); + for (const [val, label] of [['', 'All priorities'], ['high', 'High'], ['normal', 'Normal'], ['low', 'Low']]) { + const opt = document.createElement('option'); + opt.value = val; opt.textContent = label; + if (val === state.filter_priority) { opt.selected = true; } + priority_sel.appendChild(opt); + } + priority_sel.addEventListener('change', () => { state.filter_priority = priority_sel.value; render(); }); + filter_bar.appendChild(priority_sel); + + const tag_sel = document.createElement('select'); + const all_opt = document.createElement('option'); + all_opt.value = ''; all_opt.textContent = 'All tags'; + tag_sel.appendChild(all_opt); + for (const tag of all_tags()) { + const opt = document.createElement('option'); + opt.value = tag; opt.textContent = tag; + if (tag === state.filter_tag) { opt.selected = true; } + tag_sel.appendChild(opt); + } + tag_sel.addEventListener('change', () => { state.filter_tag = tag_sel.value; render(); }); + filter_bar.appendChild(tag_sel); + + const search_input = document.createElement('input'); + search_input.type = 'text'; + search_input.placeholder = 'Search…'; + search_input.value = state.search; + search_input.addEventListener('input', () => { state.search = search_input.value; render(); }); + filter_bar.appendChild(search_input); + + container.appendChild(filter_bar); + + // Task list + const tasks = filtered_tasks(); + const list = document.createElement('div'); + list.className = 'task-list'; + + if (tasks.length === 0) { + const empty = document.createElement('div'); + empty.className = 'empty-state'; + empty.textContent = 'No tasks.'; + list.appendChild(empty); + } + + for (const task of tasks) { + const row = clone('t-task-row'); + row.dataset.priority = task.priority; + row.dataset.status = task.status; + + set_text(row, '.task-id', `#${task.id}`); + + const status_el = row.querySelector('.task-status'); + status_el.textContent = task.status; + status_el.dataset.val = task.status; + + const priority_el = row.querySelector('.task-priority'); + priority_el.textContent = task.priority; + priority_el.dataset.val = task.priority; + + set_text(row, '.task-title', task.title); + + const tags_el = row.querySelector('.task-tags'); + for (const tag of task.tags) { + const span = document.createElement('span'); + span.className = 'tag'; + span.textContent = tag; + tags_el.appendChild(span); + } + + row.querySelector('.btn-edit').addEventListener('click', () => open_edit_dialog(task)); + row.querySelector('.btn-delete').addEventListener('click', () => confirm_delete(task)); + + list.appendChild(row); + } + + container.appendChild(list); +} + +function render() { + const main = document.getElementById('main'); + render_tasks(main); +} + +// --------------------------------------------------------------------------- +// Actions +// --------------------------------------------------------------------------- + +function open_add_dialog() { + task_dialog.open('New task', {}, async (data) => { + try { + const { task } = await api.create_task(data); + state.tasks.unshift(task); + render(); + } catch (err) { + alert(err.message); + } + }); +} + +function open_edit_dialog(task) { + task_dialog.open('Edit task', task, async (data) => { + try { + const { task: updated } = await api.update_task(task.id, data); + const idx = state.tasks.findIndex(t => t.id === task.id); + if (idx !== -1) { state.tasks[idx] = updated; } + render(); + } catch (err) { + alert(err.message); + } + }); +} + +function confirm_delete(task) { + if (!confirm(`Delete task #${task.id}: "${task.title}"?`)) { return; } + api.delete_task(task.id).then(() => { + state.tasks = state.tasks.filter(t => t.id !== task.id); + render(); + }).catch(err => alert(err.message)); +} + +// --------------------------------------------------------------------------- +// Init +// --------------------------------------------------------------------------- + +async function init() { + // Inject templates + const tmpl_res = await fetch('/templates.html'); + const tmpl_html = await tmpl_res.text(); + const tmpl_container = document.createElement('div'); + tmpl_container.innerHTML = tmpl_html; + for (const tmpl of tmpl_container.querySelectorAll('template')) { + document.body.appendChild(tmpl); + } + + // Load data + const { tasks } = await api.get_tasks(); + state.tasks = tasks; + + render(); +} + +init().catch(err => { + console.error('Init failed:', err); + document.getElementById('main').textContent = 'Failed to load: ' + err.message; +}); diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..5c2a7cc --- /dev/null +++ b/public/index.html @@ -0,0 +1,19 @@ + + + + + + Task Inventory + + + +
+

Task Inventory

+ +
+
+ + + diff --git a/public/lib/api.mjs b/public/lib/api.mjs new file mode 100644 index 0000000..74af5c9 --- /dev/null +++ b/public/lib/api.mjs @@ -0,0 +1,16 @@ +async function req(method, path, body) { + const opts = { method, headers: {} }; + if (body !== undefined) { + opts.headers['Content-Type'] = 'application/json'; + opts.body = JSON.stringify(body); + } + const res = await fetch(path, opts); + const data = await res.json(); + if (!data.ok) { throw new Error(data.error ?? 'Request failed'); } + return data; +} + +export const get_tasks = () => req('GET', '/api/tasks'); +export const create_task = (body) => req('POST', '/api/tasks', body); +export const update_task = (id, body) => req('PUT', `/api/tasks/${id}`, body); +export const delete_task = (id) => req('DELETE', `/api/tasks/${id}`); diff --git a/public/lib/dom.mjs b/public/lib/dom.mjs new file mode 100644 index 0000000..6ddbea1 --- /dev/null +++ b/public/lib/dom.mjs @@ -0,0 +1,16 @@ +export function qs(scope, selector) { + if (typeof scope === 'string') { return document.querySelector(scope); } + return scope.querySelector(selector); +} + +export function clone(template_id) { + return document.getElementById(template_id).content.cloneNode(true).firstElementChild; +} + +export function set_text(el, selector, text) { + const target = typeof selector === 'string' ? el.querySelector(selector) : selector; + target.textContent = text; +} + +export function show(el) { el.hidden = false; } +export function hide(el) { el.hidden = true; } diff --git a/public/style.css b/public/style.css new file mode 100644 index 0000000..8ddb606 --- /dev/null +++ b/public/style.css @@ -0,0 +1,204 @@ +*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } + +body { + font-family: system-ui, sans-serif; + font-size: 14px; + background: #1a1a1a; + color: #e0e0e0; + min-height: 100vh; +} + +header { + display: flex; + align-items: center; + gap: 1.5rem; + padding: 0.75rem 1.25rem; + background: #242424; + border-bottom: 1px solid #333; +} + +header h1 { + font-size: 1rem; + font-weight: 600; + color: #fff; +} + +nav { display: flex; gap: 0.5rem; } + +.nav-btn { + padding: 0.3rem 0.75rem; + border: 1px solid #444; + border-radius: 4px; + background: transparent; + color: #aaa; + cursor: pointer; + font-size: 13px; +} + +.nav-btn.active, .nav-btn:hover { + background: #333; + color: #fff; + border-color: #555; +} + +main { padding: 1.25rem; } + +/* Toolbar */ +.toolbar { + display: flex; + align-items: center; + gap: 0.75rem; + margin-bottom: 1rem; +} + +.toolbar h2 { font-size: 1rem; flex: 1; } + +/* Filter bar */ +.filter-bar { + display: flex; + gap: 0.5rem; + margin-bottom: 0.75rem; + flex-wrap: wrap; +} + +.filter-bar select, .filter-bar input { + padding: 0.3rem 0.5rem; + background: #2a2a2a; + border: 1px solid #444; + border-radius: 4px; + color: #e0e0e0; + font-size: 13px; +} + +/* Task list */ +.task-list { display: flex; flex-direction: column; gap: 1px; } + +.task-row { + display: grid; + grid-template-columns: 2.5rem 5rem 4.5rem 1fr auto auto; + align-items: center; + gap: 0.5rem; + padding: 0.5rem 0.75rem; + background: #242424; + border-radius: 4px; + border-left: 3px solid transparent; +} + +.task-row[data-priority='high'] { border-left-color: #e05555; } +.task-row[data-priority='normal'] { border-left-color: #5588e0; } +.task-row[data-priority='low'] { border-left-color: #555; } + +.task-row[data-status='done'] { opacity: 0.5; } +.task-row[data-status='cancelled'] { opacity: 0.4; text-decoration: line-through; } + +.task-id { color: #666; font-size: 12px; } +.task-title { font-weight: 500; } + +.task-status, .task-priority { + font-size: 11px; + padding: 0.15rem 0.4rem; + border-radius: 3px; + text-align: center; + text-transform: uppercase; + letter-spacing: 0.03em; +} + +.task-status[data-val='open'] { background: #1e3a5f; color: #6ab0f5; } +.task-status[data-val='done'] { background: #1a3a1a; color: #6ad06a; } +.task-status[data-val='cancelled'] { background: #2a1a1a; color: #c06060; } +.task-status[data-val='deferred'] { background: #2a2a1a; color: #c0b060; } + +.task-priority[data-val='high'] { background: #3a1a1a; color: #e08080; } +.task-priority[data-val='normal'] { background: transparent; color: #777; } +.task-priority[data-val='low'] { background: transparent; color: #555; } + +.task-tags { display: flex; gap: 0.25rem; flex-wrap: wrap; } + +.tag { + font-size: 11px; + padding: 0.1rem 0.35rem; + background: #2a2a3a; + border-radius: 3px; + color: #8899cc; +} + +.task-actions { display: flex; gap: 0.4rem; } + +/* Buttons */ +button { + cursor: pointer; + border: none; + border-radius: 4px; + font-size: 13px; + padding: 0.3rem 0.7rem; + background: #333; + color: #ccc; +} + +button:hover { background: #444; color: #fff; } + +.btn-primary { + background: #2a4a7a; + color: #8bb8f0; +} + +.btn-primary:hover { background: #335a90; color: #c0d8ff; } + +.btn-danger { background: #4a1a1a; color: #e08080; } +.btn-danger:hover { background: #5a2020; color: #ffaaaa; } + +.btn-edit { font-size: 12px; padding: 0.2rem 0.5rem; } +.btn-delete { font-size: 12px; padding: 0.2rem 0.5rem; } + +/* Dialog */ +dialog { + background: #242424; + border: 1px solid #444; + border-radius: 8px; + color: #e0e0e0; + padding: 1.5rem; + width: 480px; + max-width: 95vw; +} + +dialog::backdrop { background: rgba(0,0,0,0.6); } + +dialog h2 { margin-bottom: 1rem; font-size: 1rem; } + +dialog label { + display: flex; + flex-direction: column; + gap: 0.3rem; + margin-bottom: 0.75rem; + font-size: 12px; + color: #999; +} + +dialog input, dialog textarea, dialog select { + padding: 0.4rem 0.6rem; + background: #1a1a1a; + border: 1px solid #444; + border-radius: 4px; + color: #e0e0e0; + font-size: 13px; + font-family: inherit; + resize: vertical; +} + +dialog input:focus, dialog textarea:focus, dialog select:focus { + outline: none; + border-color: #5588e0; +} + +.dialog-buttons { + display: flex; + gap: 0.5rem; + justify-content: flex-end; + margin-top: 1rem; +} + +.empty-state { + color: #555; + padding: 2rem; + text-align: center; +} diff --git a/public/templates.html b/public/templates.html new file mode 100644 index 0000000..8253d1c --- /dev/null +++ b/public/templates.html @@ -0,0 +1,51 @@ + + + + + diff --git a/server.mjs b/server.mjs new file mode 100644 index 0000000..59aff12 --- /dev/null +++ b/server.mjs @@ -0,0 +1,82 @@ +process.on('unhandledRejection', (reason) => { console.error('[unhandledRejection]', reason); }); +process.on('uncaughtException', (err) => { console.error('[uncaughtException]', err); process.exit(1); }); + +import express from 'express'; +import { next_id } from './lib/ids.mjs'; +import { list_tasks, get_task, set_task, delete_task } from './lib/storage.mjs'; + +const app = express(); +app.use(express.json()); +app.use(express.static(new URL('./public/', import.meta.url).pathname)); + +const PORT = process.env.PORT ?? 3025; +const BIND_ADDRESS = process.env.BIND_ADDRESS ?? 'localhost'; + +function ok(res, data = {}) { res.json({ ok: true, ...data }); } +function fail(res, msg, status = 400) { res.status(status).json({ ok: false, error: msg }); } + +// --------------------------------------------------------------------------- +// Tasks +// --------------------------------------------------------------------------- + +app.get('/api/tasks', (req, res) => { + ok(res, { tasks: list_tasks() }); +}); + +app.post('/api/tasks', (req, res) => { + const { title, body = '', status = 'open', priority = 'normal', tags = [] } = req.body; + if (!title?.trim()) { return fail(res, 'title is required'); } + const now = Date.now(); + const task = { + id: next_id('task'), + title: title.trim(), + body: body.trim(), + status, + priority, + tags: Array.isArray(tags) ? tags : [], + created_at: now, + updated_at: now, + }; + set_task(task); + ok(res, { task }); +}); + +app.get('/api/tasks/:id', (req, res) => { + const task = get_task(Number(req.params.id)); + if (!task) { return fail(res, 'not found', 404); } + ok(res, { task }); +}); + +app.put('/api/tasks/:id', (req, res) => { + const existing = get_task(Number(req.params.id)); + if (!existing) { return fail(res, 'not found', 404); } + const { title, body, status, priority, tags } = req.body; + const updated = { ...existing, updated_at: Date.now() }; + if (title !== undefined) { updated.title = title.trim(); } + if (body !== undefined) { updated.body = body.trim(); } + if (status !== undefined) { updated.status = status; } + if (priority !== undefined) { updated.priority = priority; } + if (tags !== undefined && Array.isArray(tags)) { updated.tags = tags; } + set_task(updated); + ok(res, { task: updated }); +}); + +app.delete('/api/tasks/:id', (req, res) => { + if (!delete_task(Number(req.params.id))) { return fail(res, 'not found', 404); } + ok(res); +}); + +// SPA fallback +const INDEX_HTML = new URL('./public/index.html', import.meta.url).pathname; +app.get('/{*path}', (req, res) => res.sendFile(INDEX_HTML)); + +app.use((err, req, res, next) => { + console.error(`[express error] ${req.method} ${req.path}`, err); + if (!res.headersSent) { + res.status(500).json({ ok: false, error: err.message ?? 'Internal server error' }); + } +}); + +app.listen(PORT, BIND_ADDRESS, () => { + console.log(`Task Inventory running on http://${BIND_ADDRESS}:${PORT}`); +});