diff options
author | Emrah URHAN <raxetul@gmail.com> | 2015-11-22 19:40:18 +0200 |
---|---|---|
committer | Emrah URHAN <raxetul@gmail.com> | 2015-11-22 19:40:18 +0200 |
commit | 737da1a3748d7c82af771d3ba4aa4c76ba219eee (patch) | |
tree | b59104944ba28771752adcc1231a847b6704ac4d /public/js/gogs.js | |
parent | f63a468dfce812423b78a47cfa2583c5ad2faa49 (diff) | |
parent | efaf60ba5a4a7c0954dbaf57203859db3258281f (diff) |
Latest develop updates is merged with my RaspberryPi Dockerfile version.
Merge branch 'develop' of https://github.com/gogits/gogs into develop
Diffstat (limited to 'public/js/gogs.js')
-rw-r--r-- | public/js/gogs.js | 338 |
1 files changed, 292 insertions, 46 deletions
diff --git a/public/js/gogs.js b/public/js/gogs.js index f297191a..599f3637 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 () { @@ -395,33 +422,26 @@ function initRepository() { } } - // 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'); - console.log($choice.data('url')) - }, - message: {noResults: $branch_dropdown.data('no-results')} - }); + initFilterSearchDropdown('.choose.branch .dropdown'); } } @@ -537,6 +557,124 @@ function initAdmin() { } } +function buttonsClickOnEnter() { + $('.ui.button').keypress(function (e) { + if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar + $(this).click(); + }); +} + +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; + } + + 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(); + }); + hideWhenLostFocus('#search-user-box .results', '#search-user-box'); +} + +// FIXME: merge common parts in two functions +function searchRepositories() { + if (!$('#search-repo-box .results').length) { + return; + } + + 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 += '<div class="item"><i class="icon octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>'; + }); + $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(); + }); + hideWhenLostFocus('#search-repo-box .results', '#search-repo-box'); +} + $(document).ready(function () { csrf = $('meta[name=_csrf]').attr("content"); suburl = $('meta[name=_suburl]').attr("content"); @@ -544,9 +682,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 +712,7 @@ $(document).ready(function () { } }); $('.tabular.menu .item').tab(); + $('.tabable.menu .item').tab(); $('.toggle.button').click(function () { $($(this).data('target')).slideToggle(100); @@ -620,9 +759,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 +807,31 @@ $(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(); + searchRepositories(); + + initCommentForm(); initInstall(); initRepository(); @@ -677,4 +839,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 |