Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to capture cancel event in nwsaveas #4501

Closed
ed4becky opened this issue Mar 6, 2016 · 5 comments
Closed

Need to capture cancel event in nwsaveas #4501

ed4becky opened this issue Mar 6, 2016 · 5 comments

Comments

@ed4becky
Copy link

ed4becky commented Mar 6, 2016

I have followed the instructions for a SaveAs dialog and it works like a charms

EXCEPT...

If the user cancels, I have some code that needs to execute. However the cancel event doesn't trigger the change event.

So How can I respond to the cancel of the file saveas dialog?

function openSaveAs(prompt, cb) {
var saver = $('#saveAsFileDialog');
saver.attr('nwsaveas', prompt);
saver.unbind('change');
saver.val("");
saver.bind('change', function (evt) {
if(cb) cb($(this).val());
});
saver.trigger('click');
}

@iwege
Copy link

iwege commented Mar 6, 2016

maybe relate with this issue #3596

@TrevorPT
Copy link

I just ran into this same problem recently and figured a workaround. When the save/save-as dialog closes the window gets a focus event. So, instead of using the onchange event on the input control, I now use win.on( 'focus', ... ), adding the handler after triggering the click on the nwsaveas input (and removing it again after focus is received).

The tricky part is not knowing which event will be captured first, 'focus' on the Window or 'change' on the input, and if you check the control value too soon it will still be empty even if cancel was not used. So I use a small setTimeout to allow for the field to be updated (100 ms seems to be sufficient). Then, I just pull the .value of the input element and if it's empty, the dialog must've been cancelled (have to make sure to clear the input .value before invoking the dialog).

This technique is working with nw.js 0.12.3 on Windows and OSX.

@rogerwang rogerwang added the nw13 label Apr 12, 2016
@rogerwang rogerwang added this to the 0.13.x milestone Apr 12, 2016
@rogerwang
Copy link
Member

This is fixed in git and will be available in the next nightly build. Additional "oncancel" event listener is now supported for this.

@fritx
Copy link

fritx commented Apr 21, 2017

I was using the workaround similar to @TrevorPT and #292 (comment).

@rogerwang I've tried the cancel event in nw21.2. It works, but seems it is not yet documented.

@spacevoyager78
Copy link

spacevoyager78 commented Sep 5, 2020

Could someone provide a small sample script on how cancel event works? I haven't been able to make it work.
I use:
HTML:
<input type="file" id="myfiles" nwsaveas>
JS:

document.getElementById('myfiles').addEventListener('cancel', function() {
    console.log('user cancel');
});

The message in console doesn't appear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants