Skip to content

Commit c911d05

Browse files
committed
Change working modals and modal-toggle.
Repeat restore open modal after validation errors.
1 parent 90e0648 commit c911d05

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

resources/js/controllers/modal_controller.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export default class extends ApplicationController {
116116

117117
// Load deferred data if URL is specified and no validation errors are present
118118
if (Object.keys(this.parametersValue).length !== 0 && !options.validateError) {
119-
this.asyncLoadData(JSON.parse(options.params));
119+
this.asyncLoadData(options.params);
120120
}
121121

122122
// Store the last open modal options
@@ -130,13 +130,13 @@ export default class extends ApplicationController {
130130
* Open the last modal if validation errors are present.
131131
*/
132132
openLastModal() {
133-
const lastOpenModal = this.getLastOpenModal();
134-
135133
// If no validation errors are present, do nothing
136134
if (this.element.querySelectorAll('.invalid-feedback').length === 0) {
137135
return;
138136
}
139137

138+
const lastOpenModal = this.lastOpenModal();
139+
140140
// Reopen the last modal if it matches the current slug
141141
if (lastOpenModal && lastOpenModal.slug === this.slugValue) {
142142
this.element.classList.remove('fade', 'in');
@@ -167,21 +167,21 @@ export default class extends ApplicationController {
167167
* @param options - Modal options to store.
168168
*/
169169
storeLastOpenModal(options) {
170-
sessionStorage.setItem(this.SESSION_KEY_FOR_LAST_OPEN_MODAL, JSON.stringify(options));
170+
window.sessionStorage.setItem(this.constructor.SESSION_KEY_FOR_LAST_OPEN_MODAL, JSON.stringify(options));
171171
}
172172

173173
/**
174174
* Retrieve the last opened modal options from session storage.
175175
* @returns {Object|false} - The last opened modal options or false if not found.
176176
*/
177-
getLastOpenModal() {
178-
return JSON.parse(sessionStorage.getItem(this.SESSION_KEY_FOR_LAST_OPEN_MODAL)) ?? false;
177+
lastOpenModal() {
178+
return JSON.parse(sessionStorage.getItem(this.constructor.SESSION_KEY_FOR_LAST_OPEN_MODAL)) ?? false;
179179
}
180180

181181
/**
182182
* Clear the last opened modal options from session storage.
183183
*/
184184
clearLastOpenModal() {
185-
sessionStorage.removeItem(this.SESSION_KEY_FOR_LAST_OPEN_MODAL);
185+
sessionStorage.removeItem(this.constructor.SESSION_KEY_FOR_LAST_OPEN_MODAL);
186186
}
187187
}

resources/js/controllers/modal_toggle_controller.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,35 @@ import ApplicationController from "./application_controller";
22

33
export default class extends ApplicationController {
44

5+
static values = {
6+
title: {
7+
type: String,
8+
default: null,
9+
},
10+
key: {
11+
type: String,
12+
},
13+
action: {
14+
type: String,
15+
},
16+
parameters: {
17+
type: Object,
18+
},
19+
open: {
20+
type: Boolean,
21+
default: false,
22+
},
23+
}
24+
525
/**
626
*
727
*/
828
connect() {
929
setTimeout(() => {
10-
if (!this.data.get('open')) {
30+
if (!this.openValue) {
1131
return;
1232
}
33+
1334
this.modal.classList.remove('fade', 'in');
1435
this.targetModal();
1536
});
@@ -20,14 +41,15 @@ export default class extends ApplicationController {
2041
* @returns {*}
2142
*/
2243
targetModal(event) {
23-
this.application.getControllerForElementAndIdentifier(this.modal, 'modal')
44+
this.application
45+
.getControllerForElementAndIdentifier(this.modal, 'modal')
2446
.open({
25-
title: this.data.get('title') || this.modal.dataset.modalTitle,
26-
submit: this.data.get('action'),
27-
params: this.data.get('params', '[]'),
47+
title: this.titleValue || this.modal.dataset.modalTitle,
48+
submit: this.actionValue,
49+
params: this.parametersValue,
2850
});
2951

30-
if(event) {
52+
if (event) {
3153
return event.preventDefault();
3254
}
3355
}
@@ -37,6 +59,13 @@ export default class extends ApplicationController {
3759
* @returns {HTMLElement}
3860
*/
3961
get modal() {
40-
return document.getElementById(`screen-modal-${this.data.get('key')}`);
62+
63+
let modal = document.getElementById(`screen-modal-${this.keyValue}`);
64+
65+
if (modal === null) {
66+
this.toast('The modal element does not exist.', 'warning');
67+
}
68+
69+
return modal;
4170
}
4271
}

resources/views/actions/modal.blade.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
{{ $attributes }}
44
data-controller="modal-toggle"
55
data-action="click->modal-toggle#targetModal"
6-
data-modal-toggle-title="{{ $modalTitle ?? $title ?? '' }}"
7-
data-modal-toggle-key="{{ $modal ?? '' }}"
8-
data-modal-toggle-async="{{ $async }}"
9-
data-modal-toggle-params='@json($parameters)'
10-
data-modal-toggle-action="{{ $action }}"
11-
data-modal-toggle-open="{{ $open }}"
6+
data-modal-toggle-open-value="{{ var_export($open) }}"
7+
data-modal-toggle-title-value="{{ $modalTitle ?? $title ?? '' }}"
8+
data-modal-toggle-key-value="{{ $modal ?? '' }}"
9+
data-modal-toggle-action-value="{{ $action }}"
10+
11+
@if(!empty($parameters))
12+
data-modal-toggle-parameters-value='@json($parameters)'
13+
@endif
1214
>
1315

1416
@isset($icon)
1517
<x-orchid-icon :path="$icon" class="overflow-visible"/>
1618
@endisset
1719

18-
<span>{{ $name ?? '' }}</span>
20+
{{ $name ?? '' }}
1921
</button>
2022
@endcomponent

0 commit comments

Comments
 (0)