Close modal with onClick handler

616 views
Skip to first unread message

Sacha

unread,
Nov 12, 2016, 7:13:37 PM11/12/16
to InboxSDK
There's a method to close a ModalView: close().

However, I'm struggling to call it from the onClick handler of a button of a ModalView:

var modal = sdk.Widgets.showModalView({
       
'el':'<div>This action will archive all '+nEmailsFrom+' conversations from '+email+'. Are you sure you want to continue?</div>',
       
'title': 'Confirm archiving messages',
       
'buttons':[
         
{
           
'text':'OK',
           
'onClick': bulkArchive(email,nEmailsFrom),
           
'type':'PRIMARY_ACTION'
         
},
         
{
           
'text':'Cancel',
           
'onClick': modalClose
         
}
       
]
     
})

After the OK button has been clicked, how can I call the close() method to close the modal from the handler? I don't seem to be able to include any parameter in my function handler about the current modal.

Same question for the Cancel button...

Thanks!

Omar Ismail

unread,
Nov 14, 2016, 12:26:47 AM11/14/16
to Sacha, InboxSDK
hi Sacha, does "bulkArchive" return a function, or is that the function to actually do the bulk archive?

--
You received this message because you are subscribed to the Google Groups "InboxSDK" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inboxsdk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/inboxsdk/5eaf7f48-43ad-405e-b1dc-cb492b074fad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sacha

unread,
Nov 14, 2016, 12:41:34 AM11/14/16
to InboxSDK, sacha....@gmail.com
Hi Omar,

Thanks for getting back to me.

No, "bulkArchive" does my logic to archive all emails from "email". I was expecting to call the close() method in this function (or elsewhere), once the whole logic has been performed. 

Same for the modalClose of the Cancel button ; how can I close the modal otherwise ? Would you have a code snippet on how to execute a handler from one of the modal buttons that will close it afterwards ?

To give you the full background, I had to create an eventListener on the document to be able to call a function with the data I needed, when the user clicks on a link in a ButterBar (there's no event handler for clicks in the ButterBar) :
sdk.ButterBar.showMessage({
          'text':'',
          'time':30000,
          'html':"The conversation has been archived. <span style='text-decoration:underline' class='bulkArchive' email='"+sender[0].emailAddress+"' nEmailsFrom='"+msg.nEmailsFrom+"'>Archive all "+msg.nEmailsFrom+" emails from "+ sender[0].emailAddress + "</span>"
        })

Here's the document eventListener and the two functions that respond to clicks from buttons in the modal:
function bulkArchive(email,nEmailsFrom){
    var port = chrome.runtime.connect({name: "bulkArchive"});
    port.postMessage({'email': email, 'nEmailsFrom': nEmailsFrom });
    // close modal ?
  }

  function modalClose(m){
    // m.close() close modal ? (this doesn't work)
  }
  document.querySelector('body').addEventListener('click', function(event) {
    if (event.target.className == 'bulkArchive') {
      var email = event.target.getAttribute('email')
      var nEmailsFrom = event.target.getAttribute('nEmailsFrom')      
      var modal = sdk.Widgets.showModalView({
        'el':'<div>This action will archive all '+nEmailsFrom+' conversations from '+email+'. Are you sure you want to continue?</div>',
        'title': 'Confirm archiving messages',
        'buttons':[
          {
            'text':'OK',
            'onClick': bulkArchive(email,nEmailsFrom, modal),
            'type':'PRIMARY_ACTION'
          },
          {
            'text':'Cancel',
            'onClick': modalClose
          }
        ]
      })
    }
  });

Many thanks for your help,

Sacha

On Monday, November 14, 2016 at 4:26:47 PM UTC+11, Omar Ismail wrote:
hi Sacha, does "bulkArchive" return a function, or is that the function to actually do the bulk archive?
On Sat, Nov 12, 2016 at 4:13 PM, Sacha <sacha....@gmail.com> wrote:
There's a method to close a ModalView: close().

However, I'm struggling to call it from the onClick handler of a button of a ModalView:

var modal = sdk.Widgets.showModalView({
       
'el':'<div>This action will archive all '+nEmailsFrom+' conversations from '+email+'. Are you sure you want to continue?</div>',
       
'title': 'Confirm archiving messages',
       
'buttons':[
         
{
           
'text':'OK',
           
'onClick': bulkArchive(email,nEmailsFrom),
           
'type':'PRIMARY_ACTION'
         
},
         
{
           
'text':'Cancel',
           
'onClick': modalClose
         
}
       
]
     
})

After the OK button has been clicked, how can I call the close() method to close the modal from the handler? I don't seem to be able to include any parameter in my function handler about the current modal.

Same question for the Cancel button...

Thanks!

--
You received this message because you are subscribed to the Google Groups "InboxSDK" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inboxsdk+u...@googlegroups.com.

Omar Ismail

unread,
Nov 14, 2016, 2:04:55 AM11/14/16
to Sacha, InboxSDK
Looking at your code you're calling bulkArchive at the instantiation of "showModal".

the line:

"onClick": bulkArchive(email, nEmailsFrom) 

gets executed right away before the modal is even shown. You need to change it to:

"onClick": function(){bulkArchive(email, nEmailsFrom); modal.close();}

To unsubscribe from this group and stop receiving emails from it, send an email to inboxsdk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/inboxsdk/c977f796-d600-4d8c-8534-f01fba524a49%40googlegroups.com.

Sacha

unread,
Nov 15, 2016, 1:20:16 AM11/15/16
to InboxSDK, sacha....@gmail.com
Wonderful, thanks Omar - I learned sth new today!
Reply all
Reply to author
Forward
0 new messages