@sealcode/sealgen 0.19.12 → 0.19.14

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.
@@ -36,8 +36,10 @@ export declare abstract class FormField<Required extends boolean = boolean, Pars
36
36
  constructor(required: Required);
37
37
  predicate: Predicate;
38
38
  mapToFilter: (parsed: ParsedValue) => unknown;
39
+ disabled: boolean;
39
40
  init(): Promise<void>;
40
41
  setName(name: string): this;
42
+ setDisabled(status: boolean): this;
41
43
  setLabel(label: string): this;
42
44
  _validate(ctx: Context, value: ParsedValue): Promise<FormFieldValidationResponse>;
43
45
  abstract getEmptyValue(): ParsedValue;
@@ -1,5 +1,10 @@
1
+ import { ExtractedFieldInfo } from "../../utils/extract-fields-from-collection.js";
1
2
  import { TextBasedSimpleField } from "./simple-form-field.js";
2
3
  export declare class Password<Required extends boolean> extends TextBasedSimpleField<Required> {
3
4
  constructor(required: Required);
4
5
  sealiousValueToForm(): Promise<undefined>;
6
+ generateInitialValue(field_info: ExtractedFieldInfo, vars: {
7
+ form_field_types: string;
8
+ form_fields: string;
9
+ }): string;
5
10
  }
@@ -47,6 +47,7 @@ class SimpleInput extends FormFieldControl {
47
47
  ...!this.options.autocomplete ? [["autocomplete", "off"]] : [],
48
48
  ...this.options.step ? [["step", this.options.step]] : [],
49
49
  ["aria-invalid", String(!valid)],
50
+ ["disabled", this.field.disabled],
50
51
  ...this.options.inputmode ? [["inputmode", this.options.inputmode]] : []
51
52
  ]);
52
53
  }
@@ -11,6 +11,7 @@ class FormField {
11
11
  this.required = required;
12
12
  this.predicate = predicates.unknown;
13
13
  this.mapToFilter = (value) => value;
14
+ this.disabled = false;
14
15
  }
15
16
  async init() {
16
17
  }
@@ -18,6 +19,10 @@ class FormField {
18
19
  this.name = name;
19
20
  return this;
20
21
  }
