summaryrefslogtreecommitdiffstats
path: root/extensions/Bitly/web/js/bitly.js
blob: 62c49b650c42a6f2dee347c472f3b67016519b5e (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* 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 Dom = YAHOO.util.Dom;
    YAHOO.namespace('bitly');
    var bitly = YAHOO.bitly;

    bitly.dialog = false;
    bitly.url = { shorten: '', list: '' };

    bitly.shorten = function() {
        if (this.dialog) {
            this.dialog.show();
            var el = Dom.get('bitly_url');
            el.select();
            el.focus();
            return;
        }
        this.dialog = new YAHOO.widget.Overlay('bitly_overlay', {
            visible: true,
            close: false,
            underlay: 'shadow',
            width: '400px',
            context: [ 'bitly_shorten', 'bl', 'tl', ['windowResize'], [0, -10] ]
        });
        this.dialog.render(document.body);

        YAHOO.util.Event.addListener('bitly_close', 'click', function() {
            YAHOO.bitly.dialog.hide();
        });
        YAHOO.util.Event.addListener('bitly_url', 'keypress', function(o) {
            if (o.keyCode == 27 || o.keyCode == 13)
                YAHOO.bitly.dialog.hide();
        });
        this.execute();
        Dom.get('bitly_url').focus();
    };

    bitly.execute = function() {
        Dom.get('bitly_url').value = '';

        var type = Dom.get('bitly_type').value;
        if (this.url[type]) {
            this.set(this.url[type]);
            return;
        }

        var url = 'rest/bitly/' + type + '?url=' + encodeURIComponent(document.location);
        YAHOO.util.Connect.initHeader("Accept", "application/json");
        YAHOO.util.Connect.asyncRequest('GET', url, {
            success: function(o) {
                var response = YAHOO.lang.JSON.parse(o.responseText);
                if (response.error) {
                    bitly.set(response.message);
                }
                else {
                    bitly.url[type] = response.url;
                    bitly.set(response.url);
                }
            },
            failure: function(o) {
                try {
                    var response = YAHOO.lang.JSON.parse(o.responseText);
                    if (response.error) {
                        bitly.set(response.message);
                    }
                    else {
                        bitly.set(o.statusText);
                    }
                } catch (ex) {
                    bitly.set(o.statusText);
                }
            }
        });
    };

    bitly.set = function(value) {
        var el = Dom.get('bitly_url');
        el.value = value;
        el.select();
        el.focus();
    };

    bitly.toggle = function() {
        if (this.dialog
            && YAHOO.util.Dom.get('bitly_overlay').style.visibility == 'visible')
        {
            this.dialog.hide();
        }
        else {
            this.shorten();
        }
    };
})();