jaxon-php / jaxon-dialogs

Modal, alerts and confirmation dialogs for Jaxon with various javascript libraries https://www.jaxon-php.org.
BSD 3-Clause "New" or "Revised" License
5 stars 3 forks source link

[Request] Callback in SweetAlert Dialogs #8

Closed Waxter81 closed 6 years ago

Waxter81 commented 6 years ago

I need to make an URL redirection after user clicks button in a SweetAlert Success Dialog. Something like this:

    public function foo() {
        $this->response->dialog->success('Title','Content');
        $this->response->redirect('http://sample.com');     
        return $this->response;
    }

With a standard $response->alert() it works fine, but since SweetAlert is async, the script performs redirection before user can see the dialog.

SweetAlert allows callback functions which could help me to get my goal, so I have hacked your code implementing Modal Dialogs in SweetAlert to let me config custom options in dialog. Like this:

/jaxon-dialogs/src/Libraries/SweetAlert/Plugin.php

    public function show($title, $message, array $buttons, array $options = array())
    {
        if($message) $options['text'] = $message; 
        if($title) $options['title'] = $title;
        if(empty($options['type'])) $options['type'] = 'success';       

        // Show the alert
        $this->addCommand(array('cmd' => 'sweetalert.alert'), $options);
    }

Then, in /jaxon-dialogs/src/templates/sweetalert/alert.js , in sweetalert.alert registered method, I have a customized option to execute post-JS code after user clicks OK Button.

    if(args.data['callback'] != undefined) swal(args.data,function(res){eval(args.data['callback']);});
    else swal(args.data);   

Finally, in my Jaxon Method, I set this custom option before calling modal dialog:

    $buttons = array();
    $jsCallBack = "window.location='http://www.example.com'";
    $options = array('callback' => $jsCallBack);
    $dialog->show("Modal Dialog", $content, $buttons, $options);

Are there any easier or cleaner way to perform this?

Thanks!

feuzeu commented 6 years ago

Hi, Instead of hacking the library, you may consider writing your own response plugin. By doing so, you can register any js function with a command (eg. sweetalert.alert_redirect), and then invoke this command when the plugin is called in the PHP Response object.

Another option is to extend this library with a new class. See the Adding a new library section in the Readme file.

Waxter81 commented 6 years ago

Thanks! I will try it.

Waxter81 commented 6 years ago

¿Could you please help me to write my own response plugin outside the vendor folder?

I have write MyPlugin.php extending SweetAlert/Plugin.php. That works fine. But now, I don't know how to write a new alert.js file outside the vendor folder.

The render method in Library Class returns: return $this->xDialog->render('jaxon::dialogs::' . $sTemplate, array_merge($aLocalVars, $aVars));

Although I overwrite a render method in MyPlugin, and delete 'jaxon::dialogs::' rendering only $sTemplate path, it tries always to render the path in vendor folder:

Warning: include(....\vendor\jaxon-php\jaxon-core\templates\js/myAlert.js.php): failed to open stream: No such file or directory in ...\vendor\jaxon-php\jaxon-core\src\Utils\Template\Renderer.php on line <i>37</i>

feuzeu commented 6 years ago

To be able to render templates from a given directory, you first need to declare a view namespace.

$this->xDialog->addViewNamespace('sweetalert', $sDirectory);

Then you render a template like this.

$this->xDialog->render('sweetalert::myAlert.js' $aVars);

If you don't have template variables in your js file, you better install it on your server and load it from your HTML code.

Waxter81 commented 6 years ago

Thanks for your help. With your advices, It's working now.

I know these are stupid questions, but I'm newbie and I'm not used to using namespaces.