gogs.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // @codekit-prepend "lib/jquery-1.11.1.min.js"
  2. // @codekit-prepend "lib/lib.js"
  3. // @codekit-prepend "lib/tabs.js"
  4. var Gogs = {};
  5. (function ($) {
  6. // Extend jQuery ajax, set CSRF token value.
  7. var ajax = $.ajax;
  8. $.extend({
  9. ajax: function (url, options) {
  10. if (typeof url === 'object') {
  11. options = url;
  12. url = undefined;
  13. }
  14. options = options || {};
  15. url = options.url;
  16. var csrftoken = $('meta[name=_csrf]').attr('content');
  17. var headers = options.headers || {};
  18. var domain = document.domain.replace(/\./ig, '\\.');
  19. if (!/^(http:|https:).*/.test(url) || eval('/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/').test(url)) {
  20. headers = $.extend(headers, {'X-Csrf-Token': csrftoken});
  21. }
  22. options.headers = headers;
  23. var callback = options.success;
  24. options.success = function (data) {
  25. if (data.once) {
  26. // change all _once value if ajax data.once exist
  27. $('[name=_once]').val(data.once);
  28. }
  29. if (callback) {
  30. callback.apply(this, arguments);
  31. }
  32. };
  33. return ajax(url, options);
  34. },
  35. changeHash: function (hash) {
  36. if (history.pushState) {
  37. history.pushState(null, null, hash);
  38. }
  39. else {
  40. location.hash = hash;
  41. }
  42. },
  43. deSelect: function () {
  44. if (window.getSelection) {
  45. window.getSelection().removeAllRanges();
  46. } else {
  47. document.selection.empty();
  48. }
  49. }
  50. });
  51. }(jQuery));
  52. (function ($) {
  53. // Render markdown.
  54. Gogs.renderMarkdown = function () {
  55. var $md = $('.markdown');
  56. var $pre = $md.find('pre > code').parent();
  57. $pre.addClass('prettyprint linenums');
  58. prettyPrint();
  59. // Set anchor.
  60. var headers = {};
  61. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  62. var node = $(this);
  63. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  64. var name = val;
  65. if (headers[val] > 0) {
  66. name = val + '-' + headers[val];
  67. }
  68. if (headers[val] == undefined) {
  69. headers[val] = 1;
  70. } else {
  71. headers[val] += 1;
  72. }
  73. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  74. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  75. });
  76. };
  77. // Render code view.
  78. Gogs.renderCodeView = function () {
  79. function selectRange($list, $select, $from) {
  80. $list.removeClass('active');
  81. if ($from) {
  82. var a = parseInt($select.attr('rel').substr(1));
  83. var b = parseInt($from.attr('rel').substr(1));
  84. var c;
  85. if (a != b) {
  86. if (a > b) {
  87. c = a;
  88. a = b;
  89. b = c;
  90. }
  91. var classes = [];
  92. for (i = a; i <= b; i++) {
  93. classes.push('.L' + i);
  94. }
  95. $list.filter(classes.join(',')).addClass('active');
  96. $.changeHash('#L' + a + '-' + 'L' + b);
  97. return
  98. }
  99. }
  100. $select.addClass('active');
  101. $.changeHash('#' + $select.attr('rel'));
  102. }
  103. $(document).on('click', '.lines-num span', function (e) {
  104. var $select = $(this);
  105. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  106. selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  107. $.deSelect();
  108. });
  109. $('.code-view .lines-code > pre').each(function () {
  110. var $pre = $(this);
  111. var $lineCode = $pre.parent();
  112. var $lineNums = $lineCode.siblings('.lines-num');
  113. if ($lineNums.length > 0) {
  114. var nums = $pre.find('ol.linenums > li').length;
  115. for (var i = 1; i <= nums; i++) {
  116. $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
  117. }
  118. }
  119. });
  120. $(window).on('hashchange', function (e) {
  121. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  122. var $list = $('.code-view ol.linenums > li');
  123. if (m) {
  124. var $first = $list.filter('.' + m[1]);
  125. selectRange($list, $first, $list.filter('.' + m[2]));
  126. $("html, body").scrollTop($first.offset().top - 200);
  127. return;
  128. }
  129. m = window.location.hash.match(/^#(L\d+)$/);
  130. if (m) {
  131. var $first = $list.filter('.' + m[1]);
  132. selectRange($list, $first);
  133. $("html, body").scrollTop($first.offset().top - 200);
  134. }
  135. }).trigger('hashchange');
  136. };
  137. })(jQuery);
  138. function initCore() {
  139. Gogs.renderMarkdown();
  140. Gogs.renderCodeView();
  141. }
  142. function initRepoCreate() {
  143. // Owner switch menu click.
  144. $('#repo-create-owner-list').on('click', 'li', function () {
  145. if (!$(this).hasClass('checked')) {
  146. var uid = $(this).data('uid');
  147. $('#repo-owner-id').val(uid);
  148. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  149. $('#repo-owner-name').text($(this).text().trim());
  150. $(this).parent().find('.checked').removeClass('checked');
  151. $(this).addClass('checked');
  152. console.log("set repo owner to uid :", uid, $(this).text().trim());
  153. }
  154. });
  155. }
  156. $(document).ready(function () {
  157. initCore();
  158. if ($('#repo-create-form').length) {
  159. initRepoCreate();
  160. }
  161. Tabs('#dashboard-sidebar-menu');
  162. homepage();
  163. settingsProfile();
  164. settingsSSHKeys();
  165. settingsDelete();
  166. // Fix language drop-down menu height.
  167. var l = $('#footer-lang li').length;
  168. $('#footer-lang .drop-down').css({
  169. "top": (-31 * l) + "px",
  170. "height": (31 * l - 3) + "px"
  171. });
  172. });
  173. function homepage() {
  174. // Change method to GET if no username input.
  175. $('#promo-form').submit(function (e) {
  176. if ($('#username').val() === "") {
  177. e.preventDefault();
  178. window.location.href = '/user/login';
  179. return true
  180. }
  181. });
  182. // Redirect to register page.
  183. $('#register-button').click(function (e) {
  184. if ($('#username').val() === "") {
  185. e.preventDefault();
  186. window.location.href = '/user/sign_up';
  187. return true
  188. }
  189. $('#promo-form').attr('action', '/user/sign_up');
  190. });
  191. }
  192. function settingsProfile() {
  193. // Confirmation of change username in user profile page.
  194. $('#user-profile-form').submit(function (e) {
  195. if (($('#username').data('uname') != $('#username').val()) && !confirm('Username has been changed, do you want to continue?')) {
  196. e.preventDefault();
  197. return true;
  198. }
  199. });
  200. }
  201. function settingsSSHKeys() {
  202. // Show add SSH key panel.
  203. $('#ssh-add').click(function () {
  204. $('#user-ssh-add-form').removeClass("hide");
  205. });
  206. }
  207. function settingsDelete() {
  208. // Confirmation of delete account.
  209. $('#delete-account-button').click(function (e) {
  210. if (!confirm('This account is going to deleted, do you want to continue?')) {
  211. e.preventDefault();
  212. return true;
  213. }
  214. });
  215. }