nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

Need to capture cancel event in nwsaveas

Open ed4becky opened this issue 10 years ago • 5 comments

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'); }

ed4becky avatar Mar 06 '16 15:03 ed4becky

maybe relate with this issue https://github.com/nwjs/nw.js/issues/3596

iwege avatar Mar 06 '16 15:03 iwege

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.

TrevorPT avatar Apr 12 '16 06:04 TrevorPT

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

rogerwang avatar Apr 20 '16 05:04 rogerwang

I was using the workaround similar to @TrevorPT and https://github.com/nwjs/nw.js/issues/292#issuecomment-123098102.

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

fritx avatar Apr 21 '17 02:04 fritx

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.

spacevoyager78 avatar Sep 05 '20 11:09 spacevoyager78