@sprucelabs/spruce-form-utils 16.0.4 → 16.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,8 @@ export default class FormPlayerCardViewController extends AbstractViewController
8
8
  private shouldIgnoreChanges;
9
9
  private router?;
10
10
  private onSaveAndBeDoneHandler?;
11
+ private uploader;
12
+ private uploads;
11
13
  constructor(options: FormPlayerCardViewControllerOptions & ViewControllerOptions);
12
14
  private SwipeVc;
13
15
  private handleSlideChange;
@@ -18,7 +20,8 @@ export default class FormPlayerCardViewController extends AbstractViewController
18
20
  private renderHeader;
19
21
  setIsBusy(isLoading: boolean): void;
20
22
  private renderSlides;
21
- private handleOnChange;
23
+ private handleFormChange;
24
+ private optionallyInvokeChangeHandler;
22
25
  getHasCriticalError(): boolean;
23
26
  getIsBusy(): boolean;
24
27
  setCriticalError(err: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CriticalError): void;
@@ -27,6 +30,7 @@ export default class FormPlayerCardViewController extends AbstractViewController
27
30
  private updateFooter;
28
31
  setValues(values: FormPlayerImportObject['values']): Promise<void>;
29
32
  getValues(): Record<string, any>[];
33
+ private getValuesForForm;
30
34
  getFormVcs(): FormViewController<any>[];
31
35
  getFormVc(idx: number): FormViewController<any>;
32
36
  getSourceBuilder(): Partial<FormPlayerImportObject>;
