包文件→ .shtukbase/packages/layout-basic/test/demo-lens.html
test/demo-lens.html
72 行, 4.3 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- <!doctype html>
- <html lang="ru">
- <head>
- <meta charset="utf-8">
- <script src="/_pkg/layout-basic/lens.js"></script>
- <link rel="stylesheet" href="/_kit/kit.css">
- <link rel="stylesheet" href="/_kit/skins/calm.css">
- <link rel="stylesheet" href="/_kit/skins/desktop.css">
- <style>
- html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font); }
- .d-wrap { padding: 20px 28px; }
- .d-h { font-size: 22px; font-weight: 600; margin: 0 0 16px; }
- table { border-collapse: collapse; width: 100%; font-size: 14px; }
- th { text-align: left; font-weight: 500; color: var(--lx-muted); border-bottom: 1px solid var(--line); padding: 6px 8px; }
- td { border-bottom: 1px solid var(--line); padding: 6px 8px; }
- tr.r { cursor: pointer; } tr.r:hover td { background: var(--wash); }
- .d-board { display: flex; gap: 12px; align-items: flex-start; }
- .d-col { width: 220px; flex: none; }
- .d-col h3 { font-size: 13px; font-weight: 500; color: var(--lx-muted); margin: 0 0 8px; }
- .d-card { background: var(--lx-card); box-shadow: var(--shadow); border-radius: var(--lx-radius); padding: 10px; margin-bottom: 8px; cursor: pointer; font-size: 14px; }
- .d-meta { font-size: 13px; color: var(--lx-muted); margin: 4px 0; }
- </style>
- </head>
- <body>
- <div class="d-wrap" id="out"></div>
- <script>
- // A demo lens for the fake host: its kind comes from its file name (table/board/record).
- var kind = location.pathname.split('/').pop().replace('.html', '');
- var out = document.getElementById('out');
- function esc(s) { return String(s == null ? '' : s).replace(/[&<>"]/g, function (c) { return '&#' + c.charCodeAt(0) + ';'; }); }
- function opt(props, key, val) {
- var p = props && props[key]; var o = p && p.options && p.options.find(function (x) { return x.name === val; });
- return o ? '<span class="lx-chip dot" style="--cc: var(--c-' + esc(o.color) + ')">' + esc(o.label) + '</span>' : esc(val);
- }
- function draw() {
- if (kind === 'record') {
- return shtuk.call('records.read', { path: shtuk.path }).then(function (r) {
- var m = r.meta || {};
- out.innerHTML = '<h1 class="d-h">' + esc(m.icon || '') + ' ' + esc(m.title || r.path) + '</h1>' +
- Object.keys(m).filter(function (k) { return typeof m[k] !== 'object'; }).map(function (k) { return '<div class="d-meta">' + esc(k) + ': ' + esc(m[k]) + '</div>'; }).join('') +
- '<p>' + esc(r.body) + '</p>' +
- (m.status ? '<button class="lx-btn" id="next">Следующий статус</button>' : '');
- var b = document.getElementById('next');
- if (b) b.onclick = function () {
- var s = ['new', 'talk', 'won', 'lost']; var n = s[(s.indexOf(m.status) + 1) % 4];
- shtuk.call('records.write', { path: shtuk.path, meta: { status: n } }).then(draw);
- };
- });
- }
- return shtuk.call('records.list', { collection: shtuk.path }).then(function (r) {
- var props = r.collection.properties || {};
- var head = '<h1 class="d-h">' + esc(r.collection.icon || '') + ' ' + esc(r.collection.title) + '</h1>';
- if (kind === 'board') {
- var cols = (props.status && props.status.options) || [];
- out.innerHTML = head + '<div class="d-board">' + cols.map(function (c) {
- return '<div class="d-col"><h3>' + opt(props, 'status', c.name) + '</h3>' + r.items.filter(function (i) { return i.meta.status === c.name; }).map(function (i) {
- return '<div class="d-card" data-p="' + esc(i.path) + '">' + esc(i.title) + '<div class="d-meta">' + opt(props, 'source', i.meta.source) + '</div></div>';
- }).join('') + '</div>';
- }).join('') + '</div>';
- } else {
- out.innerHTML = head + '<table><tr><th>Название</th><th>Статус</th><th>Источник</th><th>Сумма</th></tr>' + r.items.map(function (i) {
- return '<tr class="r" data-p="' + esc(i.path) + '"><td>' + esc(i.title) + '</td><td>' + opt(props, 'status', i.meta.status) + '</td><td>' + opt(props, 'source', i.meta.source) + '</td><td>' + esc(i.meta.amount) + '</td></tr>';
- }).join('') + '</table>';
- }
- out.querySelectorAll('[data-p]').forEach(function (el) { el.onclick = function () { shtuk.open(el.dataset.p); }; });
- });
- }
- draw().then(function () { shtuk.ready(); }, function (e) { out.textContent = String(e); shtuk.ready(); });
- shtuk.on('changed', draw);
- </script>
- </body>
- </html>