summaryrefslogtreecommitdiffstats
path: root/extensions/Bitly/web/js/bitly.js
blob: 6784c83e92e437ca1ffcff4b7acd2fd45ccf7b1e (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
40
41
42
43
44
45
46
47
48
49
50
51
52
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0. */

$(function() {
    'use strict';
    var popup, urls = [];

    function execute() {
        var type = $('#bitly-type').val();

        if (urls[type]) {
            $('#bitly-url').val(urls[type]).select().focus();
            return;
        }

        $('#bitly-url').val('');
        var request = 'rest/bitly/' + type +
            '?url=' + encodeURIComponent($('#bitly-shorten').data('url')) +
            '&Bugzilla_api_token=' + encodeURIComponent(BUGZILLA.api_token);
        $.ajax(request)
            .done(function(data) {
                urls[type] = data.url;
                $('#bitly-url').val(urls[type]).select().focus();
            })
            .error(function(data) {
                $('#bitly-url').val(data.responseJSON.message);
            });
    }

    $('#bitly-shorten')
        .click(function(event) {
            event.preventDefault();
            popup = $('#bitly-overlay').bPopup({
                speed: 100,
                followSpeed: 100,
                modalColor: '#fff'
            }, execute);
        });

    $('#bitly-type')
        .change(execute);

    $('#bitly-close')
        .click(function(event) {
            event.preventDefault();
            popup.close();
        });
});