summaryrefslogtreecommitdiffstats
path: root/public/ng/js/gogs.js
blob: b3749ca0ddb8bd475289c942178195f65cf4a67c (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
// @codekit-prepend "lib/jquery-1.11.1.min.js"
// @codekit-prepend "lib/lib.js"
// @codekit-prepend "utils/tabs.js"
// @codekit-prepend "utils/preview.js"
// @codekit-prepend "gogs/issue_label.js"
// @codekit-prepend "lib/jquery.tipsy.js"

var Gogs = {};

(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();
            }
        }
    });
    $.fn.extend({
        toggleHide: function () {
            $(this).addClass("hidden");
        },
        toggleShow: function () {
            $(this).removeClass("hidden");
        },
        toggleAjax: function (successCallback, errorCallback) {
            var url = $(this).data("ajax");
            var method = $(this).data('ajax-method') || 'get';
            var ajaxName = $(this).data('ajax-name');
            var data = {};

            if (ajaxName.endsWith("preview")) {
                data["mode"] = "gfm";
                data["context"] = $(this).data('ajax-context');
            }

            $('[data-ajax-rel=' + ajaxName + ']').each(function () {
                var field = $(this).data("ajax-field");
                var t = $(this).data("ajax-val");
                if (t == "val") {
                    data[field] = $(this).val();
                    return true;
                }
                if (t == "txt") {
                    data[field] = $(this).text();
                    return true;
                }
                if (t == "html") {
                    data[field] = $(this).html();
                    return true;
                }
                if (t == "data") {
                    data[field] = $(this).data("ajax-data");
                    return true;
                }
                return true;
            });
            console.log("toggleAjax:", method, url, data);
            $.ajax({
                url: url,
                method: method.toUpperCase(),
                data: data,
                error: errorCallback,
                success: function (d) {
                    if (successCallback) {
                        successCallback(d);
                    }
                }
            })
        }
    });
}(jQuery));

(function ($) {
    // Render markdown.
    Gogs.renderMarkdown = function () {
        var $md = $('.markdown');
        var $pre = $md.find('pre > code').parent();
        $pre.addClass('prettyprint');
        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>');
        });
    };

    // Render code view.
    Gogs.renderCodeView = function () {
        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');
            var $first;
            if (m) {
                $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) {
                $first = $list.filter('.' + m[1]);
                selectRange($list, $first);
                $("html, body").scrollTop($first.offset().top - 200);
            }
        }).trigger('hashchange');
    };

    // Search users by keyword.
    Gogs.searchUsers = function (val, $target) {
        var notEmpty = function (str) {
          return str && str.length > 0;
        }
        $.ajax({
            url: Gogs.AppSubUrl + '/api/v1/users/search?q=' + val,
            dataType: "json",
            success: function (json) {
                if (json.ok && json.data.length) {
                    var html = '';
                    $.each(json.data, function (i, item) {
                        html += '<li><a><img src="' + item.avatar_url + '">' + item.username;
                        if (notEmpty(item.full_name)) {
                          html += ' (' + item.full_name + ')';
                        }
                        html += '</a></li>';
                    });
                    $target.html(html);
                    $target.toggleShow();
                } else {
                    $target.toggleHide();
                }
            }
        });
    }

    // Search repositories by keyword.
    Gogs.searchRepos = function (val, $target, $param) {
        $.ajax({
            url: Gogs.AppSubUrl + '/api/v1/repos/search?q=' + val + '&' + $param,
            dataType: "json",
            success: function (json) {
                if (json.ok && json.data.length) {
                    var html = '';
                    $.each(json.data, function (i, item) {
                        html += '<li><a><span class="octicon octicon-repo"></span> ' + item.full_name + '</a></li>';
                    });
                    $target.html(html);
                    $target.toggleShow();
                } else {
                    $target.toggleHide();
                }
            }
        });
    }

    // Copy util.
    Gogs.bindCopy = function (selector) {
        if ($(selector).hasClass('js-copy-bind')) {
            return;
        }
        $(selector).zclip({
            path: Gogs.AppSubUrl + "/js/ZeroClipboard.swf",
            copy: function () {
                var t = $(this).data("copy-val");
                var to = $($(this).data("copy-from"));
                var str = "";
                if (t == "txt") {
                    str = to.text();
                }
                if (t == 'val') {
                    str = to.val();
                }
                if (t == 'html') {
                    str = to.html();
                }
                return str;
            },
            afterCopy: function () {
                var $this = $(this);
                $this.tipsy("hide").attr('original-title', $this.data('after-title'));
                setTimeout(function () {
                    $this.tipsy("show");
                }, 200);
                setTimeout(function () {
                    $this.tipsy('hide').attr('original-title', $this.data('original-title'));
                }, 2000);
            }
        }).addClass("js-copy-bind");
    }
})(jQuery);

