Skip to content

Commit

Permalink
Move copy to clipboard fn to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
vidya-ram committed Sep 27, 2023
1 parent a1f8c8d commit e8dd1c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 43 deletions.
23 changes: 4 additions & 19 deletions funnel/assets/js/shortlink_form.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import toastr from 'toastr';
import Utils from './utils/helper';

$(() => {
async function getShortlink(url) {
Expand All @@ -25,29 +26,13 @@ $(() => {

$('.js-copy-shortlink').on('click', function clickCopyLink(event) {
event.preventDefault();
const url = $('.js-generated-url').text();
if (navigator.clipboard) {
navigator.clipboard.writeText(url).then(
() => toastr.success(window.gettext('Link copied')),
() => toastr.success(window.gettext('Could not copy link'))
);
} else {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(url[0]);
selection.removeAllRanges();
selection.addRange(range);
if (document.execCommand('copy')) {
toastr.success(window.gettext('Link copied'));
} else {
toastr.error(window.gettext('Could not copy link'));
}
selection.removeAllRanges();
}
Utils.copyToClipboard('.js-generated-url');
});

$('#js-generate-shortlink').on('submit', (event) => {
event.preventDefault();
$('.js-generated-url').text();

const url = $('.js-campaign-url').val();
const id = $('.js-campaign-id').val();
const source = $('.js-campaign-source').val();
Expand Down
24 changes: 24 additions & 0 deletions funnel/assets/js/utils/helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* global gettext */
import toastr from 'toastr';

const Utils = {
// convert array of objects into hashmap
tohashMap(objectArray, key) {
Expand Down Expand Up @@ -227,6 +229,28 @@ const Utils = {
},
});
},
copyToClipboard(elem) {
const textElem = document.querySelector(elem);
const stringToCopy = textElem.innerHTML;
if (navigator.clipboard) {
navigator.clipboard.writeText(stringToCopy).then(
() => toastr.success(window.gettext('Link copied')),
() => toastr.success(window.gettext('Could not copy link'))
);
} else {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(textElem);
selection.removeAllRanges();
selection.addRange(range);
if (document.execCommand('copy')) {
toastr.success(window.gettext('Link copied'));
} else {
toastr.error(window.gettext('Could not copy link'));
}
selection.removeAllRanges();
}
},
};

export default Utils;
26 changes: 2 additions & 24 deletions funnel/assets/js/utils/webshare.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global gettext */
import toastr from 'toastr';
import Utils from './helper';

Expand Down Expand Up @@ -43,35 +42,14 @@ const WebShare = {
$('body').on('click', '.js-copy-link', function clickCopyLink(event) {
event.preventDefault();
const linkElem = this;
const copyLink = () => {
const url = $(linkElem).find('.js-copy-url').first().text();
if (navigator.clipboard) {
navigator.clipboard.writeText(url).then(
() => toastr.success(gettext('Link copied')),
() => toastr.success(gettext('Could not copy link'))
);
} else {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents($(linkElem).find('.js-copy-url')[0]);
selection.removeAllRanges();
selection.addRange(range);
if (document.execCommand('copy')) {
toastr.success(gettext('Link copied'));
} else {
toastr.error(gettext('Could not copy link'));
}
selection.removeAllRanges();
}
};
if ($(linkElem).attr('data-shortlink')) {
copyLink();
Utils.copyToClipboard('.js-copy-url');
} else {
Utils.fetchShortUrl($(linkElem).find('.js-copy-url').first().html())
.then((shortlink) => {
$(linkElem).find('.js-copy-url').text(shortlink);
$(linkElem).attr('data-shortlink', true);
copyLink();
Utils.copyToClipboard('.js-copy-url');
})
.catch((errMsg) => {
toastr.error(errMsg);
Expand Down

0 comments on commit e8dd1c5

Please sign in to comment.