Copy to clipboard only works in onCancel not onSucces
Closed this issue · 7 comments
Hi,
and thank you very much for simpleDialog.js.
I'm using it to popup messages from my Ajax operation success/failure.
In case of failure, I offer the user an option to copy the error message to clipboard.
For some reason (I'm not a Javascript/JQuery expert), the error text is only copied to clipboard in the
first example.
This works:
<script type="text/javascript">
$(document).ready(function () {
$.simpleDialog({
title:"<?php echo PHPLIST_REST_API_SUBSCRIPTION_HEADER_ERROR?>",
message:"<?php echo PHPLIST_REST_API_SUBSCRIPTION_TEXT_ERROR.$result?>",
confirmBtnText:"<?php echo PHPLIST_CLOSE_BUTTON?>",
closeBtnText:"<?php echo PHPLIST_COPY_BUTTON?>",
onCancel:function(){
var text = "phpList Rest API\n";
var email = "<?php echo PHPLIST_REST_API_SUBSCRIPTION_EMAIL_TEXT.$subscriberEmail.'\n'?>";
var error_text = "<?php echo $result .'\n'.$time.'\n'.$ip?>";
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = text + email + error_text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
alert("<?php echo PHPLIST_CLIPBOARD_COPIED_TEXT;?>");
}
});
});
This doesnt work (the clipboard contents remain unchanged):
<script type="text/javascript">
$(document).ready(function () {
$.simpleDialog({
title:"<?php echo PHPLIST_REST_API_SUBSCRIPTION_HEADER_ERROR?>",
message:"<?php echo PHPLIST_REST_API_SUBSCRIPTION_TEXT_ERROR.$result?>",
closeBtnText:"<?php echo PHPLIST_CLOSE_BUTTON?>",
confirmBtnText:"<?php echo PHPLIST_COPY_BUTTON?>",
onSuccess:function(){
var text = "phpList Rest API\n";
var email = "<?php echo PHPLIST_REST_API_SUBSCRIPTION_EMAIL_TEXT.$subscriberEmail.'\n'?>";
var error_text = "<?php echo $result .'\n'.$time.'\n'.$ip?>";
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = text + email + error_text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
alert("<?php echo PHPLIST_CLIPBOARD_COPIED_TEXT;?>");
}
});
});
When debugging, I noticed that dummy.selectectionStart and dummy.selectionEnd both point to the end of dummy.value.
I suspect that the reason is timing related (probably setTimeout in simpleDialogjs).
If I set a breakpoint on the line "dummy.select()" and immeadiately continue, it works.
Any ideas for a fix? Or, should I just use the onCancel alternative?
@veltsumatti Thank you for your appreciation. I've added two labels to this issue, and I will try to resolve this issue asap.
@veltsumatti Could you please tell me the need of creating that dummy element.
The dummy element is temporarily created for saving the error text and copying it to clipboard. Can you come up with a more elegant solution?
@veltsumatti You can use any plugins like toastr JS
or any other javascript plugins to display the alert message. If you using toastrJS then you can display error/success messages as a toaster.
$.simpleDialog({
title: ....,
....
....
onSuccess:function(){
toastr.success("Message you want to display");
},
onCancel:function(){
toastr.error("Message you want to display");
}
});
Thanks,
but I have no need to popup additional messages, just copy the error text to clipboard, if the user so wishes. The last alert above just notifies the user that the error text has been copied to clipboard.
Actually, I didn't implement this type of features in this plugin. If you want to implement this feature in this plugin will be appreciated. I will try my best to implement this feature in the next updates.
The onCancel workaround works very well for me.