realrashid / sweet-alert

A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAVASCRIPT'S POPUP BOXES FOR LARAVEL
https://realrashid.github.io/sweet-alert
MIT License
1.12k stars 164 forks source link

Always load JS not working #165

Closed crook100 closed 4 months ago

crook100 commented 9 months ago

I think that the Always load JS function is not working as intended.

When setting alwaysLoadJS=true and neverLoadJS=false in the config, the script isnt loaded and thus causes javascript errors in the page.

Taking a look at resources/views/vendor/sweetalert/alert.blade.php, we can see this:

@if (config('sweetalert.alwaysLoadJS') === false && config('sweetalert.neverLoadJS') === false)
      <script src="{{ $cdn ?? asset('vendor/sweetalert/sweetalert.all.js') }}"></script>
@endif

So it only include the script if BOTH alwaysLoadJS and neverLoadJS are false, which is kinda weird.

Changing the if condition to: @if (config('sweetalert.alwaysLoadJS') !== false && config('sweetalert.neverLoadJS') === false) fixes the issue.

I've never had this issue with previous versions, only with the current one, and I'm pretty sure this is a bug.

realrashid commented 8 months ago

Okay i will look into this.

hoRacy commented 4 months ago

It has been confusing me for 2 years, but I figured maybe I'm not understand / missing something. I created my own alert.blade.php with the intended logic.

It seems that these two fragments of config are contradictory:

/*
|--------------------------------------------------------------------------
| Never load the sweetalert.all.js
|--------------------------------------------------------------------------
| If you want to handle the sweet alert js package by yourself
| (for eg. you might want to use laravel mix) then this can be
| handy.
| If you set always load js to true & never load js to false,
| it's going to prioritize the never load js. <------------- THIS
|
| alwaysLoadJs = true  & neverLoadJs = true  => js will not be loaded
| alwaysLoadJs = true  & neverLoadJs = false => js will be loaded <------------- THIS
| alwaysLoadJs = false & neverLoadJs = false => js will be loaded when
| you set alert/toast by using the facade/helper functions.
*/
hoRacy commented 4 months ago

Plese check my alert.blade.php file , which works as expected. It disables script loading ALWAYS when neverLoadJS is true, but when alwaysLoadJS is set to true and neverLoadJS is set to false, it always loads the script.

`

@if (config('sweetalert.alwaysLoadJS') === true || Session::has('alert.config') || Session::has('alert.delete')) @if (config('sweetalert.animation.enable')) @endif

@if (config('sweetalert.theme') != 'default')
    <link href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-{{ config('sweetalert.theme') }}" rel="stylesheet">
@endif

@if (config('sweetalert.neverLoadJS') === false)
    <script src="{{ $cdn ?? asset('vendor/sweetalert/sweetalert.all.js') }}"></script>
@endif

@if (Session::has('alert.delete') || Session::has('alert.config'))
    <script>
        @if (Session::has('alert.delete'))
        document.addEventListener('click', function(event) {
            if (event.target.matches('[data-confirm-delete]')) {
                event.preventDefault();
                Swal.fire({!! Session::pull('alert.delete') !!}).then(function(result) {
                    if (result.isConfirmed) {
                        var form = document.createElement('form');
                        form.action = event.target.href;
                        form.method = 'POST';
                        form.innerHTML = `
                    @csrf
                        @method('DELETE')
                        `;
                        document.body.appendChild(form);
                        form.submit();
                    }
                });
            }
        });
        @endif

        @if (Session::has('alert.config'))
        Swal.fire({!! Session::pull('alert.config') !!});
        @endif
    </script>
@endif

@endif

`

hoRacy commented 4 months ago

@realrashid - this change not resolving case, when You set alwaysLoadJS to true and not trigger any sweet alert via your package, because sweet-alert.js is wrapped in some additional if. As You wrote on documentation - this option should ALWAYS LOAD sweet-alert.js, not only when you previously trigger some alert. Plese check my code , which works as expected

/*
|--------------------------------------------------------------------------
| Always load the sweetalert.all.js
|--------------------------------------------------------------------------
| There might be situations where you will always want the sweet alert
| js package to be there for you. (for eg. you might use it heavily to
| show notifications or you might want to use the native js) then this
| might be handy.
|
*/
realrashid commented 3 months ago

@hoRacy Can you send a PR ?

hoRacy commented 3 months ago

@hoRacy Can you send a PR ?

Of course, here we go! https://github.com/realrashid/sweet-alert/pull/172