function initCore() {
    Gogs.renderMarkdown();
    Gogs.renderCodeView();

    // Switch list.
    $('.js-tab-nav').click(function (e) {
        if (!$(this).hasClass('js-tab-nav-show')) {
            $(this).parent().find('.js-tab-nav-show').each(function () {
                $(this).removeClass('js-tab-nav-show');
                $($(this).data('tab-target')).hide();
            });
            $(this).addClass('js-tab-nav-show');
            $($(this).data('tab-target')).show();
        }
        e.preventDefault();
    });

    // Popup.
    $(document).on('click', '.popup-modal-dismiss', function (e) {
        e.preventDefault();
        $.magnificPopup.close();
    });

    // Plugins.
    $('.collapse').hide();
    $('.tipsy-tooltip').tipsy({
        fade: true
    });
}

function initUserSetting() {
    // Confirmation of change username in user profile page.
    var $username = $('#username');
    var $profile_form = $('#user-profile-form');
    $('#change-username-btn').magnificPopup({
        modal: true,
        callbacks: {
            open: function () {
                if (($username.data('uname') == $username.val())) {
                    $.magnificPopup.close();
                    $profile_form.submit();
                }
            }
        }
    }).click(function () {
        if (($username.data('uname') != $username.val())) {
            e.preventDefault();
            return true;
        }
    });
    $('#change-username-submit').click(function () {
        $.magnificPopup.close();
        $profile_form.submit();
    });

    // Show panels.
    $('.show-form-btn').click(function () {
        $($(this).data('target-form')).removeClass("hide");
    });

    // Confirmation of delete account.
    $('#delete-account-btn').magnificPopup({
        modal: true
    }).click(function (e) {
        e.preventDefault();
        return true;
    });
    $('#delete-account-submit').click(function () {
        $.magnificPopup.close();
        $('#delete-account-form').submit();
    });
}

function initRepoCreate() {
    // Owner switch menu click.
    $('#repo-create-owner-list').on('click', 'li', function () {
        if (!$(this).hasClass('checked')) {
            var uid = $(this).data('uid');
            $('#repo-owner-id').val(uid);
            $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
            $('#repo-owner-name').text($(this).text().trim());

            $(this).parent().find('.checked').removeClass('checked');
            $(this).addClass('checked');
            console.log("set repo owner to uid :", uid, $(this).text().trim());
        }
    });

    $('#auth-button').click(function (e) {
        $('#repo-migrate-auth').slideToggle('fast');
        e.preventDefault();
    })
    console.log('initRepoCreate');
}

function initRepo() {
    // Clone link switch button.
    $('#repo-clone-ssh').click(function () {
        $(this).removeClass('btn-gray').addClass('btn-blue');
        $('#repo-clone-https').removeClass('btn-blue').addClass('btn-gray');
        $('#repo-clone-url').val($(this).data('link'));
        $('.clone-url').text($(this).data('link'))
    });
    $('#repo-clone-https').click(function () {
        $(this).removeClass('btn-gray').addClass('btn-blue');
        $('#repo-clone-ssh').removeClass('btn-blue').addClass('btn-gray');
        $('#repo-clone-url').val($(this).data('link'));
        $('.clone-url').text($(this).data('link'))
    });

    // Copy URL.
    var $clone_btn = $('#repo-clone-copy');
    $clone_btn.hover(function () {
        Gogs.bindCopy($(this));
    })
    $clone_btn.tipsy({
        fade: true
    });
}

// when user changes hook type, hide/show proper divs
function initHookTypeChange() {
    // web hook type change
    $('select#hook-type').on("change", function () {
        hookTypes = ['Gogs', 'Slack'];

        var curHook = $(this).val();
        hookTypes.forEach(function (hookType) {
            if (curHook === hookType) {
                $('div#' + hookType.toLowerCase()).toggleShow();
            }
            else {
                $('div#' + hookType.toLowerCase()).toggleHide();
            }
        });
    });
}

