aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/copyable_links.js
blob: 521eae4c5ff75e84dc36ad5400375a6d6825a5f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { $gettext } from '../lib/gettext';

$(document).on('click', 'a.copyable-link', function (event) {
    event.preventDefault();

    // Create dummy element and position it off screen
    // This element must be "visible" (as in "not hidden") or otherwise
    // the copy command will fail
    let dummy = $('<textarea>').val(this.href).css({
        position: 'absolute',
        left: '-9999px'
    }).appendTo('body');

    // Select text and copy it to clipboard
    dummy[0].select();
    document.execCommand('Copy');
    dummy.remove();

    // Show visual hint using a deferred (this way we don't need to
    // duplicate the functionality in the done() handler)
    (new Promise((resolve, reject) => {
        let confirmation = $('<div class="copyable-link-confirmation copyable-link-success">');
        confirmation.text($gettext('Link wurde kopiert'));
        confirmation.insertBefore('#content');

        // Resolve deferred when animation has ended or after 2 seconds as a
        // fail safe
        let timeout = setTimeout(() => {
            $(this).parent().off('animationend');
            resolve(confirmation);
        }, 1500);
        $(this).parent().one('animationend', () => {
            clearTimeout(timeout);
            resolve(confirmation);
        });
    })).then((confirmation, parent) => {
        confirmation.remove();
    });
});