@panneau/core 5.0.0-alpha.1 → 5.0.0-alpha.11
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/README.md +2 -0
- package/dist/commands.d.ts +38 -0
- package/dist/commands.d.ts.map +1 -0
- package/dist/commands.js +22 -0
- package/dist/commands.js.map +1 -0
- package/dist/definition/abilities.d.ts +15 -4
- package/dist/definition/abilities.d.ts.map +1 -1
- package/dist/definition/abilities.js +18 -5
- package/dist/definition/abilities.js.map +1 -1
- package/dist/definition/form.d.ts +3 -2
- package/dist/definition/form.d.ts.map +1 -1
- package/dist/definition/form.js +24 -2
- package/dist/definition/form.js.map +1 -1
- package/dist/definition/itemActions.d.ts +53 -0
- package/dist/definition/itemActions.d.ts.map +1 -0
- package/dist/definition/itemActions.js +64 -0
- package/dist/definition/itemActions.js.map +1 -0
- package/dist/definition/list.d.ts +16 -0
- package/dist/definition/list.d.ts.map +1 -1
- package/dist/definition/list.js +64 -3
- package/dist/definition/list.js.map +1 -1
- package/dist/definition/resource.d.ts +2 -2
- package/dist/definition/resource.js +1 -1
- package/dist/definition/resource.js.map +1 -1
- package/dist/form/form.d.ts +8 -3
- package/dist/form/form.d.ts.map +1 -1
- package/dist/form/form.js +1 -0
- package/dist/form/form.js.map +1 -1
- package/dist/form/messages.d.ts.map +1 -1
- package/dist/form/messages.js +1 -0
- package/dist/form/messages.js.map +1 -1
- package/dist/form/problem.d.ts +1 -1
- package/dist/form/problem.js.map +1 -1
- package/dist/form/selectors.d.ts +1 -1
- package/dist/form/selectors.js.map +1 -1
- package/dist/form/store.d.ts +1 -1
- package/dist/form/store.js.map +1 -1
- package/dist/index.d.ts +20 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/list/list.d.ts +13 -2
- package/dist/list/list.d.ts.map +1 -1
- package/dist/list/list.js +37 -9
- package/dist/list/list.js.map +1 -1
- package/dist/list/router.js.map +1 -1
- package/dist/plugins.d.ts +22 -10
- package/dist/plugins.d.ts.map +1 -1
- package/dist/plugins.js +23 -3
- package/dist/plugins.js.map +1 -1
- package/dist/preview.d.ts +14 -0
- package/dist/preview.d.ts.map +1 -0
- package/dist/react/leaveGuards.d.ts +34 -0
- package/dist/react/leaveGuards.d.ts.map +1 -0
- package/dist/react/leaveGuards.js +84 -0
- package/dist/react/leaveGuards.js.map +1 -0
- package/dist/react/provider.d.ts +4 -4
- package/dist/react/provider.js +3 -1
- package/dist/react/provider.js.map +1 -1
- package/dist/react/useForm.d.ts +1 -1
- package/dist/react/useForm.js.map +1 -1
- package/dist/react/useList.d.ts +1 -1
- package/dist/react/useList.js.map +1 -1
- package/dist/values.d.ts +1 -1
- package/dist/values.d.ts.map +1 -1
- package/dist/values.js +5 -1
- package/dist/values.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -30,6 +30,8 @@ function EditPage({ item, onSave }) {
|
|
|
30
30
|
|
|
31
31
|
`definePlugin()` declares what a plugin adds, keyed by the names the definition uses: `fields`, `displays`, `listLayouts`, `actions` (handlers of `client` actions), `pages`, `widgets`, `components` (layout nodes), `locales` and `themes`.
|
|
32
32
|
|
|
33
|
+
`resolveTheme(definition.theme, registry.themes)` returns the theme of the admin: the themes of plugins that the definition's theme `extends`, by id, then its own tokens, tones and variants. `Theme` is the theme of `@panneau/ui` as plain data, the `Theme` schema of `@panneau/protocol`.
|
|
34
|
+
|
|
33
35
|
`PanneauProvider` takes plugins, the definition and a router. Nested providers add their plugins to those of their parent, and later plugins override earlier ones, so a project can replace a built-in field. `usePlugins()` returns the merged registry, and `loadMessages(registry, locale)` the merged messages of a locale.
|
|
34
36
|
|
|
35
37
|
## Definition engine
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Definition, Resource } from '@panneau/protocol';
|
|
2
|
+
/**
|
|
3
|
+
* A command of the "Go to…" palette that a plugin adds (decision 0029): a link to follow or a
|
|
4
|
+
* function to run, found by its label, description and keywords.
|
|
5
|
+
*/
|
|
6
|
+
export interface Command {
|
|
7
|
+
/** Unique among the commands of the palette. */
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
/** The group it is listed in. Defaults to the palette's group of commands. */
|
|
11
|
+
group?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
/** More words it is found by. */
|
|
14
|
+
keywords?: readonly string[];
|
|
15
|
+
/** A Lucide icon name (`"send"`). */
|
|
16
|
+
icon?: string;
|
|
17
|
+
/** A path of the app, or a URL, to go to. */
|
|
18
|
+
href?: string;
|
|
19
|
+
/** What it does, without an `href`. */
|
|
20
|
+
perform?: () => void;
|
|
21
|
+
}
|
|
22
|
+
/** What a plugin's commands can depend on. */
|
|
23
|
+
export interface CommandContext {
|
|
24
|
+
/** The definition for the current user, or `null` outside of the app. */
|
|
25
|
+
definition: Definition | null;
|
|
26
|
+
}
|
|
27
|
+
/** Commands, or a function that makes them for the current definition. */
|
|
28
|
+
export type CommandSource = readonly Command[] | ((context: CommandContext) => readonly Command[]);
|
|
29
|
+
/** The commands of every source, in order. */
|
|
30
|
+
export declare function getCommands(sources: readonly CommandSource[], context: CommandContext): Command[];
|
|
31
|
+
/**
|
|
32
|
+
* Whether a resource is in the global search: its `features.search`, or, without it, whether its
|
|
33
|
+
* list has a search (`list.search`), so one setting serves both (decision 0029).
|
|
34
|
+
*/
|
|
35
|
+
export declare function isSearchable(resource: Resource): boolean;
|
|
36
|
+
/** The resources of the global search that the user can list. */
|
|
37
|
+
export declare function getSearchableResources(definition: Definition): Resource[];
|
|
38
|
+
//# sourceMappingURL=commands.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../src/commands.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAK9D;;;GAGG;AACH,MAAM,WAAW,OAAO;IACpB,gDAAgD;IAChD,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,8EAA8E;IAC9E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iCAAiC;IACjC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B,qCAAqC;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,6CAA6C;IAC7C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uCAAuC;IACvC,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACxB;AAED,8CAA8C;AAC9C,MAAM,WAAW,cAAc;IAC3B,yEAAyE;IACzE,UAAU,EAAE,UAAU,GAAG,IAAI,CAAC;CACjC;AAED,0EAA0E;AAC1E,MAAM,MAAM,aAAa,GAAG,SAAS,OAAO,EAAE,GAAG,CAAC,CAAC,OAAO,EAAE,cAAc,KAAK,SAAS,OAAO,EAAE,CAAC,CAAC;AAEnG,8CAA8C;AAC9C,wBAAgB,WAAW,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,EAAE,CAEjG;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAExD;AAED,iEAAiE;AACjE,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,UAAU,GAAG,QAAQ,EAAE,CAIzE"}
|
package/dist/commands.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { can } from "./definition/abilities.js";
|
|
2
|
+
import { isSearchableResource } from "@panneau/protocol/runtime";
|
|
3
|
+
//#region src/commands.ts
|
|
4
|
+
/** The commands of every source, in order. */
|
|
5
|
+
function getCommands(sources, context) {
|
|
6
|
+
return sources.flatMap((source) => typeof source === "function" ? source(context) : source);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Whether a resource is in the global search: its `features.search`, or, without it, whether its
|
|
10
|
+
* list has a search (`list.search`), so one setting serves both (decision 0029).
|
|
11
|
+
*/
|
|
12
|
+
function isSearchable(resource) {
|
|
13
|
+
return isSearchableResource(resource);
|
|
14
|
+
}
|
|
15
|
+
/** The resources of the global search that the user can list. */
|
|
16
|
+
function getSearchableResources(definition) {
|
|
17
|
+
return definition.resources.filter((resource) => isSearchable(resource) && can(resource.abilities, "viewAny"));
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { getCommands, getSearchableResources, isSearchable };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=commands.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commands.js","names":["Definition","Resource","isSearchableResource","can","Command","id","label","group","description","keywords","icon","href","perform","CommandContext","definition","CommandSource","context","getCommands","sources","flatMap","source","isSearchable","resource","getSearchableResources","resources","filter","abilities"],"sources":["../src/commands.ts"],"sourcesContent":["import type { Definition, Resource } from '@panneau/protocol';\nimport { isSearchableResource } from '@panneau/protocol/runtime';\n\nimport { can } from './definition/abilities';\n\n/**\n * A command of the \"Go to…\" palette that a plugin adds (decision 0029): a link to follow or a\n * function to run, found by its label, description and keywords.\n */\nexport interface Command {\n /** Unique among the commands of the palette. */\n id: string;\n label: string;\n /** The group it is listed in. Defaults to the palette's group of commands. */\n group?: string;\n description?: string;\n /** More words it is found by. */\n keywords?: readonly string[];\n /** A Lucide icon name (`\"send\"`). */\n icon?: string;\n /** A path of the app, or a URL, to go to. */\n href?: string;\n /** What it does, without an `href`. */\n perform?: () => void;\n}\n\n/** What a plugin's commands can depend on. */\nexport interface CommandContext {\n /** The definition for the current user, or `null` outside of the app. */\n definition: Definition | null;\n}\n\n/** Commands, or a function that makes them for the current definition. */\nexport type CommandSource = readonly Command[] | ((context: CommandContext) => readonly Command[]);\n\n/** The commands of every source, in order. */\nexport function getCommands(sources: readonly CommandSource[], context: CommandContext): Command[] {\n return sources.flatMap((source) => (typeof source === 'function' ? source(context) : source));\n}\n\n/**\n * Whether a resource is in the global search: its `features.search`, or, without it, whether its\n * list has a search (`list.search`), so one setting serves both (decision 0029).\n */\nexport function isSearchable(resource: Resource): boolean {\n return isSearchableResource(resource);\n}\n\n/** The resources of the global search that the user can list. */\nexport function getSearchableResources(definition: Definition): Resource[] {\n return definition.resources.filter(\n (resource) => isSearchable(resource) && can(resource.abilities, 'viewAny'),\n );\n}\n"],"mappings":";;;;AAoCA,SAAgBiB,YAAYC,SAAmCF,SAAoC;CAC/F,OAAOE,QAAQC,SAASC,WAAY,OAAOA,WAAW,aAAaA,OAAOJ,OAAO,IAAII,MAAO;AAChG;;;;;AAMA,SAAgBC,aAAaC,UAA6B;CACtD,OAAOpB,qBAAqBoB,QAAQ;AACxC;;AAGA,SAAgBC,uBAAuBT,YAAoC;CACvE,OAAOA,WAAWU,UAAUC,QACvBH,aAAaD,aAAaC,QAAQ,KAAKnB,IAAImB,SAASI,WAAW,SAAS,CAC7E;AACJ"}
|
|
@@ -7,15 +7,26 @@ export declare function can(abilities: Abilities | undefined, ability: string):
|
|
|
7
7
|
/** Whether the current user can run an action on the resource (`abilities.run`). */
|
|
8
8
|
export declare function canRunAction(action: Action): boolean;
|
|
9
9
|
/**
|
|
10
|
-
* Whether an action is available on an item: the user can run it, and the item
|
|
11
|
-
*
|
|
10
|
+
* Whether an action is available on an item: the user can run it, and the item allows it.
|
|
11
|
+
* - The built-in item actions need their ability (decision 0040): `view` for `show`, `update` for
|
|
12
|
+
* `edit`, `duplicate` and `delete`. A missing or false ability hides them, and so does their
|
|
13
|
+
* own id set to `false` (`"edit": false`). An item without abilities shows none of them.
|
|
14
|
+
* - Other links are allowed unless the item says otherwise (`"site": false`).
|
|
15
|
+
* - Other actions need their id in the abilities of the item (`"publish": true`).
|
|
16
|
+
* - On an item without abilities (a product's own records), other actions are allowed: the
|
|
17
|
+
* backend decides.
|
|
12
18
|
*/
|
|
13
|
-
export declare function isActionAllowedOn(action: Action, item:
|
|
19
|
+
export declare function isActionAllowedOn(action: Action, item: {
|
|
20
|
+
readonly abilities?: Abilities | undefined;
|
|
21
|
+
}): boolean;
|
|
14
22
|
/** Splits the items of a selection by whether an action is allowed on them. */
|
|
15
23
|
export declare function partitionByAction(action: Action, items: readonly Item[]): {
|
|
16
24
|
allowed: Item[];
|
|
17
25
|
skipped: Item[];
|
|
18
26
|
};
|
|
19
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Where an action appears when it doesn't say: rows and forms (the header of an item, in its
|
|
29
|
+
* panel and its form) for items, the toolbar otherwise.
|
|
30
|
+
*/
|
|
20
31
|
export declare function getActionPlacements(action: Action): ActionPlacement[];
|
|
21
32
|
//# sourceMappingURL=abilities.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abilities.d.ts","sourceRoot":"","sources":["../../src/definition/abilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"abilities.d.ts","sourceRoot":"","sources":["../../src/definition/abilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAGlF;;;GAGG;AACH,wBAAgB,GAAG,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAE9E;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAC7B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE;IAAE,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,GAAG,SAAS,CAAA;CAAE,GACrD,OAAO,CAiBT;AAED,+EAA+E;AAC/E,wBAAgB,iBAAiB,CAC7B,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,SAAS,IAAI,EAAE,GACvB;IAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAAC,OAAO,EAAE,IAAI,EAAE,CAAA;CAAE,CAKtC;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,eAAe,EAAE,CAcrE"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ITEM_ACTION_ABILITIES, isBuiltInItemAction } from "@panneau/protocol/runtime";
|
|
1
2
|
//#region src/definition/abilities.ts
|
|
2
3
|
/**
|
|
3
4
|
* Whether a set of abilities allows something. Without abilities (a backend that sends none),
|
|
@@ -11,13 +12,22 @@ function canRunAction(action) {
|
|
|
11
12
|
return action.abilities?.run !== false;
|
|
12
13
|
}
|
|
13
14
|
/**
|
|
14
|
-
* Whether an action is available on an item: the user can run it, and the item
|
|
15
|
-
*
|
|
15
|
+
* Whether an action is available on an item: the user can run it, and the item allows it.
|
|
16
|
+
* - The built-in item actions need their ability (decision 0040): `view` for `show`, `update` for
|
|
17
|
+
* `edit`, `duplicate` and `delete`. A missing or false ability hides them, and so does their
|
|
18
|
+
* own id set to `false` (`"edit": false`). An item without abilities shows none of them.
|
|
19
|
+
* - Other links are allowed unless the item says otherwise (`"site": false`).
|
|
20
|
+
* - Other actions need their id in the abilities of the item (`"publish": true`).
|
|
21
|
+
* - On an item without abilities (a product's own records), other actions are allowed: the
|
|
22
|
+
* backend decides.
|
|
16
23
|
*/
|
|
17
24
|
function isActionAllowedOn(action, item) {
|
|
18
25
|
if (!canRunAction(action)) return false;
|
|
19
|
-
|
|
20
|
-
return
|
|
26
|
+
const { abilities } = item;
|
|
27
|
+
if (isBuiltInItemAction(action.id)) return abilities?.[ITEM_ACTION_ABILITIES[action.id]] === true && abilities[action.id] !== false;
|
|
28
|
+
if (abilities === void 0) return true;
|
|
29
|
+
if (action.kind === "link") return abilities[action.id] !== false;
|
|
30
|
+
return abilities[action.id] === true;
|
|
21
31
|
}
|
|
22
32
|
/** Splits the items of a selection by whether an action is allowed on them. */
|
|
23
33
|
function partitionByAction(action, items) {
|
|
@@ -29,7 +39,10 @@ function partitionByAction(action, items) {
|
|
|
29
39
|
skipped
|
|
30
40
|
};
|
|
31
41
|
}
|
|
32
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Where an action appears when it doesn't say: rows and forms (the header of an item, in its
|
|
44
|
+
* panel and its form) for items, the toolbar otherwise.
|
|
45
|
+
*/
|
|
33
46
|
function getActionPlacements(action) {
|
|
34
47
|
if (action.placement !== void 0) return action.placement;
|
|
35
48
|
const placements = /* @__PURE__ */ new Set();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abilities.js","names":[],"sources":["../../src/definition/abilities.ts"],"sourcesContent":["import type { Abilities, Action, ActionPlacement, Item } from '@panneau/protocol';\n\n/**\n * Whether a set of abilities allows something. Without abilities (a backend that sends none),\n * everything is allowed and the backend decides. With abilities, only what is `true` is allowed.\n */\nexport function can(abilities: Abilities | undefined, ability: string): boolean {\n return abilities === undefined || abilities[ability] === true;\n}\n\n/** Whether the current user can run an action on the resource (`abilities.run`). */\nexport function canRunAction(action: Action): boolean {\n return action.abilities?.run !== false;\n}\n\n/**\n * Whether an action is available on an item: the user can run it, and the item
|
|
1
|
+
{"version":3,"file":"abilities.js","names":["Abilities","Action","ActionPlacement","Item","ITEM_ACTION_ABILITIES","isBuiltInItemAction","can","abilities","ability","undefined","canRunAction","action","run","isActionAllowedOn","item","id","kind","partitionByAction","items","allowed","skipped","forEach","push","getActionPlacements","placement","placements","Set","scope","add"],"sources":["../../src/definition/abilities.ts"],"sourcesContent":["import type { Abilities, Action, ActionPlacement, Item } from '@panneau/protocol';\nimport { ITEM_ACTION_ABILITIES, isBuiltInItemAction } from '@panneau/protocol/runtime';\n\n/**\n * Whether a set of abilities allows something. Without abilities (a backend that sends none),\n * everything is allowed and the backend decides. With abilities, only what is `true` is allowed.\n */\nexport function can(abilities: Abilities | undefined, ability: string): boolean {\n return abilities === undefined || abilities[ability] === true;\n}\n\n/** Whether the current user can run an action on the resource (`abilities.run`). */\nexport function canRunAction(action: Action): boolean {\n return action.abilities?.run !== false;\n}\n\n/**\n * Whether an action is available on an item: the user can run it, and the item allows it.\n * - The built-in item actions need their ability (decision 0040): `view` for `show`, `update` for\n * `edit`, `duplicate` and `delete`. A missing or false ability hides them, and so does their\n * own id set to `false` (`\"edit\": false`). An item without abilities shows none of them.\n * - Other links are allowed unless the item says otherwise (`\"site\": false`).\n * - Other actions need their id in the abilities of the item (`\"publish\": true`).\n * - On an item without abilities (a product's own records), other actions are allowed: the\n * backend decides.\n */\nexport function isActionAllowedOn(\n action: Action,\n item: { readonly abilities?: Abilities | undefined },\n): boolean {\n if (!canRunAction(action)) {\n return false;\n }\n const { abilities } = item;\n if (isBuiltInItemAction(action.id)) {\n return (\n abilities?.[ITEM_ACTION_ABILITIES[action.id]] === true && abilities[action.id] !== false\n );\n }\n if (abilities === undefined) {\n return true;\n }\n if (action.kind === 'link') {\n return abilities[action.id] !== false;\n }\n return abilities[action.id] === true;\n}\n\n/** Splits the items of a selection by whether an action is allowed on them. */\nexport function partitionByAction(\n action: Action,\n items: readonly Item[],\n): { allowed: Item[]; skipped: Item[] } {\n const allowed: Item[] = [];\n const skipped: Item[] = [];\n items.forEach((item) => (isActionAllowedOn(action, item) ? allowed : skipped).push(item));\n return { allowed, skipped };\n}\n\n/**\n * Where an action appears when it doesn't say: rows and forms (the header of an item, in its\n * panel and its form) for items, the toolbar otherwise.\n */\nexport function getActionPlacements(action: Action): ActionPlacement[] {\n if (action.placement !== undefined) {\n return action.placement;\n }\n const placements = new Set<ActionPlacement>();\n action.scope.forEach((scope) => {\n if (scope === 'item') {\n placements.add('row');\n placements.add('form');\n } else {\n placements.add('toolbar');\n }\n });\n return [...placements];\n}\n"],"mappings":";;;;;;AAOA,SAAgBM,IAAIC,WAAkCC,SAA0B;CAC5E,OAAOD,cAAcE,KAAAA,KAAaF,UAAUC,aAAa;AAC7D;;AAGA,SAAgBE,aAAaC,QAAyB;CAClD,OAAOA,OAAOJ,WAAWK,QAAQ;AACrC;;;;;;;;;;;AAYA,SAAgBC,kBACZF,QACAG,MACO;CACP,IAAI,CAACJ,aAAaC,MAAM,GACpB,OAAO;CAEX,MAAM,EAAEJ,cAAcO;CACtB,IAAIT,oBAAoBM,OAAOI,EAAE,GAC7B,OACIR,YAAYH,sBAAsBO,OAAOI,SAAS,QAAQR,UAAUI,OAAOI,QAAQ;CAG3F,IAAIR,cAAcE,KAAAA,GACd,OAAO;CAEX,IAAIE,OAAOK,SAAS,QAChB,OAAOT,UAAUI,OAAOI,QAAQ;CAEpC,OAAOR,UAAUI,OAAOI,QAAQ;AACpC;;AAGA,SAAgBE,kBACZN,QACAO,OACoC;CACpC,MAAMC,UAAkB,CAAA;CACxB,MAAMC,UAAkB,CAAA;CACxBF,MAAMG,SAASP,UAAUD,kBAAkBF,QAAQG,IAAI,IAAIK,UAAUC,QAAAA,CAASE,KAAKR,IAAI,CAAC;CACxF,OAAO;EAAEK;EAASC;CAAQ;AAC9B;;;;;AAMA,SAAgBG,oBAAoBZ,QAAmC;CACnE,IAAIA,OAAOa,cAAcf,KAAAA,GACrB,OAAOE,OAAOa;CAElB,MAAMC,6BAAa,IAAIC,IAAqB;CAC5Cf,OAAOgB,MAAMN,SAASM,UAAU;EAC5B,IAAIA,UAAU,QAAQ;GAClBF,WAAWG,IAAI,KAAK;GACpBH,WAAWG,IAAI,MAAM;EACzB,OACIH,WAAWG,IAAI,SAAS;CAEhC,CAAC;CACD,OAAO,CAAC,GAAGH,UAAU;AACzB"}
|
|
@@ -15,8 +15,9 @@ export interface ResolvedForm {
|
|
|
15
15
|
}
|
|
16
16
|
export interface ResolveFormOptions {
|
|
17
17
|
/**
|
|
18
|
-
* `create` removes `updateOnly` fields; `update` makes `createOnly` fields read-only.
|
|
19
|
-
* mode,
|
|
18
|
+
* `create` removes `updateOnly` fields; `update` makes `createOnly` fields read-only. Rules
|
|
19
|
+
* limited to a mode (`{ "rule": "required", "mode": "create" }`) are kept, without their
|
|
20
|
+
* `mode`, in that mode only, in nested fields too. Without a mode, every field is kept as is.
|
|
20
21
|
*/
|
|
21
22
|
mode?: FormMode;
|
|
22
23
|
/** The type of the item, among the resource's `types`: its fields are added. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"form.d.ts","sourceRoot":"","sources":["../../src/definition/form.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"form.d.ts","sourceRoot":"","sources":["../../src/definition/form.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAQ,MAAM,mBAAmB,CAAC;AAE3E,qDAAqD;AACrD,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE3C,yFAAyF;AACzF,MAAM,WAAW,YAAY;IACzB,mDAAmD;IACnD,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,2BAA2B;IAC3B,YAAY,EAAE,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACzC;;;OAGG;IACH,MAAM,EAAE,UAAU,EAAE,CAAC;CACxB;AAED,MAAM,WAAW,kBAAkB;IAC/B;;;;OAIG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,gFAAgF;IAChF,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AA2DD,gDAAgD;AAChD,wBAAgB,aAAa,CACzB,MAAM,EAAE,SAAS,KAAK,EAAE,EACxB,MAAM,GAAE,SAAS,UAAU,EAAO,EAClC,EAAE,IAAI,EAAE,GAAE,IAAI,CAAC,kBAAkB,EAAE,MAAM,CAAM,GAChD,YAAY,CAqBd;AAED,kGAAkG;AAClG,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,GAAE,kBAAuB,GAAG,YAAY,CAM9F;AAED,qDAAqD;AACrD,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,SAAS,UAAU,EAAE,GAAG,MAAM,EAAE,CAE3E"}
|
package/dist/definition/form.js
CHANGED
|
@@ -16,15 +16,37 @@ function pruneLayout(nodes, fields) {
|
|
|
16
16
|
}];
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
|
+
/** The rules that apply in the mode, without their `mode`. */
|
|
20
|
+
function resolveRules(rules, mode) {
|
|
21
|
+
return rules.flatMap((rule) => {
|
|
22
|
+
if (rule.mode === void 0) return [rule];
|
|
23
|
+
if (rule.mode !== mode) return [];
|
|
24
|
+
const { mode: _mode, ...resolved } = rule;
|
|
25
|
+
return [resolved];
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** A field with the rules of the mode, and those of its nested fields and block types. */
|
|
29
|
+
function resolveFieldRules(field, mode) {
|
|
30
|
+
const resolved = { ...field };
|
|
31
|
+
if (field.rules !== void 0) resolved.rules = resolveRules(field.rules, mode);
|
|
32
|
+
if (field.fields !== void 0) resolved.fields = field.fields.map((nested) => resolveFieldRules(nested, mode));
|
|
33
|
+
if (field.types !== void 0) resolved.types = field.types.map((type) => ({
|
|
34
|
+
...type,
|
|
35
|
+
fields: type.fields.map((nested) => resolveFieldRules(nested, mode))
|
|
36
|
+
}));
|
|
37
|
+
return resolved;
|
|
38
|
+
}
|
|
19
39
|
/** Resolves fields and a layout into a form. */
|
|
20
40
|
function resolveFields(fields, layout = [], { mode } = {}) {
|
|
21
41
|
const resolved = fields.flatMap((field) => {
|
|
42
|
+
if (mode === void 0) return [field];
|
|
22
43
|
if (mode === "create" && field.updateOnly === true) return [];
|
|
44
|
+
const withRules = resolveFieldRules(field, mode);
|
|
23
45
|
if (mode === "update" && field.createOnly === true) return [{
|
|
24
|
-
...
|
|
46
|
+
...withRules,
|
|
25
47
|
readOnly: true
|
|
26
48
|
}];
|
|
27
|
-
return [
|
|
49
|
+
return [withRules];
|
|
28
50
|
});
|
|
29
51
|
const fieldsByName = new Map(resolved.map((field) => [field.name, field]));
|
|
30
52
|
const pruned = pruneLayout(layout, fieldsByName);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"form.js","names":[],"sources":["../../src/definition/form.ts"],"sourcesContent":["import type { Field, LayoutNode, Resource } from '@panneau/protocol';\n\n/** Whether a form creates an item or updates one. */\nexport type FormMode = 'create' | 'update';\n\n/** A form ready to render: its fields and a layout that references every one of them. */\nexport interface ResolvedForm {\n /** The fields of the form, in definition order. */\n fields: Field[];\n /** The fields, by name. */\n fieldsByName: ReadonlyMap<string, Field>;\n /**\n * The layout tree. Field nodes that name an unknown field are removed, and fields that no\n * node references are appended at the end, as `field` nodes.\n */\n layout: LayoutNode[];\n}\n\nexport interface ResolveFormOptions {\n /**\n * `create` removes `updateOnly` fields; `update` makes `createOnly` fields read-only.
|
|
1
|
+
{"version":3,"file":"form.js","names":["Field","LayoutNode","Resource","Rule","FormMode","ResolvedForm","fields","fieldsByName","ReadonlyMap","layout","ResolveFormOptions","mode","type","referencedNames","nodes","names","Set","forEach","node","name","undefined","add","children","pruneLayout","flatMap","has","resolveRules","rules","rule","_mode","resolved","resolveFieldRules","field","map","nested","types","resolveFields","Pick","updateOnly","withRules","createOnly","readOnly","Map","pruned","referenced","appended","filter","resolveForm","resource","options","typeFields","find","id","form","getLayoutFieldNames"],"sources":["../../src/definition/form.ts"],"sourcesContent":["import type { Field, LayoutNode, Resource, Rule } from '@panneau/protocol';\n\n/** Whether a form creates an item or updates one. */\nexport type FormMode = 'create' | 'update';\n\n/** A form ready to render: its fields and a layout that references every one of them. */\nexport interface ResolvedForm {\n /** The fields of the form, in definition order. */\n fields: Field[];\n /** The fields, by name. */\n fieldsByName: ReadonlyMap<string, Field>;\n /**\n * The layout tree. Field nodes that name an unknown field are removed, and fields that no\n * node references are appended at the end, as `field` nodes.\n */\n layout: LayoutNode[];\n}\n\nexport interface ResolveFormOptions {\n /**\n * `create` removes `updateOnly` fields; `update` makes `createOnly` fields read-only. Rules\n * limited to a mode (`{ \"rule\": \"required\", \"mode\": \"create\" }`) are kept, without their\n * `mode`, in that mode only, in nested fields too. Without a mode, every field is kept as is.\n */\n mode?: FormMode;\n /** The type of the item, among the resource's `types`: its fields are added. */\n type?: string;\n}\n\nfunction referencedNames(nodes: readonly LayoutNode[], names = new Set<string>()): Set<string> {\n nodes.forEach((node) => {\n if (node.type === 'field' && node.name !== undefined) {\n names.add(node.name);\n }\n referencedNames(node.children ?? [], names);\n });\n return names;\n}\n\nfunction pruneLayout(\n nodes: readonly LayoutNode[],\n fields: ReadonlyMap<string, Field>,\n): LayoutNode[] {\n return nodes.flatMap((node) => {\n if (node.type === 'field' && (node.name === undefined || !fields.has(node.name))) {\n return [];\n }\n if (node.children === undefined) {\n return [node];\n }\n return [{ ...node, children: pruneLayout(node.children, fields) }];\n });\n}\n\n/** The rules that apply in the mode, without their `mode`. */\nfunction resolveRules(rules: readonly Rule[], mode: FormMode): Rule[] {\n return rules.flatMap((rule) => {\n if (rule.mode === undefined) {\n return [rule];\n }\n if (rule.mode !== mode) {\n return [];\n }\n const { mode: _mode, ...resolved } = rule;\n return [resolved as Rule];\n });\n}\n\n/** A field with the rules of the mode, and those of its nested fields and block types. */\nfunction resolveFieldRules(field: Field, mode: FormMode): Field {\n const resolved: Field = { ...field };\n if (field.rules !== undefined) {\n resolved.rules = resolveRules(field.rules, mode);\n }\n if (field.fields !== undefined) {\n resolved.fields = field.fields.map((nested) => resolveFieldRules(nested, mode));\n }\n if (field.types !== undefined) {\n resolved.types = field.types.map((type) => ({\n ...type,\n fields: type.fields.map((nested) => resolveFieldRules(nested, mode)),\n }));\n }\n return resolved;\n}\n\n/** Resolves fields and a layout into a form. */\nexport function resolveFields(\n fields: readonly Field[],\n layout: readonly LayoutNode[] = [],\n { mode }: Pick<ResolveFormOptions, 'mode'> = {},\n): ResolvedForm {\n const resolved = fields.flatMap((field) => {\n if (mode === undefined) {\n return [field];\n }\n if (mode === 'create' && field.updateOnly === true) {\n return [];\n }\n const withRules = resolveFieldRules(field, mode);\n if (mode === 'update' && field.createOnly === true) {\n return [{ ...withRules, readOnly: true }];\n }\n return [withRules];\n });\n const fieldsByName = new Map(resolved.map((field) => [field.name, field]));\n const pruned = pruneLayout(layout, fieldsByName);\n const referenced = referencedNames(pruned);\n const appended: LayoutNode[] = resolved\n .filter((field) => !referenced.has(field.name))\n .map((field) => ({ type: 'field', name: field.name }));\n return { fields: resolved, fieldsByName, layout: [...pruned, ...appended] };\n}\n\n/** Resolves the form of a resource: its fields (plus those of the item's type) and its layout. */\nexport function resolveForm(resource: Resource, options: ResolveFormOptions = {}): ResolvedForm {\n const typeFields =\n options.type !== undefined\n ? (resource.types?.find((type) => type.id === options.type)?.fields ?? [])\n : [];\n return resolveFields([...resource.fields, ...typeFields], resource.form?.layout ?? [], options);\n}\n\n/** The field names a layout references, in order. */\nexport function getLayoutFieldNames(layout: readonly LayoutNode[]): string[] {\n return [...referencedNames(layout)];\n}\n"],"mappings":";AA6BA,SAASa,gBAAgBC,OAA8BC,wBAAQ,IAAIC,IAAY,GAAgB;CAC3FF,MAAMG,SAASC,SAAS;EACpB,IAAIA,KAAKN,SAAS,WAAWM,KAAKC,SAASC,KAAAA,GACvCL,MAAMM,IAAIH,KAAKC,IAAI;EAEvBN,gBAAgBK,KAAKI,YAAY,CAAA,GAAIP,KAAK;CAC9C,CAAC;CACD,OAAOA;AACX;AAEA,SAASQ,YACLT,OACAR,QACY;CACZ,OAAOQ,MAAMU,SAASN,SAAS;EAC3B,IAAIA,KAAKN,SAAS,YAAYM,KAAKC,SAASC,KAAAA,KAAa,CAACd,OAAOmB,IAAIP,KAAKC,IAAI,IAC1E,OAAO,CAAA;EAEX,IAAID,KAAKI,aAAaF,KAAAA,GAClB,OAAO,CAACF,IAAI;EAEhB,OAAO,CAAC;GAAE,GAAGA;GAAMI,UAAUC,YAAYL,KAAKI,UAAUhB,MAAM;EAAE,CAAC;CACrE,CAAC;AACL;;AAGA,SAASoB,aAAaC,OAAwBhB,MAAwB;CAClE,OAAOgB,MAAMH,SAASI,SAAS;EAC3B,IAAIA,KAAKjB,SAASS,KAAAA,GACd,OAAO,CAACQ,IAAI;EAEhB,IAAIA,KAAKjB,SAASA,MACd,OAAO,CAAA;EAEX,MAAM,EAAEA,MAAMkB,OAAO,GAAGC,aAAaF;EACrC,OAAO,CAACE,QAAgB;CAC5B,CAAC;AACL;;AAGA,SAASC,kBAAkBC,OAAcrB,MAAuB;CAC5D,MAAMmB,WAAkB,EAAE,GAAGE,MAAM;CACnC,IAAIA,MAAML,UAAUP,KAAAA,GAChBU,SAASH,QAAQD,aAAaM,MAAML,OAAOhB,IAAI;CAEnD,IAAIqB,MAAM1B,WAAWc,KAAAA,GACjBU,SAASxB,SAAS0B,MAAM1B,OAAO2B,KAAKC,WAAWH,kBAAkBG,QAAQvB,IAAI,CAAC;CAElF,IAAIqB,MAAMG,UAAUf,KAAAA,GAChBU,SAASK,QAAQH,MAAMG,MAAMF,KAAKrB,UAAU;EACxC,GAAGA;EACHN,QAAQM,KAAKN,OAAO2B,KAAKC,WAAWH,kBAAkBG,QAAQvB,IAAI,CAAC;CACvE,EAAE;CAEN,OAAOmB;AACX;;AAGA,SAAgBM,cACZ9B,QACAG,SAAgC,CAAA,GAChC,EAAEE,SAA2C,CAAC,GAClC;CACZ,MAAMmB,WAAWxB,OAAOkB,SAASQ,UAAU;EACvC,IAAIrB,SAASS,KAAAA,GACT,OAAO,CAACY,KAAK;EAEjB,IAAIrB,SAAS,YAAYqB,MAAMM,eAAe,MAC1C,OAAO,CAAA;EAEX,MAAMC,YAAYR,kBAAkBC,OAAOrB,IAAI;EAC/C,IAAIA,SAAS,YAAYqB,MAAMQ,eAAe,MAC1C,OAAO,CAAC;GAAE,GAAGD;GAAWE,UAAU;EAAK,CAAC;EAE5C,OAAO,CAACF,SAAS;CACrB,CAAC;CACD,MAAMhC,eAAe,IAAImC,IAAIZ,SAASG,KAAKD,UAAU,CAACA,MAAMb,MAAMa,KAAK,CAAC,CAAC;CACzE,MAAMW,SAASpB,YAAYd,QAAQF,YAAY;CAC/C,MAAMqC,aAAa/B,gBAAgB8B,MAAM;CACzC,MAAME,WAAyBf,SAC1BgB,QAAQd,UAAU,CAACY,WAAWnB,IAAIO,MAAMb,IAAI,CAAC,CAAC,CAC9Cc,KAAKD,WAAW;EAAEpB,MAAM;EAASO,MAAMa,MAAMb;CAAK,EAAE;CACzD,OAAO;EAAEb,QAAQwB;EAAUvB;EAAcE,QAAQ,CAAC,GAAGkC,QAAQ,GAAGE,QAAQ;CAAE;AAC9E;;AAGA,SAAgBE,YAAYC,UAAoBC,UAA8B,CAAC,GAAiB;CAC5F,MAAMC,aACFD,QAAQrC,SAASQ,KAAAA,IACV4B,SAASb,OAAOgB,MAAMvC,SAASA,KAAKwC,OAAOH,QAAQrC,IAAI,CAAC,EAAEN,UAAU,CAAA,IACrE,CAAA;CACV,OAAO8B,cAAc,CAAC,GAAGY,SAAS1C,QAAQ,GAAG4C,UAAU,GAAGF,SAASK,MAAM5C,UAAU,CAAA,GAAIwC,OAAO;AAClG;;AAGA,SAAgBK,oBAAoB7C,QAAyC;CACzE,OAAO,CAAC,GAAGI,gBAAgBJ,MAAM,CAAC;AACtC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Abilities, Action, Resource } from '@panneau/protocol';
|
|
2
|
+
/**
|
|
3
|
+
* Where an item's actions are shown (decision 0040): the buttons of its row in a list, the
|
|
4
|
+
* overflow menu of its row, the header of its panel (the show route), the header of its form.
|
|
5
|
+
*/
|
|
6
|
+
export type ItemSurface = 'row' | 'menu' | 'panel' | 'form';
|
|
7
|
+
/** An item as its actions need it: its id, its abilities, and its values for URL templates. */
|
|
8
|
+
export interface ActionItem {
|
|
9
|
+
id: string;
|
|
10
|
+
/**
|
|
11
|
+
* What the user can do with the item. Without abilities, the built-in item actions are hidden,
|
|
12
|
+
* and the other actions are allowed: the backend decides.
|
|
13
|
+
*/
|
|
14
|
+
abilities?: Abilities | undefined;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
/** An action of an item, ready to show: a link, or an action to run. */
|
|
18
|
+
export interface ItemAction {
|
|
19
|
+
action: Action;
|
|
20
|
+
/** The link of a `link` action, or `null` for an action that runs (with the action runner). */
|
|
21
|
+
href: string | null;
|
|
22
|
+
}
|
|
23
|
+
export interface ItemActionsOptions {
|
|
24
|
+
/**
|
|
25
|
+
* The link of a `link` action without a `url`: the built-in `show` and `edit`, and actions
|
|
26
|
+
* with a `route`, which the caller resolves to its routes. A built-in link it doesn't resolve
|
|
27
|
+
* is left out; an action with a `route` then runs, and the action runner navigates.
|
|
28
|
+
*/
|
|
29
|
+
getLinkHref?: ((action: Action, item: ActionItem) => string | null) | undefined;
|
|
30
|
+
/** Leaves out the actions the caller can't run, such as `duplicate` without an adapter for it. */
|
|
31
|
+
filter?: ((action: Action) => boolean) | undefined;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The URL template of a `link` action with the values of an item: `{id}`, and other `{field}`
|
|
35
|
+
* with a string or a number. `null` without a `url`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function fillActionUrl(action: Action, item?: ActionItem): string | null;
|
|
38
|
+
/**
|
|
39
|
+
* The actions of an item on a surface, from actions already placed there (a group of
|
|
40
|
+
* `resolveList()`'s actions): those that apply to items, that the user can run and that the item
|
|
41
|
+
* allows, without the one that leads to the surface, each with its link. `getItemActions()` is
|
|
42
|
+
* this function for a resource; lists use it with the actions they are given.
|
|
43
|
+
*/
|
|
44
|
+
export declare function selectItemActions(actions: readonly Action[], item: ActionItem, surface: ItemSurface, { getLinkHref, filter }?: ItemActionsOptions): ItemAction[];
|
|
45
|
+
/**
|
|
46
|
+
* The actions of an item on a surface (decision 0040), in their order: the built-in item actions
|
|
47
|
+
* of the resource that the item's abilities allow, then its declared actions, by their placement
|
|
48
|
+
* and the item's abilities. Rows, their menu, the panel and the form show the same actions for
|
|
49
|
+
* the same item, but for the one that leads to the surface: no `show` in the panel, no `edit` in
|
|
50
|
+
* the form. The panel and the form show the actions placed in `form`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function getItemActions(resource: Resource, item: ActionItem, surface: ItemSurface, options?: ItemActionsOptions): ItemAction[];
|
|
53
|
+
//# sourceMappingURL=itemActions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"itemActions.d.ts","sourceRoot":"","sources":["../../src/definition/itemActions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAmB,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAMtF;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;AAE5D,+FAA+F;AAC/F,MAAM,WAAW,UAAU;IACvB,EAAE,EAAE,MAAM,CAAC;IACX;;;OAGG;IACH,SAAS,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;IAClC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CAC1B;AAED,wEAAwE;AACxE,MAAM,WAAW,UAAU;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,+FAA+F;IAC/F,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB;AAED,MAAM,WAAW,kBAAkB;IAC/B;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,MAAM,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC;IAChF,kGAAkG;IAClG,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,SAAS,CAAC;CACtD;AAgBD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,CAU9E;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC7B,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,WAAW,EACpB,EAAE,WAAW,EAAE,MAAM,EAAE,GAAE,kBAAuB,GACjD,UAAU,EAAE,CAqBd;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC1B,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,WAAW,EACpB,OAAO,GAAE,kBAAuB,GACjC,UAAU,EAAE,CAGd"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { isActionAllowedOn } from "./abilities.js";
|
|
2
|
+
import { getResourceActions, groupActions } from "./list.js";
|
|
3
|
+
import { isBuiltInLink } from "@panneau/protocol/runtime";
|
|
4
|
+
//#region src/definition/itemActions.ts
|
|
5
|
+
/** The placement of the actions of each surface: the panel and the form share the item's header. */
|
|
6
|
+
var SURFACE_PLACEMENTS = {
|
|
7
|
+
row: "row",
|
|
8
|
+
menu: "menu",
|
|
9
|
+
panel: "form",
|
|
10
|
+
form: "form"
|
|
11
|
+
};
|
|
12
|
+
/** The action that leads to a surface, which isn't shown on it. */
|
|
13
|
+
var SELF_ACTIONS = {
|
|
14
|
+
panel: "show",
|
|
15
|
+
form: "edit"
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The URL template of a `link` action with the values of an item: `{id}`, and other `{field}`
|
|
19
|
+
* with a string or a number. `null` without a `url`.
|
|
20
|
+
*/
|
|
21
|
+
function fillActionUrl(action, item) {
|
|
22
|
+
if (action.kind !== "link" || action.url === void 0) return null;
|
|
23
|
+
return action.url.replace(/\{([^}]+)\}/g, (match, name) => {
|
|
24
|
+
const value = item?.[name];
|
|
25
|
+
return typeof value === "string" || typeof value === "number" ? encodeURIComponent(String(value)) : match;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The actions of an item on a surface, from actions already placed there (a group of
|
|
30
|
+
* `resolveList()`'s actions): those that apply to items, that the user can run and that the item
|
|
31
|
+
* allows, without the one that leads to the surface, each with its link. `getItemActions()` is
|
|
32
|
+
* this function for a resource; lists use it with the actions they are given.
|
|
33
|
+
*/
|
|
34
|
+
function selectItemActions(actions, item, surface, { getLinkHref, filter } = {}) {
|
|
35
|
+
return actions.flatMap((action) => {
|
|
36
|
+
const allowed = isActionAllowedOn(action, item);
|
|
37
|
+
if (!action.scope.includes("item") || action.id === SELF_ACTIONS[surface] || !allowed || filter !== void 0 && !filter(action)) return [];
|
|
38
|
+
if (action.kind !== "link") return [{
|
|
39
|
+
action,
|
|
40
|
+
href: null
|
|
41
|
+
}];
|
|
42
|
+
const href = fillActionUrl(action, item) ?? getLinkHref?.(action, item) ?? null;
|
|
43
|
+
if (href === null && action.route === void 0 && isBuiltInLink(action.id)) return [];
|
|
44
|
+
return [{
|
|
45
|
+
action,
|
|
46
|
+
href
|
|
47
|
+
}];
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The actions of an item on a surface (decision 0040), in their order: the built-in item actions
|
|
52
|
+
* of the resource that the item's abilities allow, then its declared actions, by their placement
|
|
53
|
+
* and the item's abilities. Rows, their menu, the panel and the form show the same actions for
|
|
54
|
+
* the same item, but for the one that leads to the surface: no `show` in the panel, no `edit` in
|
|
55
|
+
* the form. The panel and the form show the actions placed in `form`.
|
|
56
|
+
*/
|
|
57
|
+
function getItemActions(resource, item, surface, options = {}) {
|
|
58
|
+
const placed = groupActions(getResourceActions(resource))[SURFACE_PLACEMENTS[surface]];
|
|
59
|
+
return selectItemActions(placed, item, surface, options);
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
export { fillActionUrl, getItemActions, selectItemActions };
|
|
63
|
+
|
|
64
|
+
//# sourceMappingURL=itemActions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"itemActions.js","names":["Abilities","Action","ActionPlacement","Resource","isBuiltInLink","isActionAllowedOn","getResourceActions","groupActions","ItemSurface","ActionItem","id","abilities","key","ItemAction","action","href","ItemActionsOptions","getLinkHref","item","filter","SURFACE_PLACEMENTS","Readonly","Record","row","menu","panel","form","SELF_ACTIONS","Partial","fillActionUrl","kind","url","undefined","replace","match","name","value","encodeURIComponent","String","selectItemActions","actions","surface","flatMap","allowed","scope","includes","route","getItemActions","resource","options","placed"],"sources":["../../src/definition/itemActions.ts"],"sourcesContent":["import type { Abilities, Action, ActionPlacement, Resource } from '@panneau/protocol';\nimport { isBuiltInLink } from '@panneau/protocol/runtime';\n\nimport { isActionAllowedOn } from './abilities';\nimport { getResourceActions, groupActions } from './list';\n\n/**\n * Where an item's actions are shown (decision 0040): the buttons of its row in a list, the\n * overflow menu of its row, the header of its panel (the show route), the header of its form.\n */\nexport type ItemSurface = 'row' | 'menu' | 'panel' | 'form';\n\n/** An item as its actions need it: its id, its abilities, and its values for URL templates. */\nexport interface ActionItem {\n id: string;\n /**\n * What the user can do with the item. Without abilities, the built-in item actions are hidden,\n * and the other actions are allowed: the backend decides.\n */\n abilities?: Abilities | undefined;\n [key: string]: unknown;\n}\n\n/** An action of an item, ready to show: a link, or an action to run. */\nexport interface ItemAction {\n action: Action;\n /** The link of a `link` action, or `null` for an action that runs (with the action runner). */\n href: string | null;\n}\n\nexport interface ItemActionsOptions {\n /**\n * The link of a `link` action without a `url`: the built-in `show` and `edit`, and actions\n * with a `route`, which the caller resolves to its routes. A built-in link it doesn't resolve\n * is left out; an action with a `route` then runs, and the action runner navigates.\n */\n getLinkHref?: ((action: Action, item: ActionItem) => string | null) | undefined;\n /** Leaves out the actions the caller can't run, such as `duplicate` without an adapter for it. */\n filter?: ((action: Action) => boolean) | undefined;\n}\n\n/** The placement of the actions of each surface: the panel and the form share the item's header. */\nconst SURFACE_PLACEMENTS: Readonly<Record<ItemSurface, ActionPlacement>> = {\n row: 'row',\n menu: 'menu',\n panel: 'form',\n form: 'form',\n};\n\n/** The action that leads to a surface, which isn't shown on it. */\nconst SELF_ACTIONS: Readonly<Partial<Record<ItemSurface, string>>> = {\n panel: 'show',\n form: 'edit',\n};\n\n/**\n * The URL template of a `link` action with the values of an item: `{id}`, and other `{field}`\n * with a string or a number. `null` without a `url`.\n */\nexport function fillActionUrl(action: Action, item?: ActionItem): string | null {\n if (action.kind !== 'link' || action.url === undefined) {\n return null;\n }\n return action.url.replace(/\\{([^}]+)\\}/g, (match, name: string) => {\n const value = item?.[name];\n return typeof value === 'string' || typeof value === 'number'\n ? encodeURIComponent(String(value))\n : match;\n });\n}\n\n/**\n * The actions of an item on a surface, from actions already placed there (a group of\n * `resolveList()`'s actions): those that apply to items, that the user can run and that the item\n * allows, without the one that leads to the surface, each with its link. `getItemActions()` is\n * this function for a resource; lists use it with the actions they are given.\n */\nexport function selectItemActions(\n actions: readonly Action[],\n item: ActionItem,\n surface: ItemSurface,\n { getLinkHref, filter }: ItemActionsOptions = {},\n): ItemAction[] {\n return actions.flatMap((action): ItemAction[] => {\n const allowed = isActionAllowedOn(action, item);\n if (\n !action.scope.includes('item') ||\n action.id === SELF_ACTIONS[surface] ||\n !allowed ||\n (filter !== undefined && !filter(action))\n ) {\n return [];\n }\n if (action.kind !== 'link') {\n return [{ action, href: null }];\n }\n const href = fillActionUrl(action, item) ?? getLinkHref?.(action, item) ?? null;\n if (href === null && action.route === undefined && isBuiltInLink(action.id)) {\n // A built-in link that nothing resolves goes nowhere\n return [];\n }\n return [{ action, href }];\n });\n}\n\n/**\n * The actions of an item on a surface (decision 0040), in their order: the built-in item actions\n * of the resource that the item's abilities allow, then its declared actions, by their placement\n * and the item's abilities. Rows, their menu, the panel and the form show the same actions for\n * the same item, but for the one that leads to the surface: no `show` in the panel, no `edit` in\n * the form. The panel and the form show the actions placed in `form`.\n */\nexport function getItemActions(\n resource: Resource,\n item: ActionItem,\n surface: ItemSurface,\n options: ItemActionsOptions = {},\n): ItemAction[] {\n const placed = groupActions(getResourceActions(resource))[SURFACE_PLACEMENTS[surface]];\n return selectItemActions(placed, item, surface, options);\n}\n"],"mappings":";;;;;AA0CA,IAAMoB,qBAAqE;CACvEG,KAAK;CACLC,MAAM;CACNC,OAAO;CACPC,MAAM;AACV;;AAGA,IAAMC,eAA+D;CACjEF,OAAO;CACPC,MAAM;AACV;;;;;AAMA,SAAgBG,cAAcf,QAAgBI,MAAkC;CAC5E,IAAIJ,OAAOgB,SAAS,UAAUhB,OAAOiB,QAAQC,KAAAA,GACzC,OAAO;CAEX,OAAOlB,OAAOiB,IAAIE,QAAQ,iBAAiBC,OAAOC,SAAiB;EAC/D,MAAMC,QAAQlB,OAAOiB;EACrB,OAAO,OAAOC,UAAU,YAAY,OAAOA,UAAU,WAC/CC,mBAAmBC,OAAOF,KAAK,CAAC,IAChCF;CACV,CAAC;AACL;;;;;;;AAQA,SAAgBK,kBACZC,SACAtB,MACAuB,SACA,EAAExB,aAAaE,WAA+B,CAAC,GACnC;CACZ,OAAOqB,QAAQE,SAAS5B,WAAyB;EAC7C,MAAM6B,UAAUtC,kBAAkBS,QAAQI,IAAI;EAC9C,IACI,CAACJ,OAAO8B,MAAMC,SAAS,MAAM,KAC7B/B,OAAOJ,OAAOiB,aAAac,YAC3B,CAACE,WACAxB,WAAWa,KAAAA,KAAa,CAACb,OAAOL,MAAM,GAEvC,OAAO,CAAA;EAEX,IAAIA,OAAOgB,SAAS,QAChB,OAAO,CAAC;GAAEhB;GAAQC,MAAM;EAAK,CAAC;EAElC,MAAMA,OAAOc,cAAcf,QAAQI,IAAI,KAAKD,cAAcH,QAAQI,IAAI,KAAK;EAC3E,IAAIH,SAAS,QAAQD,OAAOgC,UAAUd,KAAAA,KAAa5B,cAAcU,OAAOJ,EAAE,GAEtE,OAAO,CAAA;EAEX,OAAO,CAAC;GAAEI;GAAQC;EAAK,CAAC;CAC5B,CAAC;AACL;;;;;;;;AASA,SAAgBgC,eACZC,UACA9B,MACAuB,SACAQ,UAA8B,CAAC,GACnB;CACZ,MAAMC,SAAS3C,aAAaD,mBAAmB0C,QAAQ,CAAC,CAAC,CAAC5B,mBAAmBqB;CAC7E,OAAOF,kBAAkBW,QAAQhC,MAAMuB,SAASQ,OAAO;AAC3D"}
|
|
@@ -26,6 +26,8 @@ export interface ResolvedList {
|
|
|
26
26
|
layouts: string[];
|
|
27
27
|
perPage: number;
|
|
28
28
|
perPageOptions: number[];
|
|
29
|
+
/** `pages` (numbered, with a total), or `cursor`: the list follows the cursors of its pages. */
|
|
30
|
+
pagination: 'pages' | 'cursor';
|
|
29
31
|
/** Fields editable in the list. */
|
|
30
32
|
inlineEdit: string[];
|
|
31
33
|
/** Whether items can be selected: the list allows it and a selection action exists. */
|
|
@@ -36,6 +38,20 @@ export interface ResolvedList {
|
|
|
36
38
|
export declare const DEFAULT_PER_PAGE = 20;
|
|
37
39
|
/** Groups the actions the user can run by placement. */
|
|
38
40
|
export declare function groupActions(actions: readonly Action[]): Record<ActionPlacement, Action[]>;
|
|
41
|
+
/**
|
|
42
|
+
* The actions of a resource (decision 0040): its built-in item actions (`show`, `edit`,
|
|
43
|
+
* `duplicate`, `delete`, or those of its `itemActions`, in their order), then the actions it
|
|
44
|
+
* declares. A declared action with the id of a built-in one replaces it, at its place, with the
|
|
45
|
+
* built-in one's placement, confirmation, `destructive` and `dryRun` when it doesn't set them, and
|
|
46
|
+
* is left out with it when `itemActions` doesn't list it. With `"open": "edit"`, `show` is left out: it
|
|
47
|
+
* would go to the edit form too. Abilities of the items then hide what they don't allow.
|
|
48
|
+
*/
|
|
49
|
+
export declare function getResourceActions(resource: Resource): Action[];
|
|
50
|
+
/**
|
|
51
|
+
* Where the title of an item goes (`open` of the resource, decision 0040): `panel`, the show route
|
|
52
|
+
* with the item's details next to the list, or `edit`, its form. A value it doesn't know is `panel`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function getOpenMode(resource: Resource): 'panel' | 'edit';
|
|
39
55
|
/**
|
|
40
56
|
* Resolves the list of a resource. Without columns, it shows the item title and the next fields
|
|
41
57
|
* (up to four, skipping groups, repeaters and rich text).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../src/definition/list.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAU,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../src/definition/list.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAU,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AASlG,gCAAgC;AAChC,MAAM,WAAW,cAAc;IAC3B,yDAAyD;IACzD,EAAE,EAAE,MAAM,CAAC;IACX,kDAAkD;IAClD,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,wFAAwF;IACxF,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,QAAQ,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,KAAK,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,KAAK,CAAC;CACtC;AAED,8BAA8B;AAC9B,MAAM,WAAW,YAAY;IACzB,OAAO,EAAE,cAAc,EAAE,CAAC;IAC1B,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,wCAAwC;IACxC,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,+EAA+E;IAC/E,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,8CAA8C;IAC9C,MAAM,EAAE,OAAO,CAAC;IAChB,qDAAqD;IACrD,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,gGAAgG;IAChG,UAAU,EAAE,OAAO,GAAG,QAAQ,CAAC;IAC/B,mCAAmC;IACnC,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,uFAAuF;IACvF,UAAU,EAAE,OAAO,CAAC;IACpB,8CAA8C;IAC9C,OAAO,EAAE,MAAM,CAAC,eAAe,EAAE,MAAM,EAAE,CAAC,CAAC;CAC9C;AAMD,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAenC,wDAAwD;AACxD,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAAC,eAAe,EAAE,MAAM,EAAE,CAAC,CAM1F;AAkBD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,EAAE,CAmB/D;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,GAAG,MAAM,CAEhE;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,GAAG,YAAY,CAyC5D"}
|
package/dist/definition/list.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { canRunAction, getActionPlacements } from "./abilities.js";
|
|
2
|
+
import { BUILT_IN_ITEM_ACTIONS, isBuiltInItemAction } from "@panneau/protocol/runtime";
|
|
2
3
|
//#region src/definition/list.ts
|
|
3
4
|
/** Fields shown when the list declares no columns. */
|
|
4
5
|
var DEFAULT_COLUMN_COUNT = 4;
|
|
@@ -26,6 +27,65 @@ function groupActions(actions) {
|
|
|
26
27
|
});
|
|
27
28
|
return groups;
|
|
28
29
|
}
|
|
30
|
+
/** The built-in item actions, as the client shows them when a resource doesn't replace them. */
|
|
31
|
+
var BUILT_IN_ITEM_ACTION_DEFINITIONS = {
|
|
32
|
+
show: {
|
|
33
|
+
id: "show",
|
|
34
|
+
kind: "link",
|
|
35
|
+
scope: ["item"],
|
|
36
|
+
placement: ["row", "form"]
|
|
37
|
+
},
|
|
38
|
+
edit: {
|
|
39
|
+
id: "edit",
|
|
40
|
+
kind: "link",
|
|
41
|
+
scope: ["item"],
|
|
42
|
+
placement: ["row", "form"]
|
|
43
|
+
},
|
|
44
|
+
duplicate: {
|
|
45
|
+
id: "duplicate",
|
|
46
|
+
kind: "server",
|
|
47
|
+
scope: ["item"],
|
|
48
|
+
placement: ["menu", "form"]
|
|
49
|
+
},
|
|
50
|
+
delete: {
|
|
51
|
+
id: "delete",
|
|
52
|
+
kind: "server",
|
|
53
|
+
scope: ["item", "selection"],
|
|
54
|
+
placement: [
|
|
55
|
+
"menu",
|
|
56
|
+
"toolbar",
|
|
57
|
+
"form"
|
|
58
|
+
],
|
|
59
|
+
destructive: true,
|
|
60
|
+
confirm: true,
|
|
61
|
+
dryRun: true
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* The actions of a resource (decision 0040): its built-in item actions (`show`, `edit`,
|
|
66
|
+
* `duplicate`, `delete`, or those of its `itemActions`, in their order), then the actions it
|
|
67
|
+
* declares. A declared action with the id of a built-in one replaces it, at its place, with the
|
|
68
|
+
* built-in one's placement, confirmation, `destructive` and `dryRun` when it doesn't set them, and
|
|
69
|
+
* is left out with it when `itemActions` doesn't list it. With `"open": "edit"`, `show` is left out: it
|
|
70
|
+
* would go to the edit form too. Abilities of the items then hide what they don't allow.
|
|
71
|
+
*/
|
|
72
|
+
function getResourceActions(resource) {
|
|
73
|
+
const declared = resource.actions ?? [];
|
|
74
|
+
return [...(resource.itemActions ?? BUILT_IN_ITEM_ACTIONS).filter((id, index, all) => isBuiltInItemAction(id) && all.indexOf(id) === index && !(id === "show" && getOpenMode(resource) === "edit")).map((id) => {
|
|
75
|
+
const replacement = declared.find((action) => action.id === id);
|
|
76
|
+
return replacement !== void 0 ? {
|
|
77
|
+
...BUILT_IN_ITEM_ACTION_DEFINITIONS[id],
|
|
78
|
+
...replacement
|
|
79
|
+
} : BUILT_IN_ITEM_ACTION_DEFINITIONS[id];
|
|
80
|
+
}), ...declared.filter((action) => !isBuiltInItemAction(action.id))];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Where the title of an item goes (`open` of the resource, decision 0040): `panel`, the show route
|
|
84
|
+
* with the item's details next to the list, or `edit`, its form. A value it doesn't know is `panel`.
|
|
85
|
+
*/
|
|
86
|
+
function getOpenMode(resource) {
|
|
87
|
+
return resource.open === "edit" ? "edit" : "panel";
|
|
88
|
+
}
|
|
29
89
|
/**
|
|
30
90
|
* Resolves the list of a resource. Without columns, it shows the item title and the next fields
|
|
31
91
|
* (up to four, skipping groups, repeaters and rich text).
|
|
@@ -48,8 +108,8 @@ function resolveList(resource) {
|
|
|
48
108
|
if (column.align !== void 0) resolved.align = column.align;
|
|
49
109
|
return resolved;
|
|
50
110
|
});
|
|
51
|
-
const actions = groupActions(resource
|
|
52
|
-
const hasSelectionAction = (resource
|
|
111
|
+
const actions = groupActions(getResourceActions(resource));
|
|
112
|
+
const hasSelectionAction = getResourceActions(resource).some((action) => action.scope.includes("selection") && canRunAction(action));
|
|
53
113
|
return {
|
|
54
114
|
columns,
|
|
55
115
|
filters: list.filters ?? [],
|
|
@@ -59,12 +119,13 @@ function resolveList(resource) {
|
|
|
59
119
|
layouts: list.layouts ?? ["table"],
|
|
60
120
|
perPage: list.perPage ?? 20,
|
|
61
121
|
perPageOptions: list.perPageOptions ?? [],
|
|
122
|
+
pagination: list.pagination ?? "pages",
|
|
62
123
|
inlineEdit: list.inlineEdit ?? [],
|
|
63
124
|
selectable: (list.selectable ?? true) && hasSelectionAction,
|
|
64
125
|
actions
|
|
65
126
|
};
|
|
66
127
|
}
|
|
67
128
|
//#endregion
|
|
68
|
-
export { DEFAULT_PER_PAGE, groupActions, resolveList };
|
|
129
|
+
export { DEFAULT_PER_PAGE, getOpenMode, getResourceActions, groupActions, resolveList };
|
|
69
130
|
|
|
70
131
|
//# sourceMappingURL=list.js.map
|