function initRepoSetting() {
    // Options.
    // Confirmation of changing repository name.
    var $reponame = $('#repo_name');
    var $setting_form = $('#repo-setting-form');
    $('#change-reponame-btn').magnificPopup({
        modal: true,
        callbacks: {
            open: function () {
                if (($reponame.data('repo-name') == $reponame.val())) {
                    $.magnificPopup.close();
                    $setting_form.submit();
                }
            }
        }
    }).click(function () {
        if (($reponame.data('repo-name') != $reponame.val())) {
            e.preventDefault();
            return true;
        }
    });
    $('#change-reponame-submit').click(function () {
        $.magnificPopup.close();
        $setting_form.submit();
    });

    initHookTypeChange();

    // Transfer repository.
    $('#transfer-repo-btn').magnificPopup({
        modal: true
    });
    $('#transfer-repo-submit').click(function () {
        $.magnificPopup.close();
        $('#transfer-repo-form').submit();
    });

    // Delete repository.
    $('#delete-repo-btn').magnificPopup({
        modal: true
    });
    $('#delete-repo-submit').click(function () {
        $.magnificPopup.close();
        $('#delete-repo-form').submit();
    });

    // Collaboration.
    $('#repo-collab-list hr:last-child').remove();
    var $ul = $('#repo-collaborator').next().next().find('ul');
    $('#repo-collaborator').on('keyup', function () {
        var $this = $(this);
        if (!$this.val()) {
            $ul.toggleHide();
            return;
        }
        Gogs.searchUsers($this.val(), $ul);
    }).on('focus', function () {
        if (!$(this).val()) {
            $ul.toggleHide();
        } else {
            $ul.toggleShow();
        }
    }).next().next().find('ul').on("click", 'li', function () {
        $('#repo-collaborator').val($(this).text());
        $ul.toggleHide();
    });
}

function initOrgSetting() {
    // Options.
    // Confirmation of changing organization name.
    var $orgname = $('#orgname');
    var $setting_form = $('#org-setting-form');
    $('#change-orgname-btn').magnificPopup({
        modal: true,
        callbacks: {
            open: function () {
                if (($orgname.data('orgname') == $orgname.val())) {
                    $.magnificPopup.close();
                    $setting_form.submit();
                }
            }
        }
    }).click(function () {
        if (($orgname.data('orgname') != $orgname.val())) {
            e.preventDefault();
            return true;
        }
    });
    $('#change-orgname-submit').click(function () {
        $.magnificPopup.close();
        $setting_form.submit();
    });

    // Confirmation of delete organization.
    $('#delete-org-btn').magnificPopup({
        modal: true
    }).click(function (e) {
        e.preventDefault();
        return true;
    });
    $('#delete-org-submit').click(function () {
        $.magnificPopup.close();
        $('#delete-org-form').submit();
    });

    initHookTypeChange();
}

function initInvite() {
    // Invitation.
    var $ul = $('#org-member-invite-list');
    $('#org-member-invite').on('keyup', function () {
        var $this = $(this);
        if (!$this.val()) {
            $ul.toggleHide();
            return;
        }
        Gogs.searchUsers($this.val(), $ul);
    }).on('focus', function () {
        if (!$(this).val()) {
            $ul.toggleHide();
        } else {
            $ul.toggleShow();
        }
    }).next().next().find('ul').on("click", 'li', function () {
        $('#org-member-invite').val($(this).text());
        $ul.toggleHide();
    });
}

function initOrgTeamCreate() {
    // Delete team.
    $('#org-team-delete').magnificPopup({
        modal: true
    }).click(function (e) {
        e.preventDefault();
        return true;
    });
    $('#delete-team-submit').click(function () {
        $.magnificPopup.close();
        var $form = $('#team-create-form');
        $form.attr('action', $form.data('delete-url'));
    });
}

function initTeamMembersList() {
    // Add team member.
    var $ul = $('#org-team-members-list');
    $('#org-team-members-add').on('keyup', function () {
        var $this = $(this);
        if (!$this.val()) {
            $ul.toggleHide();
            return;
        }
        Gogs.searchUsers($this.val(), $ul);
    }).on('focus', function () {
        if (!$(this).val()) {
            $ul.toggleHide();
        } else {
            $ul.toggleShow();
        }
    }).next().next().find('ul').on("click", 'li', function () {
        $('#org-team-members-add').val($(this).text());
        $ul.toggleHide();
    });
}

function initTeamRepositoriesList() {
    // Add team repository.
    var $ul = $('#org-team-repositories-list');
    $('#org-team-repositories-add').on('keyup', function () {
        var $this = $(this);
        if (!$this.val()) {
            $ul.toggleHide();
            return;
        }
        Gogs.searchRepos($this.val(), $ul, 'uid=' + $this.data('uid'));
    }).on('focus', function () {
        if (!$(this).val()) {
            $ul.toggleHide();
        } else {
            $ul.toggleShow();
        }
    }).next().next().find('ul').on("click", 'li', function () {
        $('#org-team-repositories-add').val($(this).text());
        $ul.toggleHide();
    });
}

