This repository has been archived by the owner on May 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LivewireResponseManager.php
76 lines (58 loc) · 1.91 KB
/
LivewireResponseManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Flasher\Livewire;
use Flasher\Prime\Config\ConfigInterface;
final class LivewireResponseManager
{
private $config;
public function __construct(ConfigInterface $config)
{
$this->config = $config;
}
public function render(): string
{
$importScript = '';
$rootScript = $this->config->get('root_script');
if (!empty($rootScript)) {
$importScript = <<<JAVASCRIPT
if (!window.Flasher && !document.querySelector('script[src="${rootScript}"]')) {
var tag = document.createElement('script');
tag.setAttribute('src', '${rootScript}');
tag.setAttribute('type', 'text/javascript');
document.body.appendChild(tag);
}
JAVASCRIPT;
}
return <<<JAVASCRIPT
<script type="text/javascript">
${importScript}
window.addEventListener('flasher:render', function (event) {
Flasher.getInstance().render(event.detail);
});
window.addEventListener('flasher:sweet_alert:promise', function (event) {
var envelope = event.detail.envelope;
var context = envelope.context;
if (!context.livewire || !context.livewire.id) {
return;
}
var detail = event.detail;
if (envelope.livewire_context) {
detail.context = envelope.livewire_context;
}
var component = context.livewire.id;
Livewire.components.emitSelf(component, 'sweetAlertEvent', detail);
var promise = event.detail.promise;
if (promise.isConfirmed) {
Livewire.components.emitSelf(component, 'sweetAlertConfirmed', detail);
}
if (promise.isDenied) {
Livewire.components.emitSelf(component, 'sweetAlertDenied', detail);
}
if (promise.isDismissed) {
Livewire.components.emitSelf(component, 'sweetAlertDismissed', detail);
}
}, false);
</script>
JAVASCRIPT;
}
}