From 52c8f691630548fe091d30bcfe8164545a05d3d5 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 22 Nov 2015 01:32:09 -0500 Subject: fix #650 --- public/js/gogs.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'public/js') diff --git a/public/js/gogs.js b/public/js/gogs.js index d429516e4b..0fd2b46ab9 100644 --- a/public/js/gogs.js +++ b/public/js/gogs.js @@ -564,6 +564,16 @@ function buttonsClickOnEnter() { }); } +function hideWhenLostFocus(body, parent) { + $(document).click(function (e) { + var target = e.target; + + if (!$(target).is(body) && !$(target).parents().is(parent)) { + $(body).hide(); + } + }); +} + function searchUsers() { if (!$('#search-user-box .results').length) { return; @@ -613,12 +623,53 @@ function searchUsers() { $search_user_box.find('input').focus(function () { $search_user_box.keyup(); }); - $(document).click(function (e) { - var target = e.target; + hideWhenLostFocus('#search-user-box .results', '#search-user-box'); +} + +function searchRepositories() { + if (!$('#search-repo-box .results').length) { + return; + } - if (!$(target).is('#search-user-box .results') && !$(target).parents().is('#search-user-box')) { - $('#search-user-box .results').hide(); + var $search_repo_box = $('#search-repo-box'); + var $result_list = $search_repo_box.find('.results'); + $search_repo_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/repos/search?q=' + keyword + "&uid=" + $search_repo_box.data('uid'), + 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 += '
' + item.full_name + '
'; + }); + $result_list.html(html); + $this.find('.results .item').click(function () { + $this.find('input').val($(this).find('.fullname').text().split("/")[1]); + $result_list.hide(); + }); + $result_list.show(); + } else { + $result_list.hide(); + } + } + }); + }); + $search_repo_box.find('input').focus(function () { + $search_repo_box.keyup(); }); } @@ -776,6 +827,7 @@ $(document).ready(function () { buttonsClickOnEnter(); searchUsers(); + searchRepositories(); initCommentForm(); -- cgit v1.2.3