diff --git a/funnel/assets/js/shortlink_form.js b/funnel/assets/js/shortlink_form.js new file mode 100644 index 000000000..754e60076 --- /dev/null +++ b/funnel/assets/js/shortlink_form.js @@ -0,0 +1,53 @@ +import toastr from 'toastr'; +import Utils from './utils/helper'; + +$(() => { + async function getShortlink(url) { + const response = await fetch(window.Hasgeek.Config.shorturlApi, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams({ + url, + }).toString(), + }).catch(() => { + toastr.error(window.Hasgeek.Config.errorMsg.serverError); + }); + if (response.ok) { + const responseData = await response.json(); + $('.js-generated-url').text(responseData.shortlink); + toastr.success(window.gettext('Shortlink generated')); + } else { + toastr.error(window.gettext('This URL is not valid for a shortlink')); + } + } + + $('.js-copy-shortlink').on('click', function clickCopyLink(event) { + event.preventDefault(); + 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(); + const medium = $('.js-campaign-medium').val(); + const name = $('.js-campaign-name').val(); + const term = $('.js-campaign-term').val(); + const content = $('.js-campaign-content').val(); + const campaignUrl = new URL(url); + + if (source) campaignUrl.searchParams.set('utm_source', source); + if (medium) campaignUrl.searchParams.set('utm_medium', medium); + if (name) campaignUrl.searchParams.set('utm_campaign', name); + if (id) campaignUrl.searchParams.set('utm_id', id); + if (term) campaignUrl.searchParams.set('utm_term', term); + if (content) campaignUrl.searchParams.set('utm_content', content); + getShortlink(campaignUrl.href); + }); +}); diff --git a/funnel/assets/js/utils/helper.js b/funnel/assets/js/utils/helper.js index ecd116762..9093db0d7 100644 --- a/funnel/assets/js/utils/helper.js +++ b/funnel/assets/js/utils/helper.js @@ -1,4 +1,6 @@ /* global gettext */ +import toastr from 'toastr'; + const Utils = { // convert array of objects into hashmap tohashMap(objectArray, key) { @@ -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; diff --git a/funnel/assets/js/utils/webshare.js b/funnel/assets/js/utils/webshare.js index c989b2f95..54625b114 100644 --- a/funnel/assets/js/utils/webshare.js +++ b/funnel/assets/js/utils/webshare.js @@ -1,4 +1,3 @@ -/* global gettext */ import toastr from 'toastr'; import Utils from './helper'; @@ -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); diff --git a/funnel/assets/sass/base/_utils.scss b/funnel/assets/sass/base/_utils.scss index ad53ee2f1..3f9d64542 100644 --- a/funnel/assets/sass/base/_utils.scss +++ b/funnel/assets/sass/base/_utils.scss @@ -130,6 +130,10 @@ flex-shrink: 0; } +.flex-item-grow { + flex-grow: 1; +} + // ============================================================================ // DISPLAY // ============================================================================ diff --git a/funnel/templates/siteadmin_generate_shortlinks.html.jinja2 b/funnel/templates/siteadmin_generate_shortlinks.html.jinja2 index 27a4c157f..3b671fc45 100644 --- a/funnel/templates/siteadmin_generate_shortlinks.html.jinja2 +++ b/funnel/templates/siteadmin_generate_shortlinks.html.jinja2 @@ -7,30 +7,74 @@ {% endblock pageheaders %} {% block content %} -
+