domma-cms 1.16.0 → 1.16.1
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/CHANGELOG.md +4 -0
- package/admin/js/templates/docs/api-navigation.html +9 -3
- package/admin/js/templates/docs/usage-navigation.html +4 -0
- package/admin/js/views/menu-editor.js +16 -16
- package/docs/DommaFeatures.md +1 -0
- package/package.json +1 -1
- package/server/routes/api/menus.js +8 -4
- package/server/services/menus.js +27 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.16.1] - 2026-10-29
|
|
4
|
+
|
|
5
|
+
- Menus: saving a menu that was changed somewhere else after you opened it (another tab, someone else, or by hand on the server) now warns you instead of quietly undoing that change. Choose to save over it, or load the latest version with your changes offered back as a draft. `GET /api/menus/:slug` returns a `revision`; `PUT` with it answers 409 when the menu has changed since.
|
|
6
|
+
|
|
3
7
|
## [1.16.0] - 2026-10-29
|
|
4
8
|
|
|
5
9
|
- Site Settings: Search now has its own tab with the existing controls, query preview and draft protection. Existing Search links open that tab. Icon actions on Site Settings have tooltips.
|
|
@@ -81,14 +81,16 @@
|
|
|
81
81
|
|
|
82
82
|
<h3><span class="method-badge method-get">GET</span><span class="endpoint-path">/api/menus/:slug</span></h3>
|
|
83
83
|
<p class="auth-note">Requires Bearer token + <code>menus</code> read permission.</p>
|
|
84
|
-
<p>One menu, with its items. Sub-items go under <code>items</code
|
|
84
|
+
<p>One menu, with its items. Sub-items go under <code>items</code>. <code>revision</code> is a fingerprint of the
|
|
85
|
+
menu as stored; send it back on <code>PUT</code> to be warned of changes made since.</p>
|
|
85
86
|
<pre class="code-block"><code>// Response 200
|
|
86
87
|
{ "slug": "main", "name": "Main navigation", "variant": "dark", "position": "sticky",
|
|
87
88
|
"items": [
|
|
88
89
|
{ "text": "Home", "url": "/", "icon": "home" },
|
|
89
90
|
{ "text": "Services", "items": [ { "text": "Design", "url": "/design" } ] }
|
|
90
91
|
],
|
|
91
|
-
"meta": { "createdAt": "...", "updatedAt": "..." }
|
|
92
|
+
"meta": { "createdAt": "...", "updatedAt": "..." },
|
|
93
|
+
"revision": "3f9a1c0e5b7d2a46" }</code></pre>
|
|
92
94
|
|
|
93
95
|
<h3><span class="method-badge method-post">POST</span><span class="endpoint-path">/api/menus</span></h3>
|
|
94
96
|
<p class="auth-note">Requires Bearer token + <code>menus</code> create permission.</p>
|
|
@@ -102,7 +104,11 @@
|
|
|
102
104
|
|
|
103
105
|
<h3><span class="method-badge method-put">PUT</span><span class="endpoint-path">/api/menus/:slug</span></h3>
|
|
104
106
|
<p class="auth-note">Requires Bearer token + <code>menus</code> update permission.</p>
|
|
105
|
-
<p>Save a menu. The public pages that show it are refreshed
|
|
107
|
+
<p>Save a menu. The public pages that show it are refreshed. Include the <code>revision</code> you loaded and the
|
|
108
|
+
save is refused if the menu has changed since (leave it out to overwrite regardless). The response carries the
|
|
109
|
+
new <code>revision</code>.</p>
|
|
110
|
+
<pre class="code-block"><code>// Error 409 - changed since it was loaded; nothing is written
|
|
111
|
+
{ "error": "Menu \"main\" has changed since it was opened", "stale": true }</code></pre>
|
|
106
112
|
|
|
107
113
|
<h3><span class="method-badge method-delete">DELETE</span><span class="endpoint-path">/api/menus/:slug</span></h3>
|
|
108
114
|
<p class="auth-note">Requires Bearer token + <code>menus</code> delete permission.</p>
|
|
@@ -63,6 +63,10 @@
|
|
|
63
63
|
<li><strong>Meta</strong> - the menu's id, which is its file name and what
|
|
64
64
|
<code>[menu slug="..."]</code> names.</li>
|
|
65
65
|
</ul>
|
|
66
|
+
<p>If the menu was changed somewhere else after you opened it - in another tab, by someone else, or
|
|
67
|
+
on the server - Save warns you instead of quietly undoing that change. Choose <strong>OK</strong>
|
|
68
|
+
to save yours over it, or <strong>Cancel</strong> to load the latest version; your changes are
|
|
69
|
+
then offered back as a draft.</p>
|
|
66
70
|
|
|
67
71
|
<h3>Pill bar and card dropdowns</h3>
|
|
68
72
|
<p>Two choices on the <strong>Behaviour</strong> tab change how the navbar looks. Both are for a
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import{api as
|
|
2
|
-
<div class="menu-tree-row menu-tree-row--separator" data-id="${
|
|
1
|
+
import{api as D}from"../api.js";import{colourToCss as xt,HIGHLIGHT_HOVER as dt,HIGHLIGHT_ACTIVE as ut,HIGHLIGHT_HOVER_LABELS as mt,HIGHLIGHT_ACTIVE_LABELS as pt,cssClassProblems as ht,normaliseCssClasses as At,ENTRANCE_ANIMATIONS as nt,ENTRANCE_ANIMATION_LABELS as Et}from"/public/js/menu-decor.mjs?v=20261014-menu-entrance";import{openIconPicker as $t}from"../lib/card-builder.js";import{normalisePanel as Ct,DESCRIPTION_MAX as Lt}from"/public/js/menu-look.mjs?v=20261029-bar-shapes";import{sortableMenuRows as Tt}from"../lib/menu-drag.js";import{placeTools as _t,ensureLockedTools as It,isLockedUrl as Ht,groupKeyFor as Nt,slugify as Ot,GROUP_ICON as Rt,TOOLS_FOLDER_KEY as Bt}from"../lib/sidebar-grouping.js";import{flatten as ft,nest as Dt,uid as Ve}from"../lib/menu-rows.js";import{addBannerAction as ot}from"../lib/plugin-chrome.js";import{helpPopovers as Pt}from"../lib/help-popover.js";import{onceClosed as jt,openMenuAt as at,sidebarCount as Ft}from"../lib/tool-kit.js";import{addChild as Mt,bindingFrom as zt,duplicateRow as Ut,indentRow as Wt,insertBelow as Kt,isStructural as ge,menuFromForm as qt,menuProblems as Gt,moveRow as gt,outdentRow as Vt,siblingsOf as Yt,snapshot as Ye}from"../lib/menu-editor-arrange.js";import{fmtDateTime as Xt}from"../lib/dates.js";const Jt=760,Qt="admin-sidebar",Zt=/^\/menus\/(new|edit\/[^?#]+)(\?.*)?$/;function A(n){return String(n||"").replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<")}const De=n=>String(n??"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""");function en(n,u){let i=0,r=u.find(l=>l.id===n);for(;r&&r.parentId;)i++,r=u.find(l=>l.id===r.parentId);return i}function vt(n){const u=new Set,i=new Set,r=l=>{for(const w of n.filter(L=>L.parentId===l))!w.type&&Ht(w.url)&&!i.has(w.url)&&(i.add(w.url),u.add(w.id)),r(w.id)};return r(null),u}const Xe='<button type="button" class="btn btn-sm btn-ghost mex-kebab" aria-label="Actions" title="Actions (right-click, or Shift+F10)">\u22EE</button>';function tn(n,u,i=!1){const r=i?vt(u):new Set,l=[];function w(L){for(const m of u.filter(G=>G.parentId===L)){const C=`style="margin-left:${en(m.id,u)*28}px"`;if(m.type==="separator"){l.push(`
|
|
2
|
+
<div class="menu-tree-row menu-tree-row--separator" data-id="${m.id}" tabindex="0" ${C}>
|
|
3
3
|
<span class="menu-tree-grip" data-icon="grip-vertical" data-icon-size="18" title="Drag to move"></span>
|
|
4
4
|
<span class="me-sep-line"><span class="me-sep-label">Separator</span></span>
|
|
5
5
|
${Xe}
|
|
6
|
-
</div>`);continue}if(
|
|
7
|
-
<div class="menu-tree-row menu-tree-row--spacer" data-id="${
|
|
6
|
+
</div>`);continue}if(m.type==="spacer"){const ve=["xs","sm","md","lg","xl","flex"].map(te=>`<option value="${te}"${(m.size||"md")===te?" selected":""}>${te==="flex"?"flexible":te}</option>`).join("");l.push(`
|
|
7
|
+
<div class="menu-tree-row menu-tree-row--spacer" data-id="${m.id}" tabindex="0" ${C}>
|
|
8
8
|
<span class="menu-tree-grip" data-icon="grip-vertical" data-icon-size="18" title="Drag to move"></span>
|
|
9
9
|
<span class="me-spacer-line">
|
|
10
10
|
<span class="me-spacer-label">Spacer</span>
|
|
11
11
|
<select class="form-input me-spacer-size" title="Spacer size - flexible fills the leftover room" aria-label="Spacer size">${ve}</select>
|
|
12
12
|
</span>
|
|
13
13
|
${Xe}
|
|
14
|
-
</div>`);continue}if(
|
|
15
|
-
<div class="menu-tree-row menu-tree-row--switcher" data-id="${
|
|
14
|
+
</div>`);continue}if(m.type==="theme-switcher"){l.push(`
|
|
15
|
+
<div class="menu-tree-row menu-tree-row--switcher" data-id="${m.id}" tabindex="0" ${C}>
|
|
16
16
|
<span class="menu-tree-grip" data-icon="grip-vertical" data-icon-size="18" title="Drag to move"></span>
|
|
17
17
|
<span class="me-sep-line"><span class="me-sep-label"><span data-icon="palette"></span> Theme switcher - the themes chosen under Theme > Theme switcher</span></span>
|
|
18
18
|
${Xe}
|
|
19
|
-
</div>`);continue}const
|
|
20
|
-
<div class="menu-tree-row menu-tree-row--slim${
|
|
19
|
+
</div>`);continue}const V=m.badge&&(m.badge.text||m.badge.countFrom)?`<span class="dm-menu-badge">${A(String(m.badge.text||"#"))}</span>`:"",N=m.tool,f=N&&(N.persisted||m.touched),le=N?N.orphan?' <span class="badge badge-secondary me-tool-badge" title="A placeholder for a Tool that is not enabled - it returns here when it is">Not enabled</span>':` <span class="badge badge-secondary me-tool-badge" title="${f?"A Tool, placed here by this menu":"A Tool in its default place - move or edit it to place it here"}">${A(N.source||"Tool")}${f?"":" \xB7 default"}</span>`:"",Z=r.has(m.id),se=xt(m.colour),ee=se?` style="color:${A(se)}"`:"",ie=Z?'<button type="button" class="btn btn-sm btn-ghost" disabled title="Always shown - it can be moved, not hidden" aria-label="Always shown"><span data-icon="lock"></span></button>':`<button type="button" class="btn btn-sm btn-ghost me-hidden ${m.hidden?"active":""}" title="${m.hidden?"Hidden - click to show":"Hide"}" aria-label="${m.hidden?"Show":"Hide"}" aria-pressed="${m.hidden?"true":"false"}"><span data-icon="${m.hidden?"eye-off":"eye"}"></span></button>`;l.push(`
|
|
20
|
+
<div class="menu-tree-row menu-tree-row--slim${m.hidden?" menu-tree-row--hidden":""}${N?" menu-tree-row--tool":""}${N&&!f?" menu-tree-row--default":""}" data-id="${m.id}" tabindex="0" ${C}>
|
|
21
21
|
<span class="menu-tree-grip" data-icon="grip-vertical" data-icon-size="18" title="Drag to move"></span>
|
|
22
|
-
<span class="me-row-label${!
|
|
23
|
-
<span class="me-row-url">${A(
|
|
22
|
+
<span class="me-row-label${!m.text&&!N?" mex-untitled":""}"${ee}>${A(m.text||N?.label||"(no label - click to add one)")}${V}${le}</span>
|
|
23
|
+
<span class="me-row-url">${A(m.url||"")}</span>
|
|
24
24
|
${ie}
|
|
25
25
|
<button type="button" class="btn btn-sm btn-ghost me-edit" title="Edit" aria-label="Edit"><span data-icon="edit"></span></button>
|
|
26
26
|
${Xe}
|
|
27
|
-
</div>`),
|
|
28
|
-
`),placeholder:"One per line, up to six"})),t=
|
|
29
|
-
`),button:{text:f("featureBtnText"),url:f("featureBtnUrl")}}:null})}Se=!0,_.close(!0),l()})}let K=null,bt=!1;function an(){bt||!window.R?.use||(bt=!0,R.use((n,d,i)=>{const r=d?.path||"";return!(Qt.test(r)&&(n?.path||"")!==r)||!K?.dirty()?i():E.confirm(`You have unsaved changes to the menu "${Ye(K.name())}". Leave anyway? They are kept as a draft for your next visit.`).then(k=>{k?i():history.replaceState(null,"",`#${r}`)})}),M.subscribe("router:afterChange",({to:n}={})=>{if(K&&(n?.path||"")!==K.path){const d=K;K=null,d.dispose()}}))}function yt(n,d){const i=n.querySelector(".dm-plugin-banner-name"),r=i?.querySelector(".dm-plugin-banner-sep");if(!(!i||!r)){for(;r.nextSibling;)r.nextSibling.remove();i.appendChild(document.createTextNode(d))}}export const menuEditorView={templateUrl:"/admin/js/templates/menu-editor.html",async onMount(n){if(an(),K){const e=K;K=null,e.dispose()}const d=[],i=e=>(d.push(e),e);let r=!1;const l=n.get(0).querySelector(".mex")||n.get(0),k=()=>!r&&l.isConnected,L=location.hash.replace(/^#/,""),u=location.hash.match(/^#\/menus\/edit\/([^?#]+)/),q=u?decodeURIComponent(u[1]):null,C=!q,Q=`menu-editor.draft.${q||"new"}`,N=E.loader(n.get(0),{type:"dots"});let v,le,Z,se,ee,ie,ve;try{if(C)v={slug:"",name:"",description:"",items:[]};else try{v=await P.menus.get(q)}catch(e){E.toast(`Failed to load menu: ${e.message||e}`,{type:"error"}),location.hash="#/menus";return}[le,Z,se,ee,ie,ve]=await Promise.all([P.projects.list().catch(()=>[]),H.get("/api/auth/permissions-registry").catch(()=>({resources:[]})),P.collections.list().catch(()=>[]),P.get("/collections/roles/entries?limit=100").catch(()=>null),P.menuLocations.registry().catch(()=>[]),P.pages.list().catch(()=>[])])}finally{N.destroy()}if(!l.isConnected)return;const be=(Array.isArray(ee)?ee:ee?.entries||[]).map(e=>({name:e.data?.name,label:e.data?.label,level:e.data?.level??99})).filter(e=>e.name).sort((e,o)=>o.level-e.level),D=!C&&q===Jt;let $e=[];if(D&&($e=await H.get("/api/sidebar/registered-items").catch(()=>(E.toast("Could not load the Tools - they are not shown, and saving keeps their places as they are.",{type:"warning",duration:8e3}),[])),!l.isConnected))return;const Ce=e=>ft(D?_t(Tt(e||[],$e,{forEditor:!0}),{leaveInHiddenFolders:!0}):e);let h=Ce(v.items);const j=M.observable(!1),re=M.observable(!1),Pe=M.observable(""),Le=M.observable(0);let G="";const T=l.querySelector("#me-items-list");function ye(){en(T,h,D),Le.value=h.filter(e=>!ge(e)).length}const B=()=>{ye(),Y()},V=()=>D?vt(h):new Set,ce=e=>h.find(o=>o.id===e);function de(e){requestAnimationFrame(()=>T.querySelector(`.menu-tree-row[data-id="${CSS.escape(e)}"]`)?.focus())}function Te(e){const o=g=>h.find(s=>!s.type&&!s.url&&!s.tool&&(s.key||"").toLowerCase()===g)||h.find(s=>!s.type&&!s.url&&!s.tool&&!s.key&&(s.text||"").toLowerCase()===g);let a=o(String(e.tool.folder||"").toLowerCase())||o(Bt);const p=String(e.tool.group||"").trim();if(a&&p){const g=Ht(a.key||Nt(a.text),p),s=c=>c!==e&&!c.type&&!c.url&&!c.tool;let w=h.find(c=>s(c)&&(c.key||"").toLowerCase()===g)||h.find(c=>s(c)&&c.parentId===a.id&&(!c.key||c.key.toLowerCase()===g)&&(c.text||"").toLowerCase()===p.toLowerCase());w||([w]=ft([{text:p,key:g,icon:Rt,synthesised:!0}],a.id),h.push(w)),a=w}e.parentId=a?a.id:null,h.splice(h.indexOf(e),1),h.push(e),Object.assign(e,{text:e.tool.defaults.text,icon:e.tool.defaults.icon,visibility:"",hidden:!1,badge:null,pill:null,colour:"",highlight:null,touched:!1}),e.tool={...e.tool,persisted:!1}}const _e=e=>!V().has(e.id)&&!(e.tool&&!e.tool.orphan);async function je(e){const o=ce(e);if(!o||!_e(o))return;const a=c=>{let X=c;for(;X;){if(X.id===e)return!0;X=h.find(z=>z.id===X.parentId)}return!1},p=h.filter(c=>c.id!==e&&a(c));if(p.length&&!await E.confirm(`Remove "${Ye(o.text||o.type||"this item")}" and the ${p.length} item${p.length===1?"":"s"} beneath it?`))return;const g=V(),s=p.filter(c=>c.tool),w=p.filter(c=>!c.tool&&g.has(c.id));h=h.filter(c=>!a(c)||s.includes(c)||w.includes(c));for(const c of w)c.parentId=o.parentId??null;for(const c of s)c.tool.orphan?c.parentId=o.parentId??null:Te(c);B()}function ue(e){const o=ce(e);!o||ge(o)||V().has(e)||(o.hidden=!o.hidden,o.touched=!0,B())}function x(e){const o=ce(e);!o||ge(o)||(o.locked=D&&V().has(o.id),on(o,Z.resources||[],se,be,()=>{B(),de(e)},{cards:!D}))}function O(e){const o={id:Ge(),parentId:null,...e==="separator"||e==="theme-switcher"?{type:e}:e==="spacer"?{type:e,size:"md"}:{text:"",url:"/",icon:"",visibility:"",permission:"",hidden:!1,bundled:!1}};h.push(o),B(),ge(o)?de(o.id):x(o.id)}const ke=e=>o=>{e(o)&&(B(),de(o))},Fe=ke(e=>gt(h,e,-1)),Me=ke(e=>gt(h,e,1)),Ie=ke(e=>Ut(h,e)),ze=ke(e=>Gt(h,e));function Je(e){const o=ce(e);if(!o)return[];const a=ge(o),p=V().has(e),g=Vt(h,e),s=g.findIndex(z=>z.id===e),w=o.tool,c=w&&(w.persisted||o.touched),X=z=>()=>{const Oe=Wt(h,e,z,Ge());B(),Oe&&!ge(Oe)?x(Oe.id):Oe&&de(Oe.id)};return[{header:a?{separator:"Separator",spacer:"Spacer","theme-switcher":"Theme switcher"}[o.type]:o.text||w?.label||"(untitled)"},...a?[]:[{label:"Edit\u2026",icon:"edit",action:()=>x(e)}],{type:"divider"},{label:"Add item below",icon:"plus",action:X("item")},{label:"Add sub-item",icon:"corner-down-right",disabled:a,action:()=>{const z=Ft(h,e,Ge());B(),z&&x(z.id)}},{label:"Add separator below",icon:"minus",action:X("separator")},{label:"Add spacer below",icon:"move",action:X("spacer")},...D?[]:[{label:"Add theme switcher below",icon:"palette",action:X("theme-switcher")}],{type:"divider"},{label:"Move up",icon:"arrow-up",shortcut:"Alt+\u2191",disabled:s<=0,action:()=>Fe(e)},{label:"Move down",icon:"arrow-down",shortcut:"Alt+\u2193",disabled:s<0||s>=g.length-1,action:()=>Me(e)},{label:"Indent",icon:"chevron-right",shortcut:"Alt+\u2192",disabled:s<=0||ge(g[s-1]),action:()=>Ie(e)},{label:"Outdent",icon:"chevron-left",shortcut:"Alt+\u2190",disabled:o.parentId==null,action:()=>ze(e)},{type:"divider"},...a?[]:[{label:o.hidden?"Show":"Hide",icon:o.hidden?"eye":"eye-off",disabled:p,action:()=>ue(e)}],{label:"Duplicate",icon:"copy",disabled:!!w,action:()=>{const z=zt(h,e,Ge);B(),z&&de(z.id)}},...w&&!w.orphan&&c?[{label:"Back to its default place",icon:"rotate-ccw",action:()=>{Te(o),B()}}]:[],{label:p?"Remove - always in the sidebar":w&&!w.orphan?"Remove - a Tool is hidden, not removed":"Remove\u2026",icon:"trash",danger:!0,disabled:!_e(o),action:()=>je(e)}]}const me=E.contextMenu(T,{cursor:"glyph",match:".menu-tree-row",inherit:!1,density:"compact",items:e=>Je(e.dataset.id)});i(()=>me?.destroy?.());const Qe=Lt(T,()=>h,B,{items:".menu-tree-row",handle:".menu-tree-grip"});i(()=>Qe?.destroy());const Ue=e=>{const o=e.target.closest(".menu-tree-row");if(!o)return;const a=o.dataset.id;if(e.target.closest(".mex-kebab")){at(e.target.closest(".mex-kebab"));return}if(e.target.closest(".me-hidden")){ue(a);return}if(e.target.closest(".me-edit")){x(a);return}e.target.closest("select, .menu-tree-grip, button")||x(a)},We=e=>{const o=e.target.closest(".menu-tree-row");if(!o||e.target!==o){o&&e.key==="F10"&&e.shiftKey&&(e.preventDefault(),at(o));return}const a=o.dataset.id;if(e.key==="F10"&&e.shiftKey)e.preventDefault(),at(o);else if(e.key==="Enter")e.preventDefault(),x(a);else if(e.key==="Delete")e.preventDefault(),je(a);else if(e.altKey&&e.key==="ArrowUp")e.preventDefault(),Fe(a);else if(e.altKey&&e.key==="ArrowDown")e.preventDefault(),Me(a);else if(e.altKey&&e.key==="ArrowRight")e.preventDefault(),Ie(a);else if(e.altKey&&e.key==="ArrowLeft")e.preventDefault(),ze(a);else if(e.key==="ArrowDown"||e.key==="ArrowUp"){const p=[...T.querySelectorAll(".menu-tree-row")],g=p.indexOf(o)+(e.key==="ArrowDown"?1:-1);p[g]&&(e.preventDefault(),p[g].focus())}},Ke=e=>{if(!e.target.matches(".me-spacer-size"))return;const o=ce(e.target.closest(".menu-tree-row")?.dataset.id);o&&(o.size=e.target.value,Y())};T.addEventListener("click",Ue),T.addEventListener("keydown",We),T.addEventListener("change",Ke),i(()=>{T.removeEventListener("click",Ue),T.removeEventListener("keydown",We),T.removeEventListener("change",Ke)});const t=e=>n.find(e),we=t("#me-float-accent"),pe=t("#me-float-accent-hex"),He=()=>{pe.get(0).style.display=we.val()==="__custom__"?"":"none"},Ne=()=>we.val()==="__custom__"?pe.val():we.val(),U=t("#me-highlight-accent"),_=t("#me-highlight-accent-hex"),Re=()=>{_.get(0).style.display=U.val()==="__custom__"?"":"none"},Ze=()=>U.val()==="__custom__"?_.val():U.val(),Se=()=>{const e=!!(t("#me-entrance-enabled").get(0)||{}).checked;t(".dm-entrance-field").each(function(){this.style.display=e?"":"none"});const o=t("#me-position").val()==="floating";t(".dm-float-field").each(function(){this.style.display=o?"":"none"}),o&&He();const a=t("#me-orientation").val()==="vertical";t(".dm-vertical-field").each(function(){this.style.display=a?"":"none"}),t(".dm-dock-rail-field").each(function(){this.style.display=a&&!o?"":"none"}),t(".dm-dock-bar-field").each(function(){this.style.display=!a&&!o?"":"none"});const p=!!(t("#me-dock-collapsible").get(0)||{}).checked,g=!!(t("#me-dock-autohide").get(0)||{}).checked;t(".dm-dock-collapsed-field").each(function(){this.style.display=p&&!g?"":"none"});const s=!!(t("#me-float-collapsible").get(0)||{}).checked;t(".dm-float-collapsed-field").each(function(){this.style.display=o&&s?"":"none"});const w=!a&&!D;t(".dm-look-field").each(function(){this.style.display=w?"":"none"});const c=w&&t("#me-look-dropdown").val()==="cards";t(".dm-look-cards-field").each(function(){this.style.display=c?"":"none"})},xe=(e,o,a,p)=>{const g=t(e).get(0);g&&(g.innerHTML="",g.add(new Option(p,"")),o.forEach(s=>g.add(new Option(a[s]||s,s))))};xe("#me-highlight-hover",dt,mt,"- theme default -"),xe("#me-highlight-active",ut,pt,"- theme default -");{const e=t("#me-entrance-animation").get(0);e&&(e.innerHTML="",nt.forEach(o=>e.add(new Option(At[o]||o,o))))}const Be=t("#me-bind-slot"),Ae=t("#me-bind-projects"),f=t("#me-bind-pages");Be.html('<option value="">- not bound -</option>'+(ie||[]).map(e=>`<option value="${A(e.slot)}">${A(e.label||e.slot)} (${A(e.slot)})</option>`).join("")),Ae.html(le.map(e=>`<option value="${A(e.slug)}">${A(e.name||e.slug)}</option>`).join("")),t("#me-bind-page-pick").html('<option value="">- pick a page -</option>'+ve.map(e=>{const o=e.urlPath||e.url||"";return o?`<option value="${A(o)}">${A(e.title||o)} - ${A(o)}</option>`:""}).join(""));const m=()=>Mt({slot:Be.val(),projects:Array.from(Ae.get(0).selectedOptions).map(e=>e.value),pagesText:f.val()});function F(){const e=m(),o=t("#me-bind-summary");if(!e){o.html('<span class="text-muted">Not bound - this menu renders only where it is mapped or placed by a shortcode.</span>');return}const a=[];e.projects.length&&a.push(`pages in ${e.projects.map(s=>`<code>${A(s)}</code>`).join(", ")}`),e.pages.length&&a.push(`pages matching ${e.pages.map(s=>`<code>${A(s)}</code>`).join(", ")}`);const p=e.pages.filter(s=>!/^\/[^\s?#*]*(\/\*)?$|^\/\*$/.test(s)),g=p.length?`<div class="text-danger" style="margin-top:.4rem">Not valid page patterns: ${p.map(s=>`<code>${A(s)}</code>`).join(", ")}</div>`:"";o.html(a.length?`Takes over the <code>${A(e.slot)}</code> slot on ${a.join(" and ")}.${g}`:`<span class="text-warning">Slot <code>${A(e.slot)}</code> selected but no project or page chosen - the binding will never match.</span>`)}const{getProjectFromHash:he}=await import("../lib/project-context.js");if(!k())return;const Ee=C?he():null;t("#me-project").html('<option value="">- none -</option>'+le.map(e=>`<option value="${A(e.slug)}">${A(e.name||e.slug)}</option>`).join(""));function fe(e){h=Ce(e.items),t("#me-variant").val(e.variant||""),t("#me-position").val(e.position||"");const o=t("#me-appearOnHover").get(0);o&&(o.checked=e.appearOnHover===!0),t("#me-float-anchor").val(e.float?.anchor||"TL"),t("#me-float-offsetX").val(e.float?.offsetX??""),t("#me-float-offsetY").val(e.float?.offsetY??""),t("#me-float-width").val(e.float?.width??""),t("#me-float-radius").val(e.float?.radius??""),t("#me-float-shadow").val(e.float?.shadow||""),t("#me-float-opacity").val(e.float?.opacity??""),t("#me-float-title").val(e.float?.title||"");for(const[s,w]of[["#me-float-collapsible",e.float?.collapsible],["#me-float-collapsed",e.float?.collapsed],["#me-float-movable",e.float?.movable],["#me-float-resizable",e.float?.resizable]]){const c=t(s).get(0);c&&(c.checked=w===!0)}const a=e.float?.accent||"";we.val(a.startsWith("#")?"__custom__":a),a.startsWith("#")&&pe.val(a),He(),t("#me-orientation").val(e.orientation||""),t("#me-align").val(e.align||""),t("#me-mobile-hamburger").get(0).checked=e.mobile?.hamburgerOnly===!0,t("#me-mobile-brand").get(0).checked=e.mobile?.hideBrand===!0,t("#me-look-bar").val(e.look?.bar||""),t("#me-look-brand").val(e.look?.brand||""),t("#me-look-strip").val(e.look?.strip||""),t("#me-look-dropdown").val(e.look?.dropdown||""),t("#me-look-tiles").val(e.look?.tiles||""),t("#me-look-morph").get(0).checked=e.look?.morph===!0,t("#me-look-shrink").get(0).checked=e.look?.shrink===!0,t("#me-side").val(e.side||"left"),t("#me-dock-width").val(e.dock?.width??"");for(const s of["collapsible","collapsed","autohide","hideOnScroll"]){const w=t(`#me-dock-${s}`).get(0);w&&(w.checked=e.dock?.[s]===!0)}t("#me-class").val(e.class||""),t("#me-highlight-hover").val(e.highlight?.hover||""),t("#me-highlight-active").val(e.highlight?.active||"");const p=e.highlight?.accent||"";U.val(p.startsWith("#")?"__custom__":p),p.startsWith("#")&&_.val(p),Re();{const s=t("#me-entrance-enabled").get(0);s&&(s.checked=e.entrance?.enabled===!0)}t("#me-entrance-animation").val(nt.includes(e.entrance?.animation)?e.entrance.animation:nt[0]),t("#me-entrance-stagger").val(e.entrance?.stagger??""),t("#me-entrance-duration").val(e.entrance?.duration??""),t("#me-fontFamily").val(e.style?.fontFamily||""),t("#me-fontSize").val(e.style?.fontSize||""),t("#me-fontWeight").val(e.style?.fontWeight||""),t("#me-letterSpacing").val(e.style?.letterSpacing||""),t("#me-iconSize").val(e.style?.iconSize||""),Be.val(e.binding?.slot||"");const g=new Set(e.binding?.projects||[]);Array.from(Ae.get(0).options).forEach(s=>{s.selected=g.has(s.value)}),f.val((e.binding?.pages||[]).join(`
|
|
30
|
-
`)),t("#me-slug").val(e.slug||""),t("#me-name").val(e.name||""),t("#me-description").val(e.description||""),t("#me-project").val(e.meta?.project||Ee||""),Se(),F(),ye()}const $=e=>!!(t(e).get(0)||{}).checked;function ne(){const e={variant:t("#me-variant").val(),position:t("#me-position").val(),appearOnHover:$("#me-appearOnHover"),floatAnchor:t("#me-float-anchor").val(),floatOffsetX:t("#me-float-offsetX").val(),floatOffsetY:t("#me-float-offsetY").val(),floatWidth:t("#me-float-width").val(),floatRadius:t("#me-float-radius").val(),floatShadow:t("#me-float-shadow").val(),floatAccent:Ne(),floatOpacity:t("#me-float-opacity").val(),floatTitle:t("#me-float-title").val(),floatCollapsible:$("#me-float-collapsible"),floatCollapsed:$("#me-float-collapsed"),floatMovable:$("#me-float-movable"),floatResizable:$("#me-float-resizable"),highlightHover:t("#me-highlight-hover").val(),highlightActive:t("#me-highlight-active").val(),highlightAccent:Ze(),class:t("#me-class").val(),entranceEnabled:$("#me-entrance-enabled"),entranceAnimation:t("#me-entrance-animation").val(),entranceStagger:t("#me-entrance-stagger").val(),entranceDuration:t("#me-entrance-duration").val(),orientation:t("#me-orientation").val(),align:t("#me-align").val(),side:t("#me-side").val(),fontFamily:t("#me-fontFamily").val(),fontSize:t("#me-fontSize").val(),fontWeight:t("#me-fontWeight").val(),letterSpacing:t("#me-letterSpacing").val(),iconSize:t("#me-iconSize").val(),dockWidth:t("#me-dock-width").val(),dockCollapsible:$("#me-dock-collapsible"),dockCollapsed:$("#me-dock-collapsed"),dockAutohide:$("#me-dock-autohide"),mobileHamburgerOnly:$("#me-mobile-hamburger"),mobileHideBrand:$("#me-mobile-brand"),lookBar:t("#me-look-bar").val(),lookDropdown:t("#me-look-dropdown").val(),lookBrand:t("#me-look-brand").val(),lookStrip:t("#me-look-strip").val(),lookTiles:t("#me-look-tiles").val(),lookMorph:$("#me-look-morph"),lookShrink:$("#me-look-shrink"),dockHideOnScroll:$("#me-dock-hideOnScroll")},o={slug:t("#me-slug").val(),name:t("#me-name").val(),description:t("#me-description").val(),project:t("#me-project").val()};return
|
|
31
|
-
`).map(a=>a.trim()).filter(Boolean);o.includes(e)||o.push(e),
|
|
32
|
-
`)),F(),
|
|
27
|
+
</div>`),w(m.id)}}w(null),n.innerHTML=l.join("")||'<p class="mex-note">No items yet - Add item, or right-click an item for more.</p>',I.scan(n)}function b(n,u={},...i){const r=document.createElement(n);Object.assign(r,u);for(const l of i)l!=null&&r.append(l);return r}function k(n,u,i){const r=b("label",{className:"form-field"},b("span",{className:"form-label",textContent:n}),u);return i&&r.append(b("small",{className:"form-hint",textContent:i,style:"display:block;font-size:.78em;opacity:.7;margin-top:.25rem;line-height:1.35"})),r}function nn(n,u){const i=[["","- Everyone -"],["public","Public - anyone, incl. logged-out"],...(u||[]).map(l=>[l.name,`${l.label||l.name} and above`]),...(u||[]).filter(l=>l.level!==0).map(l=>[`=${l.name}`,`${l.label||l.name} only`]),["private","Private - super-admin only"]];n&&!i.some(([l])=>l===n)&&i.push([n,`${n} (custom)`]);const r=K(i,n||"");return r.name="visibility",r}function K(n,u){const i=b("select",{className:"form-select"});return n.forEach(([r,l])=>i.add(new Option(l,r))),i.value=u??"",i}function Pe(n,u){const i=typeof u=="string"&&u.startsWith("#"),r=K([["","- none -"],["primary","primary"],["success","success"],["danger","danger"],["warning","warning"],["info","info"],["neutral","neutral"],["__custom__","Custom\u2026"]],i?"__custom__":u||""),l=b("input",{type:"color",className:"form-input me-colour-hex",value:i?u:"#000000"});l.style.display=i?"":"none",r.addEventListener("change",()=>{l.style.display=r.value==="__custom__"?"":"none"});const w=k(n,r);return w.append(l),w._read=()=>r.value==="__custom__"?l.value:r.value,w}function on(n,u){const i=b("input",{className:"form-input me-icon",name:"icon",value:u||""}),r=b("span",{className:"me-icon-preview"});u&&r.setAttribute("data-icon",u);const l=b("button",{type:"button",className:"btn btn-sm btn-ghost",textContent:"Browse\u2026"}),w=L=>{L?r.setAttribute("data-icon",L):r.removeAttribute("data-icon"),Domma.icons.scan(r.parentNode||r)};return l.addEventListener("click",()=>$t(L=>{i.value=L,w(L)})),i.addEventListener("input",()=>w(i.value.trim())),k(n,b("div",{className:"me-icon-field"},i,l,r))}function Q(...n){return b("div",{className:"me-field-grid"},...n)}async function an(n,u,i,r,l,{cards:w=!0}={}){const L=n.text||n.tool?.label||"",m=p=>b("h4",{className:"me-form-section",textContent:p}),G=k("Label",b("input",{className:"form-input",name:"text",value:n.text||""}),"The text shown for this item."),C=b("input",{className:"form-input",name:"url",value:n.url||""}),V=k("URL",C,n.tool||n.locked?"A Tool's address is its identity, so it cannot be changed here.":"Where it links. Must start with /, #, https://, or mailto: . Leave blank for a parent that only groups sub-items.");(n.tool||n.locked)&&(C.readOnly=!0);const N=on("Icon",n.icon||""),f=nn(n.visibility||"",r),le=k("Visibility",f,n.locked?"Always in the sidebar for everyone who may use it, so it cannot be restricted here.":'Who can see this item. "Role and above" shows it to that role and anyone more senior; "Role only" shows it to holders of that role alone (and the super-admin). Blank = everyone.');n.locked&&(f.disabled=!0);const Z=K([["","- no permission gate -"],...(u||[]).map(p=>[p.key,p.label||p.key])],n.permission||"");Z.name="permission";const se=k("Permission",Z,n.tool||n.locked?"A Tool is guarded by its own permission, which always applies.":"Hide unless the viewer holds this specific permission. Independent of Visibility - both must pass.");(n.tool||n.locked)&&(Z.disabled=!0);const ee=k("Badge text",b("input",{className:"form-input",name:"badgeText",value:n.badge?.text||""})),ie=K([["","- none -"],...(i||[]).map(p=>[p.slug,p.title||p.slug])],n.badge?.countFrom||"");ie.name="badgeCount";const ve=k("Badge count from",ie,"Show a live count of this collection\u2019s entries as the badge."),te=Pe("Badge colour",n.badge?.variant||""),be=K([["link","Link"],["pill","Pill"]],n.pill?"pill":"link");be.name="renderAs";const P=k("Render as",be,"Pill draws the item as a rounded chip. Pill styling applies to top-level navbar items."),$e=k("Pill style",K([["filled","Filled"],["outline","Outline"]],n.pill?.style||"filled"));$e.querySelector("select").name="pillStyle";const Ce=Pe("Pill colour",n.pill?.variant||""),h=b("div",{className:"me-pill-wrap"},Q($e,Ce));h.style.display=n.pill?"":"none",be.addEventListener("change",()=>{h.style.display=be.value==="pill"?"":"none"});const j=Pe("Text colour",n.colour||""),re=K([["","- use the menu default -"],...dt.map(p=>[p,mt[p]||p])],n.highlight?.hover||"");re.name="hlHover";const je=k("Hover effect",re,"Overrides the menu-wide hover effect for this item only."),Le=K([["","- use the menu default -"],...ut.map(p=>[p,pt[p]||p])],n.highlight?.active||"");Le.name="hlActive";const U=k("When selected",Le,"How this item is marked when it points at the page being viewed."),T=Pe("Highlight colour",n.highlight?.accent||""),ye=b("input",{className:"form-input",name:"class",value:n.class||"",placeholder:"nav-cta push-right",autocomplete:"off",spellcheck:!1}),O=k("CSS classes",ye,"Your own class names, separated by spaces, added to this item's <li> - target the link with .your-class > a. Style them in Custom CSS."),Y=b("small",{className:"text-danger",style:"display:none;margin-top:.25rem"});O.append(Y);const ce=b("textarea",{className:"form-input",name:"description",rows:2,maxLength:Lt,value:n.description||"",placeholder:"Answers every new lead in under a minute."}),de=k("Description",ce,"One short line under the label, shown when the menu's dropdowns are Cards."),Te=Pe("Tile colour",n.tile||""),_e=b("input",{type:"checkbox",name:"chip",checked:!!n.chip,style:"width:auto;margin:0"}),Fe=b("label",{className:"form-field",style:"display:flex;gap:.5rem;align-items:center;font-weight:400"},_e,b("span",{textContent:"Show as a small chip in the card's footer strip"})),ue=n.parentId==null,x=n.panel||{},B=(p,F,he,Ee=80)=>b("input",{className:"form-input",name:p,value:F||"",placeholder:he,maxLength:Ee}),ke=k("Card heading",B("panelHeading",x.heading,"The crew - six agents, one job each"),"Small capitals over the entries."),Me=k("Link text",B("panelLinkText",x.link?.text,"Meet the crew",60)),ze=k("Link URL",B("panelLinkUrl",x.link?.url,"/crew",500)),Ie=K([["","Automatic"],["1","1"],["2","2"],["3","3"],["4","4"]],x.columns?String(x.columns):"");Ie.name="panelColumns";const Ue=k("Columns",Ie),Je=k("Chip strip label",B("panelChipsLabel",x.chipsLabel,"Add to your crew",60),'Words before the chips - tick "Show as a small chip" on the sub-items that belong there.'),me=K([["","- no side box -"],["dark","Dark"],["light","Light"],["accent","Theme colour"]],x.feature?x.feature.tone||"dark":"");me.name="featureTone";const Qe=k("Side box",me,"A box down the right of the card - a call to action or a short list."),We=k("Box small heading",B("featureEyebrow",x.feature?.eyebrow,"Free - 20 minutes",60)),Ke=k("Box title",B("featureTitle",x.feature?.title,"See it live")),qe=k("Box points",b("textarea",{className:"form-input",name:"featurePoints",rows:3,value:(x.feature?.points||[]).join(`
|
|
28
|
+
`),placeholder:"One per line, up to six"})),t=k("Button text",B("featureBtnText",x.feature?.button?.text,"Book a demo",60)),we=k("Button URL",B("featureBtnUrl",x.feature?.button?.url,"/book",500)),pe=b("div",{},Q(We,Ke),qe,Q(t,we));pe.style.display=me.value?"":"none",me.addEventListener("change",()=>{pe.style.display=me.value?"":"none"});const He=w?[m("Card dropdown"),b("small",{className:"form-hint",style:"display:block;opacity:.7;margin-bottom:.4rem",textContent:"Used when the menu's Dropdown style is Cards (Behaviour tab)."}),de,Q(Te,ue?null:Fe),...ue?[ke,Q(Me,ze),Q(Ue,Je),Qe,pe]:[]]:[],Ne=b("button",{className:"btn btn-primary",textContent:"Apply",title:"Apply to the item (Ctrl+Enter) - Save the menu to keep it"}),W=b("div",{className:"me-item-form"},m("Link"),G,V,N,m("Access"),Q(le,se),m("Badge"),Q(ee,te),ve,m("Appearance"),P,h,j,m("Highlighting"),Q(je,U),T,...He,m("Styling"),O,Ne),_=E.slideover({title:L?`Edit ${De(L)}`:"Edit menu item",size:"lg",position:"right"});_.element.appendChild(W);const Oe=()=>JSON.stringify([...W.querySelectorAll("input, select, textarea")].map(p=>p.type==="checkbox"?p.checked:p.value)),Ze=Oe();let Se=!1,xe=!1;jt(_);const Re=_.close.bind(_);_.close=async p=>{if(!Se&&p!==!0&&Oe()!==Ze){if(xe)return;xe=!0;const F=await E.confirm("Discard the changes to this item?");if(xe=!1,!F)return}return Se=!0,Re()},W.addEventListener("keydown",p=>{(p.ctrlKey||p.metaKey)&&p.key==="Enter"&&(p.preventDefault(),Ne.click())}),_.open();const Ae=_.element.closest(".dm-slideover")||_.element.querySelector(".dm-slideover")||_.element;Ae.style.setProperty("width",Jt+"px","important"),Ae.style.setProperty("max-width","95vw","important"),Domma.icons.scan(W);const g=p=>W.querySelector(`[name="${p}"]`)?.value||"";Ne.addEventListener("click",()=>{const p=ht(g("class"));if(p.length){Y.textContent=p.join("; "),Y.style.display="block",ye.focus();return}n.class=At(g("class")),n.text=g("text"),n.icon=g("icon"),n.locked||(n.visibility=g("visibility")),!n.tool&&!n.locked?(n.url=g("url"),n.permission=g("permission")):n.touched=!0;const F=g("badgeText"),he=g("badgeCount"),Ee=te._read();if(n.badge=F||he?{...F&&{text:F},...he&&{countFrom:he},...Ee&&{variant:Ee}}:null,g("renderAs")==="pill"){const oe=Ce._read();n.pill={style:g("pillStyle")||"filled",...oe&&{variant:oe}}}else n.pill=null;n.colour=j._read();const fe=g("hlHover"),$=g("hlActive"),ne=T._read();if(n.highlight=fe||$||ne?{...fe&&{hover:fe},...$&&{active:$},...ne&&{accent:ne}}:null,w&&(n.description=g("description").trim(),n.tile=Te._read(),n.chip=!ue&&_e.checked,ue)){const oe=g("featureTone");n.panel=Ct({heading:g("panelHeading"),link:{text:g("panelLinkText"),url:g("panelLinkUrl")},columns:g("panelColumns"),chipsLabel:g("panelChipsLabel"),feature:oe?{tone:oe,eyebrow:g("featureEyebrow"),title:g("featureTitle"),points:g("featurePoints").split(`
|
|
29
|
+
`),button:{text:g("featureBtnText"),url:g("featureBtnUrl")}}:null})}Se=!0,_.close(!0),l()})}let q=null,bt=!1;function ln(){bt||!window.R?.use||(bt=!0,R.use((n,u,i)=>{const r=u?.path||"";return!(Zt.test(r)&&(n?.path||"")!==r)||!q?.dirty()?i():E.confirm(`You have unsaved changes to the menu "${De(q.name())}". Leave anyway? They are kept as a draft for your next visit.`).then(w=>{w?i():history.replaceState(null,"",`#${r}`)})}),M.subscribe("router:afterChange",({to:n}={})=>{if(q&&(n?.path||"")!==q.path){const u=q;q=null,u.dispose()}}))}function yt(n,u){const i=n.querySelector(".dm-plugin-banner-name"),r=i?.querySelector(".dm-plugin-banner-sep");if(!(!i||!r)){for(;r.nextSibling;)r.nextSibling.remove();i.appendChild(document.createTextNode(u))}}export const menuEditorView={templateUrl:"/admin/js/templates/menu-editor.html",async onMount(n){if(ln(),q){const e=q;q=null,e.dispose()}const u=[],i=e=>(u.push(e),e);let r=!1;const l=n.get(0).querySelector(".mex")||n.get(0),w=()=>!r&&l.isConnected,L=location.hash.replace(/^#/,""),m=location.hash.match(/^#\/menus\/edit\/([^?#]+)/),G=m?decodeURIComponent(m[1]):null,C=!G,V=`menu-editor.draft.${G||"new"}`,N=E.loader(n.get(0),{type:"dots"});let f,le,Z,se,ee,ie,ve;try{if(C)f={slug:"",name:"",description:"",items:[]};else try{f=await D.menus.get(G)}catch(e){E.toast(`Failed to load menu: ${e.message||e}`,{type:"error"}),location.hash="#/menus";return}[le,Z,se,ee,ie,ve]=await Promise.all([D.projects.list().catch(()=>[]),H.get("/api/auth/permissions-registry").catch(()=>({resources:[]})),D.collections.list().catch(()=>[]),D.get("/collections/roles/entries?limit=100").catch(()=>null),D.menuLocations.registry().catch(()=>[]),D.pages.list().catch(()=>[])])}finally{N.destroy()}if(!l.isConnected)return;const be=(Array.isArray(ee)?ee:ee?.entries||[]).map(e=>({name:e.data?.name,label:e.data?.label,level:e.data?.level??99})).filter(e=>e.name).sort((e,o)=>o.level-e.level),P=!C&&G===Qt;let $e=[];if(P&&($e=await H.get("/api/sidebar/registered-items").catch(()=>(E.toast("Could not load the Tools - they are not shown, and saving keeps their places as they are.",{type:"warning",duration:8e3}),[])),!l.isConnected))return;const Ce=e=>ft(P?It(_t(e||[],$e,{forEditor:!0}),{leaveInHiddenFolders:!0}):e);let h=Ce(f.items);const j=M.observable(!1),re=M.observable(!1),je=M.observable(""),Le=M.observable(0);let U="";const T=l.querySelector("#me-items-list");function ye(){tn(T,h,P),Le.value=h.filter(e=>!ge(e)).length}const O=()=>{ye(),X()},Y=()=>P?vt(h):new Set,ce=e=>h.find(o=>o.id===e);function de(e){requestAnimationFrame(()=>T.querySelector(`.menu-tree-row[data-id="${CSS.escape(e)}"]`)?.focus())}function Te(e){const o=v=>h.find(s=>!s.type&&!s.url&&!s.tool&&(s.key||"").toLowerCase()===v)||h.find(s=>!s.type&&!s.url&&!s.tool&&!s.key&&(s.text||"").toLowerCase()===v);let a=o(String(e.tool.folder||"").toLowerCase())||o(Bt);const c=String(e.tool.group||"").trim();if(a&&c){const v=Nt(a.key||Ot(a.text),c),s=d=>d!==e&&!d.type&&!d.url&&!d.tool;let y=h.find(d=>s(d)&&(d.key||"").toLowerCase()===v)||h.find(d=>s(d)&&d.parentId===a.id&&(!d.key||d.key.toLowerCase()===v)&&(d.text||"").toLowerCase()===c.toLowerCase());y||([y]=ft([{text:c,key:v,icon:Rt,synthesised:!0}],a.id),h.push(y)),a=y}e.parentId=a?a.id:null,h.splice(h.indexOf(e),1),h.push(e),Object.assign(e,{text:e.tool.defaults.text,icon:e.tool.defaults.icon,visibility:"",hidden:!1,badge:null,pill:null,colour:"",highlight:null,touched:!1}),e.tool={...e.tool,persisted:!1}}const _e=e=>!Y().has(e.id)&&!(e.tool&&!e.tool.orphan);async function Fe(e){const o=ce(e);if(!o||!_e(o))return;const a=d=>{let J=d;for(;J;){if(J.id===e)return!0;J=h.find(z=>z.id===J.parentId)}return!1},c=h.filter(d=>d.id!==e&&a(d));if(c.length&&!await E.confirm(`Remove "${De(o.text||o.type||"this item")}" and the ${c.length} item${c.length===1?"":"s"} beneath it?`))return;const v=Y(),s=c.filter(d=>d.tool),y=c.filter(d=>!d.tool&&v.has(d.id));h=h.filter(d=>!a(d)||s.includes(d)||y.includes(d));for(const d of y)d.parentId=o.parentId??null;for(const d of s)d.tool.orphan?d.parentId=o.parentId??null:Te(d);O()}function ue(e){const o=ce(e);!o||ge(o)||Y().has(e)||(o.hidden=!o.hidden,o.touched=!0,O())}function x(e){const o=ce(e);!o||ge(o)||(o.locked=P&&Y().has(o.id),an(o,Z.resources||[],se,be,()=>{O(),de(e)},{cards:!P}))}function B(e){const o={id:Ve(),parentId:null,...e==="separator"||e==="theme-switcher"?{type:e}:e==="spacer"?{type:e,size:"md"}:{text:"",url:"/",icon:"",visibility:"",permission:"",hidden:!1,bundled:!1}};h.push(o),O(),ge(o)?de(o.id):x(o.id)}const ke=e=>o=>{e(o)&&(O(),de(o))},Me=ke(e=>gt(h,e,-1)),ze=ke(e=>gt(h,e,1)),Ie=ke(e=>Wt(h,e)),Ue=ke(e=>Vt(h,e));function Je(e){const o=ce(e);if(!o)return[];const a=ge(o),c=Y().has(e),v=Yt(h,e),s=v.findIndex(z=>z.id===e),y=o.tool,d=y&&(y.persisted||o.touched),J=z=>()=>{const Be=Kt(h,e,z,Ve());O(),Be&&!ge(Be)?x(Be.id):Be&&de(Be.id)};return[{header:a?{separator:"Separator",spacer:"Spacer","theme-switcher":"Theme switcher"}[o.type]:o.text||y?.label||"(untitled)"},...a?[]:[{label:"Edit\u2026",icon:"edit",action:()=>x(e)}],{type:"divider"},{label:"Add item below",icon:"plus",action:J("item")},{label:"Add sub-item",icon:"corner-down-right",disabled:a,action:()=>{const z=Mt(h,e,Ve());O(),z&&x(z.id)}},{label:"Add separator below",icon:"minus",action:J("separator")},{label:"Add spacer below",icon:"move",action:J("spacer")},...P?[]:[{label:"Add theme switcher below",icon:"palette",action:J("theme-switcher")}],{type:"divider"},{label:"Move up",icon:"arrow-up",shortcut:"Alt+\u2191",disabled:s<=0,action:()=>Me(e)},{label:"Move down",icon:"arrow-down",shortcut:"Alt+\u2193",disabled:s<0||s>=v.length-1,action:()=>ze(e)},{label:"Indent",icon:"chevron-right",shortcut:"Alt+\u2192",disabled:s<=0||ge(v[s-1]),action:()=>Ie(e)},{label:"Outdent",icon:"chevron-left",shortcut:"Alt+\u2190",disabled:o.parentId==null,action:()=>Ue(e)},{type:"divider"},...a?[]:[{label:o.hidden?"Show":"Hide",icon:o.hidden?"eye":"eye-off",disabled:c,action:()=>ue(e)}],{label:"Duplicate",icon:"copy",disabled:!!y,action:()=>{const z=Ut(h,e,Ve);O(),z&&de(z.id)}},...y&&!y.orphan&&d?[{label:"Back to its default place",icon:"rotate-ccw",action:()=>{Te(o),O()}}]:[],{label:c?"Remove - always in the sidebar":y&&!y.orphan?"Remove - a Tool is hidden, not removed":"Remove\u2026",icon:"trash",danger:!0,disabled:!_e(o),action:()=>Fe(e)}]}const me=E.contextMenu(T,{cursor:"glyph",match:".menu-tree-row",inherit:!1,density:"compact",items:e=>Je(e.dataset.id)});i(()=>me?.destroy?.());const Qe=Tt(T,()=>h,O,{items:".menu-tree-row",handle:".menu-tree-grip"});i(()=>Qe?.destroy());const We=e=>{const o=e.target.closest(".menu-tree-row");if(!o)return;const a=o.dataset.id;if(e.target.closest(".mex-kebab")){at(e.target.closest(".mex-kebab"));return}if(e.target.closest(".me-hidden")){ue(a);return}if(e.target.closest(".me-edit")){x(a);return}e.target.closest("select, .menu-tree-grip, button")||x(a)},Ke=e=>{const o=e.target.closest(".menu-tree-row");if(!o||e.target!==o){o&&e.key==="F10"&&e.shiftKey&&(e.preventDefault(),at(o));return}const a=o.dataset.id;if(e.key==="F10"&&e.shiftKey)e.preventDefault(),at(o);else if(e.key==="Enter")e.preventDefault(),x(a);else if(e.key==="Delete")e.preventDefault(),Fe(a);else if(e.altKey&&e.key==="ArrowUp")e.preventDefault(),Me(a);else if(e.altKey&&e.key==="ArrowDown")e.preventDefault(),ze(a);else if(e.altKey&&e.key==="ArrowRight")e.preventDefault(),Ie(a);else if(e.altKey&&e.key==="ArrowLeft")e.preventDefault(),Ue(a);else if(e.key==="ArrowDown"||e.key==="ArrowUp"){const c=[...T.querySelectorAll(".menu-tree-row")],v=c.indexOf(o)+(e.key==="ArrowDown"?1:-1);c[v]&&(e.preventDefault(),c[v].focus())}},qe=e=>{if(!e.target.matches(".me-spacer-size"))return;const o=ce(e.target.closest(".menu-tree-row")?.dataset.id);o&&(o.size=e.target.value,X())};T.addEventListener("click",We),T.addEventListener("keydown",Ke),T.addEventListener("change",qe),i(()=>{T.removeEventListener("click",We),T.removeEventListener("keydown",Ke),T.removeEventListener("change",qe)});const t=e=>n.find(e),we=t("#me-float-accent"),pe=t("#me-float-accent-hex"),He=()=>{pe.get(0).style.display=we.val()==="__custom__"?"":"none"},Ne=()=>we.val()==="__custom__"?pe.val():we.val(),W=t("#me-highlight-accent"),_=t("#me-highlight-accent-hex"),Oe=()=>{_.get(0).style.display=W.val()==="__custom__"?"":"none"},Ze=()=>W.val()==="__custom__"?_.val():W.val(),Se=()=>{const e=!!(t("#me-entrance-enabled").get(0)||{}).checked;t(".dm-entrance-field").each(function(){this.style.display=e?"":"none"});const o=t("#me-position").val()==="floating";t(".dm-float-field").each(function(){this.style.display=o?"":"none"}),o&&He();const a=t("#me-orientation").val()==="vertical";t(".dm-vertical-field").each(function(){this.style.display=a?"":"none"}),t(".dm-dock-rail-field").each(function(){this.style.display=a&&!o?"":"none"}),t(".dm-dock-bar-field").each(function(){this.style.display=!a&&!o?"":"none"});const c=!!(t("#me-dock-collapsible").get(0)||{}).checked,v=!!(t("#me-dock-autohide").get(0)||{}).checked;t(".dm-dock-collapsed-field").each(function(){this.style.display=c&&!v?"":"none"});const s=!!(t("#me-float-collapsible").get(0)||{}).checked;t(".dm-float-collapsed-field").each(function(){this.style.display=o&&s?"":"none"});const y=!a&&!P;t(".dm-look-field").each(function(){this.style.display=y?"":"none"});const d=y&&t("#me-look-dropdown").val()==="cards";t(".dm-look-cards-field").each(function(){this.style.display=d?"":"none"})},xe=(e,o,a,c)=>{const v=t(e).get(0);v&&(v.innerHTML="",v.add(new Option(c,"")),o.forEach(s=>v.add(new Option(a[s]||s,s))))};xe("#me-highlight-hover",dt,mt,"- theme default -"),xe("#me-highlight-active",ut,pt,"- theme default -");{const e=t("#me-entrance-animation").get(0);e&&(e.innerHTML="",nt.forEach(o=>e.add(new Option(Et[o]||o,o))))}const Re=t("#me-bind-slot"),Ae=t("#me-bind-projects"),g=t("#me-bind-pages");Re.html('<option value="">- not bound -</option>'+(ie||[]).map(e=>`<option value="${A(e.slot)}">${A(e.label||e.slot)} (${A(e.slot)})</option>`).join("")),Ae.html(le.map(e=>`<option value="${A(e.slug)}">${A(e.name||e.slug)}</option>`).join("")),t("#me-bind-page-pick").html('<option value="">- pick a page -</option>'+ve.map(e=>{const o=e.urlPath||e.url||"";return o?`<option value="${A(o)}">${A(e.title||o)} - ${A(o)}</option>`:""}).join(""));const p=()=>zt({slot:Re.val(),projects:Array.from(Ae.get(0).selectedOptions).map(e=>e.value),pagesText:g.val()});function F(){const e=p(),o=t("#me-bind-summary");if(!e){o.html('<span class="text-muted">Not bound - this menu renders only where it is mapped or placed by a shortcode.</span>');return}const a=[];e.projects.length&&a.push(`pages in ${e.projects.map(s=>`<code>${A(s)}</code>`).join(", ")}`),e.pages.length&&a.push(`pages matching ${e.pages.map(s=>`<code>${A(s)}</code>`).join(", ")}`);const c=e.pages.filter(s=>!/^\/[^\s?#*]*(\/\*)?$|^\/\*$/.test(s)),v=c.length?`<div class="text-danger" style="margin-top:.4rem">Not valid page patterns: ${c.map(s=>`<code>${A(s)}</code>`).join(", ")}</div>`:"";o.html(a.length?`Takes over the <code>${A(e.slot)}</code> slot on ${a.join(" and ")}.${v}`:`<span class="text-warning">Slot <code>${A(e.slot)}</code> selected but no project or page chosen - the binding will never match.</span>`)}const{getProjectFromHash:he}=await import("../lib/project-context.js");if(!w())return;const Ee=C?he():null;t("#me-project").html('<option value="">- none -</option>'+le.map(e=>`<option value="${A(e.slug)}">${A(e.name||e.slug)}</option>`).join(""));function fe(e){h=Ce(e.items),t("#me-variant").val(e.variant||""),t("#me-position").val(e.position||"");const o=t("#me-appearOnHover").get(0);o&&(o.checked=e.appearOnHover===!0),t("#me-float-anchor").val(e.float?.anchor||"TL"),t("#me-float-offsetX").val(e.float?.offsetX??""),t("#me-float-offsetY").val(e.float?.offsetY??""),t("#me-float-width").val(e.float?.width??""),t("#me-float-radius").val(e.float?.radius??""),t("#me-float-shadow").val(e.float?.shadow||""),t("#me-float-opacity").val(e.float?.opacity??""),t("#me-float-title").val(e.float?.title||"");for(const[s,y]of[["#me-float-collapsible",e.float?.collapsible],["#me-float-collapsed",e.float?.collapsed],["#me-float-movable",e.float?.movable],["#me-float-resizable",e.float?.resizable]]){const d=t(s).get(0);d&&(d.checked=y===!0)}const a=e.float?.accent||"";we.val(a.startsWith("#")?"__custom__":a),a.startsWith("#")&&pe.val(a),He(),t("#me-orientation").val(e.orientation||""),t("#me-align").val(e.align||""),t("#me-mobile-hamburger").get(0).checked=e.mobile?.hamburgerOnly===!0,t("#me-mobile-brand").get(0).checked=e.mobile?.hideBrand===!0,t("#me-look-bar").val(e.look?.bar||""),t("#me-look-brand").val(e.look?.brand||""),t("#me-look-strip").val(e.look?.strip||""),t("#me-look-dropdown").val(e.look?.dropdown||""),t("#me-look-tiles").val(e.look?.tiles||""),t("#me-look-morph").get(0).checked=e.look?.morph===!0,t("#me-look-shrink").get(0).checked=e.look?.shrink===!0,t("#me-side").val(e.side||"left"),t("#me-dock-width").val(e.dock?.width??"");for(const s of["collapsible","collapsed","autohide","hideOnScroll"]){const y=t(`#me-dock-${s}`).get(0);y&&(y.checked=e.dock?.[s]===!0)}t("#me-class").val(e.class||""),t("#me-highlight-hover").val(e.highlight?.hover||""),t("#me-highlight-active").val(e.highlight?.active||"");const c=e.highlight?.accent||"";W.val(c.startsWith("#")?"__custom__":c),c.startsWith("#")&&_.val(c),Oe();{const s=t("#me-entrance-enabled").get(0);s&&(s.checked=e.entrance?.enabled===!0)}t("#me-entrance-animation").val(nt.includes(e.entrance?.animation)?e.entrance.animation:nt[0]),t("#me-entrance-stagger").val(e.entrance?.stagger??""),t("#me-entrance-duration").val(e.entrance?.duration??""),t("#me-fontFamily").val(e.style?.fontFamily||""),t("#me-fontSize").val(e.style?.fontSize||""),t("#me-fontWeight").val(e.style?.fontWeight||""),t("#me-letterSpacing").val(e.style?.letterSpacing||""),t("#me-iconSize").val(e.style?.iconSize||""),Re.val(e.binding?.slot||"");const v=new Set(e.binding?.projects||[]);Array.from(Ae.get(0).options).forEach(s=>{s.selected=v.has(s.value)}),g.val((e.binding?.pages||[]).join(`
|
|
30
|
+
`)),t("#me-slug").val(e.slug||""),t("#me-name").val(e.name||""),t("#me-description").val(e.description||""),t("#me-project").val(e.meta?.project||Ee||""),Se(),F(),ye()}const $=e=>!!(t(e).get(0)||{}).checked;function ne(){const e={variant:t("#me-variant").val(),position:t("#me-position").val(),appearOnHover:$("#me-appearOnHover"),floatAnchor:t("#me-float-anchor").val(),floatOffsetX:t("#me-float-offsetX").val(),floatOffsetY:t("#me-float-offsetY").val(),floatWidth:t("#me-float-width").val(),floatRadius:t("#me-float-radius").val(),floatShadow:t("#me-float-shadow").val(),floatAccent:Ne(),floatOpacity:t("#me-float-opacity").val(),floatTitle:t("#me-float-title").val(),floatCollapsible:$("#me-float-collapsible"),floatCollapsed:$("#me-float-collapsed"),floatMovable:$("#me-float-movable"),floatResizable:$("#me-float-resizable"),highlightHover:t("#me-highlight-hover").val(),highlightActive:t("#me-highlight-active").val(),highlightAccent:Ze(),class:t("#me-class").val(),entranceEnabled:$("#me-entrance-enabled"),entranceAnimation:t("#me-entrance-animation").val(),entranceStagger:t("#me-entrance-stagger").val(),entranceDuration:t("#me-entrance-duration").val(),orientation:t("#me-orientation").val(),align:t("#me-align").val(),side:t("#me-side").val(),fontFamily:t("#me-fontFamily").val(),fontSize:t("#me-fontSize").val(),fontWeight:t("#me-fontWeight").val(),letterSpacing:t("#me-letterSpacing").val(),iconSize:t("#me-iconSize").val(),dockWidth:t("#me-dock-width").val(),dockCollapsible:$("#me-dock-collapsible"),dockCollapsed:$("#me-dock-collapsed"),dockAutohide:$("#me-dock-autohide"),mobileHamburgerOnly:$("#me-mobile-hamburger"),mobileHideBrand:$("#me-mobile-brand"),lookBar:t("#me-look-bar").val(),lookDropdown:t("#me-look-dropdown").val(),lookBrand:t("#me-look-brand").val(),lookStrip:t("#me-look-strip").val(),lookTiles:t("#me-look-tiles").val(),lookMorph:$("#me-look-morph"),lookShrink:$("#me-look-shrink"),dockHideOnScroll:$("#me-dock-hideOnScroll")},o={slug:t("#me-slug").val(),name:t("#me-name").val(),description:t("#me-description").val(),project:t("#me-project").val()};return qt(e,o,p(),Dt(h),f.meta||{})}const oe=e=>[...Gt(e),...ht(e.class).map(o=>`CSS classes: ${o} (Behaviour tab).`)];let lt=!1,et=null;function X(){if(!U)return;const e=ne();et=e,j.value=Ye(e)!==U,je.value=j.peek()||lt?oe(e).join(" "):""}const Ge=e=>{e.target.closest("#me-items-list")||(e.target.matches("#me-position, #me-orientation, #me-look-dropdown, #me-float-collapsible, #me-dock-collapsible, #me-dock-autohide, #me-entrance-enabled")&&Se(),e.target.matches("#me-float-accent")&&He(),e.target.matches("#me-highlight-accent")&&Oe(),e.target.closest("#me-binding")&&F(),X())};l.addEventListener("input",Ge),l.addEventListener("change",Ge),i(()=>{l.removeEventListener("input",Ge),l.removeEventListener("change",Ge)}),t("#me-bind-page-add").on("click",()=>{const e=t("#me-bind-page-pick").val();if(!e)return;const o=g.val().split(`
|
|
31
|
+
`).map(a=>a.trim()).filter(Boolean);o.includes(e)||o.push(e),g.val(o.join(`
|
|
32
|
+
`)),F(),X()}),fe(f),U=Ye(ne()),X();const st=()=>{try{S.remove?S.remove(V):S.set(V,null)}catch{}};async function tt(){if(re.peek())return;const e=ne(),o=oe(e);if(o.length){lt=!0,X(),E.toast(o.join(" "),{type:"warning",duration:7e3});return}re.value=!0;let a=null;try{if(C)await D.menus.create(e);else if(e.slug!==f.slug){await D.menus.create(e);const c=await D.menuLocations.get();let v=!1;for(const[s,y]of Object.entries(c))y===f.slug&&(c[s]=e.slug,v=!0);v&&await D.menuLocations.save(c),await D.menus.remove(f.slug)}else{const c=await kt(e);if(!c)return;a=c.revision}if(U=Ye(e),j.value=!1,st(),Ft("#/menus"),E.toast("Saved.",{type:"success"}),C||e.slug!==f.slug)location.hash="#/menus/edit/"+encodeURIComponent(e.slug);else{f={...f,...e,revision:a};for(const c of h)c.tool&&c.touched&&(c.tool={...c.tool,persisted:!0});ye(),U=Ye(ne()),X(),yt(n.get(0),e.name||e.slug)}if(P)try{document.dispatchEvent(new CustomEvent("dm:sidebar-rebuild"))}catch{}}catch(c){E.toast(`Save failed: ${c.message||c}`,{type:"error"})}finally{re.value=!1}}async function kt(e){try{return await D.menus.update(f.slug,{...e,revision:f.revision})}catch(a){if(a.status!==409)throw a}if(await E.confirm(`The menu "${De(f.name||f.slug)}" was changed somewhere else after you opened it - in another tab, by someone else, or on the server. Saving now would undo those changes.<br><br><strong>OK</strong> saves yours over them. <strong>Cancel</strong> loads the latest version, and offers your changes back as a draft.`))return D.menus.update(f.slug,e);try{S.set(V,{menu:e,base:U,at:new Date().toISOString()})}catch{}return j.value=!1,location.reload(),null}async function wt(){!j.peek()||!await E.confirm("Throw away the changes you have not saved?")||(fe(f),X())}E.tabs(t("#me-tabs").get(0));const St=M.applyBindings({dirty:j,saving:re,problems:je,itemCount:Le},l,{methods:{save:()=>tt(),discard:()=>wt(),addItem:()=>B("item"),addSeparator:()=>B("separator"),addSpacer:()=>B("spacer"),addSwitcher:()=>B("theme-switcher")}});i(()=>St.dispose()),i(Pt(l,{position:"bottom"})),P&&t("#me-add-switcher").hide(),yt(n.get(0),C?"New menu":f.name||f.slug),ot(n,{id:"menu-editor-back",icon:"arrow-left",title:"All menus",onClick:()=>{location.hash="#/menus"}}),ot(n,{id:"menu-editor-locations",icon:"map-pin",title:"Menu locations",onClick:()=>{location.hash="#/menu-locations"}}),ot(n,{id:"menu-editor-save",icon:"save",title:"Save (Ctrl+S)",onClick:()=>tt()});const it=e=>{if(!(!(e.ctrlKey||e.metaKey)||e.key!=="s")){if(e.preventDefault(),e.stopImmediatePropagation(),document.querySelector(".me-item-form")?.offsetParent){E.toast("Apply the item first (Ctrl+Enter), then Save.",{type:"info"});return}tt()}};document.addEventListener("keydown",it,!0),i(()=>document.removeEventListener("keydown",it,!0));const rt=()=>{if(!(!j.peek()||!et))try{S.set(V,{menu:et,base:U,at:new Date().toISOString()})}catch{}},ct=e=>{j.peek()&&(rt(),e.preventDefault(),e.returnValue="")};window.addEventListener("beforeunload",ct),i(()=>window.removeEventListener("beforeunload",ct)),q={path:L,dirty:()=>j.peek(),name:()=>t("#me-name").val()||f.name||f.slug||"new menu",dispose(){r=!0,rt(),u.splice(0).reverse().forEach(e=>{try{e()}catch{}})}},I.scan(l);let ae=null;try{ae=S.get(V)}catch{}if(ae?.menu){st();const e=ae.base&&ae.base!==U,o=ae.at?` (${Xt(ae.at)})`:"",a=C?"the new menu you started":`the menu "${De(f.name||f.slug)}"`,c=e?`You left unsaved changes to ${a}${o}, but the saved menu has changed since. Restore your version anyway?`:`You left unsaved changes to ${a}${o}. Restore them?`;await E.confirm(c)&&w()&&(fe({...ae.menu,meta:{...f.meta||{},...ae.menu.meta||{}}}),X())}}};
|
package/docs/DommaFeatures.md
CHANGED
|
@@ -350,6 +350,7 @@ headings (Layouts → Page contents settings, and `[contents]` per page).
|
|
|
350
350
|
- **Menus as data** - any number of menus in `config/menus/<slug>.json`, mapped to slots (`navbar`, `footer-primary`, `footer-legal`, plugin slots) in `config/menu-locations.json`. Items can be links, separators, spacers or the theme switcher, with icons, badges and per-item `visibility` (same ladder and `=role` as pages) or `hidden`.
|
|
351
351
|
- Where: **Content > Menus**, **Menu locations**
|
|
352
352
|
- **Drag to move and nest** - in the Menus editor, drag an item by its grip: a line shows where it will land (above or below a row), and an outline shows it will go inside a row (the middle half of the row). The item follows the pointer and the rows glide to their new places when you let go. Esc cancels. Works with a mouse, pen or finger. Built on Domma's `E.sortable` (domma-js 0.45.0); on an older Domma core the right-click menu, Alt+arrows and buttons still move items.
|
|
353
|
+
- **Safe saving** - the editor sends back the menu's `revision` (a fingerprint of the stored file from `GET /api/menus/:slug`); if the menu changed since it was opened - another tab, another person, a hand edit on the server - `PUT` answers 409 and the editor asks: overwrite, or load the latest with your changes kept as a draft. `menuRevision()` in `server/services/menus.js`.
|
|
353
354
|
- **Bindings** - a menu can take over a slot only on chosen pages or projects.
|
|
354
355
|
- **Overlay slot** - menus bound to the core `overlay` slot render as floating panels anywhere on the page; floating panels can be collapsible, movable, resizable, with radius, shadow, accent and opacity. Since 0.42.0.
|
|
355
356
|
- **Presentation** - variant, position (sticky, fixed, static, floating), orientation (vertical navbar docks as a side rail), alignment (left, centre, right, spread), and highlight effects for hover (colour, underline, overline, background, uppercase, grow, breathe, glow) and current page (bold, underline, overline, background, darken, pill).
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* GET /api/menus/_count - the sidebar badge (the underscore can never be a slug)
|
|
6
6
|
* GET /api/menus/:slug - full menu
|
|
7
7
|
* POST /api/menus - create
|
|
8
|
-
* PUT /api/menus/:slug - replace
|
|
8
|
+
* PUT /api/menus/:slug - replace (409 if `revision` is given and the menu has changed since)
|
|
9
9
|
* DELETE /api/menus/:slug - delete (409 if mapped to a slot)
|
|
10
10
|
* POST /api/menus/:slug/duplicate - copy as <slug>-copy
|
|
11
11
|
*
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
getLocations,
|
|
27
27
|
getMenu,
|
|
28
28
|
listMenus,
|
|
29
|
+
menuRevision,
|
|
29
30
|
updateMenu
|
|
30
31
|
} from '../../services/menus.js';
|
|
31
32
|
import * as cache from '../../services/cache/index.js';
|
|
@@ -110,7 +111,8 @@ export async function menusRoutes(fastify, opts = {}) {
|
|
|
110
111
|
if (!canSeeArtefact(request.user, menu)) {
|
|
111
112
|
return reply.status(403).send({error: 'Access denied for this project'});
|
|
112
113
|
}
|
|
113
|
-
|
|
114
|
+
// The fingerprint the editor sends back on save (see menuRevision).
|
|
115
|
+
return {...menu, revision: menuRevision(menu)};
|
|
114
116
|
});
|
|
115
117
|
|
|
116
118
|
fastify.post('/menus', canCreate, async (request, reply) => {
|
|
@@ -126,10 +128,12 @@ export async function menusRoutes(fastify, opts = {}) {
|
|
|
126
128
|
|
|
127
129
|
fastify.put('/menus/:slug', canUpdate, async (request, reply) => {
|
|
128
130
|
try {
|
|
129
|
-
const
|
|
131
|
+
const {revision, ...body} = request.body || {};
|
|
132
|
+
const menu = await updateMenu(request.params.slug, body, {baseRevision: revision});
|
|
130
133
|
await cache.invalidateTags([`menu:${menu.slug}`, 'nav']);
|
|
131
|
-
return menu;
|
|
134
|
+
return {...menu, revision: menuRevision(menu)};
|
|
132
135
|
} catch (err) {
|
|
136
|
+
if (err.code === 'MENU_STALE') return reply.status(409).send({error: err.message, stale: true});
|
|
133
137
|
const status = /not found/.test(err.message) ? 404 : 400;
|
|
134
138
|
return reply.status(status).send({error: err.message});
|
|
135
139
|
}
|
package/server/services/menus.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* concept from the map) is held in process memory and seeded by core +
|
|
9
9
|
* plugin hooks at startup.
|
|
10
10
|
*/
|
|
11
|
+
import {createHash} from 'crypto';
|
|
11
12
|
import fs from 'fs/promises';
|
|
12
13
|
import path from 'path';
|
|
13
14
|
import {fileURLToPath} from 'url';
|
|
@@ -645,18 +646,42 @@ export async function createMenu(input) {
|
|
|
645
646
|
return menu;
|
|
646
647
|
}
|
|
647
648
|
|
|
649
|
+
/**
|
|
650
|
+
* A short fingerprint of a menu as stored. The editor sends back the one it
|
|
651
|
+
* loaded, so a save over a menu changed since - in another tab, by someone
|
|
652
|
+
* else, or by hand on the server - is caught instead of quietly undone. It
|
|
653
|
+
* hashes the whole file, not meta.updatedAt, because a hand edit leaves that
|
|
654
|
+
* date alone.
|
|
655
|
+
*
|
|
656
|
+
* @param {object} menu
|
|
657
|
+
* @returns {string}
|
|
658
|
+
*/
|
|
659
|
+
export function menuRevision(menu) {
|
|
660
|
+
return createHash('sha256').update(JSON.stringify(menu)).digest('hex').slice(0, 16);
|
|
661
|
+
}
|
|
662
|
+
|
|
648
663
|
/**
|
|
649
664
|
* Replace an existing menu. Preserves meta.createdAt; updates meta.updatedAt.
|
|
650
665
|
* The slug in the path wins over any slug in the body.
|
|
651
666
|
*
|
|
667
|
+
* `opts.baseRevision` is the revision the caller loaded (menuRevision); when
|
|
668
|
+
* given and the menu has changed since, nothing is written and the error
|
|
669
|
+
* carries `code: 'MENU_STALE'`. Leave it out to overwrite regardless.
|
|
670
|
+
*
|
|
652
671
|
* @param {string} slug
|
|
653
672
|
* @param {object} input
|
|
673
|
+
* @param {{baseRevision?: string}} [opts]
|
|
654
674
|
* @returns {Promise<object>} the persisted menu
|
|
655
|
-
* @throws {Error} if the menu does not exist
|
|
675
|
+
* @throws {Error} if the menu does not exist, or has changed since baseRevision
|
|
656
676
|
*/
|
|
657
|
-
export async function updateMenu(slug, input) {
|
|
677
|
+
export async function updateMenu(slug, input, opts = {}) {
|
|
658
678
|
const existing = await getMenu(slug);
|
|
659
679
|
if (!existing) throw new Error(`Menu "${slug}" not found`);
|
|
680
|
+
if (opts.baseRevision && opts.baseRevision !== menuRevision(existing)) {
|
|
681
|
+
const err = new Error(`Menu "${slug}" has changed since it was opened`);
|
|
682
|
+
err.code = 'MENU_STALE';
|
|
683
|
+
throw err;
|
|
684
|
+
}
|
|
660
685
|
const binding = normaliseBinding(input.binding);
|
|
661
686
|
const errors = validateMenu({...input, slug, binding});
|
|
662
687
|
if (errors.length) {
|