javascriptecmascript-6alertifyjs

JS: How do I override keyupHandler(event) function in AlertifyJS?


The issue is that I am using Select2 combobox on Alertify dialog, now whenever I try to select the select2 option using keyboard by pressing Enter key the dialog closes because the Enter key is bind to Ok button and and ESC key is bind to Cancel button, I want to override keyupHandler handler function without changing the source js.

Source: https://github.com/MohammadYounes/AlertifyJS/blob/master/build/alertify.js +1269

Alert.js code:

import alertify from 'alertify';

// How can I override the default keyupHandler function after importing it in my code?

I want to add validation that if event.target is Select2 and it is open then do not trigger the events which close the dialog.


Solution

  • You may combine events from both libraries to achieve this, First you listen to open and close events of select2, then use them to allow or deny closing the dialog.

    var isClosed = false
    // wire select2 events
    $('select').select2().on('select2:open', function(e){
      isClosed = false
    }).on('select2:close', function(e){
      setTimeout(function(){
        isClosed = true
       },200)
    });
    
    // wire alertifyjs events
    alertify.confirm().set('onok', function(){
      return isClosed
    })