diff options
Diffstat (limited to 'public/js/gogs.js')
-rw-r--r-- | public/js/gogs.js | 300 |
1 files changed, 246 insertions, 54 deletions
diff --git a/public/js/gogs.js b/public/js/gogs.js index d835fa05..d429516e 100644 --- a/public/js/gogs.js +++ b/public/js/gogs.js @@ -21,6 +21,8 @@ function initCommentPreviewTab($form) { } ); }); + + buttonsClickOnEnter(); } function initCommentForm() { @@ -204,6 +206,31 @@ function initRepository() { return; } + function initFilterSearchDropdown(selector) { + var $dropdown = $(selector); + $dropdown.dropdown({ + fullTextSearch: true, + onChange: function (text, value, $choice) { + window.location.href = $choice.data('url'); + console.log($choice.data('url')) + }, + message: {noResults: $dropdown.data('no-results')} + }); + } + + // File list + if ($('.repository.file.list').length > 0) { + initFilterSearchDropdown('.choose.reference .dropdown'); + + $('.reference.column').click(function () { + $('.choose.reference .scrolling.menu').css('display', 'none'); + $('.choose.reference .text').removeClass('black'); + $($(this).data('target')).css('display', 'block'); + $(this).find('.text').addClass('black'); + return false; + }); + } + // Options if ($('.repository.settings.options').length > 0) { $('#repo_name').keyup(function () { @@ -287,23 +314,23 @@ function initRepository() { $('#edit-title').click(editTitleToggle); $('#cancel-edit-title').click(editTitleToggle); $('#save-edit-title').click(editTitleToggle). - click(function () { - if ($edit_input.val().length == 0 || - $edit_input.val() == $issue_title.text()) { - $edit_input.val($issue_title.text()); - return false; - } - - $.post($(this).data('update-url'), { - "_csrf": csrf, - "title": $edit_input.val() - }, - function (data) { - $edit_input.val(data.title); - $issue_title.text(data.title); - }); + click(function () { + if ($edit_input.val().length == 0 || + $edit_input.val() == $issue_title.text()) { + $edit_input.val($issue_title.text()); return false; - }); + } + + $.post($(this).data('update-url'), { + "_csrf": csrf, + "title": $edit_input.val() + }, + function (data) { + $edit_input.val(data.title); + $issue_title.text(data.title); + }); + return false; + }); // Edit issue or comment content $('.edit-content').click(function () { @@ -384,44 +411,37 @@ function initRepository() { // Diff if ($('.repository.diff').length > 0) { var $counter = $('.diff-counter'); - if ($counter.length < 1) { - return; + if ($counter.length >= 1) { + $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 + "%"); + }); } - $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 + "%"); - }); } - // Quick start - if ($('.repository.quickstart').length > 0) { - $('#repo-clone-ssh').click(function () { - $('.clone-url').text($(this).data('link')); - $('#repo-clone-url').val($(this).data('link')); - $(this).addClass('blue'); - $('#repo-clone-https').removeClass('blue'); - }); - $('#repo-clone-https').click(function () { - $('.clone-url').text($(this).data('link')); - $('#repo-clone-url').val($(this).data('link')); - $(this).addClass('blue'); - $('#repo-clone-ssh').removeClass('blue'); - }); - } + // Quick start and repository home + $('#repo-clone-ssh').click(function () { + $('.clone-url').text($(this).data('link')); + $('#repo-clone-url').val($(this).data('link')); + $(this).addClass('blue'); + $('#repo-clone-https').removeClass('blue'); + }); + $('#repo-clone-https').click(function () { + $('.clone-url').text($(this).data('link')); + $('#repo-clone-url').val($(this).data('link')); + $(this).addClass('blue'); + $('#repo-clone-ssh').removeClass('blue'); + }); + $('#repo-clone-url').click(function () { + $(this).select(); + }); // Pull request if ($('.repository.compare.pull').length > 0) { - var $branch_dropdown = $('.choose.branch .dropdown'); - $branch_dropdown.dropdown({ - fullTextSearch: true, - onChange: function (text, value, $choice) { - window.location.href = $choice.data('url'); - }, - message: {noResults: $branch_dropdown.data('no-results')} - }); + initFilterSearchDropdown('.choose.branch .dropdown'); } } @@ -537,6 +557,71 @@ function initAdmin() { } } +function buttonsClickOnEnter() { + $('.ui.button').keypress(function (e) { + if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar + $(this).click(); + }); +} + +function searchUsers() { + if (!$('#search-user-box .results').length) { + return; + } + + var $search_user_box = $('#search-user-box'); + var $result_list = $search_user_box.find('.results'); + $search_user_box.keyup(function () { + var $this = $(this); + var keyword = $this.find('input').val(); + if (keyword.length < 2) { + $result_list.hide(); + return; + } + + $.ajax({ + url: suburl + '/api/v1/users/search?q=' + keyword, + dataType: "json", + success: function (response) { + var notEmpty = function (str) { + return str && str.length > 0; + }; + + $result_list.html(''); + + if (response.ok && response.data.length) { + var html = ''; + $.each(response.data, function (i, item) { + html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>'; + if (notEmpty(item.full_name)) { + html += ' (' + item.full_name + ')'; + } + html += '</div>'; + }); + $result_list.html(html); + $this.find('.results .item').click(function () { + $this.find('input').val($(this).find('.username').text()); + $result_list.hide(); + }); + $result_list.show(); + } else { + $result_list.hide(); + } + } + }); + }); + $search_user_box.find('input').focus(function () { + $search_user_box.keyup(); + }); + $(document).click(function (e) { + var target = e.target; + + if (!$(target).is('#search-user-box .results') && !$(target).parents().is('#search-user-box')) { + $('#search-user-box .results').hide(); + } + }); +} + $(document).ready(function () { csrf = $('meta[name=_csrf]').attr("content"); suburl = $('meta[name=_suburl]').attr("content"); @@ -544,9 +629,9 @@ $(document).ready(function () { // Show exact time $('.time-since').each(function () { $(this).addClass('poping up'). - attr('data-content', $(this).attr('title')). - attr('data-variation', 'inverted tiny'). - attr('title', ''); + attr('data-content', $(this).attr('title')). + attr('data-variation', 'inverted tiny'). + attr('title', ''); }); // Semantic UI modules. @@ -574,6 +659,7 @@ $(document).ready(function () { } }); $('.tabular.menu .item').tab(); + $('.tabable.menu .item').tab(); $('.toggle.button').click(function () { $($(this).data('target')).slideToggle(100); @@ -620,9 +706,7 @@ $(document).ready(function () { emojify.setConfig({ img_dir: suburl + '/img/emoji' }); - $('.emojify').each(function () { - emojify.run($(this)[0]); - }); + emojify.run(); // Clipboard JS var clipboard = new Clipboard('.clipboard'); @@ -670,6 +754,30 @@ $(document).ready(function () { $($(this).data('modal')).modal('show'); }); + // Set anchor. + $('.markdown').each(function () { + var headers = {}; + $(this).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>'); + }); + }); + + buttonsClickOnEnter(); + searchUsers(); + + initCommentForm(); initInstall(); initRepository(); @@ -677,4 +785,88 @@ $(document).ready(function () { initUser(); initWebhook(); initAdmin(); +}); + +$(window).load(function () { + function changeHash(hash) { + if (history.pushState) { + history.pushState(null, null, hash); + } + else { + location.hash = hash; + } + } + + function deSelect() { + if (window.getSelection) { + window.getSelection().removeAllRanges(); + } else { + document.selection.empty(); + } + } + + 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')); + } + + // Code view. + if ($('.code-view').length > 0) { + var $block = $('.code-view .linenums'); + var lines = $block.html().split("\n"); + $block.html(''); + + var $num_list = $('.code-view .lines-num'); + + // Building blocks. + for (var i = 0; i < lines.length; i++) { + $block.append('<li class="L' + (i + 1) + '" rel="L' + (i + 1) + '">' + lines[i] + '</li>'); + $num_list.append('<span id="L' + (i + 1) + '">' + (i + 1) + '</span>'); + } + + $(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('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null)); + deSelect(); + }); + + $(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'); + } });
\ No newline at end of file |