1

Problem:

The 'kartik-v\yii2-dialog' which is used by 'kartik-v\tree-manager' overrides Sweetalert dialog/message box.

How does one disable the treeview-manager's dependency 'kartik-v\yii2-dialog' in order to use SweetAlerts?

Tried:

'assetManager' => ['bundles' => [ 'kartik\dialog\DialogAsset' => ['js' => [],], ... ,

Sweetalert starts working in grids and confirm events, but then treemanager no longer works (Uncaught ReferenceError: KrajeeDialog is not defined)

In Pictures:

Have:

Kartik-v\dialog

Want:

SweetAlert

Any input would be greatly appreciated.

Update:

Here is the override code, which has worked, but now kartik\yii2-dialog is loaded afterward and overrides this:

yii.confirm = function(message, okCallback, cancelCallback) {
if (message.constructor === Array) {
    swal(
        {
            html: true, // SweetAlert1
            title: message[0],
            text: message[1],
            //html: message[1], // SweetAlert2
            //confirmButtonColor: '#E80000',
            confirmButtonColor: message[3],
            //type: 'warning',
            type: message[2],
            showCancelButton: true,
            cancelButtonText: 'Avbryt',
            closeOnConfirm: true,
            allowOutsideClick: true,
            buttonsStyling: false,
        },
        okCallback
    );
} else {
    swal(
        {
            html: true, // SweetAlert1
            title: message,
            type: 'warning',
            showCancelButton: true,
            cancelButtonText: 'Avbryt',
            closeOnConfirm: true,
            allowOutsideClick: true,
            buttonsStyling: false,
        },
        okCallback
    );
}
};

confirm = function(message, okCallback, cancelCallback) {
    if (message.constructor === Array) {
        swal(
            {
                html: true, // SweetAlert 1
                title: message[0],
                text: message[1],
                //html: message[1], // SweetAlert2
                //confirmButtonColor: '#E80000',
                confirmButtonColor: message[3],
                //type: 'warning',
                type: message[2],
                showCancelButton: true,
                cancelButtonText: 'Avbryt',
                closeOnConfirm: true,
                allowOutsideClick: true,
                buttonsStyling: false,
            },
            okCallback
        );
    } else {
        swal(
            {
                html: true, // SweetAlert 1
                title: message,
                type: 'warning',
                showCancelButton: true,
                cancelButtonText: 'Avbryt',
                closeOnConfirm: true,
                allowOutsideClick: true,
            },
            okCallback
        );
    }
};

yii.alert = function(message, okCallback, cancelCallback) {
    swal(
        {
            title: message,
            type: 'warning',
            showCancelButton: false,
            closeOnConfirm: true,
            allowOutsideClick: false,
            buttonsStyling: false,
        },
        okCallback
    );
};

alert = function(message, okCallback, cancelCallback) {
    swal(
        {
            title: message,
            type: 'warning',
            showCancelButton: false,
            closeOnConfirm: true,
            allowOutsideClick: false,
            buttonsStyling: false,
        },
        okCallback
    );
};
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
aalesund
  • 313
  • 1
  • 4
  • 13
  • Which Version of Sweet Alert are you using ? 1 or 2 – Muhammad Omer Aslam Mar 12 '19 at 17:14
  • Sweet alert 1 thank you @MuhammadOmerAslam – aalesund Mar 12 '19 at 18:06
  • i am also using the sweetalert 1 in one of my projects where i am using the TreeView but i am not experiencing what you are saying. can you show your code how are you overriding the default Yii confirm dialog for the gridview – Muhammad Omer Aslam Mar 12 '19 at 19:38
  • i am talking about what you are saying here **Sweetalert starts working in grids and confirm events, but then treemanager no longer works (Uncaught ReferenceError: KrajeeDialog is not defined)**, although it is true that treeview shows the default yii2-dialog widgets confirm and not sweetalert, first you need to add your code and also clearify that in the treview it shows you the kartik/yi2-dialog's prompt or you receive the error message ? – Muhammad Omer Aslam Mar 12 '19 at 20:15
  • in kartik\dialog\Dialog there are two properties that are relevant: $overrideYiiConfirm and $useNative, but it is not clear how I set those properties globally. The dialog widget is never used directly only indirectly by other widgets. Have tried: 'dialog' => [ 'class' => '\kartik\dialog\Dialog', 'useNative' => true, 'overrideYiiConfirm' => false, ], in config file, but no effect. If the properties are modified in the vendor file then everything works perfectly, but this is not an option. @MuhammadOmerAslam – aalesund Mar 13 '19 at 08:41
  • The source of the problem seems to come from: https://github.com/kartik-v/yii2-dialog/issues/28 – aalesund Mar 13 '19 at 08:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189937/discussion-between-aalesund-and-muhammad-omer-aslam). – aalesund Mar 13 '19 at 09:22
  • have you tried adding the `'krajeeDialogSettings' => ['overrideYiiConfirm' => true, 'useNative' => true],` inside the `TreeView` widget configurations? – Muhammad Omer Aslam Mar 13 '19 at 12:55
  • were you able to solve the problem with the suggested solution or still having problem? – Muhammad Omer Aslam Mar 17 '19 at 19:26

1 Answers1

1

Although there is an option provided as krajeeDialogSettings in TreeView which controls the yii2-dialog via using,

'krajeeDialogSettings' => ['overrideYiiConfirm' => true, 'useNative' => true],

According to the docs it should work but for me, it didn't worked, and the yii2-dialog always overrode the sweetalert confirm, I wanted to rule out the prompt or the yii2-dialog from the treeview and for that removing the dependency is not that straight forward because the calls are nested and integrated in the Treeview script.

So, I had to override the krajeeDialog.confirm where i was loading the TreeView widget so that whenever the krajeeDialog.confirm is called my custom confirm dialog would be called.

Just add the below on the top of the view where you are loading the TreeView widget.

<?php 

 $js = <<< JS

 krajeeDialog.confirm = function (message, callback) {
    swal({
        title: message,
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: true,
        allowOutsideClick: true
    }, callback);
}
JS;
    $this->registerJs($js, yii\web\view::POS_READY);

Although i didnt like the dual approach but that was the only one that worked for me, maybe someone else could post a better solution.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68