moonshot-rails 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5cff18680c071a47e756d4e2c374e225786f91c20fcf36cdff9874b6d3646fe3
4
+ data.tar.gz: 5650ffebc7e361df9cbb0bc3ed9f46adbd9b05f9f559dbc2a1520f5719e09cc2
5
+ SHA512:
6
+ metadata.gz: d36236cd06717459b4d7865d6377d200a8bce9013942f91ed2f0c0c0dc12d74b03a06198e41453447f3384e0f6039c256dcf2c6efee9f846fb331a9c2553031f
7
+ data.tar.gz: 99cf637fd557d987c3ea2336202ec867851839c3c1983e8ad46c555c142472e51d2b7cc5924453053b4e68b7ea3964022c3475d247614886d22c2bc7b5749c48
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 Catalin Ionescu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # Moonshot Rails
@@ -0,0 +1,7 @@
1
+ import Shepherd from './shepherd_controller'
2
+
3
+ export { Shepherd }
4
+
5
+ export function registerControllers(application) {
6
+ application.register('moonshot-shepherd', Shepherd)
7
+ }
@@ -0,0 +1,75 @@
1
+ import Shepherd from "shepherd.js";
2
+ import { post } from '@rails/request.js'
3
+ import { Controller } from "@hotwired/stimulus";
4
+
5
+ export default class extends Controller {
6
+ static values={
7
+ tourName: String,
8
+ endpoint: String,
9
+ config: Object,
10
+ context: Object
11
+ };
12
+
13
+ initialize() {
14
+ this.tour = new Shepherd.Tour(this.configValue.tour);
15
+ }
16
+
17
+ connect() {
18
+ const steps = this.processTourConfigAction(this.configValue.steps)
19
+
20
+ if (steps) {
21
+ this.addTourListeners()
22
+
23
+ this.tour.addSteps(steps);
24
+ this.tour.start();
25
+ }
26
+ }
27
+
28
+ addTourListeners() {
29
+ [
30
+ 'hide',
31
+ 'cancel',
32
+ 'complete',
33
+ 'start'
34
+ ].map(eventName => {
35
+ this.tour.on(eventName, (event) => this.processTourEvent({ event, tourName: this.tourNameValue, eventName }))
36
+ })
37
+ }
38
+
39
+ async processTourEvent({ event, tourName, eventName }) {
40
+ const body = JSON.stringify({ tour: tourName, event: eventName, context: this.contextValue })
41
+ const response = await post(this.endpointValue, { body })
42
+ if (!response.ok) {
43
+ console.warn('Failed', response)
44
+ }
45
+ }
46
+
47
+ processTourConfigAction(steps) {
48
+ if (!steps.length) { return false }
49
+
50
+ return steps.map((step => {
51
+ let {buttons: buttons, ...rest} = step;
52
+ if (buttons) {
53
+ buttons = buttons.map((button => {
54
+ switch (button.action) {
55
+ case "next":
56
+ button.action = this.tour.next;
57
+ break;
58
+
59
+ case "back":
60
+ button.action = this.tour.back;
61
+ break;
62
+
63
+ default:
64
+ button.action = this.tour.complete;
65
+ }
66
+ return button;
67
+ }));
68
+ }
69
+ return {
70
+ buttons: buttons,
71
+ ...rest
72
+ };
73
+ }));
74
+ }
75
+ }