@sprucelabs/spruce-form-utils 10.6.2 → 11.1.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.
@@ -1,39 +1,29 @@
1
- import { AbstractViewController, CardViewController, FormViewController, ViewControllerOptions } from '@sprucelabs/heartwood-view-controllers';
1
+ import { AbstractViewController, FormViewController, SwipeCardViewController, ViewControllerOptions, Router } from '@sprucelabs/heartwood-view-controllers';
2
2
  import { SpruceSchemas } from '@sprucelabs/spruce-core-schemas';
3
- export declare type FormPlayerImportObject = SpruceSchemas.HeartwoodViewControllers.v2021_02_11.FormBuilderImportExportObject & {
4
- values: Record<string, any>[];
5
- };
6
- declare type ChangeHandler = (options: {
7
- values: Record<string, any>[];
8
- }) => Promise<void> | void;
9
- export declare type FormPlayerCardViewControllerOptions = Partial<FormPlayerImportObject> & {
10
- onChange?: ChangeHandler;
11
- footer?: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CardFooter;
12
- isBusy?: boolean;
13
- id?: string;
14
- };
15
3
  export default class FormPlayerCardViewController extends AbstractViewController<SpruceSchemas.HeartwoodViewControllers.v2021_02_11.Card> {
16
- private cardVc;
4
+ private swipeVc;
17
5
  static id: string;
18
6
  private onChangeHandler?;
19
7
  private builderSource;
20
8
  private shouldIgnoreChanges;
9
+ private router?;
21
10
  constructor(options: FormPlayerCardViewControllerOptions & ViewControllerOptions);
22
- private buildHeader;
11
+ private SwipeVc;
12
+ private handleSlideChange;
13
+ private renderFooter;
14
+ private handleClickSaveProgress;
15
+ private handleClickSaveAndDone;
16
+ private handleClickNext;
17
+ private renderHeader;
23
18
  setIsBusy(isLoading: boolean): void;
24
- private buildSlides;
19
+ private renderSlides;
25
20
  private handleOnChange;
26
21
  getHasCriticalError(): boolean;
27
22
  getIsBusy(): boolean;
28
23
  setCriticalError(err: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CriticalError): void;
29
- getCardVc(): import("@sprucelabs/heartwood-view-controllers").SwipeViewControllerImpl & {
30
- isBusy: () => boolean;
31
- setIsBusy: (isBusy: boolean) => void;
32
- getHasCriticalError: () => boolean;
33
- setCriticalError: (criticalError: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CriticalError) => void;
34
- clearCriticalError: () => void;
35
- } & CardViewController;
36
- load(source: Partial<FormPlayerImportObject>): Promise<void>;
24
+ getSwipeVc(): SwipeCardViewController;
25
+ load(options: PlayerLoadOptions): Promise<void>;
26
+ private updateFooter;
37
27
  setValues(values: FormPlayerImportObject['values']): Promise<void>;
38
28
  getValues(): Record<string, any>[];
39
29
  getFormVcs(): FormViewController<any>[];
@@ -41,4 +31,18 @@ export default class FormPlayerCardViewController extends AbstractViewController
41
31
  getSourceBuilder(): Partial<FormPlayerImportObject>;
42
32
  render(): SpruceSchemas.HeartwoodViewControllers.v2021_02_11.Card;
43
33
  }
34
+ export declare type FormPlayerImportObject = SpruceSchemas.HeartwoodViewControllers.v2021_02_11.FormBuilderImportExportObject & {
35
+ values: Record<string, any>[];
36
+ };
37
+ declare type ChangeHandler = (options: {
38
+ values: Record<string, any>[];
39
+ }) => Promise<void> | void;
40
+ export declare type FormPlayerCardViewControllerOptions = Partial<FormPlayerImportObject> & {
41
+ onChange?: ChangeHandler;
42
+ isBusy?: boolean;
43
+ id?: string;
44
+ };
45
+ export declare type PlayerLoadOptions = Partial<FormPlayerImportObject> & {
46
+ router?: Router;
47
+ };
44
48
  export {};
