create-pmos 0.407.0 → 0.409.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +25 -25
- package/payload/KIT-VERSION +2 -2
- package/payload/planning/evals/eval-calibration.md +1 -1
- package/payload/planning/okrs/health-budgets.md +1 -1
- package/payload/state/discoveries.md +1 -1
- package/payload/state/log.md +1 -1
- package/workspace/board-programs-test.js +1 -1
- package/workspace/board-wf-shot.js +45 -0
- package/workspace/board-workflows-test.js +186 -0
- package/workspace/briefing-premium-board-test.js +2 -2
- package/workspace/briefing-premium-initiative-test.js +1 -1
- package/workspace/changelog.json +24 -0
- package/workspace/fixtures/board-workflows-facts.js +49 -0
- package/workspace/fixtures/workflows-facts.js +53 -0
- package/workspace/guide/home.md +2 -2
- package/workspace/guide/index.md +1 -0
- package/workspace/guide/workflows.md +58 -0
- package/workspace/home-other-product-test.js +94 -0
- package/workspace/ini-workflow-test.js +177 -0
- package/workspace/lib/board-premium.js +30 -5
- package/workspace/lib/board-trail.js +24 -7
- package/workspace/lib/briefing-premium-agent.js +3 -0
- package/workspace/lib/engine/boards.js +30 -2
- package/workspace/lib/engine/workflows.js +204 -0
- package/workspace/lib/pmos-verbs.js +3 -0
- package/workspace/lib/programs.js +22 -7
- package/workspace/lib/route-modules.js +1 -0
- package/workspace/lib/routes-boards.js +157 -7
- package/workspace/lib/routes-programs.js +5 -0
- package/workspace/lib/routes-workflows.js +105 -0
- package/workspace/lib/workflow-chat.js +26 -0
- package/workspace/lib/workflow-cli.js +141 -0
- package/workspace/lib/workflows-store.js +108 -0
- package/workspace/package-lock.json +2 -2
- package/workspace/package.json +1 -1
- package/workspace/program-charter-test.js +243 -0
- package/workspace/program-panel-test.js +1 -1
- package/workspace/public/pm/bd-acts.js +10 -5
- package/workspace/public/pm/bd-asks.js +2 -2
- package/workspace/public/pm/bd-prog-panel.js +33 -14
- package/workspace/public/pm/bd-prog.js +11 -3
- package/workspace/public/pm/bd-views.js +128 -45
- package/workspace/public/pm/bd-wf.js +99 -0
- package/workspace/public/pm/cozy-board.css +67 -6
- package/workspace/public/pm/cozy-evmodel.js +1 -1
- package/workspace/public/pm/cozy-panel.css +12 -6
- package/workspace/public/pm/cozy-settings.js +4 -2
- package/workspace/public/pm/cozy-viewer.js +36 -9
- package/workspace/public/pm/cozy-vw-shell.css +8 -0
- package/workspace/public/pm/goal-chat.js +1 -1
- package/workspace/public/pm/home-app.js +50 -13
- package/workspace/public/pm/home-ini-model.js +2 -2
- package/workspace/public/pm/home-ini.css +30 -0
- package/workspace/public/pm/home-ini.js +52 -9
- package/workspace/public/pm/home-list.js +5 -3
- package/workspace/public/pm/home-today.js +4 -4
- package/workspace/public/pm/ini-wf-model.js +91 -0
- package/workspace/public/pm/ini-wf.js +160 -0
- package/workspace/public/pm/premium-board.js +98 -30
- package/workspace/public/pm/premium-initiative-head.js +18 -8
- package/workspace/public/pm/program-charter.css +135 -0
- package/workspace/public/pm/program-charter.js +333 -0
- package/workspace/public/pm/program-model.js +44 -4
- package/workspace/public/pm/wf-edit.js +119 -0
- package/workspace/public/pm/wf-model.js +48 -0
- package/workspace/public/pm/wf-page.css +162 -0
- package/workspace/public/pm/wf-page.js +398 -0
- package/workspace/public/pm/wf-stage.js +187 -0
- package/workspace/public/premium-workflows.html +31 -0
- package/workspace/public/premium.html +1 -0
- package/workspace/server.js +1 -1
- package/workspace/skills/workflow-setup.skill +57 -0
- package/workspace/test-lists/briefing-premium.json +3 -1
- package/workspace/test-lists/ini-workflow.json +6 -0
- package/workspace/test-lists/program-charter.json +9 -0
- package/workspace/test-lists/workflows-board.json +13 -0
- package/workspace/test-lists/workflows-setup.json +13 -0
- package/workspace/test-lists/workflows.json +5 -0
- package/workspace/workflows-setup-test.js +346 -0
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/* pm/wf-page.js — Settings › Workflows (initiative workflows per initiative, D99; canvas WfAModel, WfAStage, WfACond, WfARemove, WfANew,
|
|
2
|
+
* WfADraft). One page, reached from Settings:
|
|
3
|
+
* the list every workflow with its count of initiatives and +; Product feature first, archived ones last and quiet
|
|
4
|
+
* the picked workflow one numbered list across the To do, In progress and Done columns; drag a stage to reorder it or to move it to
|
|
5
|
+
* another column; Add a stage per column; the dashed Won't do · Archived column (in every workflow); ⋯ = Copy, Archive.
|
|
6
|
+
* The name is the rename press (RnA*: tap it, one field with a check).
|
|
7
|
+
* a stage opens in the Viewer (the docked, resizable side panel): pm/wf-stage.js
|
|
8
|
+
* + New workflow: the normal chat, scoped "New workflow". The PM describes the work; PMOS drafts it (skill workflow-setup,
|
|
9
|
+
* `pmos workflow draft`) and the draft shows in the Viewer with Create workflow and Edit by hand. "Start blank" and
|
|
10
|
+
* "from a copy" sit under the box.
|
|
11
|
+
* Reads GET /api/workflows and GET /api/workflows/draft. Every write is the PM's press: POST /api/workflows/save | copy | archive | draft.
|
|
12
|
+
* Product feature ships with PMOS and is read-only here: Copy makes an editable one. */
|
|
13
|
+
import { pmGet, pmPost, pmHeaders } from './http.js';
|
|
14
|
+
import { esc, svg, ICON, wsParam, wsQ } from './cozy-evui.js';
|
|
15
|
+
import { mountNav } from './cozy-nav.js';
|
|
16
|
+
import * as VW from './cozy-viewer.js';
|
|
17
|
+
import { CATS, wfIcon, PRODUCT_FEATURE_ID } from './wf-model.js';
|
|
18
|
+
import * as E from './wf-edit.js';
|
|
19
|
+
import { openStage, setStageHost } from './wf-stage.js';
|
|
20
|
+
|
|
21
|
+
const $ = (s) => document.querySelector(s);
|
|
22
|
+
const post = (path, body) => pmPost(path, wsParam() ? { ws: wsParam(), ...body } : body); // the workspace this page shows (?ws=), else the server's own
|
|
23
|
+
const PHONE = 760;
|
|
24
|
+
const isPhone = () => innerWidth <= PHONE;
|
|
25
|
+
const CHAT_KEY = 'pmos.wf.chat';
|
|
26
|
+
const store = { get(k) { try { return localStorage.getItem(k); } catch { return null; } }, set(k, v) { try { if (v) localStorage.setItem(k, v); else localStorage.removeItem(k); } catch { /* a private window */ } } };
|
|
27
|
+
const DRAG = 'M9 6h.01M15 6h.01M9 12h.01M15 12h.01M9 18h.01M15 18h.01', PLUS = 'M12 5v14M5 12h14', BRANCH = ICON.branch, OK = 'M5 12l5 5 9-10';
|
|
28
|
+
|
|
29
|
+
/* S.list: [wf + { count }] | null while reading; S.sel: the picked id, 'new' for New workflow; S.edit: a draft opened by hand (not saved yet) */
|
|
30
|
+
const S = { list: null, sel: null, draft: null, edit: null, err: '', msg: '', renaming: false, renErr: '', busy: false, pop: null, chat: null, poll: 0, phoneOpen: false };
|
|
31
|
+
const wfOf = (id) => (S.edit && id === S.edit.id ? S.edit : (S.list || []).find((w) => w.id === id) || null);
|
|
32
|
+
const cur = () => (S.sel && S.sel !== 'new' ? wfOf(S.sel) : null);
|
|
33
|
+
const readOnly = (wf) => !!wf && (wf.builtin || wf.id === PRODUCT_FEATURE_ID);
|
|
34
|
+
|
|
35
|
+
function toast(text, undo) {
|
|
36
|
+
const t = $('#wfToast'); if (!t) return;
|
|
37
|
+
t.innerHTML = esc(text) + (undo ? ' <button type="button" class="wf-undo">Undo</button>' : '');
|
|
38
|
+
t.hidden = false; t.classList.add('on');
|
|
39
|
+
const b = t.querySelector('.wf-undo'); if (b) b.onclick = () => { t.hidden = true; undo(); };
|
|
40
|
+
clearTimeout(toast.t); toast.t = setTimeout(() => { t.classList.remove('on'); t.hidden = true; }, 6000);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* ── reading ── */
|
|
44
|
+
async function load() {
|
|
45
|
+
const r = await pmGet('/api/workflows' + wsQ());
|
|
46
|
+
if (r && r.ok && Array.isArray(r.workflows)) { S.list = r.workflows; S.err = ''; }
|
|
47
|
+
else { S.list = S.list || []; S.err = (r && r.error) || 'The workflows could not be read.'; }
|
|
48
|
+
const want = new URLSearchParams(location.search).get('wf');
|
|
49
|
+
if (!S.sel || (S.sel !== 'new' && !wfOf(S.sel))) S.sel = want && wfOf(want) ? want : (S.list[0] && S.list[0].id) || null;
|
|
50
|
+
draw();
|
|
51
|
+
}
|
|
52
|
+
async function readDraft() {
|
|
53
|
+
const r = await pmGet('/api/workflows/draft' + wsQ());
|
|
54
|
+
const d = r && r.ok && r.draft ? r.draft.draft || null : null; // { draft, note, at } | null
|
|
55
|
+
const was = JSON.stringify(S.draft), now = JSON.stringify(d);
|
|
56
|
+
S.draft = d;
|
|
57
|
+
if (was !== now && S.sel === 'new') { drawList(); if (d) showDraft(); else if (VW.current() && VW.current().id === 'wf-draft') VW.close(); }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* ── the list (WfAModel's left column) ── */
|
|
61
|
+
function rowHtml(w) {
|
|
62
|
+
const n = w.count > 0 ? `<span class="n">${w.count}</span>` : ''; // zero is silence
|
|
63
|
+
return `<button type="button" class="wfl${S.sel === w.id ? ' sel' : ''}${w.archived ? ' arch' : ''}" data-wf="${esc(w.id)}"${S.sel === w.id ? ' aria-current="true"' : ''}>${wfIcon(w, 22)}<span class="t">${esc(w.name)}</span>${n}</button>`;
|
|
64
|
+
}
|
|
65
|
+
function drawList() {
|
|
66
|
+
const el = $('#wfList'); if (!el) return;
|
|
67
|
+
const L = S.list;
|
|
68
|
+
const on = (L || []).filter((w) => !w.archived), off = (L || []).filter((w) => w.archived);
|
|
69
|
+
const newRow = S.sel === 'new' ? `<button type="button" class="wfl sel" data-wf="new" aria-current="true"><span class="wfi new" aria-hidden="true">${svg(PLUS, 14)}</span><span class="t">${esc((S.draft && S.draft.name) || 'New workflow')}</span><span class="n">draft</span></button>` : '';
|
|
70
|
+
el.innerHTML = `<div class="lhead"><h2>Workflows</h2><button type="button" class="ib sm" data-wf-new aria-label="New workflow: describe it and PMOS drafts it" title="New workflow: describe it and PMOS drafts it">${svg(PLUS, 16)}</button></div>`
|
|
71
|
+
+ (L === null ? '<p class="wf-mut wf-small wf-pad">Reading the workflows.</p>' : on.map(rowHtml).join('') + newRow
|
|
72
|
+
+ (off.length ? `<p class="wf-sub">Archived</p>${off.map(rowHtml).join('')}` : ''));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* ── the picked workflow (WfAModel) ── */
|
|
76
|
+
function cardHtml(wf, s, ro) {
|
|
77
|
+
const n = E.numOf(wf, s.key), mode = (s.when && s.when.mode) || 'always';
|
|
78
|
+
const out = s.output && s.output.name ? ` <span class="wf-mut">· ${esc(s.output.name)}</span>` : '';
|
|
79
|
+
const pill = mode === 'optional' ? '<span class="wf-pill dash">Optional</span>' : mode === 'only' ? `<span class="wf-pill y">${svg(BRANCH, 14)}<span>Only when ${esc(s.when.text || '…')}</span></span>` : '';
|
|
80
|
+
const drag = !ro && E.canMove(wf, s.key);
|
|
81
|
+
return `<button type="button" class="wf-card${mode === 'optional' ? ' opt' : ''}" data-stage="${esc(s.key)}"${drag ? ' draggable="true"' : ''} aria-label="${esc('Stage ' + n + ', ' + s.name + (ro ? '' : '. Open to edit'))}">`
|
|
82
|
+
+ `<span class="wf-grip" aria-hidden="true">${drag ? svg(DRAG, 14) : ''}</span><span class="wf-num">${n}</span>`
|
|
83
|
+
+ `<span class="wf-ct"><b>${s.milestone ? '◆ ' : ''}${esc(s.name)}</b>${out}</span>${pill}</button>`;
|
|
84
|
+
}
|
|
85
|
+
function colsHtml(wf, ro) {
|
|
86
|
+
const flex = { todo: 1, doing: 2.6, done: 1 };
|
|
87
|
+
return '<div class="wf-cols">' + CATS.map(([k, l]) => {
|
|
88
|
+
const st = wf.stages.filter((s) => s.cat === k);
|
|
89
|
+
return `<section class="wf-col" data-cat="${k}" style="flex:${flex[k]}" aria-label="${esc(l)}"><h3>${esc(l)}</h3><div class="wf-drop" data-cat="${k}">${st.map((s) => cardHtml(wf, s, ro)).join('')}</div>`
|
|
90
|
+
+ (ro ? '' : `<button type="button" class="wf-btn sm ghost" data-add="${k}">${svg(PLUS, 16)}Add a stage</button>`) + '</section>';
|
|
91
|
+
}).join('') + '<section class="wf-col wf-end" aria-label="Won\'t do and Archived"><h3>Won\'t do · Archived</h3><p class="wf-small wf-mut">In every workflow. Any stage can go here, and come back.</p></section></div>';
|
|
92
|
+
}
|
|
93
|
+
function titleHtml(wf, ro) {
|
|
94
|
+
if (S.renaming) return `<form class="wf-ren" data-wf-ren><input id="wfName" type="text" value="${esc(wf.name)}" maxlength="80" aria-label="Name" autocomplete="off"${S.busy ? ' disabled' : ''}><button type="submit" class="wf-sv" aria-label="Save the name" title="Save">${svg(OK, 16)}</button>${S.renErr ? `<p class="wf-err" role="alert">${esc(S.renErr)}</p>` : ''}</form>`;
|
|
95
|
+
return `<h1 class="wf-h1">${ro ? esc(wf.name) : `<button type="button" class="wf-rn" data-wf-rn aria-label="${esc('Rename ' + wf.name)}" title="Rename">${esc(wf.name)}</button>`}</h1>`;
|
|
96
|
+
}
|
|
97
|
+
function editorHtml(wf) {
|
|
98
|
+
const ro = readOnly(wf), n = wf.stages.length, c = Number(wf.count) || 0;
|
|
99
|
+
const meta = n + ' stage' + (n === 1 ? '' : 's') + (c > 0 ? ' · used by ' + c + ' initiative' + (c === 1 ? '' : 's') : '');
|
|
100
|
+
const isDraft = S.edit && S.edit.id === wf.id;
|
|
101
|
+
const head = `<div class="wf-head">${isPhone() ? `<button type="button" class="ib sm" data-wf-back aria-label="All workflows" title="All workflows">${svg(ICON.back, 18)}</button>` : ''}${wfIcon(wf, 28)}${titleHtml(wf, ro)}<span class="wf-small wf-mut">${esc(isDraft ? meta + ' · draft' : meta)}</span>`
|
|
102
|
+
+ (isDraft ? '<button type="button" class="wf-btn dark sm wf-push" data-wf-create>Create workflow</button>'
|
|
103
|
+
: `<button type="button" class="ib sm wf-push" data-wf-more aria-haspopup="menu" aria-label="${esc('More for ' + wf.name + ': copy, ' + (wf.archived ? 'restore' : 'archive'))}" title="More">${svg(ICON.more, 16)}</button>`) + '</div>';
|
|
104
|
+
const note = ro ? `<p class="wf-box wf-small">${esc(wf.name)} ships with PMOS, so it cannot change here. <button type="button" class="wf-link" data-wf-copy>Copy it</button> to make a workflow of your own.</p>` : '';
|
|
105
|
+
const arch = wf.archived ? '<p class="wf-box wf-small">Archived. New initiatives cannot use it. <button type="button" class="wf-link" data-wf-unarch>Restore it</button></p>' : '';
|
|
106
|
+
return head + note + arch + colsHtml(wf, ro)
|
|
107
|
+
+ '<div class="wf-box wf-small wf-how"><b>One order, top to bottom</b><span>Stages run in the numbered order; drag to change it. A stage can be <b>Optional</b> (you may skip it) or run <b>Only when</b> a condition holds; otherwise PMOS skips it and the track shows it dashed. Each stage sits in To do, In progress or Done, as a status does in Jira.</span></div>'
|
|
108
|
+
+ `<p class="wf-small" role="status">${esc(S.msg)}</p>`;
|
|
109
|
+
}
|
|
110
|
+
function drawMain() {
|
|
111
|
+
const m = $('#wfMain'), c = $('#wfChat'); if (!m) return;
|
|
112
|
+
const isNew = S.sel === 'new';
|
|
113
|
+
m.hidden = isNew; c.hidden = !isNew;
|
|
114
|
+
// the draft sits beside the chat (WfADraft); a stage's panel lies over the columns and the list stays (WfAStage)
|
|
115
|
+
const host = $('#wfVwHost'); if (host) host.toggleAttribute('data-cz-viewer-host', isNew);
|
|
116
|
+
document.body.classList.toggle('wf-open', !!S.sel && (S.phoneOpen || !isPhone()));
|
|
117
|
+
if (isNew) return;
|
|
118
|
+
const wf = cur();
|
|
119
|
+
if (S.list === null) { m.innerHTML = '<p class="wf-mut">Reading the workflows.</p>'; return; }
|
|
120
|
+
if (!wf) { m.innerHTML = `<div class="wf-box">${S.err ? `<p class="wf-err">${esc(S.err)}</p>` : ''}<p>No workflow yet. Press + to describe one, and PMOS drafts it.</p><button type="button" class="wf-btn dark sm" data-wf-new>New workflow</button></div>`; return; }
|
|
121
|
+
const ae = document.activeElement, keep = ae && m.contains(ae) && ae.dataset ? Object.entries(ae.dataset)[0] : null;
|
|
122
|
+
m.innerHTML = editorHtml(wf);
|
|
123
|
+
if (S.renaming) { const i = m.querySelector('#wfName'); if (i) { i.focus(); i.select(); } }
|
|
124
|
+
else if (keep) { const f = m.querySelector(`[data-${keep[0].replace(/[A-Z]/g, (x) => '-' + x.toLowerCase())}="${CSS.escape(keep[1])}"]`); if (f) f.focus(); }
|
|
125
|
+
}
|
|
126
|
+
function drawPop() {
|
|
127
|
+
const p = $('#wfPop'); if (!p) return;
|
|
128
|
+
p.hidden = !S.pop; $('#wfScrim').hidden = !S.pop; p.toggleAttribute('data-esc-layer', !!S.pop); // Escape closes the pop first, then the Viewer
|
|
129
|
+
if (!S.pop) { p.innerHTML = ''; return; }
|
|
130
|
+
const { wf, key, inUse, to } = S.pop, s = wf.stages.find((x) => x.key === key) || { name: key };
|
|
131
|
+
const left = wf.stages.filter((x) => x.key !== key);
|
|
132
|
+
const pick = `<label class="wf-vh" for="wfMoveTo">Move to</label><select id="wfMoveTo" class="wf-pick" data-wf-moveto>${left.map((x) => `<option value="${esc(x.key)}"${x.key === to ? ' selected' : ''}>${esc(E.label(wf, x.key))}</option>`).join('')}</select>`;
|
|
133
|
+
const n = inUse.length;
|
|
134
|
+
const rows = n === 1 ? `<div class="wf-box wf-mv">${wfIcon(wf, 20)}<b class="wf-small">${esc(inUse[0].title || inUse[0].id)}</b><span class="wf-small wf-mut">to</span>${pick}</div>`
|
|
135
|
+
: `<div class="wf-box wf-mvl">${inUse.map((r) => `<span class="wf-mv">${wfIcon(wf, 18)}<b class="wf-small">${esc(r.title || r.id)}</b></span>`).join('')}<span class="wf-mv"><span class="wf-small wf-mut">Move them to</span>${pick}</span></div>`;
|
|
136
|
+
p.setAttribute('aria-label', 'Remove the ' + s.name + ' stage');
|
|
137
|
+
p.innerHTML = `<p class="wf-h3">Remove ${esc(s.name)} from ${esc(wf.name)}?</p><p class="wf-small">${n === 1 ? '1 initiative is in it now. Move it first.' : n + ' initiatives are in it now. Move them first.'}</p>${rows}`
|
|
138
|
+
+ `<p class="wf-small wf-mut">Initiatives that already passed ${esc(s.name)} keep it in their history.</p>`
|
|
139
|
+
+ `<p class="wf-err" role="alert">${esc(S.pop.err || '')}</p><div class="wf-acts"><button type="button" class="wf-btn dark" data-wf-moverm>Move and remove</button><button type="button" class="wf-btn ghost" data-wf-popx>Cancel</button></div>`;
|
|
140
|
+
const f = p.querySelector('select'); if (f) f.focus();
|
|
141
|
+
}
|
|
142
|
+
function draw() { drawList(); drawMain(); drawPop(); }
|
|
143
|
+
|
|
144
|
+
/* ── writes ── */
|
|
145
|
+
/** Save a workflow (a draft opened by hand saves to the draft). Resolves { ok, workflow } or { ok: false, error, inUse? }. */
|
|
146
|
+
async function persist(wf, { moveRemoved } = {}) {
|
|
147
|
+
if (S.edit && wf.id === S.edit.id) {
|
|
148
|
+
const r = await post('/api/workflows/draft', { draft: wf });
|
|
149
|
+
if (r && r.ok) { S.edit = wf; S.draft = wf; draw(); return { ok: true, workflow: wf }; }
|
|
150
|
+
return { ok: false, error: (r && r.error) || 'The draft was not saved.' };
|
|
151
|
+
}
|
|
152
|
+
const r = await post('/api/workflows/save', moveRemoved ? { wf, moveRemoved } : { wf });
|
|
153
|
+
if (r && r.ok && r.workflow) {
|
|
154
|
+
const i = S.list.findIndex((w) => w.id === r.workflow.id), count = i >= 0 ? S.list[i].count : 0;
|
|
155
|
+
const w = { ...r.workflow, count: r.workflow.count != null ? r.workflow.count : count };
|
|
156
|
+
if (i >= 0) S.list[i] = w; else S.list.push(w);
|
|
157
|
+
draw();
|
|
158
|
+
return { ok: true, workflow: w };
|
|
159
|
+
}
|
|
160
|
+
return { ok: false, error: (r && r.error) || 'Not saved.', inUse: r && r.inUse, status: r && r.status };
|
|
161
|
+
}
|
|
162
|
+
async function edit(next, say) {
|
|
163
|
+
const r = await persist(next);
|
|
164
|
+
if (!r.ok) { S.msg = r.error; drawMain(); }
|
|
165
|
+
else if (say) { S.msg = ''; drawMain(); }
|
|
166
|
+
return r;
|
|
167
|
+
}
|
|
168
|
+
async function removeStage(wf, key) {
|
|
169
|
+
const next = E.removeStage(wf, key);
|
|
170
|
+
const r = await persist(next);
|
|
171
|
+
if (r.ok) { VW.close(); toast('Removed ' + ((wf.stages.find((s) => s.key === key) || {}).name || 'the stage') + '.'); return r; }
|
|
172
|
+
if (r.inUse && r.inUse.length) {
|
|
173
|
+
const nx = E.nextOf(wf, key) || wf.stages.find((s) => s.key !== key);
|
|
174
|
+
S.pop = { wf, key, next, inUse: r.inUse, to: nx ? nx.key : 'triage' }; drawPop(); return r;
|
|
175
|
+
}
|
|
176
|
+
S.msg = r.error; drawMain(); return r;
|
|
177
|
+
}
|
|
178
|
+
async function moveAndRemove() {
|
|
179
|
+
const P = S.pop; if (!P) return;
|
|
180
|
+
const sel = $('#wfMoveTo'); if (sel) P.to = sel.value;
|
|
181
|
+
const r = await persist(P.next, { moveRemoved: { [P.key]: P.to } });
|
|
182
|
+
if (!r.ok) { P.err = r.error; drawPop(); return; }
|
|
183
|
+
const name = (P.wf.stages.find((s) => s.key === P.key) || {}).name;
|
|
184
|
+
S.pop = null; drawPop(); VW.close();
|
|
185
|
+
const w = S.list.find((x) => x.id === P.wf.id); if (w && Number.isFinite(w.count)) { /* counts stay: the initiatives moved, not left */ }
|
|
186
|
+
toast('Moved ' + P.inUse.length + ' and removed ' + name + '.');
|
|
187
|
+
}
|
|
188
|
+
async function copy(wf) {
|
|
189
|
+
const r = await post('/api/workflows/copy', { id: wf.id });
|
|
190
|
+
if (!(r && r.ok && r.workflow)) { S.msg = (r && r.error) || 'Not copied.'; drawMain(); return; }
|
|
191
|
+
S.list.push({ ...r.workflow, count: 0 }); select(r.workflow.id);
|
|
192
|
+
toast('Copied as ' + r.workflow.name + '.');
|
|
193
|
+
}
|
|
194
|
+
async function archive(wf, on) {
|
|
195
|
+
const r = await post('/api/workflows/archive', { id: wf.id, archived: on });
|
|
196
|
+
if (!(r && r.ok)) {
|
|
197
|
+
const u = r && r.inUse && r.inUse.length;
|
|
198
|
+
S.msg = u ? wf.name + ' is in use by ' + u + ' initiative' + (u === 1 ? '' : 's') + ' (' + r.inUse.slice(0, 3).map((x) => x.title || x.id).join(', ') + (u > 3 ? ', …' : '') + '). Move them to another workflow first.' : (r && r.error) || 'Not changed.';
|
|
199
|
+
drawMain(); return;
|
|
200
|
+
}
|
|
201
|
+
await load();
|
|
202
|
+
toast(on ? 'Archived ' + wf.name + '.' : 'Restored ' + wf.name + '.', () => archive(wf, !on));
|
|
203
|
+
}
|
|
204
|
+
async function rename(wf, name) {
|
|
205
|
+
const v = String(name || '').trim();
|
|
206
|
+
if (!v || v === wf.name) { S.renaming = false; S.renErr = ''; drawMain(); return; }
|
|
207
|
+
S.busy = true; drawMain();
|
|
208
|
+
const from = wf.name, r = await persist({ ...stripCount(wf), name: v });
|
|
209
|
+
S.busy = false;
|
|
210
|
+
if (!r.ok) { S.renErr = r.error; drawMain(); return; }
|
|
211
|
+
S.renaming = false; S.renErr = ''; drawMain();
|
|
212
|
+
toast('Renamed', () => { const w = wfOf(wf.id); if (w) persist({ ...stripCount(w), name: from }); });
|
|
213
|
+
}
|
|
214
|
+
const stripCount = (w) => { const { count, ...rest } = w; return rest; };
|
|
215
|
+
|
|
216
|
+
/* ── the stage panel (the Viewer) ── */
|
|
217
|
+
function stageCtx() {
|
|
218
|
+
return {
|
|
219
|
+
wf: () => cur(), readOnly: () => readOnly(cur()), save: (next) => edit(next), remove: (wf, key) => removeStage(wf, key),
|
|
220
|
+
toast, label: (wf, key) => E.label(wf, key),
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function openKey(key, o = {}) { const wf = cur(); if (wf) openStage(wf, key, o); }
|
|
224
|
+
|
|
225
|
+
/* ── New workflow (WfANew, WfADraft) ── */
|
|
226
|
+
async function startNew() {
|
|
227
|
+
S.sel = 'new'; S.edit = null; S.phoneOpen = true; VW.close();
|
|
228
|
+
draw();
|
|
229
|
+
let key = store.get(CHAT_KEY);
|
|
230
|
+
if (!key) { key = 'pop:workflow:new-' + Date.now().toString(36); store.set(CHAT_KEY, key); }
|
|
231
|
+
const chat = await mountWfChat();
|
|
232
|
+
chat.open({
|
|
233
|
+
key, title: 'New workflow', scope: { text: 'New workflow', icon: 'flow' }, role: { name: 'Claude', initial: 'C' },
|
|
234
|
+
context: { title: 'New workflow', subject: 'Settings › Workflows', say: 'The PM is describing a new workflow. Follow the workflow-setup skill: draft it with pmos workflow draft, and explain each choice in the chat.' },
|
|
235
|
+
intro: { role: { name: 'Claude', initial: 'C' }, text: 'Tell me how this work goes, in your own words: who does what, in what order, and what has to be true before each step. Add a policy or an old checklist if you have one. I will draft the stages and show them beside the chat.' },
|
|
236
|
+
});
|
|
237
|
+
hint();
|
|
238
|
+
await readDraft();
|
|
239
|
+
if (S.draft) showDraft();
|
|
240
|
+
clearInterval(S.poll); S.poll = setInterval(() => { if (S.sel === 'new' && !document.hidden) readDraft(); }, 3000);
|
|
241
|
+
}
|
|
242
|
+
/** "You can also start blank or from a copy." under the box (WfANew). */
|
|
243
|
+
function hint() {
|
|
244
|
+
const c = $('#wfChat'); if (!c) return;
|
|
245
|
+
let h = c.querySelector('.wf-hint');
|
|
246
|
+
if (!h) { h = document.createElement('p'); h.className = 'wf-hint wf-small wf-mut'; c.appendChild(h); }
|
|
247
|
+
h.innerHTML = 'You can also start <button type="button" class="wf-link" data-wf-blank>blank</button> or <button type="button" class="wf-link" data-wf-fromcopy aria-haspopup="menu">from a copy</button>.';
|
|
248
|
+
}
|
|
249
|
+
let chatMod = null;
|
|
250
|
+
async function mountWfChat() {
|
|
251
|
+
if (S.chat) return S.chat;
|
|
252
|
+
chatMod = chatMod || await import('./home-chat.js');
|
|
253
|
+
const noop = () => {};
|
|
254
|
+
S.chat = chatMod.mountChat($('#wfChatIn'), {
|
|
255
|
+
esc, toast: (t) => toast(t), pmGet, pmPost, pressHeaders: () => { const h = pmHeaders(); delete h['Content-Type']; return h; },
|
|
256
|
+
roles: () => [], seen: () => null, markSeen: noop, session: () => null, queued: () => [], queue: noop, unqueue: noop,
|
|
257
|
+
onScope: noop, suggestLink: () => null, linkTo: noop, dismissLink: noop, onLink: noop, onTo: noop, workItem: noop, openTerminal: noop,
|
|
258
|
+
viewerClosed: () => !VW.isOpen() && !isPhone(), desk: () => !isPhone(), reopenViewer: () => { if (S.draft) showDraft(); }, head: () => '',
|
|
259
|
+
});
|
|
260
|
+
return S.chat;
|
|
261
|
+
}
|
|
262
|
+
function draftHtml(d) {
|
|
263
|
+
const st = d.stages || [];
|
|
264
|
+
const body = CATS.map(([k, l]) => {
|
|
265
|
+
const rows = st.filter((s) => s.cat === k);
|
|
266
|
+
if (!rows.length) return '';
|
|
267
|
+
return `<p class="wf-lbl">${esc(l)}</p>` + rows.map((s) => {
|
|
268
|
+
const n = st.indexOf(s) + 1, mode = (s.when && s.when.mode) || 'always';
|
|
269
|
+
const out = [s.output && s.output.name, s.output && s.output.kind === 'jira' ? 'Jira' : ''].filter(Boolean).join(' · ');
|
|
270
|
+
const pill = mode === 'only' ? `<br><span class="wf-pill y">Only when ${esc(s.when.text || '…')}</span>` : mode === 'optional' ? '<br><span class="wf-pill dash">Optional</span>' : '';
|
|
271
|
+
return `<div class="wf-trow"><span class="wf-num">${n}</span><span><b>${s.milestone ? '◆ ' : ''}${esc(s.name)}</b>${out ? ` <span class="wf-small wf-mut">· ${esc(out)}</span>` : ''}${pill}</span></div>`;
|
|
272
|
+
}).join('');
|
|
273
|
+
}).join('');
|
|
274
|
+
return `<div class="wf-draft"><div class="wf-dh"><h2 class="wf-h2">${esc(d.name || 'New workflow')}</h2><span class="wf-small wf-mut">${st.length} stage${st.length === 1 ? '' : 's'}</span></div>${body}`
|
|
275
|
+
+ `<p class="wf-err" role="alert" id="wfDraftErr"></p><div class="wf-acts wf-dacts"><button type="button" class="wf-btn dark" data-wf-dcreate>Create workflow</button><button type="button" class="wf-btn ghost" data-wf-dedit>Edit by hand</button></div></div>`;
|
|
276
|
+
}
|
|
277
|
+
function showDraft() {
|
|
278
|
+
const d = S.draft; if (!d) return;
|
|
279
|
+
VW.open({ id: 'wf-draft', kind: 'Workflow draft', title: d.name || 'New workflow', icon: 'M7 3h7l5 5v13H7zM14 3v5h5', keep: VW.current() && VW.current().id === 'wf-draft',
|
|
280
|
+
render: (b) => { b.innerHTML = draftHtml(d); b.onclick = (e) => { if (e.target.closest('[data-wf-dcreate]')) createDraft(d); else if (e.target.closest('[data-wf-dedit]')) editDraft(d); }; } });
|
|
281
|
+
}
|
|
282
|
+
function withId(d) { return { ...d, id: d.id || E.slug(d.name || 'workflow') + '-' + Date.now().toString(36), stages: (d.stages || []).map((s) => ({ then: null, unless: [], finish: [], when: { mode: 'always' }, ...s })) }; }
|
|
283
|
+
async function createDraft(d) {
|
|
284
|
+
const wf = withId(d);
|
|
285
|
+
const r = await post('/api/workflows/save', { wf });
|
|
286
|
+
if (!(r && r.ok && r.workflow)) { const e = document.getElementById('wfDraftErr'); if (e) e.textContent = (r && r.error) || 'Not created.'; return; }
|
|
287
|
+
store.set(CHAT_KEY, ''); S.draft = null; S.edit = null; clearInterval(S.poll);
|
|
288
|
+
VW.close();
|
|
289
|
+
await load(); select(r.workflow.id);
|
|
290
|
+
toast('Created ' + r.workflow.name + '.');
|
|
291
|
+
}
|
|
292
|
+
function editDraft(d) {
|
|
293
|
+
S.edit = withId(d); S.sel = S.edit.id; clearInterval(S.poll); VW.close(); draw();
|
|
294
|
+
}
|
|
295
|
+
async function createEdited() {
|
|
296
|
+
const wf = S.edit; if (!wf) return;
|
|
297
|
+
const r = await post('/api/workflows/save', { wf });
|
|
298
|
+
if (!(r && r.ok && r.workflow)) { S.msg = (r && r.error) || 'Not created.'; drawMain(); return; }
|
|
299
|
+
store.set(CHAT_KEY, ''); S.draft = null; S.edit = null;
|
|
300
|
+
await load(); select(r.workflow.id);
|
|
301
|
+
toast('Created ' + r.workflow.name + '.');
|
|
302
|
+
}
|
|
303
|
+
async function startBlank() {
|
|
304
|
+
const wf = E.blankWorkflow();
|
|
305
|
+
const r = await post('/api/workflows/save', { wf });
|
|
306
|
+
if (!(r && r.ok && r.workflow)) { toast((r && r.error) || 'Not created.'); return; }
|
|
307
|
+
await load(); select(r.workflow.id);
|
|
308
|
+
S.renaming = true; drawMain();
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/* ── a small menu (⋯, from a copy) ── */
|
|
312
|
+
function menu(btn, items) {
|
|
313
|
+
closeMenu();
|
|
314
|
+
const m = document.createElement('div'); m.className = 'wf-menu'; m.setAttribute('role', 'menu'); m.id = 'wfMenu'; m.setAttribute('data-esc-layer', '');
|
|
315
|
+
m.innerHTML = items.map((it, i) => `<button type="button" role="menuitem" data-i="${i}"${it.danger ? ' class="danger"' : ''}>${esc(it.label)}</button>`).join('');
|
|
316
|
+
document.body.appendChild(m);
|
|
317
|
+
const r = btn.getBoundingClientRect();
|
|
318
|
+
m.style.top = Math.min(innerHeight - m.offsetHeight - 8, r.bottom + 6) + 'px';
|
|
319
|
+
m.style.left = Math.max(8, Math.min(innerWidth - m.offsetWidth - 8, r.right - m.offsetWidth)) + 'px';
|
|
320
|
+
m.onclick = (e) => { const b = e.target.closest('[data-i]'); if (!b) return; closeMenu(); items[Number(b.dataset.i)].go(); };
|
|
321
|
+
m.onkeydown = (e) => { if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); closeMenu(); btn.focus(); } };
|
|
322
|
+
const f = m.querySelector('button'); if (f) f.focus();
|
|
323
|
+
setTimeout(() => document.addEventListener('click', closeMenu, { once: true }), 0);
|
|
324
|
+
}
|
|
325
|
+
function closeMenu() { const m = document.getElementById('wfMenu'); if (m) m.remove(); }
|
|
326
|
+
|
|
327
|
+
function select(id) {
|
|
328
|
+
if (S.sel === 'new' && id !== 'new') clearInterval(S.poll);
|
|
329
|
+
S.sel = id; S.renaming = false; S.msg = ''; S.phoneOpen = true;
|
|
330
|
+
if (S.edit && id !== S.edit.id) S.edit = null;
|
|
331
|
+
VW.close();
|
|
332
|
+
const u = new URL(location.href); if (id && id !== 'new') u.searchParams.set('wf', id); else u.searchParams.delete('wf'); history.replaceState(null, '', u);
|
|
333
|
+
draw();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/* ── drag to reorder (WfAModel: "drag to change it") ── */
|
|
337
|
+
function bindDrag(main) {
|
|
338
|
+
let key = null;
|
|
339
|
+
main.addEventListener('dragstart', (e) => { const c = e.target.closest('.wf-card[draggable="true"]'); if (!c) return; key = c.dataset.stage; e.dataTransfer.effectAllowed = 'move'; try { e.dataTransfer.setData('text/plain', key); } catch { /* old browser */ } c.classList.add('drag'); });
|
|
340
|
+
main.addEventListener('dragend', () => { key = null; main.querySelectorAll('.drag, .over').forEach((x) => x.classList.remove('drag', 'over')); });
|
|
341
|
+
main.addEventListener('dragover', (e) => { const d = e.target.closest('.wf-drop'); if (!d || !key) return; e.preventDefault(); main.querySelectorAll('.wf-drop.over').forEach((x) => x !== d && x.classList.remove('over')); d.classList.add('over'); });
|
|
342
|
+
main.addEventListener('drop', (e) => {
|
|
343
|
+
const d = e.target.closest('.wf-drop'); if (!d || !key) return;
|
|
344
|
+
e.preventDefault();
|
|
345
|
+
const before = [...d.querySelectorAll('.wf-card')].find((c) => { const r = c.getBoundingClientRect(); return e.clientY < r.top + r.height / 2 && c.dataset.stage !== key; });
|
|
346
|
+
const wf = cur(); const k = key; key = null;
|
|
347
|
+
if (!wf) return;
|
|
348
|
+
const next = E.moveStage(stripCount(wf), k, d.dataset.cat, before ? before.dataset.stage : null);
|
|
349
|
+
if (JSON.stringify(next.stages) !== JSON.stringify(wf.stages)) edit(next);
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/* ── presses ── */
|
|
354
|
+
function bind() {
|
|
355
|
+
$('#wfList').addEventListener('click', (e) => {
|
|
356
|
+
if (e.target.closest('[data-wf-new]')) { startNew(); return; }
|
|
357
|
+
const b = e.target.closest('[data-wf]'); if (!b) return;
|
|
358
|
+
if (b.dataset.wf === 'new') return;
|
|
359
|
+
select(b.dataset.wf);
|
|
360
|
+
});
|
|
361
|
+
const main = $('#wfMain');
|
|
362
|
+
bindDrag(main);
|
|
363
|
+
main.addEventListener('click', (e) => {
|
|
364
|
+
const t = e.target.closest('button'); if (!t) return;
|
|
365
|
+
const wf = cur();
|
|
366
|
+
if ('wfNew' in t.dataset) { startNew(); return; }
|
|
367
|
+
if ('wfBack' in t.dataset) { S.phoneOpen = false; VW.close(); drawMain(); return; }
|
|
368
|
+
if (!wf) return;
|
|
369
|
+
if ('wfRn' in t.dataset) { S.renaming = true; S.renErr = ''; drawMain(); return; }
|
|
370
|
+
if ('wfCreate' in t.dataset) { createEdited(); return; }
|
|
371
|
+
if ('wfCopy' in t.dataset) { copy(wf); return; }
|
|
372
|
+
if ('wfUnarch' in t.dataset) { archive(wf, false); return; }
|
|
373
|
+
if ('wfMore' in t.dataset) { menu(t, [{ label: 'Copy', go: () => copy(wf) }, wf.archived ? { label: 'Restore', go: () => archive(wf, false) } : { label: 'Archive', go: () => archive(wf, true), danger: true }].filter((x) => !(x.label === 'Archive' && readOnly(wf)))); return; }
|
|
374
|
+
if (t.dataset.add) { const { wf: next, key } = E.addStage(stripCount(wf), t.dataset.add); edit(next).then((r) => { if (r.ok) openKey(key, { rename: true }); }); return; }
|
|
375
|
+
if (t.dataset.stage) { openKey(t.dataset.stage); }
|
|
376
|
+
});
|
|
377
|
+
main.addEventListener('submit', (e) => { if (e.target.closest('[data-wf-ren]')) { e.preventDefault(); const i = $('#wfName'); const wf = cur(); if (wf && i) rename(wf, i.value); } });
|
|
378
|
+
main.addEventListener('keydown', (e) => { if (e.key === 'Escape' && e.target.closest('[data-wf-ren]')) { e.preventDefault(); e.stopPropagation(); S.renaming = false; S.renErr = ''; drawMain(); const b = main.querySelector('[data-wf-rn]'); if (b) b.focus(); } });
|
|
379
|
+
$('#wfChat').addEventListener('click', (e) => {
|
|
380
|
+
if (e.target.closest('[data-wf-blank]')) { startBlank(); return; }
|
|
381
|
+
const c = e.target.closest('[data-wf-fromcopy]');
|
|
382
|
+
if (c) menu(c, (S.list || []).filter((w) => !w.archived).map((w) => ({ label: w.name, go: () => copy(w) })));
|
|
383
|
+
});
|
|
384
|
+
$('#wfPop').addEventListener('click', (e) => { if (e.target.closest('[data-wf-moverm]')) moveAndRemove(); else if (e.target.closest('[data-wf-popx]')) { S.pop = null; drawPop(); } });
|
|
385
|
+
$('#wfPop').addEventListener('keydown', (e) => { if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); S.pop = null; drawPop(); } });
|
|
386
|
+
$('#wfScrim').addEventListener('click', () => { S.pop = null; drawPop(); });
|
|
387
|
+
addEventListener('resize', () => drawMain());
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export function boot() {
|
|
391
|
+
mountNav('settings');
|
|
392
|
+
setStageHost(stageCtx());
|
|
393
|
+
bind();
|
|
394
|
+
draw();
|
|
395
|
+
load();
|
|
396
|
+
if (new URLSearchParams(location.search).get('new') === '1') startNew();
|
|
397
|
+
}
|
|
398
|
+
if (typeof document !== 'undefined' && document.getElementById('wfMain')) boot();
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/* pm/wf-stage.js — one stage of a workflow, in the Viewer (initiative workflows per initiative, D99; canvas WfAStage, WfACond).
|
|
2
|
+
* The Viewer is the docked, resizable side panel (cozy-viewer.js): its title is the stage's name and the rename press (RnA*), its ⋯ holds
|
|
3
|
+
* Move up, Move down and Remove this stage. The body is two columns:
|
|
4
|
+
* left Category (To do, In progress, Done) · Output: what finishes it, and its kind (Document, Jira tickets, Sign-off, Checklist only)
|
|
5
|
+
* · Template (a Document's) · To finish (the list, add and remove items)
|
|
6
|
+
* right When it runs (Always, Optional, Only when…) · Then: the next stage in the list or a picked one, the fallback when the next one
|
|
7
|
+
* runs only sometimes, and "Unless <words> go to <stage>" · Milestone ◆ · Moves on by itself
|
|
8
|
+
* "Only when…" opens the condition (WfACond) as a drill-in: plain words, no rule builder; How it reads today (the initiatives on this
|
|
9
|
+
* workflow, with PMOS's reading and its reason); Who decides (per workflow: PMOS and tells why, or PMOS suggests and you confirm).
|
|
10
|
+
* Every change saves at once through the page's save (POST /api/workflows/save). A workflow that ships with PMOS shows read-only.
|
|
11
|
+
* setStageHost(ctx) · openStage(wf, key, { rename? }) */
|
|
12
|
+
import { pmGet } from './http.js';
|
|
13
|
+
import { esc, svg, wsQ } from './cozy-evui.js';
|
|
14
|
+
import * as VW from './cozy-viewer.js';
|
|
15
|
+
import { CATS, OUTPUT_KINDS, WHEN, wfIcon } from './wf-model.js';
|
|
16
|
+
import * as E from './wf-edit.js';
|
|
17
|
+
|
|
18
|
+
let H = null;
|
|
19
|
+
const PENDING = new Map(); // an Unless row with no words yet: the model refuses it, so it waits here until it has them
|
|
20
|
+
export function setStageHost(ctx) { H = ctx; }
|
|
21
|
+
const X = 'M6 6l12 12M18 6L6 18', PLUS = 'M12 5v14M5 12h14', DRAG = 'M9 6h.01M15 6h.01M9 12h.01M15 12h.01M9 18h.01M15 18h.01', STAGE_ICON = 'M4 6h16M4 12h10M4 18h6';
|
|
22
|
+
const KIND_HELP = 'The kind says how progress counts: sections written, tickets done, people signed, or items ticked. The Board shows it in the Output column.';
|
|
23
|
+
|
|
24
|
+
const seg = (label, key, cur, opts, dis) => `<div class="wf-seg" role="radiogroup" aria-label="${esc(label)}">${opts.map(([k, w]) => `<button type="button" class="sb" role="radio" data-st-${key}="${esc(k)}" aria-checked="${cur === k}" aria-pressed="${cur === k}"${dis ? ' disabled' : ''}>${esc(w)}</button>`).join('')}</div>`;
|
|
25
|
+
const sw = (on, key, title, sub, dis) => `<div class="wf-swl"><button type="button" class="wf-sw" role="switch" aria-checked="${!!on}" data-st-sw="${key}" aria-label="${esc(title)}"${dis ? ' disabled' : ''}><i></i></button><span><b>${esc(title)}</b> <span class="wf-mut">· ${esc(sub)}</span></span></div>`;
|
|
26
|
+
const pick = (id, label, cur, opts, attr, dis) => `<label class="wf-vh" for="${id}">${esc(label)}</label><select id="${id}" class="wf-pick" ${attr}${dis ? ' disabled' : ''}>${opts.map(([v, w]) => `<option value="${esc(v)}"${v === cur ? ' selected' : ''}>${esc(w)}</option>`).join('')}</select>`;
|
|
27
|
+
|
|
28
|
+
function bodyHtml(wf, s, ro) {
|
|
29
|
+
const id = (k) => 'wfSt' + k;
|
|
30
|
+
const n = E.numOf(wf, s.key), tri = E.isTriage(s), out = s.output || {}, mode = (s.when && s.when.mode) || 'always';
|
|
31
|
+
const others = wf.stages.filter((x) => x.key !== s.key);
|
|
32
|
+
const nextInList = wf.stages[n] || null;
|
|
33
|
+
const then = [['', 'Next in the list' + (nextInList ? ' · ' + E.label(wf, nextInList.key) : '')]].concat(others.map((x) => [x.key, E.label(wf, x.key)]));
|
|
34
|
+
const note = E.thenNote(wf, s.key);
|
|
35
|
+
const left = `<div class="wf-field"><span class="wf-fl" id="${id('Cat')}">Category</span>${seg('Category', 'cat', s.cat, CATS, ro || tri)}${tri ? '<p class="wf-small wf-mut">Triage is always first, in To do.</p>' : ''}</div>`
|
|
36
|
+
+ `<div class="wf-field"><label class="wf-fl" for="${id('Out')}">Output · what finishes it</label><input id="${id('Out')}" class="wf-inp" data-st-out value="${esc(out.name || '')}" maxlength="120" placeholder="For example: Scored shortlist"${ro ? ' disabled' : ''}>`
|
|
37
|
+
+ `<div class="wf-chips" role="radiogroup" aria-label="Output kind">${OUTPUT_KINDS.map(([k, w]) => `<button type="button" class="wf-chip${out.kind === k ? ' on' : ''}" role="radio" aria-checked="${out.kind === k}" data-st-kind="${k}"${ro ? ' disabled' : ''}>${esc(w)}</button>`).join('')}</div>`
|
|
38
|
+
+ `<p class="wf-small wf-mut">${KIND_HELP}</p></div>`
|
|
39
|
+
+ (out.kind === 'doc' ? `<div class="wf-field"><label class="wf-fl" for="${id('Tpl')}">Template</label><input id="${id('Tpl')}" class="wf-inp" data-st-tpl value="${esc(out.template || '')}" maxlength="160" placeholder="The template PMOS drafts it from"${ro ? ' disabled' : ''}></div>` : '')
|
|
40
|
+
+ `<div class="wf-field"><span class="wf-fl">To finish</span><div class="wf-box wf-fin">${(s.finish || []).map((f, i) => `<div class="wf-fi"><span class="wf-mut" aria-hidden="true">${svg(DRAG, 14)}</span><label class="wf-vh" for="${id('F' + i)}">Item ${i + 1}</label><input id="${id('F' + i)}" class="wf-inp bare" data-st-fin="${i}" value="${esc(f)}" maxlength="160"${ro ? ' disabled' : ''}>${ro ? '' : `<button type="button" class="ib sm" data-st-finx="${i}" aria-label="${esc('Remove ' + f)}" title="Remove">${svg(X, 16)}</button>`}</div>`).join('')}`
|
|
41
|
+
+ (ro ? (s.finish || []).length ? '' : '<p class="wf-small wf-mut">No items.</p>' : `<button type="button" class="wf-link wf-addi" data-st-finadd>${svg(PLUS, 16)}Add an item</button>`) + '</div></div>';
|
|
42
|
+
const cond = mode === 'only' ? `<div class="wf-box wf-small wf-cond"><span>${'Runs only when ' + esc(s.when.text || '…')}</span>${ro ? '' : '<button type="button" class="wf-link" data-st-cond>Edit</button>'}</div>` : '';
|
|
43
|
+
const right = `<div class="wf-field"><span class="wf-fl">When it runs</span>${seg('When it runs', 'when', mode, WHEN, ro || tri)}${cond}</div>`
|
|
44
|
+
+ `<div class="wf-field"><span class="wf-fl">Then</span><div class="wf-box wf-small wf-then">`
|
|
45
|
+
+ `<div class="wf-row"><span class="wf-grow">${s.then ? 'Goes to' : 'Goes to the next stage in the list'}</span>${pick(id('Then'), 'Then goes to', s.then || '', then, 'data-st-then', ro)}</div>`
|
|
46
|
+
+ (note ? `<p class="wf-mut">${esc(note)}</p>` : '')
|
|
47
|
+
+ (s.unless || []).map((u, i) => `<div class="wf-row wf-wrap"><span>Unless</span><label class="wf-vh" for="${id('U' + i)}">Unless</label><input id="${id('U' + i)}" class="wf-inp wf-grow" data-st-ut="${i}" value="${esc(u.text || '')}" maxlength="200" placeholder="plain words, for example: no bid scores above 60"${ro ? ' disabled' : ''}><span>go to</span>${pick(id('UTo' + i), 'go to', u.to, others.map((x) => [x.key, E.label(wf, x.key)]), `data-st-uto="${i}"`, ro)}${ro ? '' : `<button type="button" class="ib sm" data-st-ux="${i}" aria-label="Remove this exception" title="Remove">${svg(X, 16)}</button>`}</div>`).join('')
|
|
48
|
+
+ (PENDING.has(wf.id + ':' + s.key) ? (() => { const u = PENDING.get(wf.id + ':' + s.key); return `<div class="wf-row wf-wrap"><span>Unless</span><label class="wf-vh" for="${id('UP')}">Unless</label><input id="${id('UP')}" class="wf-inp wf-grow" data-st-pt value="${esc(u.text)}" maxlength="200" placeholder="plain words, for example: no bid scores above 60"><span>go to</span>${pick(id('UPTo'), 'go to', u.to, others.map((x) => [x.key, E.label(wf, x.key)]), 'data-st-pto', false)}<button type="button" class="ib sm" data-st-px aria-label="Remove this exception" title="Remove">${svg(X, 16)}</button></div>`; })() : '')
|
|
49
|
+
+ (ro || !others.length || PENDING.has(wf.id + ':' + s.key) ? '' : `<button type="button" class="wf-link wf-addi" data-st-uadd>${svg(PLUS, 16)}${(s.unless || []).length ? 'Another exception' : 'An exception'}</button>`) + '</div></div>'
|
|
50
|
+
+ sw(s.milestone, 'milestone', 'Milestone', 'a ◆ checkpoint with a date', ro)
|
|
51
|
+
+ sw(s.auto, 'auto', 'Moves on by itself', 'when every item is done', ro);
|
|
52
|
+
return `<p class="wf-lbl">${esc(wf.name)} · stage ${n} of ${wf.stages.length}</p>`
|
|
53
|
+
+ (ro ? `<p class="wf-box wf-small">${esc(wf.name)} ships with PMOS. Copy it to change a stage.</p>` : '')
|
|
54
|
+
+ `<div class="wf-sgrid"><div class="wf-scol">${left}</div><div class="wf-scol">${right}</div></div><p class="wf-small" role="status" id="wfStMsg"></p>`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Open (or redraw) the stage panel. */
|
|
58
|
+
export function openStage(wf, key, o = {}) {
|
|
59
|
+
const s = wf.stages.find((x) => x.key === key); if (!s) return;
|
|
60
|
+
const ro = H.readOnly();
|
|
61
|
+
const id = 'wf-stage:' + wf.id + ':' + key;
|
|
62
|
+
const same = VW.current() && VW.current().id === id;
|
|
63
|
+
const n = E.numOf(wf, key);
|
|
64
|
+
const more = ro ? null : () => [
|
|
65
|
+
{ label: 'Move up', disabled: n <= 2 || E.isTriage(s), on: () => save(E.shiftStage(fresh(), key, -1)) },
|
|
66
|
+
{ label: 'Move down', disabled: n >= wf.stages.length || E.isTriage(s), on: () => save(E.shiftStage(fresh(), key, 1)) },
|
|
67
|
+
...(E.isTriage(s) ? [] : [{ sep: true }, { label: 'Remove this stage', danger: true, on: () => H.remove(fresh(), key) }]),
|
|
68
|
+
];
|
|
69
|
+
const rename = ro ? null : { max: 60, save: async (title) => { const r = await H.save(E.setStage(fresh(), key, { name: title })); if (!r.ok) return { ok: false, error: r.error }; H.toast('Renamed'); return { ok: true, title }; } };
|
|
70
|
+
VW.open({ id, kind: 'Workflow stage', title: s.name, icon: STAGE_ICON, keep: same, more, rename, render: (b) => draw(b, key) });
|
|
71
|
+
if (!same && !VW.widthOf('Workflow stage') && !matchMedia('(max-width: 760px)').matches) VW.setWidth(820, false);
|
|
72
|
+
if (o.rename) { const t = document.querySelector('#czViewer [data-cz-vw-rn]'); if (t) t.click(); }
|
|
73
|
+
}
|
|
74
|
+
const fresh = () => { const { count, ...w } = H.wf(); return w; };
|
|
75
|
+
async function save(next) {
|
|
76
|
+
const r = await H.save(next);
|
|
77
|
+
const m = document.getElementById('wfStMsg');
|
|
78
|
+
if (!r.ok) { if (m) m.textContent = r.error; return r; }
|
|
79
|
+
const b = VW.body(), cur = VW.current();
|
|
80
|
+
if (b && cur && cur.id.startsWith('wf-stage:')) { const k = cur.id.split(':').slice(2).join(':'); if (H.wf().stages.some((x) => x.key === k)) draw(b, k); }
|
|
81
|
+
return r;
|
|
82
|
+
}
|
|
83
|
+
function draw(b, key) {
|
|
84
|
+
const wf = H.wf(), s = wf && wf.stages.find((x) => x.key === key); if (!s) return;
|
|
85
|
+
const ae = document.activeElement, keepId = ae && b.contains(ae) ? ae.id : '';
|
|
86
|
+
// a field the PM is typing in is not redrawn under them: the panel redraws when they leave it
|
|
87
|
+
if (ae && b.contains(ae) && ae.matches('input:not([type]), input[type="text"], textarea')) { b.onfocusout = () => { b.onfocusout = null; setTimeout(() => { if (VW.body() === b && !b.contains(document.activeElement)) draw(b, key); }, 0); }; return; }
|
|
88
|
+
b.innerHTML = `<div class="wf-stage">${bodyHtml(wf, s, H.readOnly())}</div>`;
|
|
89
|
+
if (keepId) { const f = document.getElementById(keepId); if (f) f.focus(); }
|
|
90
|
+
b.onclick = (e) => press(e, key);
|
|
91
|
+
b.onchange = (e) => change(e, key);
|
|
92
|
+
}
|
|
93
|
+
function press(e, key) {
|
|
94
|
+
const t = e.target.closest('button'); if (!t || t.disabled) return;
|
|
95
|
+
const d = t.dataset, s = fresh().stages.find((x) => x.key === key);
|
|
96
|
+
if (d.stCat) { save(E.setStage(fresh(), key, { cat: d.stCat })); return; }
|
|
97
|
+
if (d.stKind) { save(E.setStage(fresh(), key, { output: { kind: d.stKind } })); return; }
|
|
98
|
+
if (d.stWhen) {
|
|
99
|
+
if (d.stWhen === 'only') { openCond(key); return; }
|
|
100
|
+
save(E.setStage(fresh(), key, { when: { mode: d.stWhen } })); return;
|
|
101
|
+
}
|
|
102
|
+
if ('stCond' in d) { openCond(key); return; }
|
|
103
|
+
if (d.stSw) { save(E.setStage(fresh(), key, { [d.stSw]: !s[d.stSw] })); return; }
|
|
104
|
+
if ('stFinadd' in d) { save(E.setStage(fresh(), key, { finish: [...(s.finish || []), 'New item'] })).then(() => { const i = document.getElementById('wfStF' + (s.finish || []).length); if (i) { i.focus(); i.select(); } }); return; }
|
|
105
|
+
if (d.stFinx) { const f = [...(s.finish || [])]; f.splice(Number(d.stFinx), 1); save(E.setStage(fresh(), key, { finish: f })); return; }
|
|
106
|
+
if ('stUadd' in d) { const wf = fresh(), to = (wf.stages.find((x) => x.key !== key) || {}).key; PENDING.set(wf.id + ':' + key, { text: '', to }); draw(VW.body(), key); const i = document.getElementById('wfStUP'); if (i) i.focus(); return; } // saved once it has words
|
|
107
|
+
if ('stPx' in d) { PENDING.delete(fresh().id + ':' + key); draw(VW.body(), key); return; }
|
|
108
|
+
if (d.stUx) { const u = [...(s.unless || [])]; u.splice(Number(d.stUx), 1); save(E.setStage(fresh(), key, { unless: u })); }
|
|
109
|
+
}
|
|
110
|
+
function change(e, key) {
|
|
111
|
+
const t = e.target, d = t.dataset, s = fresh().stages.find((x) => x.key === key); if (!s) return;
|
|
112
|
+
if ('stOut' in d) save(E.setStage(fresh(), key, { output: { name: t.value.trim() } }));
|
|
113
|
+
else if ('stTpl' in d) save(E.setStage(fresh(), key, { output: { template: t.value.trim() } }));
|
|
114
|
+
else if (d.stFin !== undefined) { const f = [...(s.finish || [])], v = t.value.trim(); if (v) f[Number(d.stFin)] = v; else f.splice(Number(d.stFin), 1); save(E.setStage(fresh(), key, { finish: f })); }
|
|
115
|
+
else if ('stThen' in d) save(E.setStage(fresh(), key, { then: t.value || null }));
|
|
116
|
+
else if (d.stUt !== undefined) { const u = (s.unless || []).map((x) => ({ ...x })), v = t.value.trim(); if (v) u[Number(d.stUt)].text = v; else u.splice(Number(d.stUt), 1); save(E.setStage(fresh(), key, { unless: u })); }
|
|
117
|
+
else if ('stPt' in d || 'stPto' in d) {
|
|
118
|
+
const pk = fresh().id + ':' + key, u = PENDING.get(pk); if (!u) return;
|
|
119
|
+
if ('stPt' in d) { u.text = t.value.trim(); const to = document.getElementById('wfStUPTo'); if (to) u.to = to.value; } else u.to = t.value;
|
|
120
|
+
if (u.text) { PENDING.delete(pk); save(E.setStage(fresh(), key, { unless: [...(s.unless || []), { text: u.text, to: u.to }] })); }
|
|
121
|
+
}
|
|
122
|
+
else if (d.stUto !== undefined) { const u = (s.unless || []).map((x) => ({ ...x })); u[Number(d.stUto)].to = t.value; save(E.setStage(fresh(), key, { unless: u })); }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/* ── Only when (WfACond) ── */
|
|
126
|
+
let ROWS = null;
|
|
127
|
+
async function readRows() {
|
|
128
|
+
const r = await pmGet('/api/board-premium' + wsQ()).catch(() => null);
|
|
129
|
+
ROWS = r && r.ok && r.model && Array.isArray(r.model.rows) ? r.model.rows : r && r.state === 'reading' ? 'reading' : [];
|
|
130
|
+
return ROWS;
|
|
131
|
+
}
|
|
132
|
+
/** One line per initiative on this workflow: runs, skipped or not sure, with PMOS's reason (rows' wfStages[].reading or .skipped). */
|
|
133
|
+
export function readingLines(rows, wf, key) {
|
|
134
|
+
const on = (Array.isArray(rows) ? rows : []).filter((r) => r && r.wf && r.wf.id === wf.id);
|
|
135
|
+
const i = E.numOf(wf, key);
|
|
136
|
+
return on.map((r) => {
|
|
137
|
+
const st = (r.wfStages || []).find((x) => x.key === key) || {}, at = r.wfStage && r.wfStage.n;
|
|
138
|
+
const rd = st.reading || null;
|
|
139
|
+
if (st.skipped) return { title: r.title, word: 'skipped', why: st.skipped.why || '' };
|
|
140
|
+
if (rd && rd.runs === true) return { title: r.title, word: 'runs', why: rd.why || '' };
|
|
141
|
+
if (rd && rd.runs === false) return { title: r.title, word: 'skipped', why: rd.why || '' };
|
|
142
|
+
if (rd && rd.runs === null) return { title: r.title, word: 'not sure', why: (rd.why ? rd.why + ' ' : '') + 'PMOS will ask you.' };
|
|
143
|
+
if (at && at > i) return { title: r.title, word: 'ran', why: '' };
|
|
144
|
+
return { title: r.title, word: 'not read yet', why: 'PMOS reads its documents when it reaches the stage before.' };
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
function condHtml(wf, s, lines, err) {
|
|
148
|
+
const text = s.when && s.when.mode === 'only' ? s.when.text || '' : '';
|
|
149
|
+
const how = lines === 'reading' ? '<div class="wf-box wf-small"><b>How it reads today</b><span class="wf-mut">Reading the Board.</span></div>'
|
|
150
|
+
: lines && lines.length ? `<div class="wf-box wf-small wf-reads"><b>How it reads today</b>${lines.map((l) => `<span class="wf-rd">${wfIcon(wf, 18)}<span><b>${esc(l.title)}</b> · ${esc(l.word)}.${l.why ? ' ' + esc(l.why) : ''}</span></span>`).join('')}</div>` : '';
|
|
151
|
+
return `<div class="wf-stage wf-condp"><p class="wf-lbl">${esc(wf.name)} · stage ${E.numOf(wf, s.key)} of ${wf.stages.length}</p>`
|
|
152
|
+
+ `<label class="wf-vh" for="wfCondT">${esc(s.name)} runs only when</label><textarea id="wfCondT" class="wf-inp ta" rows="3" maxlength="400" placeholder="For example: the contract is over $50k, or the supplier will handle personal data">${esc(text)}</textarea>`
|
|
153
|
+
+ '<p class="wf-small wf-mut">Write it in your words. There is no rule builder: PMOS reads the initiative’s documents when it reaches the stage before, and decides.</p>'
|
|
154
|
+
+ how
|
|
155
|
+
+ `<div class="wf-field"><span class="wf-fl">Who decides</span>${seg('Who decides', 'decide', wf.decide === 'confirm' ? 'confirm' : 'pmos', [['pmos', 'PMOS, and tells you why'], ['confirm', 'PMOS suggests, you confirm']])}</div>`
|
|
156
|
+
+ '<p class="wf-small wf-mut">Either way the stage card shows the reason, and you can undo a skip.</p>'
|
|
157
|
+
+ `<p class="wf-err" role="alert" id="wfCondErr">${esc(err || '')}</p><div class="wf-acts"><button type="button" class="wf-btn dark" data-st-csave>Save</button><button type="button" class="wf-btn ghost" data-st-cx>Cancel</button></div></div>`;
|
|
158
|
+
}
|
|
159
|
+
function openCond(key) {
|
|
160
|
+
const wf = H.wf(), s = wf.stages.find((x) => x.key === key); if (!s) return;
|
|
161
|
+
let decide = wf.decide === 'confirm' ? 'confirm' : 'pmos';
|
|
162
|
+
const paint = (b, err) => {
|
|
163
|
+
const t = b.querySelector('#wfCondT'), v = t ? t.value : null;
|
|
164
|
+
b.innerHTML = condHtml(H.wf(), s, ROWS === null ? 'reading' : ROWS === 'reading' ? 'reading' : readingLines(ROWS, wf, key), err);
|
|
165
|
+
const nt = b.querySelector('#wfCondT'); if (v !== null) nt.value = v;
|
|
166
|
+
b.querySelectorAll('[data-st-decide]').forEach((x) => { const on = x.dataset.stDecide === decide; x.setAttribute('aria-checked', String(on)); x.setAttribute('aria-pressed', String(on)); });
|
|
167
|
+
};
|
|
168
|
+
VW.open({ id: 'wf-cond:' + wf.id + ':' + key, kind: 'Workflow stage', title: s.name + ' runs only when', icon: E.isTriage(s) ? undefined : 'M6 3v12M18 9a3 3 0 1 0 0-6a3 3 0 1 0 0 6M6 21a3 3 0 1 0 0-6a3 3 0 1 0 0 6M18 9a9 9 0 0 1-9 9', back: true,
|
|
169
|
+
render: (b) => {
|
|
170
|
+
paint(b);
|
|
171
|
+
const ta = b.querySelector('#wfCondT'); if (ta) ta.focus();
|
|
172
|
+
if (ROWS === null || ROWS === 'reading') readRows().then(() => { if (VW.body() === b) paint(b); });
|
|
173
|
+
b.onclick = async (e) => {
|
|
174
|
+
const t = e.target.closest('button'); if (!t) return;
|
|
175
|
+
if (t.dataset.stDecide) { decide = t.dataset.stDecide; paint(b); return; }
|
|
176
|
+
if ('stCx' in t.dataset) { VW.back(); return; }
|
|
177
|
+
if ('stCsave' in t.dataset) {
|
|
178
|
+
const v = (b.querySelector('#wfCondT').value || '').trim();
|
|
179
|
+
if (!v) { paint(b, 'Write the condition in your words.'); return; }
|
|
180
|
+
const next = { ...E.setStage(fresh(), key, { when: { mode: 'only', text: v } }), decide };
|
|
181
|
+
const r = await H.save(next);
|
|
182
|
+
if (!r.ok) { paint(b, r.error); return; }
|
|
183
|
+
VW.back();
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
} });
|
|
187
|
+
}
|