22
+ setDisabled(status) {
23
+ this.disabled = status;
24
+ return this;
25
+ }
21
26
  setLabel(label) {
22
27
  this.label = label;
23
28
  return this;
@@ -1,3 +1,4 @@
1
+ import { wrapAttribute, wrapKeyName } from "../../utils/wrap-attributes.js";
1
2
  import { TextBasedSimpleField } from "./simple-form-field.js";
2
3
  class Password extends TextBasedSimpleField {
3
4
  constructor(required) {
@@ -6,6 +7,9 @@ class Password extends TextBasedSimpleField {
6
7
  async sealiousValueToForm() {
7
8
  return void 0;
8
9
  }
10
+ generateInitialValue(field_info, vars) {
11
+ return `${wrapKeyName(field_info.name)}: await ${vars.form_fields}${wrapAttribute(field_info.name)}.sealiousValueToForm()`;
12
+ }
9
13
  }
10
14
  export {
11
15
  Password
@@ -28,6 +28,9 @@ class TextBasedSimpleField extends SimpleFormField {
28
28
  };
29
29
  }
30
30
  async getSealiousCreateValue(fctx) {
31
+ if (this.disabled) {
32
+ return null;
33
+ }
31
34
  const { parsed } = await this.getParsedValue(
32
35
  fctx.ctx,
33
36
  fctx.data.raw_values
@@ -26,11 +26,11 @@ class MountableWithFields extends Mountable {
26
26
  } else if (!this.fields) {
27
27
  throw new Error("Fields not defined and getter not provided");
28
28
  }
29
- this.controls = this.getControls(this.fields);
30
29
  for (const [field_name, field] of Object.entries(this.fields)) {
31
30
  void field.init();
32
31
  field.setName(field_name);
33
32
  }
33
+ this.controls = this.getControls(this.fields);
34
34
  this.initialized = true;
35
35
  }
36
36
  makeFormControlContext(ctx, data, validate, field_name_prefix = this.field_names_prefix, form_id = this.form_id) {
@@ -8,7 +8,7 @@ const defaults = {
8
8
  ()=><const>[new Controls.SimpleInput(fields.password, {
9
9
  label: "Password:",
10
10
  type: "password",
11
- })],`,
11
+ })]`,
12
12
  validate_method: `async validateValues(
13
13
  ctx: Context,
14
14
  data: Record<string, FormDataValue>
@@ -24,8 +24,14 @@ async function editItemFormTemplate(edit_action_name, newfilefullpath, collectio
24
24
  )}"),
25
25
  ...get${toPascalCase(collection_name)}FormControls(fields)]`,
26
26
  can_access: `canAccess = async (ctx: Context) => {
27
- const policy = ${toPascalCase(collection_name)}.getPolicy("edit");
28
- const response = await policy.check(ctx.$context);
27
+ const policy = ${toPascalCase(collection_name)}.getPolicy("edit");
28
+ const response = await policy.check(ctx.$context, async () => {
29
+ const id = await this.getID(ctx);
30
+ const {
31
+ items: [item],
32
+ } = await ctx.$app.collections${wrapAttribute(collection_name)}.list(ctx.$context).ids([id]).fetch();
33
+ return item!;
34
+ });
29
35
  return { canAccess: response?.allowed || false, message: response?.reason || "" };
30
36
  };`,
31
37
  onsubmit: `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.19.12",
3
+ "version": "0.19.14",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -92,7 +92,7 @@
92
92
  "peerDependencies": {
93
93
  "koa": "^2.13.0",
94
94
  "koa-responsive-image-router": "^0.2.24",
95
- "sealious": "^0.21.20",
95
+ "sealious": "^0.21.29",
96
96
  "stimulus": "^3.2.2"
97
97
  }
98
98
  }
@@ -48,9 +48,9 @@ export class SimpleInput<
48
48
  }
49
49
 
50
50
  getLabel(): string {
51
- return this.options.label != undefined
52
- ? this.options.label
53
- : this.field.label || this.field.name;
51
+ return this.options.label != undefined ?
52
+ this.options.label
53
+ : this.field.label || this.field.name;
54
54
  }
55
55
 
56
56
  getID(): string {
@@ -61,9 +61,9 @@ export class SimpleInput<
61
61
  if (this.field instanceof SimpleFormField && this.field.placeholder) {
62
62
  return this.field.placeholder;
63
63
  }
64
- return this.options.placeholder === ""
65
- ? ""
66
- : this.options.placeholder || this.getType();
64
+ return this.options.placeholder === "" ?
65
+ ""
66
+ : this.options.placeholder || this.getType();
67
67
  }
68
68
 
69
69
  async getInputAttributes(
@@ -88,16 +88,17 @@ export class SimpleInput<
88
88
  ...(!this.options.autocomplete ? [["autocomplete", "off"]] : []),
89
89
  ...(this.options.step ? [["step", this.options.step]] : []),
90
90
  ["aria-invalid", String(!valid)],
91
- ...(this.options.inputmode
92
- ? [["inputmode", this.options.inputmode]]
93
- : []),
91
+ ["disabled", this.field.disabled],
92
+ ...(this.options.inputmode ?
93
+ [["inputmode", this.options.inputmode]]
94
+ : []),
94
95
  ]) as Record<string, string>;
95
96
  }
96
97
 
97
98
  async postInput(_: Context): Promise<FlatTemplatable> {
98
- return this.options.suffix
99
- ? /* HTML */ `<span class="suffix">${this.options.suffix}</span>`
100
- : "";
99
+ return this.options.suffix ?
100
+ /* HTML */ `<span class="suffix">${this.options.suffix}</span>`
101
+ : "";
101
102
  }
102
103
 
103
104
  async renderInput(
@@ -138,15 +139,14 @@ export class SimpleInput<
138
139
  fctx.data.raw_values
139
140
  )}
140
141
  ${this.postInput(fctx.ctx)}
141
- ${~valid && !this.options.hide_errors && fctx.validate
142
- ? `<div class="input__error">${[
143
- message,
144
- fctx.data.field_messages?.[this.field.name]
145
- ?.message,
146
- ]
147
- .filter((e) => !!e)
148
- .join(" · ")}</div>`
149
- : ""}
142
+ ${~valid && !this.options.hide_errors && fctx.validate ?
143
+ `<div class="input__error">${[
144
+ message,
145
+ fctx.data.field_messages?.[this.field.name]?.message,
146
+ ]
147
+ .filter((e) => !!e)
148
+ .join(" · ")}</div>`
149
+ : ""}
150
150
  `
151
151
  );
152
152
  }
@@ -69,6 +69,7 @@ export abstract class FormField<
69
69
  constructor(public readonly required: Required) {}
70
70
  predicate: Predicate = predicates.unknown;
71
71
  mapToFilter: (parsed: ParsedValue) => unknown = (value) => value;
72
+ disabled = false;
72
73
 
73
74
  async init(): Promise<void> {}
74
75
 
@@ -77,6 +78,11 @@ export abstract class FormField<
77
78
  return this;
78
79
  }
79
80
 
81
+ setDisabled(status: boolean) {
82
+ this.disabled = status;
83
+ return this;
84
+ }
85
+
80
86
  setLabel(label: string): this {
81
87
  this.label = label;
82
88
  return this;
@@ -1,3 +1,5 @@
1
+ import { ExtractedFieldInfo } from "../../utils/extract-fields-from-collection.js";
2
+ import { wrapAttribute, wrapKeyName } from "../../utils/wrap-attributes.js";
1
3
  import { TextBasedSimpleField } from "./simple-form-field.js";
2
4
 
3
5
  export class Password<
@@ -10,4 +12,16 @@ export class Password<
10
12
  async sealiousValueToForm(): Promise<undefined> {
11
13
  return undefined;
12
14
  }
15
+
16
+ generateInitialValue(
17
+ field_info: ExtractedFieldInfo,
18
+ vars: {
19
+ form_field_types: string;
20
+ form_fields: string;
21
+ }
22
+ ): string {
23
+ return `${wrapKeyName(field_info.name)}: await ${
24
+ vars.form_fields
25
+ }${wrapAttribute(field_info.name)}.sealiousValueToForm()`;
26
+ }
13
27
  }
@@ -59,6 +59,9 @@ export class TextBasedSimpleField<
59
59
  async getSealiousCreateValue(
60
60
  fctx: FormControlContext
61
61
  ): Promise<string | null> {
62
+ if (this.disabled) {
63
+ return null;
64
+ }
62
65
  const { parsed } = await this.getParsedValue(
63
66
  fctx.ctx,
64
67
  fctx.data.raw_values
@@ -57,11 +57,11 @@ export abstract class MountableWithFields<
57
57
  } else if (!this.fields) {
58
58
  throw new Error("Fields not defined and getter not provided");
59
59
  }
60
- this.controls = this.getControls(this.fields);
61
60
  for (const [field_name, field] of Object.entries(this.fields)) {
62
61
  void field.init();
63
62
  field.setName(field_name);
64
63
  }
64
+ this.controls = this.getControls(this.fields);
65
65
  this.initialized = true;
66
66
  }
67
67
 
@@ -9,7 +9,7 @@ const defaults = {
9
9
  ()=><const>[new Controls.SimpleInput(fields.password, {
10
10
  label: "Password:",
11
11
  type: "password",
12
- })],`,
12
+ })]`,
13
13
  validate_method: `async validateValues(
14
14
  ctx: Context,
15
15
  data: Record<string, FormDataValue>
@@ -35,8 +35,14 @@ export async function editItemFormTemplate(
35
35
  ...get${toPascalCase(collection_name)}FormControls(fields)]`,
36
36
 
37
37
  can_access: `canAccess = async (ctx: Context) => {
38
- const policy = ${toPascalCase(collection_name)}.getPolicy("edit");
39
- const response = await policy.check(ctx.$context);
38
+ const policy = ${toPascalCase(collection_name)}.getPolicy("edit");
39
+ const response = await policy.check(ctx.$context, async () => {
40
+ const id = await this.getID(ctx);
41
+ const {
42
+ items: [item],
43
+ } = await ctx.$app.collections${wrapAttribute(collection_name)}.list(ctx.$context).ids([id]).fetch();
44
+ return item!;
45
+ });
40
46
  return { canAccess: response?.allowed || false, message: response?.reason || "" };
41
47
  };`,
42
48