@@ -24,26 +24,84 @@ export default class FormPlayerCardViewController extends AbstractViewController
24
24
  constructor(options) {
25
25
  super(options);
26
26
  this.shouldIgnoreChanges = false;
27
- const { onChange, isBusy, id, footer } = options, builderSource = __rest(options
28
- //@ts-ignore
29
- , ["onChange", "isBusy", "id", "footer"]);
30
- //@ts-ignore
31
- this.cardVc = this.vcFactory.Controller('swipeCard', {
32
- id,
33
- header: this.buildHeader(builderSource),
34
- slides: this.buildSlides(builderSource),
35
- footer,
36
- });
27
+ const { onChange, isBusy, id } = options, builderSource = __rest(options, ["onChange", "isBusy", "id"]);
28
+ this.builderSource = removeUniversalViewOptions(builderSource);
29
+ this.swipeVc = this.SwipeVc(id, builderSource);
37
30
  if (!options.pages || isBusy) {
38
- this.cardVc.setIsBusy(true);
31
+ this.swipeVc.setIsBusy(true);
39
32
  }
40
33
  else if (options.values) {
41
34
  void this.setValues(options.values);
42
35
  }
43
36
  this.onChangeHandler = onChange;
44
- this.builderSource = removeUniversalViewOptions(builderSource);
45
37
  }
46
- buildHeader(options) {
38
+ SwipeVc(id, builderSource) {
39
+ return this.Controller('swipeCard', {
40
+ id,
41
+ header: this.renderHeader(builderSource),
42
+ slides: this.renderSlides(builderSource),
43
+ footer: this.renderFooter(),
44
+ onSlideChange: this.handleSlideChange.bind(this),
45
+ });
46
+ }
47
+ handleSlideChange() {
48
+ this.updateFooter();
49
+ }
50
+ renderFooter() {
51
+ var _a, _b, _c, _d;
52
+ const totalPages = (_b = (_a = this.builderSource.pages) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
53
+ const currentPage = (_d = (_c = this.swipeVc) === null || _c === void 0 ? void 0 : _c.getPresentSlide()) !== null && _d !== void 0 ? _d : 0;
54
+ const buttons = [
55
+ {
56
+ id: 'saveProgress',
57
+ label: 'Save progress',
58
+ onClick: this.handleClickSaveProgress.bind(this),
59
+ },
60
+ ];
61
+ if (totalPages === currentPage + 1) {
62
+ buttons.push({
63
+ id: 'save',
64
+ label: 'Save and be done',
65
+ type: 'primary',
66
+ onClick: this.handleClickSaveAndDone.bind(this),
67
+ });
68
+ }
69
+ else {
70
+ buttons.push({
71
+ id: 'next',
72
+ label: 'Next page',
73
+ type: 'primary',
74
+ onClick: this.handleClickNext.bind(this),
75
+ });
76
+ }
77
+ return {
78
+ buttons,
79
+ };
80
+ }
81
+ handleClickSaveProgress() {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ yield this.alert({
84
+ message: `Progress saved! 💪`,
85
+ style: 'success',
86
+ });
87
+ });
88
+ }
89
+ handleClickSaveAndDone() {
90
+ var _a;
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ yield this.alert({
93
+ message: `Progress saved, lets get outa here! 🚀`,
94
+ style: 'success',
95
+ });
96
+ yield ((_a = this.router) === null || _a === void 0 ? void 0 : _a.redirect('profile.root'));
97
+ });
98
+ }
99
+ handleClickNext() {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ yield this.swipeVc.jumpToSlide(this.swipeVc.getPresentSlide() + 1);
102
+ });
103
+ }
104
+ renderHeader(options) {
47
105
  var _a;
48
106
  return {
49
107
  title: (_a = options.title) !== null && _a !== void 0 ? _a : 'Loading your form...',
@@ -51,9 +109,9 @@ export default class FormPlayerCardViewController extends AbstractViewController
51
109
  };
52
110
  }
53
111
  setIsBusy(isLoading) {
54
- this.cardVc.setIsBusy(isLoading);
112
+ this.swipeVc.setIsBusy(isLoading);
55
113
  }
56
- buildSlides(options) {
114
+ renderSlides(options) {
57
115
  var _a, _b;
58
116
  return ((_b = (_a = options.pages) === null || _a === void 0 ? void 0 : _a.map((_a) => {
59
117
  var { title } = _a, form = __rest(_a, ["title"]);
@@ -74,30 +132,36 @@ export default class FormPlayerCardViewController extends AbstractViewController
74
132
  });
75
133
  }
76
134
  getHasCriticalError() {
77
- return this.cardVc.getHasCriticalError();
135
+ return this.swipeVc.getHasCriticalError();
78
136
  }
79
137
  getIsBusy() {
80
- return this.cardVc.isBusy();
138
+ return this.swipeVc.isBusy();
81
139
  }
82
140
  setCriticalError(err) {
83
- this.cardVc.setCriticalError(err);
141
+ this.swipeVc.setCriticalError(err);
84
142
  }
85
- getCardVc() {
86
- return this.cardVc;
143
+ getSwipeVc() {
144
+ return this.swipeVc;
87
145
  }
88
- load(source) {
146
+ load(options) {
89
147
  return __awaiter(this, void 0, void 0, function* () {
90
- const { title, subtitle } = this.buildHeader(source);
91
- this.cardVc.setHeaderTitle(title !== null && title !== void 0 ? title : null);
92
- this.cardVc.setHeaderSubtitle(subtitle !== null && subtitle !== void 0 ? subtitle : null);
93
- this.cardVc.setSections(this.buildSlides(source));
148
+ const { router } = options, source = __rest(options, ["router"]);
149
+ const { title, subtitle } = this.renderHeader(source);
150
+ this.router = router;
151
+ this.swipeVc.setHeaderTitle(title !== null && title !== void 0 ? title : null);
152
+ this.swipeVc.setHeaderSubtitle(subtitle !== null && subtitle !== void 0 ? subtitle : null);
153
+ this.swipeVc.setSections(this.renderSlides(source));
94
154
  if (source.values) {
95
155
  yield this.setValues(source.values);
96
156
  }
97
157
  this.builderSource = source;
98
- this.cardVc.setIsBusy(false);
158
+ this.updateFooter();
159
+ this.swipeVc.setIsBusy(false);
99
160
  });
100
161
  }
162
+ updateFooter() {
163
+ this.swipeVc.setFooter(this.router ? this.renderFooter() : null);
164
+ }
101
165
  setValues(values) {
102
166
  return __awaiter(this, void 0, void 0, function* () {
103
167
  if (!Array.isArray(values)) {
@@ -115,7 +179,7 @@ export default class FormPlayerCardViewController extends AbstractViewController
115
179
  }
116
180
  getFormVcs() {
117
181
  var _a, _b;
118
- return ((_b = (_a = this.cardVc.getSections()) === 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 : []);
182
+ 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 : []);
119
183
  }
120
184
  getFormVc(idx) {
121
185
  const formVc = this.getFormVcs()[idx];
@@ -132,7 +196,7 @@ export default class FormPlayerCardViewController extends AbstractViewController
132
196
  return this.builderSource;
133
197
  }
134
198
  render() {
135
- return this.cardVc.render();
199
+ return this.swipeVc.render();
136
200
  }
137
201
  }
138
202
  FormPlayerCardViewController.id = 'form-player-card';
@@ -1,39 +1,29 @@
1
- import { AbstractViewController, CardViewController, FormViewController, ViewControllerOptions } from '@sprucelabs/heartwood-view-controllers';
1
+ import { AbstractViewController, FormViewController, SwipeCardViewController, ViewControllerOptions, Router } from '@sprucelabs/heartwood-view-controllers';
2
2
  import { SpruceSchemas } from '@sprucelabs/spruce-core-schemas';
3
- export declare type FormPlayerImportObject = SpruceSchemas.HeartwoodViewControllers.v2021_02_11.FormBuilderImportExportObject & {
4
- values: Record<string, any>[];
5
- };
6
- declare type ChangeHandler = (options: {
7
- values: Record<string, any>[];
8
- }) => Promise<void> | void;
9
- export declare type FormPlayerCardViewControllerOptions = Partial<FormPlayerImportObject> & {
10
- onChange?: ChangeHandler;
11
- footer?: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CardFooter;
12
- isBusy?: boolean;
13
- id?: string;
14
- };
15
3
  export default class FormPlayerCardViewController extends AbstractViewController<SpruceSchemas.HeartwoodViewControllers.v2021_02_11.Card> {
16
- private cardVc;
4
+ private swipeVc;
17
5
  static id: string;
18
6
  private onChangeHandler?;
19
7
  private builderSource;
20
8
  private shouldIgnoreChanges;
9
+ private router?;
21
10
  constructor(options: FormPlayerCardViewControllerOptions & ViewControllerOptions);
22
- private buildHeader;
11
+ private SwipeVc;
12
+ private handleSlideChange;
13
+ private renderFooter;
14
+ private handleClickSaveProgress;
15
+ private handleClickSaveAndDone;
16
+ private handleClickNext;
17
+ private renderHeader;
23
18
  setIsBusy(isLoading: boolean): void;
24
- private buildSlides;
19
+ private renderSlides;
25
20
  private handleOnChange;
26
21
  getHasCriticalError(): boolean;
27
22
  getIsBusy(): boolean;
28
23
  setCriticalError(err: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CriticalError): void;
29
- getCardVc(): import("@sprucelabs/heartwood-view-controllers").SwipeViewControllerImpl & {
30
- isBusy: () => boolean;
31
- setCriticalError: (criticalError: SpruceSchemas.HeartwoodViewControllers.v2021_02_11.CriticalError) => void;
32
- getHasCriticalError: () => boolean;
33
- clearCriticalError: () => void;
34
- setIsBusy: (isBusy: boolean) => void;
35
- } & CardViewController;
36
- load(source: Partial<FormPlayerImportObject>): Promise<void>;
24
+ getSwipeVc(): SwipeCardViewController;
25
+ load(options: PlayerLoadOptions): Promise<void>;
26
+ private updateFooter;
37
27
  setValues(values: FormPlayerImportObject['values']): Promise<void>;
38
28
  getValues(): Record<string, any>[];
39
29
  getFormVcs(): FormViewController<any>[];
@@ -41,4 +31,18 @@ export default class FormPlayerCardViewController extends AbstractViewController
41
31
  getSourceBuilder(): Partial<FormPlayerImportObject>;
42
32
  render(): SpruceSchemas.HeartwoodViewControllers.v2021_02_11.Card;
43
33
  }
34
+ export declare type FormPlayerImportObject = SpruceSchemas.HeartwoodViewControllers.v2021_02_11.FormBuilderImportExportObject & {
35
+ values: Record<string, any>[];
36
+ };
37
+ declare type ChangeHandler = (options: {
38
+ values: Record<string, any>[];
39
+ }) => Promise<void> | void;
40
+ export declare type FormPlayerCardViewControllerOptions = Partial<FormPlayerImportObject> & {
41
+ onChange?: ChangeHandler;
42
+ isBusy?: boolean;
43
+ id?: string;
44
+ };
45
+ export declare type PlayerLoadOptions = Partial<FormPlayerImportObject> & {
46
+ router?: Router;
47
+ };
44
48
  export {};
@@ -17,26 +17,78 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
17
17
  constructor(options) {
18
18
  super(options);
19
19
  this.shouldIgnoreChanges = false;
20
- const { onChange, isBusy, id, footer } = options, builderSource = __rest(options
21
- //@ts-ignore
22
- , ["onChange", "isBusy", "id", "footer"]);
23
- //@ts-ignore
24
- this.cardVc = this.vcFactory.Controller('swipeCard', {
25
- id,
26
- header: this.buildHeader(builderSource),
27
- slides: this.buildSlides(builderSource),
28
- footer,
29
- });
20
+ const { onChange, isBusy, id } = options, builderSource = __rest(options, ["onChange", "isBusy", "id"]);
21
+ this.builderSource = (0, heartwood_view_controllers_1.removeUniversalViewOptions)(builderSource);
22
+ this.swipeVc = this.SwipeVc(id, builderSource);
30
23
  if (!options.pages || isBusy) {
31
- this.cardVc.setIsBusy(true);
24
+ this.swipeVc.setIsBusy(true);
32
25
  }
33
26
  else if (options.values) {
34
27
  void this.setValues(options.values);
35
28
  }
36
29
  this.onChangeHandler = onChange;
37
- this.builderSource = (0, heartwood_view_controllers_1.removeUniversalViewOptions)(builderSource);
38
30
  }
39
- buildHeader(options) {
31
+ SwipeVc(id, builderSource) {
32
+ return this.Controller('swipeCard', {
33
+ id,
34
+ header: this.renderHeader(builderSource),
35
+ slides: this.renderSlides(builderSource),
36
+ footer: this.renderFooter(),
37
+ onSlideChange: this.handleSlideChange.bind(this),
38
+ });
39
+ }
40
+ handleSlideChange() {
41
+ this.updateFooter();
42
+ }
43
+ renderFooter() {
44
+ var _a, _b, _c, _d;
45
+ const totalPages = (_b = (_a = this.builderSource.pages) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
46
+ const currentPage = (_d = (_c = this.swipeVc) === null || _c === void 0 ? void 0 : _c.getPresentSlide()) !== null && _d !== void 0 ? _d : 0;
47
+ const buttons = [
48
+ {
49
+ id: 'saveProgress',
50
+ label: 'Save progress',
51
+ onClick: this.handleClickSaveProgress.bind(this),
52
+ },
53
+ ];
54
+ if (totalPages === currentPage + 1) {
55
+ buttons.push({
56
+ id: 'save',
57
+ label: 'Save and be done',
58
+ type: 'primary',
59
+ onClick: this.handleClickSaveAndDone.bind(this),
60
+ });
61
+ }
62
+ else {
63
+ buttons.push({
64
+ id: 'next',
65
+ label: 'Next page',
66
+ type: 'primary',
67
+ onClick: this.handleClickNext.bind(this),
68
+ });
69
+ }
70
+ return {
71
+ buttons,
72
+ };
73
+ }
74
+ async handleClickSaveProgress() {
75
+ await this.alert({
76
+ message: `Progress saved! 💪`,
77
+ style: 'success',
78
+ });
79
+ }
80
+ async handleClickSaveAndDone() {
81
+ var _a;
82
+ await this.alert({
83
+ message: `Progress saved, lets get outa here! 🚀`,
84
+ style: 'success',
85
+ });
86
+ await ((_a = this.router) === null || _a === void 0 ? void 0 : _a.redirect('profile.root'));
87
+ }
88
+ async handleClickNext() {
89
+ await this.swipeVc.jumpToSlide(this.swipeVc.getPresentSlide() + 1);
90
+ }
91
+ renderHeader(options) {
40
92
  var _a;
41
93
  return {
42
94
  title: (_a = options.title) !== null && _a !== void 0 ? _a : 'Loading your form...',
@@ -44,9 +96,9 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
44
96
  };
45
97
  }
46
98
  setIsBusy(isLoading) {
47
- this.cardVc.setIsBusy(isLoading);
99
+ this.swipeVc.setIsBusy(isLoading);
48
100
  }
49
- buildSlides(options) {
101
+ renderSlides(options) {
50
102
  var _a, _b;
51
103
  return ((_b = (_a = options.pages) === null || _a === void 0 ? void 0 : _a.map((_a) => {
52
104
  var { title } = _a, form = __rest(_a, ["title"]);
@@ -65,27 +117,33 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
65
117
  }
66
118
  }
67
119
  getHasCriticalError() {
68
- return this.cardVc.getHasCriticalError();
120
+ return this.swipeVc.getHasCriticalError();
69
121
  }
70
122
  getIsBusy() {
71
- return this.cardVc.isBusy();
123
+ return this.swipeVc.isBusy();
72
124
  }
73
125
  setCriticalError(err) {
74
- this.cardVc.setCriticalError(err);
75
- }
76
- getCardVc() {
77
- return this.cardVc;
78
- }
79
- async load(source) {
80
- const { title, subtitle } = this.buildHeader(source);
81
- this.cardVc.setHeaderTitle(title !== null && title !== void 0 ? title : null);
82
- this.cardVc.setHeaderSubtitle(subtitle !== null && subtitle !== void 0 ? subtitle : null);
83
- this.cardVc.setSections(this.buildSlides(source));
126
+ this.swipeVc.setCriticalError(err);
127
+ }
128
+ getSwipeVc() {
129
+ return this.swipeVc;
130
+ }
131
+ async load(options) {
132
+ const { router } = options, source = __rest(options, ["router"]);
133
+ const { title, subtitle } = this.renderHeader(source);
134
+ this.router = router;
135
+ this.swipeVc.setHeaderTitle(title !== null && title !== void 0 ? title : null);
136
+ this.swipeVc.setHeaderSubtitle(subtitle !== null && subtitle !== void 0 ? subtitle : null);
137
+ this.swipeVc.setSections(this.renderSlides(source));
84
138
  if (source.values) {
85
139
  await this.setValues(source.values);
86
140
  }
87
141
  this.builderSource = source;
88
- this.cardVc.setIsBusy(false);
142
+ this.updateFooter();
143
+ this.swipeVc.setIsBusy(false);
144
+ }
145
+ updateFooter() {
146
+ this.swipeVc.setFooter(this.router ? this.renderFooter() : null);
89
147
  }
90
148
  async setValues(values) {
91
149
  if (!Array.isArray(values)) {
@@ -102,7 +160,7 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
102
160
  }
103
161
  getFormVcs() {
104
162
  var _a, _b;
105
- return ((_b = (_a = this.cardVc.getSections()) === 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 : []);
163
+ 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 : []);
106
164
  }
107
165
  getFormVc(idx) {
108
166
  const formVc = this.getFormVcs()[idx];
@@ -119,7 +177,7 @@ class FormPlayerCardViewController extends heartwood_view_controllers_1.Abstract
119
177
  return this.builderSource;
120
178
  }
121
179
  render() {
122
- return this.cardVc.render();
180
+ return this.swipeVc.render();
123
181
  }
124
182
  }
125
183
  exports.default = FormPlayerCardViewController;
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": "10.6.2",
4
+ "version": "11.1.1",
5
5
  "skill": {
6
6
  "namespace": "forms"
7
7
  },