03 plugin.js (1/5)
File: examples/plugins/web-settings/plugin.js (150 lines)
// web-settings: a reusable settings page for any plugin.
//
// Edit FIELDS below. Values are saved FLAT to <plugin dir>/config.json, so a
// device.json in the same folder can use them as {cfg.KEY}. Rules the device
// enforces on config.json: flat object, string or whole-number values only,
// max 2 KB. Booleans would read as empty on the device, so checkboxes are
// stored as "1" / "0".
//
// Special key: "dest_dir" overrides download.dest_dir of a device.json catalog.
const FIELDS = [
{ key: 'server', label: 'Server URL', type: 'url', placeholder: 'https://books.example.com' },
{ key: 'username', label: 'Username', type: 'text' },
{ key: 'password', label: 'Password', type: 'password' },
{ key: 'dest_dir', label: 'Save books to', type: 'text', placeholder: '/Books/Example' },
{ key: 'page', label: 'Items per page', type: 'number', min: 1, max: 16, value: '8' },
{ key: 'sort', label: 'Sort order', type: 'select',
options: [['added', 'Recently added'], ['title', 'Title'], ['author', 'Author']] },
{ key: 'wifi_ok', label: 'Sync at sleep', type: 'checkbox' },
];
// Optional: a GET request to run when "Test" is clicked. Return null to hide the button.
const testUrl = (cfg) => (cfg.server ? cfg.server + '/' : null);