@@ -13,14 +13,16 @@ var __rest = (this && this.__rest) || function (s, e) {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  const heartwood_view_controllers_1 = require("@sprucelabs/heartwood-view-controllers");
15
15
  const schema_1 = require("@sprucelabs/schema");
16
+ const spruce_image_utils_1 = require("@sprucelabs/spruce-image-utils");
16
17
  class FormPlayerCardViewController extends heartwood_view_controllers_1.AbstractViewController {
17
18
  constructor(options) {
18
19
  super(options);
19
20
  this.shouldIgnoreChanges = false;
21
+ this.uploads = {};
20
22
  const { onChange, isBusy, id, onSaveAndBeDone } = options, builderSource = __rest(options, ["onChange", "isBusy", "id", "onSaveAndBeDone"]);
21
23
  this.builderSource = (0, heartwood_view_controllers_1.removeUniversalViewOptions)(builderSource);
22
24
  this.onSaveAndBeDoneHandler = onSaveAndBeDone;
23
- this.swipeVc = this.SwipeVc(id, builderSource);
25
+ this.swipeVc = this.SwipeVc(id, this.builderSource);
24
26
  if (!options.pages || isBusy) {
25
27
  this.swipeVc.setIsBusy(true);
26
28
  }
@@ -105,17 +107,33 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
105
107
  }
106
108
  renderSlides(options) {
107
109
  var _a, _b;
108
- return ((_b = (_a = options.pages) === null || _a === void 0 ? void 0 : _a.map((_a) => {
110
+ const slides = (_b = (_a = options.pages) === null || _a === void 0 ? void 0 : _a.map((_a) => {
109
111
  var { title } = _a, form = __rest(_a, ["title"]);
110
- return ({
112
+ const formVc = this.Controller('form', Object.assign(Object.assign({}, form), { shouldShowSubmitControls: false, onChange: async () => {
113
+ await this.handleFormChange(formVc);
114
+ } }));
115
+ return {
111
116
  title,
112
- form: this.Controller('form', Object.assign(Object.assign({}, form), { shouldShowSubmitControls: false, onChange: async () => {
113
- await this.handleOnChange();
114
- } })).render(),
115
- });
116
- })) !== null && _b !== void 0 ? _b : []);
117
+ form: formVc.render(),
118
+ };
119
+ })) !== null && _b !== void 0 ? _b : [];
120
+ return slides;
117
121
  }
118
- async handleOnChange() {
122
+ async handleFormChange(formVc) {
123
+ var _a;
124
+ const schema = formVc.getSchema();
125
+ for (const [name, field] of Object.entries(schema.fields)) {
126
+ if (field.type === 'image' && formVc.isFieldRendering(name)) {
127
+ const value = formVc.getValue(name);
128
+ if (value && ((_a = this.uploads[name]) === null || _a === void 0 ? void 0 : _a.original) !== value.base64) {
129
+ const file = await this.uploader.upload(value);
130
+ this.uploads[name] = { original: value.base64, uploaded: file };
131
+ }
132
+ }
133
+ }
134
+ await this.optionallyInvokeChangeHandler();
135
+ }
136
+ async optionallyInvokeChangeHandler() {
119
137
  var _a;
120
138
  if (!this.shouldIgnoreChanges) {
121
139
  await ((_a = this.onChangeHandler) === null || _a === void 0 ? void 0 : _a.call(this, { values: this.getValues() }));
@@ -139,13 +157,19 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
139
157
  this.router = router;
140
158
  this.swipeVc.setHeaderTitle(title !== null && title !== void 0 ? title : null);
141
159
  this.swipeVc.setHeaderSubtitle(subtitle !== null && subtitle !== void 0 ? subtitle : null);
142
- this.swipeVc.setSections(this.renderSlides(source));
160
+ if (source.pages) {
161
+ this.swipeVc.setSections(this.renderSlides(source));
162
+ }
143
163
  if (source.values) {
144
164
  await this.setValues(source.values);
145
165
  }
146
166
  this.builderSource = source;
147
167
  this.updateFooter();
148
168
  this.swipeVc.setIsBusy(false);
169
+ const client = await this.connectToApi();
170
+ this.uploader = spruce_image_utils_1.RemoteImageUploader.Uploader({
171
+ client,
172
+ });
149
173
  }
150
174
  updateFooter() {
151
175
  this.swipeVc.setFooter(this.router ? this.renderFooter() : null);
@@ -158,14 +182,24 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
158
182
  this.shouldIgnoreChanges = true;
159
183
  await Promise.all(values.map(async (v, idx) => vcs[idx].setValues(v)));
160
184
  this.shouldIgnoreChanges = false;
161
- await this.handleOnChange();
185
+ await this.optionallyInvokeChangeHandler();
162
186
  }
163
187
  getValues() {
164
- return this.getFormVcs().map((vc) => vc.getValues());
188
+ return this.getFormVcs().map((vc) => this.getValuesForForm(vc));
189
+ }
190
+ getValuesForForm(vc) {
191
+ const values = vc.getValues();
192
+ for (const [name] of Object.entries(values)) {
193
+ if (this.uploads[name]) {
194
+ values[name] = this.uploads[name].uploaded;
195
+ }
196
+ }
197
+ return values;
165
198
  }
166
199
  getFormVcs() {
167
- var _a, _b;
168
- return ((_b = (_a = this.swipeVc.getSlides()) === null || _a === void 0 ? void 0 : _a.map((s) => { var _a; return (_a = s.form) === null || _a === void 0 ? void 0 : _a.controller; })) !== null && _b !== void 0 ? _b : []);
200
+ var _a;
201
+ const slides = this.swipeVc.getSlides();
202
+ 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 : []);
169
203
  }
170
204
  getFormVc(idx) {
171
205
  const formVc = this.getFormVcs()[idx];
@@ -8,6 +8,8 @@ export default class FormPlayerCardViewController extends AbstractViewController
8
8
  private shouldIgnoreChanges;
9
9
  private router?;
10
10
  private onSaveAndBeDoneHandler?;
11
+ private uploader;
12
+ private uploads;
11
13
  constructor(options: FormPlayerCardViewControllerOptions & ViewControllerOptions);
12
14
  private SwipeVc;
13
15
  private handleSlideChange;
@@ -18,7 +20,8 @@ export default class FormPlayerCardViewController extends AbstractViewController
18
20
  private renderHeader;
19
21
  setIsBusy(isLoading: boolean): void;
20
22
  private renderSlides;
21
- private handleOnChange;
23
+ private handleFormChange;
24
+ private optionallyInvokeChangeHandler;
22
25
  getHasCriticalError(): boolean;
23
26
  getIsBusy(): boolean;
24
27
  setCriticalError(err: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CriticalError): void;
@@ -27,6 +30,7 @@ export default class FormPlayerCardViewController extends AbstractViewController
27
30
  private updateFooter;
28
31
  setValues(values: FormPlayerImportObject['values']): Promise<void>;
29
32
  getValues(): Record<string, any>[];
33
+ private getValuesForForm;
30
34
  getFormVcs(): FormViewController<any>[];
31
35
  getFormVc(idx: number): FormViewController<any>;
32
36
  getSourceBuilder(): Partial<FormPlayerImportObject>;
@@ -20,14 +20,16 @@ var __rest = (this && this.__rest) || function (s, e) {
20
20
  };
21
21
  import { AbstractViewController, removeUniversalViewOptions, } from '@sprucelabs/heartwood-view-controllers';
22
22
  import { SchemaError } from '@sprucelabs/schema';
23
+ import { RemoteImageUploader, } from '@sprucelabs/spruce-image-utils';
23
24
  class FormPlayerCardViewController extends AbstractViewController {
24
25
  constructor(options) {
25
26
  super(options);
26
27
  this.shouldIgnoreChanges = false;
28
+ this.uploads = {};
27
29
  const { onChange, isBusy, id, onSaveAndBeDone } = options, builderSource = __rest(options, ["onChange", "isBusy", "id", "onSaveAndBeDone"]);
28
30
  this.builderSource = removeUniversalViewOptions(builderSource);
29
31
  this.onSaveAndBeDoneHandler = onSaveAndBeDone;
30
- this.swipeVc = this.SwipeVc(id, builderSource);
32
+ this.swipeVc = this.SwipeVc(id, this.builderSource);
31
33
  if (!options.pages || isBusy) {
32
34
  this.swipeVc.setIsBusy(true);
33
35
  }
@@ -118,17 +120,35 @@ class FormPlayerCardViewController extends AbstractViewController {
118
120
  }
119
121
  renderSlides(options) {
120
122
  var _a, _b;
121
- return ((_b = (_a = options.pages) === null || _a === void 0 ? void 0 : _a.map((_a) => {
123
+ const slides = (_b = (_a = options.pages) === null || _a === void 0 ? void 0 : _a.map((_a) => {
122
124
  var { title } = _a, form = __rest(_a, ["title"]);
123
- return ({
125
+ const formVc = this.Controller('form', Object.assign(Object.assign({}, form), { shouldShowSubmitControls: false, onChange: () => __awaiter(this, void 0, void 0, function* () {
126
+ yield this.handleFormChange(formVc);
127
+ }) }));
128
+ return {
124
129
  title,
125
- form: this.Controller('form', Object.assign(Object.assign({}, form), { shouldShowSubmitControls: false, onChange: () => __awaiter(this, void 0, void 0, function* () {
126
- yield this.handleOnChange();
127
- }) })).render(),
128
- });
129
- })) !== null && _b !== void 0 ? _b : []);
130
+ form: formVc.render(),
131
+ };
132
+ })) !== null && _b !== void 0 ? _b : [];
133
+ return slides;
130
134
  }
131
- handleOnChange() {
135
+ handleFormChange(formVc) {
136
+ var _a;
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ const schema = formVc.getSchema();
139
+ for (const [name, field] of Object.entries(schema.fields)) {
140
+ if (field.type === 'image' && formVc.isFieldRendering(name)) {
141
+ const value = formVc.getValue(name);
142
+ if (value && ((_a = this.uploads[name]) === null || _a === void 0 ? void 0 : _a.original) !== value.base64) {
143
+ const file = yield this.uploader.upload(value);
144
+ this.uploads[name] = { original: value.base64, uploaded: file };
145
+ }
146
+ }
147
+ }
148
+ yield this.optionallyInvokeChangeHandler();
149
+ });
150
+ }
151
+ optionallyInvokeChangeHandler() {
132
152
  var _a;
133
153
  return __awaiter(this, void 0, void 0, function* () {
134
154
  if (!this.shouldIgnoreChanges) {
@@ -155,13 +175,19 @@ class FormPlayerCardViewController extends AbstractViewController {
155
175
  this.router = router;
156
176
  this.swipeVc.setHeaderTitle(title !== null && title !== void 0 ? title : null);
157
177
  this.swipeVc.setHeaderSubtitle(subtitle !== null && subtitle !== void 0 ? subtitle : null);
158
- this.swipeVc.setSections(this.renderSlides(source));
178
+ if (source.pages) {
179
+ this.swipeVc.setSections(this.renderSlides(source));
180
+ }
159
181
  if (source.values) {
160
182
  yield this.setValues(source.values);
161
183
  }
162
184
  this.builderSource = source;
163
185
  this.updateFooter();
164
186
  this.swipeVc.setIsBusy(false);
187
+ const client = yield this.connectToApi();
188
+ this.uploader = RemoteImageUploader.Uploader({
189
+ client,
190
+ });
165
191
  });
166
192
  }
167
193
  updateFooter() {
@@ -176,15 +202,25 @@ class FormPlayerCardViewController extends AbstractViewController {
176
202
  this.shouldIgnoreChanges = true;
177
203
  yield Promise.all(values.map((v, idx) => __awaiter(this, void 0, void 0, function* () { return vcs[idx].setValues(v); })));
178
204
  this.shouldIgnoreChanges = false;
179
- yield this.handleOnChange();
205
+ yield this.optionallyInvokeChangeHandler();
180
206
  });
181
207
  }
182
208
  getValues() {
183
- return this.getFormVcs().map((vc) => vc.getValues());
209
+ return this.getFormVcs().map((vc) => this.getValuesForForm(vc));
210
+ }
211
+ getValuesForForm(vc) {
212
+ const values = vc.getValues();
213
+ for (const [name] of Object.entries(values)) {
214
+ if (this.uploads[name]) {
215
+ values[name] = this.uploads[name].uploaded;
216
+ }
217
+ }
218
+ return values;
184
219
  }
185
220
  getFormVcs() {
186
- var _a, _b;
187
- return ((_b = (_a = this.swipeVc.getSlides()) === null || _a === void 0 ? void 0 : _a.map((s) => { var _a; return (_a = s.form) === null || _a === void 0 ? void 0 : _a.controller; })) !== null && _b !== void 0 ? _b : []);
221
+ var _a;
222
+ const slides = this.swipeVc.getSlides();
223
+ 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 : []);
188
224
  }
189
225
  getFormVc(idx) {
190
226
  const formVc = this.getFormVcs()[idx];
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": "16.0.4",
4
+ "version": "16.1.0",
5
5
  "skill": {
6
6
  "namespace": "forms"
7
7
  },