@radioactive-labs/plutonium 0.4.3 → 0.4.5

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.
@@ -0,0 +1,109 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // Connects to data-controller="key-value-store"
4
+ export default class extends Controller {
5
+ static targets = ["container", "pair", "template", "addButton", "keyInput", "valueInput"]
6
+ static values = { limit: Number }
7
+
8
+ connect() {
9
+ this.updateIndices()
10
+ this.updateAddButtonState()
11
+ }
12
+
13
+ addPair(event) {
14
+ event.preventDefault()
15
+
16
+ if (this.pairTargets.length >= this.limitValue) {
17
+ return
18
+ }
19
+
20
+ const template = this.templateTarget
21
+ const newPair = template.content.cloneNode(true)
22
+ const index = this.pairTargets.length
23
+
24
+ // Update the template placeholders with actual indices
25
+ this.updatePairIndices(newPair, index)
26
+
27
+ this.containerTarget.appendChild(newPair)
28
+ this.updateAddButtonState()
29
+
30
+ // Focus on the key input of the new pair
31
+ const newKeyInput = this.containerTarget.lastElementChild.querySelector('[data-key-value-store-target="keyInput"]')
32
+ if (newKeyInput) {
33
+ newKeyInput.focus()
34
+ }
35
+ }
36
+
37
+ removePair(event) {
38
+ event.preventDefault()
39
+
40
+ const pair = event.target.closest('[data-key-value-store-target="pair"]')
41
+ if (pair) {
42
+ pair.remove()
43
+ this.updateIndices()
44
+ this.updateAddButtonState()
45
+ }
46
+ }
47
+
48
+ updateIndices() {
49
+ this.pairTargets.forEach((pair, index) => {
50
+ const keyInput = pair.querySelector('[data-key-value-store-target="keyInput"]')
51
+ const valueInput = pair.querySelector('[data-key-value-store-target="valueInput"]')
52
+
53
+ if (keyInput) {
54
+ keyInput.name = keyInput.name.replace(/\[\d+\]/, `[${index}]`)
55
+ }
56
+ if (valueInput) {
57
+ valueInput.name = valueInput.name.replace(/\[\d+\]/, `[${index}]`)
58
+ }
59
+ })
60
+ }
61
+
62
+ updatePairIndices(element, index) {
63
+ const inputs = element.querySelectorAll('input')
64
+ inputs.forEach(input => {
65
+ if (input.name) {
66
+ input.name = input.name.replace('__INDEX__', index)
67
+ }
68
+ })
69
+ }
70
+
71
+ updateAddButtonState() {
72
+ const addButton = this.addButtonTarget
73
+ if (this.pairTargets.length >= this.limitValue) {
74
+ addButton.disabled = true
75
+ addButton.classList.add('opacity-50', 'cursor-not-allowed')
76
+ } else {
77
+ addButton.disabled = false
78
+ addButton.classList.remove('opacity-50', 'cursor-not-allowed')
79
+ }
80
+ }
81
+
82
+ // Serialize the current key-value pairs to JSON
83
+ toJSON() {
84
+ const pairs = {}
85
+ this.pairTargets.forEach(pair => {
86
+ const keyInput = pair.querySelector('[data-key-value-store-target="keyInput"]')
87
+ const valueInput = pair.querySelector('[data-key-value-store-target="valueInput"]')
88
+
89
+ if (keyInput && valueInput && keyInput.value.trim()) {
90
+ pairs[keyInput.value.trim()] = valueInput.value
91
+ }
92
+ })
93
+ return JSON.stringify(pairs)
94
+ }
95
+
96
+ // Get the current key-value pairs as an object
97
+ toObject() {
98
+ const pairs = {}
99
+ this.pairTargets.forEach(pair => {
100
+ const keyInput = pair.querySelector('[data-key-value-store-target="keyInput"]')
101
+ const valueInput = pair.querySelector('[data-key-value-store-target="valueInput"]')
102
+
103
+ if (keyInput && valueInput && keyInput.value.trim()) {
104
+ pairs[keyInput.value.trim()] = valueInput.value
105
+ }
106
+ })
107
+ return pairs
108
+ }
109
+ }
@@ -19,6 +19,7 @@ import AttachmentPreviewContainerController from "./attachment_preview_container
19
19
  import SidebarController from "./sidebar_controller.js"
20
20
  import PasswordVisibilityController from "./password_visibility_controller.js"
21
21
  import RemoteModalController from "./remote_modal_controller.js"
22
+ import KeyValueStoreController from "./key_value_st\ore_controller.js"
22
23
 
23
24
  export default function (application) {
24
25
  // Register controllers here
@@ -42,4 +43,5 @@ export default function (application) {
42
43
  application.register("attachment-preview", AttachmentPreviewController)
43
44
  application.register("attachment-preview-container", AttachmentPreviewContainerController)
44
45
  application.register("remote-modal", RemoteModalController)
46
+ application.register("key-value-store", KeyValueStoreController)
45
47
  }
@@ -1,5 +1,3 @@
1
- import * as Turbo from "@hotwired/turbo"
2
-
3
- import "./turbo_debug"
4
- // import "./turbo_actions"
1
+ import "./turbo_actions"
2
+ // import "./turbo_debug"
5
3
  // import "./turbo_frame_monkey_patch"