Browse code

Add turbo frame compatibility

Benjamin Roth authored on28/02/2025 13:41:43
Showing2 changed files
... ...
@@ -1,4 +1,4 @@
1 1
 <h3><?= $this->generateLabel().$this->xlabel ?></h3>
2 2
 <div class="grid-container" data-rows="<?= $this->intRows ?>" data-cols="<?= $this->intCols ?>" data-field-id="<?= $this->id ?>">
3 3
 </div>
4
-<input type="hidden" name="<?= $this->id ?>" id="<?= $this->id ?>" value="<?= $this->value ?>">
5 4
\ No newline at end of file
5
+<input type="hidden" name="<?= $this->id ?>" id="ctrl_<?= $this->id ?>" value="<?= $this->value ?>">
6 6
\ No newline at end of file
... ...
@@ -1,4 +1,4 @@
1
-document.addEventListener('DOMContentLoaded', () => {
1
+function init() {
2 2
   const gridContainer = document.querySelector('.grid-container');
3 3
 
4 4
   if (!gridContainer) return;
... ...
@@ -11,7 +11,8 @@ document.addEventListener('DOMContentLoaded', () => {
11 11
   // Grid erstellen
12 12
   const fragment = document.createDocumentFragment();
13 13
   // Erstelle die Zellen basierend auf Reihen und Spalten (gridRows * gridCols)
14
-  for (let i = 0; i < gridRows * gridCols; i++) {
14
+  for (let i = 0; i < gridRows * gridCols; i++)
15
+  {
15 16
     const gridItem = document.createElement('div');
16 17
     gridItem.classList.add('grid-cell');
17 18
     gridItem.dataset.index = i; // Weise jeder Zelle ihren Index zu
... ...
@@ -25,10 +26,12 @@ document.addEventListener('DOMContentLoaded', () => {
25 26
    * @param {number} index - Der Index der Zelle
26 27
    */
27 28
   function toggleCell(index) {
28
-    if (selectedIndices.includes(index)) {
29
+    if (selectedIndices.includes(index))
30
+    {
29 31
       // Zelle abwählen
30 32
       removeRowOrColumn(index);
31
-    } else {
33
+    } else
34
+    {
32 35
       // Zelle hinzufügen
33 36
       selectedIndices.push(index);
34 37
       fillSelection(); // Rechteck auffüllen
... ...
@@ -46,14 +49,16 @@ document.addEventListener('DOMContentLoaded', () => {
46 49
     const row = Math.floor(index / gridCols); // Berechnet die aktuelle Reihe
47 50
     const col = index % gridCols; // Berechnet die aktuelle Spalte
48 51
 
49
-    if (col === 0) {
52
+    if (col === 0)
53
+    {
50 54
       // Entferne die gesamte Reihe, wenn das Feld in der ersten Spalte ist
51 55
       // UND entferne alle darunterliegenden Reihen
52 56
       selectedIndices = selectedIndices.filter(cell => {
53 57
         const cellRow = Math.floor(cell / gridCols);
54 58
         return cellRow < row; // Entferne alle Reihen >= der aktuellen
55 59
       });
56
-    } else {
60
+    } else
61
+    {
57 62
       // Entferne die gesamte Spalte, wenn das Feld NICHT in der ersten Spalte ist
58 63
       selectedIndices = selectedIndices.filter(cell => {
59 64
         const cellCol = cell % gridCols;
... ...
@@ -76,11 +81,13 @@ document.addEventListener('DOMContentLoaded', () => {
76 81
     const lastCol = Math.max(...selectedIndices.map(cell => cell % gridCols));
77 82
 
78 83
     // Prüfe jede Spalte von links nach rechts
79
-    for (let col = firstCol; col <= lastCol; col++) {
84
+    for (let col = firstCol; col <= lastCol; col++)
85
+    {
80 86
       // Überprüfen, ob diese Spalte komplett leer ist
81 87
       const isColumnEmpty = !selectedIndices.some(cell => cell % gridCols === col);
82 88
 
83
-      if (isColumnEmpty) {
89
+      if (isColumnEmpty)
90
+      {
84 91
         // Entferne alle Zellen in Spalten rechts von der leeren Spalte
85 92
         selectedIndices = selectedIndices.filter(cell => cell % gridCols < col);
86 93
         break; // Stoppe, wenn wir eine leere Spalte finden
... ...
@@ -102,8 +109,10 @@ document.addEventListener('DOMContentLoaded', () => {
102 109
 
103 110
     // Rechteck auffüllen: Alle Zellen zwischen den Ecken hinzufügen
104 111
     const newSelection = [];
105
-    for (let row = firstRow; row <= lastRow; row++) {
106
-      for (let col = firstCol; col <= lastCol; col++) {
112
+    for (let row = firstRow; row <= lastRow; row++)
113
+    {
114
+      for (let col = firstCol; col <= lastCol; col++)
115
+      {
107 116
         const cellIndex = row * gridCols + col;
108 117
         newSelection.push(cellIndex);
109 118
       }
... ...
@@ -133,9 +142,12 @@ document.addEventListener('DOMContentLoaded', () => {
133 142
    * Speichert die Auswahl in einem versteckten Eingabeelement
134 143
    */
135 144
   function saveSelection() {
136
-    const gridDataInput = document.getElementById('grid-data');
137
-    if (gridDataInput) {
145
+    const gridDataInput = document.getElementById('ctrl_vr_gpw_grid');
146
+    if (gridDataInput)
147
+    {
138 148
       gridDataInput.value = JSON.stringify(selectedIndices);
139 149
     }
140 150
   }
141
-});
151
+}
152
+document.addEventListener('DOMContentLoaded', () => {init()});
153
+document.addEventListener('turbo:render', () => {init()});