function initAdmin() {
    // Create account.
    $('#login-type').on("change", function () {
        var v = $(this).val();
        if (v.indexOf("0-") + 1) {
            $('.auth-name').toggleHide();
            $(".pwd").find("input").attr("required", "required")
                .end().toggleShow();
        } else {
            $(".pwd").find("input").removeAttr("required")
                .end().toggleHide();
            $('.auth-name').toggleShow();
        }
    });

    // Delete account.
    $('#delete-account-btn').magnificPopup({
        modal: true
    }).click(function (e) {
        e.preventDefault();
        return true;
    });
    $('#delete-account-submit').click(function () {
        $.magnificPopup.close();
        var $form = $('#user-profile-form');
        $form.attr('action', $form.data('delete-url'));
    });

    // Create authorization.
    $('#auth-type').on("change", function () {
        var v = $(this).val();
        if (v == 2) {
            $('.ldap').toggleShow();
            $('.smtp').toggleHide();
        }
        if (v == 3) {
            $('.smtp').toggleShow();
            $('.ldap').toggleHide();
        }
    });

    // Delete authorization.
    $('#delete-auth-btn').magnificPopup({
        modal: true
    }).click(function (e) {
        e.preventDefault();
        return true;
    });
    $('#delete-auth-submit').click(function () {
        $.magnificPopup.close();
        var $form = $('#auth-setting-form');
        $form.attr('action', $form.data('delete-url'));
    });
}

function initInstall() {
    // Change database type.
    (function () {
        var mysql_default = '127.0.0.1:3306';
        var postgres_default = '127.0.0.1:5432';

        $('#install-database').on("change", function () {
            var val = $(this).val();
            if (val != "SQLite3") {
                $('.server-sql').show();
                $('.sqlite-setting').addClass("hide");
                if (val == "PostgreSQL") {
                    $('.pgsql-setting').removeClass("hide");

                    // Change the host value to the Postgres default, but only
                    // if the user hasn't already changed it from the MySQL
                    // default.
                    if ($('#database-host').val() == mysql_default) {
                        $('#database-host').val(postgres_default);
                    }
                } else if (val == 'MySQL') {
                    $('.pgsql-setting').addClass("hide");
                    if ($('#database-host').val() == postgres_default) {
                        $('#database-host').val(mysql_default);
                    }
                } else {
                    $('.pgsql-setting').addClass("hide");
                }
            } else {
                $('.server-sql').hide();
                $('.pgsql-setting').hide();
                $('.sqlite-setting').removeClass("hide");
            }
        });
    }());
}

function initProfile() {
    // Avatar.
    $('#profile-avatar').tipsy({
        fade: true
    });
}

function initTimeSwitch() {
    // Time switch.
    $(".time-since[title]").on("click", function () {
        var $this = $(this);

        var title = $this.attr("title");
        var text = $this.text();

        $this.text(title);
        $this.attr("title", text);
    });
}

function initDiff() {
    $('.diff-detail-box>a').click(function () {
        $($(this).data('target')).slideToggle(100);
    })

    var $counter = $('.diff-counter');
    if ($counter.length < 1) {
        return;
    }
    $counter.each(function (i, item) {
        var $item = $(item);
        var addLine = $item.find('span[data-line].add').data("line");
        var delLine = $item.find('span[data-line].del').data("line");
        var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
        $item.find(".bar .add").css("width", addPercent + "%");
    });
}

$(document).ready(function () {
    Gogs.AppSubUrl = $('head').data('suburl') || '';
    initCore();
    if ($('#user-profile-setting').length) {
        initUserSetting();
    }
    if ($('#repo-create-form').length || $('#repo-migrate-form').length) {
        initRepoCreate();
    }
    if ($('#repo-header').length) {
        initRepo();
    }
    if ($('#repo-setting').length) {
        initRepoSetting();
    }
    if ($('#org-setting').length) {
        initOrgSetting();
    }
    if ($('#invite-box').length) {
        initInvite();
    }
    if ($('#team-create-form').length) {
        initOrgTeamCreate();
    }
    if ($('#team-members-list').length) {
        initTeamMembersList();
    }
    if ($('#team-repositories-list').length) {
        initTeamRepositoriesList();
    }
    if ($('#admin-setting').length) {
        initAdmin();
    }
    if ($('#install-form').length) {
        initInstall();
    }
    if ($('#user-profile-page').length) {
        initProfile();
    }
    if ($('#diff-page').length) {
        initTimeSwitch();
        initDiff();
    }

    $('#dashboard-sidebar-menu').tabs();
    $('#pull-issue-preview').markdown_preview(".issue-add-comment");

    homepage();

    // 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 = Gogs.AppSubUrl + '/user/login';
            return true
        }
    });
    // Redirect to register page.
    $('#register-button').click(function (e) {
        if ($('#username').val() === "") {
            e.preventDefault();
            window.location.href = Gogs.AppSubUrl + '/user/sign_up';
            return true
        }
        $('#promo-form').attr('action', Gogs.AppSubUrl + '/user/sign_up');
    });
}