summaryrefslogtreecommitdiffstats
path: root/public/ng/js/gogs.js
blob: e5f109af012d2daad193019cb614a94eb7f61c83 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// @codekit-prepend "lib/jquery-1.11.1.min.js"
// @codekit-prepend "lib/tabs.js"

(function ($) {
    // extend jQuery ajax, set csrf token value
    var ajax = $.ajax;
    $.extend({
        ajax: function (url, options) {
            if (typeof url === 'object') {
                options = url;
                url = undefined;
            }
            options = options || {};
            url = options.url;
            var csrftoken = $('meta[name=_csrf]').attr('content');
            var headers = options.headers || {};
            var domain = document.domain.replace(/\./ig, '\\.');
            if (!/^(http:|https:).*/.test(url) || eval('/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/').test(url)) {
                headers = $.extend(headers, {'X-Csrf-Token': csrftoken});
            }
            options.headers = headers;
            var callback = options.success;
            options.success = function (data) {
                if (data.once) {
                    // change all _once value if ajax data.once exist
                    $('[name=_once]').val(data.once);
                }
                if (callback) {
                    callback.apply(this, arguments);
                }
            };
            return ajax(url, options);
        },

        changeHash: function (hash) {
            if (history.pushState) {
                history.pushState(null, null, hash);
            }
            else {
                location.hash = hash;
            }
        },

        deSelect: function () {
            if (window.getSelection) {
                window.getSelection().removeAllRanges();
            } else {
                document.selection.empty();
            }
        }
    });
}(jQuery));

$(document).ready(function () {
    Tabs('#dashboard-sidebar-menu');

    homepage();
    settingsProfile();
    settingsSSHKeys();
    settingsDelete();
    renderMarkdown();
    renderCodeView();

    // Fix language drop-down menu height.
    var l = $('#footer-lang li').length;
    $('#footer-lang .drop-down').css({
        "top": (-31 * l) + "px",
        "height": (31 * l - 3) + "px"
    });
});

function homepage() {
    // Change method to GET if no username input.
    $('#promo-form').submit(function (e) {
        if ($('#username').val() === "") {
            e.preventDefault();
            window.location.href = '/user/login';
            return true
        }
    });
    // Redirect to register page.
    $('#register-button').click(function (e) {
        if ($('#username').val() === "") {
            e.preventDefault();
            window.location.href = '/user/sign_up';
            return true
        }
        $('#promo-form').attr('action', '/user/sign_up');
    });
}

function settingsProfile() {
    // Confirmation of change username in user profile page.
    $('#user-profile-form').submit(function (e) {
        if (($('#username').data('uname') != $('#username').val()) && !confirm('Username has been changed, do you want to continue?')) {
            e.preventDefault();
            return true;
        }
    });
}

function settingsSSHKeys() {
    // Show add SSH key panel.
    $('#ssh-add').click(function () {
        $('#user-ssh-add-form').removeClass("hide");
    });
}

function settingsDelete() {
    // Confirmation of delete account.
    $('#delete-account-button').click(function (e) {
        if (!confirm('This account is going to deleted, do you want to continue?')) {
            e.preventDefault();
            return true;
        }
    });
}

function renderMarkdown() {
    var $md = $('.markdown');
    var $pre = $md.find('pre > code').parent();
    $pre.addClass('prettyprint linenums');
    prettyPrint();

    // Set anchor.
    var headers = {};
    $md.find('h1, h2, h3, h4, h5, h6').each(function () {
        var node = $(this);
        var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
        var name = val;
        if (headers[val] > 0) {
            name = val + '-' + headers[val];
        }
        if (headers[val] == undefined) {
            headers[val] = 1;
        } else {
            headers[val] += 1;
        }
        node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
        node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
    });
}

function renderCodeView() {
    function selectRange($list, $select, $from) {
        $list.removeClass('active');
        if ($from) {
            var a = parseInt($select.attr('rel').substr(1));
            var b = parseInt($from.attr('rel').substr(1));
            var c;
            if (a != b) {
                if (a > b) {
                    c = a;
                    a = b;
                    b = c;
                }
                var classes = [];
                for (i = a; i <= b; i++) {
                    classes.push('.L' + i);
                }
                $list.filter(classes.join(',')).addClass('active');
                $.changeHash('#L' + a + '-' + 'L' + b);
                return
            }
        }
        $select.addClass('active');
        $.changeHash('#' + $select.attr('rel'));
    }

    $(document).on('click', '.lines-num span', function (e) {
        var $select = $(this);
        var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
        selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
        $.deSelect();
    });

    $('.code-view .lines-code > pre').each(function () {
        var $pre = $(this);
        var $lineCode = $pre.parent();
        var $lineNums = $lineCode.siblings('.lines-num');
        if ($lineNums.length > 0) {
            var nums = $pre.find('ol.linenums > li').length;
            for (var i = 1; i <= nums; i++) {
                $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
            }
        }
    });

    $(window).on('hashchange', function (e) {
        var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
        var $list = $('.code-view ol.linenums > li');
        if (m) {
            var $first = $list.filter('.' + m[1]);
            selectRange($list, $first, $list.filter('.' + m[2]));
            $("html, body").scrollTop($first.offset().top - 200);
            return;
        }
        m = window.location.hash.match(/^#(L\d+)$/);
        if (m) {
            var $first = $list.filter('.' + m[1]);
            selectRange($list, $first);
            $("html, body").scrollTop($first.offset().top - 200);
        }
    }).trigger('hashchange');
}