@sprucelabs/spruce-form-utils 18.0.3 → 18.0.4

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.
@@ -4,17 +4,10 @@ const heartwood_view_controllers_1 = require("@sprucelabs/heartwood-view-control
4
4
  const schema_1 = require("@sprucelabs/schema");
5
5
  const spruce_image_utils_1 = require("@sprucelabs/spruce-image-utils");
6
6
  class FormPlayerCardViewController extends heartwood_view_controllers_1.AbstractViewController {
7
- swipeVc;
8
- static id = 'form-player-card';
9
- onChangeHandler;
10
- builderSource;
11
- shouldIgnoreChanges = false;
12
- router;
13
- onSaveAndBeDoneHandler;
14
- uploader;
15
- uploads = {};
16
7
  constructor(options) {
17
8
  super(options);
9
+ this.shouldIgnoreChanges = false;
10
+ this.uploads = {};
18
11
  const { onChange, isBusy, id, onSaveAndBeDone, ...builderSource } = options;
19
12
  this.builderSource = (0, heartwood_view_controllers_1.removeUniversalViewOptions)(builderSource);
20
13
  this.onSaveAndBeDoneHandler = onSaveAndBeDone;
@@ -40,8 +33,9 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
40
33
  this.updateFooter();
41
34
  }
42
35
  renderFooter() {
43
- const totalPages = this.builderSource.pages?.length ?? 0;
44
- const currentPage = this.swipeVc?.getPresentSlide() ?? 0;
36
+ var _a, _b, _c, _d;
37
+ const totalPages = (_b = (_a = this.builderSource.pages) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
38
+ const currentPage = (_d = (_c = this.swipeVc) === null || _c === void 0 ? void 0 : _c.getPresentSlide()) !== null && _d !== void 0 ? _d : 0;
45
39
  const buttons = [
46
40
  {
47
41
  id: 'saveProgress',
@@ -77,22 +71,24 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
77
71
  });
78
72
  }
79
73
  async handleClickSaveAndDone() {
74
+ var _a, _b;
80
75
  if (this.onSaveAndBeDoneHandler) {
81
- await this.onSaveAndBeDoneHandler?.();
76
+ await ((_a = this.onSaveAndBeDoneHandler) === null || _a === void 0 ? void 0 : _a.call(this));
82
77
  return;
83
78
  }
84
79
  await this.alert({
85
80
  message: `Progress saved, lets get outa here! 🚀`,
86
81
  style: 'success',
87
82
  });
88
- await this.router?.redirect('profile.root');
83
+ await ((_b = this.router) === null || _b === void 0 ? void 0 : _b.redirect('profile.root'));
89
84
  }
90
85
  async handleClickNext() {
91
86
  await this.swipeVc.jumpToSlide(this.swipeVc.getPresentSlide() + 1);
92
87
  }
93
88
  renderHeader(options) {
89
+ var _a;
94
90
  return {
95
- title: options.title ?? 'Loading your form...',
91
+ title: (_a = options.title) !== null && _a !== void 0 ? _a : 'Loading your form...',
96
92
  subtitle: options.subtitle,
97
93
  };
98
94
  }
@@ -100,7 +96,8 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
100
96
  this.swipeVc.setIsBusy(isLoading);
101
97
  }
102
98
  renderSlides(options) {
103
- const slides = options.pages?.map(({ title, ...form }) => {
99
+ var _a, _b;
100
+ const slides = (_b = (_a = options.pages) === null || _a === void 0 ? void 0 : _a.map(({ title, ...form }) => {
104
101
  const formVc = this.Controller('form', {
105
102
  ...form,
106
103
  shouldShowSubmitControls: false,
@@ -112,15 +109,16 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
112
109
  title,
113
110
  form: formVc.render(),
114
111
  };
115
- }) ?? [];
112
+ })) !== null && _b !== void 0 ? _b : [];
116
113
  return slides;
117
114
  }
118
115
  async handleFormChange(formVc) {
116
+ var _a;
119
117
  const schema = formVc.getSchema();
120
118
  for (const [name, field] of Object.entries(schema.fields)) {
121
119
  if (field.type === 'image' && formVc.isFieldRendering(name)) {
122
120
  const value = formVc.getValue(name);
123
- if (value && this.uploads[name]?.original !== value.base64) {
121
+ if (value && ((_a = this.uploads[name]) === null || _a === void 0 ? void 0 : _a.original) !== value.base64) {
124
122
  const file = await this.uploader.upload(value);
125
123
  this.uploads[name] = {
126
124
  original: value.base64,
@@ -132,8 +130,9 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
132
130
  await this.optionallyInvokeChangeHandler();
133
131
  }
134
132
  async optionallyInvokeChangeHandler() {
133
+ var _a;
135
134
  if (!this.shouldIgnoreChanges) {
136
- await this.onChangeHandler?.({ values: this.getValues() });
135
+ await ((_a = this.onChangeHandler) === null || _a === void 0 ? void 0 : _a.call(this, { values: this.getValues() }));
137
136
  }
138
137
  }
139
138
  getHasCriticalError() {
@@ -152,8 +151,8 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
152
151
  const { router, ...source } = options;
153
152
  const { title, subtitle } = this.renderHeader(source);
154
153
  this.router = router;
155
- this.swipeVc.setHeaderTitle(title ?? null);
156
- this.swipeVc.setHeaderSubtitle(subtitle ?? null);
154
+ this.swipeVc.setHeaderTitle(title !== null && title !== void 0 ? title : null);
155
+ this.swipeVc.setHeaderSubtitle(subtitle !== null && subtitle !== void 0 ? subtitle : null);
157
156
  if (source.pages) {
158
157
  this.swipeVc.setSections(this.renderSlides(source));
159
158
  }
@@ -194,9 +193,9 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
194
193
  return values;
195
194
  }
196
195
  getFormVcs() {
196
+ var _a;
197
197
  const slides = this.swipeVc.getSlides();
198
- return (slides?.map((s) => s.form?.controller) ??
199
- []);
198
+ return ((_a = slides === null || slides === void 0 ? void 0 : slides.map((s) => { var _a; return (_a = s.form) === null || _a === void 0 ? void 0 : _a.controller; })) !== null && _a !== void 0 ? _a : []);
200
199
  }
201
200
  getFormVc(idx) {
202
201
  const formVc = this.getFormVcs()[idx];
@@ -216,4 +215,5 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
216
215
  return this.swipeVc.render();
217
216
  }
218
217
  }
218
+ FormPlayerCardViewController.id = 'form-player-card';
219
219
  exports.default = FormPlayerCardViewController;
@@ -23,15 +23,16 @@ function validateOptions(pages, values) {
23
23
  }
24
24
  const formCompletionCalculator = {
25
25
  calculate(pages, values) {
26
+ var _a, _b, _c, _d;
26
27
  validateOptions(pages, values);
27
28
  const requiredFields = [];
28
29
  let totalFields = 0;
29
30
  for (const page of pages) {
30
- const fields = page.schema.fields ?? {};
31
+ const fields = (_a = page.schema.fields) !== null && _a !== void 0 ? _a : {};
31
32
  for (const section of page.sections) {
32
- for (const field of section.fields ?? []) {
33
- const fieldKey = field.name ?? field;
34
- const f = fields[fieldKey] ?? {};
33
+ for (const field of (_b = section.fields) !== null && _b !== void 0 ? _b : []) {
34
+ const fieldKey = (_c = field.name) !== null && _c !== void 0 ? _c : field;
35
+ const f = (_d = fields[fieldKey]) !== null && _d !== void 0 ? _d : {};
35
36
  if (f.isRequired) {
36
37
  totalFields++;
37
38
  requiredFields.push(fieldKey);
@@ -44,7 +45,7 @@ const formCompletionCalculator = {
44
45
  }
45
46
  let totalAnswered = 0;
46
47
  for (const value of values) {
47
- totalAnswered += Object.keys(value ?? {}).filter((k) => {
48
+ totalAnswered += Object.keys(value !== null && value !== void 0 ? value : {}).filter((k) => {
48
49
  return !!value[k] && requiredFields.indexOf(k) !== -1;
49
50
  }).length;
50
51
  }
@@ -4,9 +4,6 @@ exports.buildFormCard = void 0;
4
4
  const heartwood_view_controllers_1 = require("@sprucelabs/heartwood-view-controllers");
5
5
  const schema_1 = require("@sprucelabs/schema");
6
6
  class FormCardViewController extends heartwood_view_controllers_1.AbstractViewController {
7
- static id = 'card';
8
- formVc;
9
- cardVc;
10
7
  constructor(options) {
11
8
  super(options);
12
9
  const { fields, id, header, ...form } = (0, schema_1.assertOptions)(options, [
@@ -61,6 +58,7 @@ class FormCardViewController extends heartwood_view_controllers_1.AbstractViewCo
61
58
  return this.cardVc.render();
62
59
  }
63
60
  }
61
+ FormCardViewController.id = 'card';
64
62
  exports.default = FormCardViewController;
65
63
  function buildFormCard(options) {
66
64
  return options;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sprucelabs/spruce-form-utils",
3
3
  "description": "Support for working with forms and Sprucebot. 📄",
4
- "version": "18.0.3",
4
+ "version": "18.0.4",
5
5
  "skill": {
6
6
  "namespace": "forms"
7
7
  },